SPB Git

Repository Public

NoteLinkPicker.swift 3.8 KB 81 lines swift

1// SP Notes iOS2// File: Features/Notes/NoteLinkPicker.swift3// Description: Picks a note to link to from the note being edited: searches the local database (works offline) and hands back the4//              id and title of the chosen note, which the editor inserts as a mention.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import SPNotesCore9import SwiftUI1011struct NoteLinkPicker: View {12    /// The note being edited (not offered as a target).13    var excluding: String?14    var onPick: (_ noteId: String, _ title: String) -> Void15    var onCancel: () -> Void1617    @Environment(AppModel.self) private var app18    @State private var query = ""19    @State private var results: [PageSummary] = []20    @State private var loaded = false2122    /// Notes that can be linked: not the note itself, not a database (they are not opened in this app).23    static func candidates(_ pages: [PageSummary], excluding: String?) -> [PageSummary] {24        pages.filter { $0.id != excluding && PageKind(type: $0.type) != .database && $0.deletedAt == nil }25    }2627    var body: some View {28        NavigationStack {29            List {30                if loaded && results.isEmpty {31                    Text(query.trimmingCharacters(in: .whitespaces).isEmpty ? "Aucune autre note pour l’instant." : "Aucune note ne correspond.")32                        .font(.subheadline).foregroundStyle(Color.spTextSecondary)33                }34                ForEach(results) { page in35                    Button { onPick(page.id, AppModel.displayTitle(page.title)) } label: {36                        HStack(spacing: 12) {37                            Image(systemName: page.isFolder ? "folder" : "doc.text").foregroundStyle(Color.accentColor).frame(width: 26).accessibilityHidden(true)38                            VStack(alignment: .leading, spacing: 2) {39                                Text(AppModel.displayTitle(page.title)).font(.body).foregroundStyle(Color.spText).lineLimit(1)40                                if !page.preview.isEmpty { Text(page.preview).font(.caption).foregroundStyle(Color.spTextSecondary).lineLimit(1) }41                            }42                            Spacer(minLength: 0)43                            Text(page.updatedAt.listLabel).font(.caption).foregroundStyle(Color.spMuted)44                        }45                        .frame(minHeight: 44)46                        .contentShape(Rectangle())47                    }48                    .buttonStyle(.plain)49                    .accessibilityIdentifier("note-link-row")50                }51            }52            .listStyle(.plain)53            .scrollContentBackground(.hidden)54            .background(Color.spBackground)55            .searchable(text: $query, placement: .navigationBarDrawer(displayMode: .always), prompt: Text("Rechercher une note"))56            .navigationTitle("Lien vers une note")57            .navigationBarTitleDisplayMode(.inline)58            .toolbar { ToolbarItem(placement: .cancellationAction) { Button("Annuler") { onCancel() }.accessibilityIdentifier("note-link-cancel") } }59            .task(id: query) {60                if !query.isEmpty { try? await Task.sleep(for: .milliseconds(180)) }61                guard !Task.isCancelled else { return }62                await search()63            }64        }65        .accessibilityIdentifier("note-link-picker")66    }6768    private func search() async {69        let text = query.trimmingCharacters(in: .whitespacesAndNewlines)70        let pages: [PageSummary]71        if text.isEmpty {72            pages = (try? await app.store.summaries(list: .recent, limit: 60)) ?? []73        } else {74            pages = ((try? await app.store.search(query: text, limit: 40)) ?? []).map(\.page)75        }76        guard !Task.isCancelled else { return }77        results = Self.candidates(pages, excluding: excluding)78        loaded = true79    }80}81
Files main
Menu

Theme