// The link picker: a record of another table, picked from a ranked search. // // A search line over the target table (its title column read through the // Records verbs), the matches in `PickerRanking`'s order — a title that starts // with what was typed, then one in which a word does, then any other — and // before anything is typed, the record already linked first and then the // table's own order, for any table a column links to. // // It never saves: a picked record's id goes to `onPick`. import CottonValues import SwiftUI struct LinkPicker: View { @Environment(\.theme) private var theme let column: ColumnDefinition let linkedId: String? var filter: [String: RecordValue] = [:] let onPick: (RecordValue) -> Void @Environment(\.records) private var records @Environment(\.tableDefinitions) private var definitions @State private var query = "" @State private var rows: [TableRecord] = [] @State private var failed = false @FocusState private var focused: Bool private var target: TableDefinition? { column.linkTo.flatMap { definitions[$0] } } var body: some View { ScrollView { VStack(alignment: .leading, spacing: 0) { Text(EditorWords.name(column)).paperTitle(theme.scale.titleSize + 7) .accessibilityAddTraits(.isHeader) PaperTextField( "Search \((target?.plural ?? "records").lowercased())", text: $query, focus: $focused ) .padding(.top, 12) let shown = LinkPicking.ranked(rows, query: query, linkedId: linkedId, definition: target) if shown.isEmpty { Text(failed ? ShellLines.line("editing", "link.these_records_can_t") : ShellLines.line("empty", "matches")) .paperMeta(13) .padding(.top, 14) } ForEach(shown, id: \.id) { record in LinkRow( title: LinkPicking.title(of: record, in: target), chosen: record.id == linkedId ) { onPick(.text(record.id)) } } if linkedId != nil, !column.required { PaperTypedButton(ShellLines.line("editing", "link.remove_link"), destructive: true) { onPick(.null) } .padding(.top, 18) } } .frame(maxWidth: .infinity, alignment: .leading) } .task(id: query) { await load() } } /// The records answer the search themselves (a big table is never read /// whole into the phone); the ranking then orders what they returned. private func load() async { guard let records, let table = column.linkTo else { failed = true return } do { rows = try await LinkPicking.records(in: records, table: table, query: query, where: filter) failed = false } catch { rows = [] failed = true } } } /// One record to pick; Me's "Choose who you are" list draws the same row. struct LinkRow: View { @Environment(\.theme) private var theme let title: String let chosen: Bool let action: () -> Void var body: some View { Button(action: action) { VStack(alignment: .leading, spacing: 4) { HStack(spacing: 10) { Text(title).paperBody(theme.scale.bodySize + 2) .frame(maxWidth: .infinity, alignment: .leading) if chosen { HandTick(theme: theme).frame(width: 18, height: 14) } } .padding(.vertical, 9) PaperFormRule(seed: "link-\(title)") } .contentShape(Rectangle()) } .buttonStyle(OpacityButtonStyle(pressed: 0.7)) .accessibilityLabel(title) .accessibilityAddTraits(chosen ? [.isButton, .isSelected] : .isButton) } } /// How a linked record is named and ordered. Pure (`EditorsTests`). enum LinkPicking { /// Narrow at the reader before its first page: categories may lie beyond /// hundreds of ordinary accounts, including while a search is active. static func records( in store: any Records, table: String, query: String, where filter: [String: RecordValue] ) async throws -> [TableRecord] { let trimmed = query.trimmingCharacters(in: .whitespacesAndNewlines) return try await store.list( table: table, project: nil, query: trimmed.isEmpty ? nil : trimmed, page: 0, where: filter ).records } /// The record's title column as words; the id when the table has no /// definition here or the title is empty — never a blank row. static func title(of record: TableRecord, in definition: TableDefinition?) -> String { if let key = definition?.title, case .text(let text) = record[key], !text.trimmingCharacters(in: .whitespaces).isEmpty { return text } return record.id } /// With a query: `PickerRanking`'s order over the titles. Without one: the /// linked record first, then the order the records came in. static func ranked( _ rows: [TableRecord], query: String, linkedId: String?, definition: TableDefinition? ) -> [TableRecord] { if query.trimmingCharacters(in: .whitespaces).isEmpty { return PickerRanking.suggestions( rows, chosen: linkedId.map { [$0] } ?? [], limit: rows.count, id: \.id) } return PickerRanking.search(rows, matching: query, name: { title(of: $0, in: definition) }) } }