// What the board draws, in order: the database's package tools (owner's // ruling, 21 Oct 2026: tools are records only), each a tile drawn from its // home table's definition. The app ships no tool, so there is no tile of its // own beside them. Pure, so the order is settled by tests. import CottonKit import Foundation enum BoardItem: Equatable, Identifiable { /// A package tool and its home table's definition. case dataTile(ToolDefinition, TableDefinition) var id: String { switch self { case .dataTile(let tool, _): return "data-" + tool.id } } /// The tool's home table. var table: String? { switch self { case .dataTile(_, let definition): return definition.id } } } enum BoardLayout { /// The definition a tool's tile and route are drawn from: its first table, /// else — a tool of views alone, which owns no table (Birthdays over /// People's person, Web pages over Documents' document; a package may own /// zero tables, 8ef9dbd3a) — the table its default view reads. Before /// 21 Oct 2026 such a tool drew no tile at all, so it never reached the fan. static func home(for tool: ToolDefinition, in definitions: TableDefinitions) -> TableDefinition? { if let first = tool.tables.first, let home = definitions[first] { return home } let views = tool.views.filter { $0.partition == nil } guard let view = views.first(where: \.isDefault) ?? views.first, case .string(let table)? = view.read["table"] else { return nil } return definitions[table] } /// The tables a tool's sections page draws: every table it lists but the /// supporting ones — a table the home table's page shows as a `list` /// section is read on each home record's page, never as a section of its own. static func tables(for tool: ToolDefinition, in definitions: TableDefinitions) -> [TableDefinition] { let supporting = supportingTables(for: tool, in: definitions) return tool.tables.filter { !supporting.contains($0) }.compactMap { definitions[$0] } } /// Where a tool opens straight to a list: its home table (`tables.first`) /// when every other table it lists is a `list` section of the home's page — /// Quotas' readings on each account's page — exactly as a one-table tool. /// A table that is neither keeps the tool's sections page — unless the /// tool declares views: then it opens on its default view with its views /// on the ring, whatever its table count (architect, 21 Oct 2026: Money /// opens on Past, never on its tables' cards). static func directTable(for tool: ToolDefinition, in definitions: TableDefinitions) -> TableDefinition? { guard let home = home(for: tool, in: definitions) else { return nil } if opensOnViews(tool, in: definitions) { return home } let supporting = supportingTables(for: tool, in: definitions) return tool.tables.dropFirst().allSatisfy(supporting.contains) ? home : nil } /// Whether the tool declares views: it opens on its default view, read by /// `ToolViewRoute`, never on its home table's plain list. static func opensOnViews(_ tool: ToolDefinition, in definitions: TableDefinitions) -> Bool { let keyed = definitions.all.first { table in table.tool.map { ($0.key ?? table.id) == tool.id } ?? false } return (keyed?.tool?.views ?? []).contains { $0.partition == nil } || tool.views.contains { $0.partition == nil } } /// The tables, other than the home, that the home table's page lists. static func supportingTables(for tool: ToolDefinition, in definitions: TableDefinitions) -> Set { guard let home = home(for: tool, in: definitions) else { return [] } let listed = Set(home.page.compactMap { $0.list?.table }) return Set(tool.tables.dropFirst().filter(listed.contains)) } /// A table's own list: the Settings route that draws a Settings source's /// list, else the table's list (`/lists/[table]`), as the tool's page /// opens it through its declared home. static func listRoute(for definition: TableDefinition) -> Route { listRoute(forTable: definition.id) } /// The same, by the table's id — a table tile on Home, which the core /// names by its table. static func listRoute(forTable table: String) -> Route { SettingsTables.route(forTable: table) ?? .tableList(table: table, filter: nil, title: nil, query: nil) } /// Whether a route asked for arrives at the tile drawn for `tool`: its /// own route, or the bare tool URL, which reaches the tile even when an /// older route names a different table. static func arrives(_ asked: Route, at drawn: Route?, tool: String?) -> Bool { asked == drawn || tool.map { asked.path == "/\($0)" } == true } /// THE route rule for a tool's tile — the tile tap, Find and a deep link /// all open a tool here. A kit tool's home table names the hand-built /// screen that draws it (`tool.route`: Todos' `/todos`, Calendar's /// `/calendar`), and that screen opens; else its home table's list /// ([`directTable`]), or — a package of tables that stand on their own — /// its own board (`/tools/board/[id]`). A tool over a table another /// tool owns (`ownsHome`: Birthdays over People's person) opens its /// own page, drawn from its own views (`ToolBoardRoute`), never the /// owner's screen or the table's list. static func route(for tool: ToolDefinition, in definitions: TableDefinitions, tools: [ToolDefinition]) -> Route { if !ownsHome(tool, in: definitions, tools: tools) { return .toolBoard(id: tool.id) } if let declared = declaredRoute(for: tool, in: definitions, tools: tools) { return declared } // Its board route draws the default view (`ToolBoardRoute`). if opensOnViews(tool, in: definitions) { return .toolBoard(id: tool.id) } return directTable(for: tool, in: definitions).map(listRoute(for:)) ?? .toolBoard(id: tool.id) } /// RULE: a table belongs to one tool — the one its tool block is keyed to, or, where no tool carries that key (Notebook's note table is keyed `notes`), a tool that lists it; another tool reading the same table (an imported Birthdays over People's person) does not own it, so it never draws the owner's hand-built screen (lane ringviews, 21 Oct 2026; the core's `tool_views::owns`). A tool with no home table in hand owns nothing to give away. static func ownsHome(_ tool: ToolDefinition, in definitions: TableDefinitions, tools: [ToolDefinition]) -> Bool { guard let home = home(for: tool, in: definitions) else { return true } let key = home.tool?.key ?? home.id return key == tool.id || (!tools.contains { $0.id == key } && tools.contains { $0.id == tool.id && $0.tables.contains(home.id) }) } /// The screen a tool's home table's definition names (`tool.route`); nil /// when it names none, and the tool opens its list or its board — and nil /// for a tool that does not own that table: the screen draws its owner. static func declaredRoute(for tool: ToolDefinition, in definitions: TableDefinitions, tools: [ToolDefinition]) -> Route? { guard ownsHome(tool, in: definitions, tools: tools) else { return nil } return home(for: tool, in: definitions).flatMap(declaredRoute(of:)) } /// The screen a table's definition names for its tool; nil for none. static func declaredRoute(of definition: TableDefinition) -> Route? { definition.tool?.route.flatMap(Route.parse) } /// Where a table's tile opens — the same rule by the table: the screen /// its definition names, else its list. static func route(forTable definition: TableDefinition) -> Route { declaredRoute(of: definition) ?? listRoute(for: definition) } /// The tiles in Home's order; Tools is drawn last by the grid itself (B1). static func items(definitions: TableDefinitions, dataTools: ToolDefinitions, order: [String] = []) -> [BoardItem] { laid(definitions: definitions, dataTools: dataTools, order: order, only: false) } /// Home's tiles, and nothing else, in the tiles' draw order — the ones /// with something to show in the person's order, then the empty ones /// (`HomeTiles.keys`; the crate orders them). A tool whose list is no /// tile is not drawn. static func items(definitions: TableDefinitions, dataTools: ToolDefinitions, tiles: HomeTiles) -> [BoardItem] { items(definitions: definitions, dataTools: dataTools, keys: tiles.keys) } /// The tools named by `keys`, in that order, and no other. static func items(definitions: TableDefinitions, dataTools: ToolDefinitions, keys: [String]) -> [BoardItem] { laid(definitions: definitions, dataTools: dataTools, order: keys, only: true) } private static func laid(definitions: TableDefinitions, dataTools: ToolDefinitions, order: [String], only: Bool) -> [BoardItem] { let items = dataTools.all.compactMap { tool in home(for: tool, in: definitions).map { BoardItem.dataTile(tool, $0) } } guard !order.isEmpty || only else { return items } func key(_ item: BoardItem) -> String { switch item { case .dataTile(let tool, _): return tool.id } } let positions = Dictionary(order.enumerated().map { ($0.element, $0.offset) }, uniquingKeysWith: { first, _ in first }) return items.enumerated().filter { !only || positions[key($0.element)] != nil }.sorted { let a = positions[key($0.element)] ?? Int.max let b = positions[key($1.element)] ?? Int.max return a == b ? $0.offset < $1.offset : a < b }.map(\.element) } }