Repository Public
1// SP Notes iOS2// File: Features/Backlinks/BacklinksSheet.swift3// Description: « Liens » of a note (from its "…" menu): the notes that link to it, its relations (AI ones marked) and the notes4// it mentions. Tapping a row opens that note, at the block of the link when there is one.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import SPNotesCore9import SPNotesDesign10import SwiftUI1112struct BacklinksSheet: View {13 let noteId: String14 let noteTitle: String15 /// Opens a note (and a block of it).16 let onOpen: (String, String?) -> Void17 @Environment(AppModel.self) private var app18 @Environment(\.dismiss) private var dismiss19 @State private var model: BacklinksModel?2021 private struct ReloadKey: Equatable { var edges: Int; var page: Int; var reset: Int }2223 var body: some View {24 NavigationStack {25 Group {26 if let model, model.isLoaded { content(model) } else { ProgressView() }27 }28 .intelligentPanel()29 .background(NVColor.bg)30 .navigationTitle("Liens")31 .navigationBarTitleDisplayMode(.inline)32 .toolbar { ToolbarItem(placement: .confirmationAction) { Button("OK") { dismiss() }.accessibilityIdentifier("links-close") } }33 }34 .presentationDetents([.medium, .large])35 .task { if model == nil { model = BacklinksModel(pageId: noteId, graph: app.repositories.graph, store: app.store) } }36 .task(id: ReloadKey(edges: app.feed.entities("edge"), page: app.feed.page(noteId), reset: app.feed.resetGeneration)) { await model?.load() }37 }3839 @ViewBuilder private func content(_ model: BacklinksModel) -> some View {40 List {41 Section {42 VStack(alignment: .leading, spacing: NVSpace.xs) {43 Text(NoteTitle.display(noteTitle)).nvFont(.h2).foregroundStyle(NVColor.fg).lineLimit(3)44 Text(BacklinksText.count(model.count)).font(.subheadline).foregroundStyle(NVColor.fgMuted)45 }46 .frame(maxWidth: .infinity, alignment: .leading)47 }48 .listRowSeparator(.hidden)49 .listRowBackground(Color.clear)5051 if model.isEmpty {52 Section {53 NVEmptyState(systemImage: "link", title: String(localized: "Aucun lien pour l’instant"),54 message: String(localized: "Mentionne une note avec « Lien vers une note » dans le menu Insérer : les liens apparaissent ici, dans les deux sens."))55 .frame(maxWidth: .infinity)56 .accessibilityIdentifier("links-empty")57 }58 .listRowSeparator(.hidden)59 .listRowBackground(Color.clear)60 }61 group(String(localized: "Notes qui mènent ici"), model.incoming, symbol: "arrow.down.left")62 group(String(localized: "Relations"), model.relations, symbol: "point.3.connected.trianglepath.dotted")63 group(String(localized: "Notes mentionnées"), model.outgoing, symbol: "arrow.up.right")64 }65 .listStyle(.plain)66 .scrollContentBackground(.hidden)67 .accessibilityIdentifier("links-sheet")68 }6970 @ViewBuilder private func group(_ title: String, _ rows: [NoteLinkRow], symbol: String) -> some View {71 if !rows.isEmpty {72 Section {73 ForEach(rows) { row in74 Button {75 dismiss()76 onOpen(row.pageId, row.blockId)77 } label: { label(row, symbol: symbol) }78 .buttonStyle(.plain)79 .listRowBackground(NVColor.bg)80 .accessibilityIdentifier("link-row")81 }82 } header: { NVSectionHeader(title) { NVChip("\(rows.count)") }.textCase(nil) }83 }84 }8586 private func label(_ row: NoteLinkRow, symbol: String) -> some View {87 HStack(alignment: .top, spacing: NVSpace.md) {88 Image(systemName: symbol).font(.footnote.weight(.semibold)).foregroundStyle(row.isAI ? NVColor.ai : NVColor.accent)89 .frame(width: 24, height: 24).accessibilityHidden(true)90 VStack(alignment: .leading, spacing: NVSpace.xs) {91 Text(NoteTitle.display(row.title)).foregroundStyle(NVColor.fg).multilineTextAlignment(.leading)92 if row.relationLabel != nil || row.isAI || row.blockId != nil {93 ViewThatFits(in: .horizontal) {94 HStack(spacing: NVSpace.sm) { chips(row) }95 VStack(alignment: .leading, spacing: NVSpace.xs) { chips(row) }96 }97 }98 if let note = row.note, !note.isEmpty {99 Text(note).font(.footnote).foregroundStyle(row.isAI ? NVColor.ai : NVColor.fgMuted).lineLimit(3).multilineTextAlignment(.leading)100 }101 }102 Spacer(minLength: 0)103 Image(systemName: "chevron.right").font(.footnote.weight(.semibold)).foregroundStyle(NVColor.fgMuted).accessibilityHidden(true)104 }105 .frame(minHeight: 44)106 .contentShape(Rectangle())107 .accessibilityElement(children: .combine)108 }109110 @ViewBuilder private func chips(_ row: NoteLinkRow) -> some View {111 if let relation = row.relationLabel { NVChip(relation, tone: row.isAI ? .ai : .accent) }112 if row.isAI { NVAIBadge(model: row.model, style: .compact) }113 if row.isSuggested { NVChip(String(localized: "Suggestion, non acceptée"), tone: .ai) }114 if row.blockId != nil { NVChip(String(localized: "Passage précis"), systemImage: "text.alignleft") }115 }116}117