// 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`.importCottonValuesimportSwiftUIstructLinkPicker: View { @Environment(\.theme) privatevar themelet column: ColumnDefinitionlet linkedId: String?var filter: [String: RecordValue] = [:]let onPick: (RecordValue) -> Void @Environment(\.records) privatevar records @Environment(\.tableDefinitions) privatevar definitions @State privatevar query = "" @State privatevar rows: [TableRecord] = [] @State privatevar failed = false @FocusState privatevar focused: Boolprivatevar target: TableDefinition? { column.linkTo.flatMap { definitions[$0] } }var body: someView {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 inLinkRow( 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.privatefunc load() async {guardlet records, let table = column.linkTo else { failed = truereturn }do { rows = tryawaitLinkPicking.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.structLinkRow: View { @Environment(\.theme) privatevar themelet title: Stringlet chosen: Boollet action: () -> Voidvar body: someView {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`).enumLinkPicking {/// Narrow at the reader before its first page: categories may lie beyond/// hundreds of ordinary accounts, including while a search is active.staticfunc records(in store: any Records, table: String, query: String, where filter: [String: RecordValue] ) asyncthrows -> [TableRecord] {let trimmed = query.trimmingCharacters(in: .whitespacesAndNewlines)returntryawait 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.staticfunc title(of record: TableRecord, in definition: TableDefinition?) -> String {iflet 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.staticfunc ranked( _ rows: [TableRecord], query: String, linkedId: String?, definition: TableDefinition? ) -> [TableRecord] {if query.trimmingCharacters(in: .whitespaces).isEmpty {returnPickerRanking.suggestions( rows, chosen: linkedId.map { [$0] } ?? [], limit: rows.count, id: \.id) }returnPickerRanking.search(rows, matching: query, name: { title(of: $0, in: definition) }) }}