Cotton code
157 lines · 6.7 KB · SwiftRaw
// Find's list, as data.//// Pure, so the section order, the filter, the rail, the collapse rule and the// recents are unit-tested without drawing anything.//// **Zero state is RECENT SEARCHES and nothing else.** Pinned, For now, the Tools// grid and the tag chips all left: a search screen that is secretly the menu is// the smell that round removed, and the tools directory moved to Home where a// launcher belongs. A first-week person can reach Documents// without knowing to open search.//// **Typed state** is the file answering: matching PLACES first (TOOLS, as one// chips rail — what you typed is often a place you know the name of), then the// record sections exactly as the core answers them (`read.find`'s `sections`:// grouped, ordered, named and counted there). Three hits show per section; See// all expands the section in place.//// **The rows this reads are `HomeData.findTools`, not a second copy**: the// shown database's package tools (the app ships none — owner's ruling, 21// Oct 2026), each opening by the one route rule, and the places in// `Resources/Places/places.json`. What is Find's own — matching a typed word// against a tool's NAME — is below.import CottonKitimport Foundation/// A tool whose NAME matches the typed query — the places rail.public struct SearchPlaceChip: Equatable, Sendable, Identifiable {    public let key: String    /// The proper noun the user reads.    public let label: String    /// A kebab-case lucide name; never an emoji.    public let icon: String    /// Where the chip goes. A PLACE is a swap, so the screen replaces rather    /// than pushes — see `SearchModel`.    public let tool: FindTool    public var id: String { key }    public init(key: String, label: String, icon: String, tool: FindTool) {        self.key = key        self.label = label        self.icon = icon        self.tool = tool    }}/// How many hits a section shows before "See all".public let searchSectionCollapsed = 3/// How many matching places the typed rail carries.public let searchPlaceChipCap = 8/// One row of Find's one list.enum SearchItem: Equatable, Sendable, Identifiable {    case recentsLabel(key: String)    /// The words, and only the words.    ///    /// A DELIBERATE deferral from the approved frame, which draws each recent    /// with the section it opened — "quote v2 · Documents", "Priya · People". A    /// recent is stored as a plain string, so the section is not knowable at    /// rest without either a second key on the store or a search re-run per row,    /// and the second would put eight queries against the database behind a    /// screen that has been asked nothing.    ///    /// It is deferred because the row is already useful without it: tapping    /// re-runs the search, and the answer names its own sections one line later.    case recent(key: String, query: String)    case placeChips(key: String, chips: [SearchPlaceChip])    /// A section's heading: its id, its words and its count, as answered.    case sectionLabel(key: String, section: String, words: String, count: Int)    /// A hit, with its section's id and words.    case hit(key: String, hit: FindHit, section: String, words: String)    case seeAll(key: String, section: String, words: String, remaining: Int)    var id: String {        switch self {        case .recentsLabel(let key): return key        case .recent(let key, _): return key        case .placeChips(let key, _): return key        case .sectionLabel(let key, _, _, _): return key        case .hit(let key, _, _, _): return key        case .seeAll(let key, _, _, _): return key        }    }}/// Find's list for one render.////// - Parameters:///   - tools: the package tools and places, `HomeData.findTools(…)`.///   - recents: past searches that opened something, newest first.///   - sections: the core's answer for the SETTLED query (`read.find`'s///     `sections`), drawn in its order. Empty until the query bites: the///     sections exist only as an ANSWER, never as furniture.///   - expanded: sections whose "See all" has been opened this visit.///   - pages: the device and database pages' places (`PageFind.places`).///   - query: the RAW field, which is what the places rail filters on — the///     rail answers the instant it is typed, because the directory is static.func searchItems(    tools: [FindTool],    recents: [String] = [],    sections: [FindSection] = [],    expanded: [String] = [],    pages: [PageFind.Place] = [],    query: String) -> [SearchItem] {    let needle = query.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()    var items: [SearchItem] = []    if needle.isEmpty {        // The searches that opened something, and NOTHING else — not the tools,        // not a pinned row. With no recents this list is empty and the screen is        // the question it asks.        if !recents.isEmpty {            items.append(.recentsLabel(key: "recents-label"))            for recent in recents {                items.append(.recent(key: "recent-\(recent)", query: recent))            }        }        return items    }    // ── typed: places first, then the record sections ─────────────────────    var chips: [SearchPlaceChip] = []    for tool in tools {        if chips.count >= searchPlaceChipCap { break }        if tool.name.lowercased().contains(needle) {            chips.append(                SearchPlaceChip(key: "tool-\(tool.key)", label: tool.name, icon: tool.icon, tool: tool)            )        }    }    // The device and database pages are places too, after the tools.    chips += PageFind.chips(matching: needle, in: pages).prefix(max(searchPlaceChipCap - chips.count, 0))    if !chips.isEmpty {        items.append(.placeChips(key: "place-chips", chips: chips))    }    for section in sections {        items.append(.sectionLabel(key: "section-\(section.id)", section: section.id, words: section.words, count: section.count))        let shown = expanded.contains(section.id) ? section.hits : Array(section.hits.prefix(searchSectionCollapsed))        for hit in shown {            // One id in two databases is two rows: the hit's database is in its key.            let key = FeedRowDatabase.key(hit.database, id: "hit-\(hit.kind)-\(hit.recordId)")            items.append(.hit(key: key, hit: hit, section: section.id, words: section.words))        }        // Open, the row stays at the section's tail until its last hit is in        // hand: reaching it reads the next page.        let remaining = section.count - shown.count        if remaining > 0 {            items.append(.seeAll(key: "see-all-\(section.id)", section: section.id, words: section.words, remaining: remaining))        }    }    return items}