Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: App/Navigation.swift3// Description: Typed navigation of a window: destinations (notes and the feature screens), routes that carry a page and a block, deep links (spnotes://), and the small hosting protocol through which feature screens are provided — by this app today, by SPNotesFeatures once it is merged.4import SwiftUI5import SPNotesStore67/// What the detail area of a window can show. Only the destinations a `FeatureHosting` provides appear in the sidebar:8/// a section whose screen does not exist is never shown.9enum Destination: String, CaseIterable, Hashable, Codable, Identifiable {10 case notes, today, inbox, tasks, databases, graph, constellation, timeline, health, search, research, canvas, ask11 var id: String { rawValue }12 var title: String {13 switch self {14 case .notes: "Notes"; case .today: "Aujourd'hui"; case .inbox: "Boîte de réception"; case .tasks: "Tâches"; case .databases: "Bases de données"15 case .graph: "Graphe"; case .constellation: "Constellation (Labs)"; case .timeline: "Chronologie"; case .health: "Santé"; case .search: "Rechercher partout"; case .research: "Recherche"16 case .canvas: "Canvas"; case .ask: "Demander"17 }18 }19 var symbol: String {20 switch self {21 case .notes: "note.text"; case .today: "sun.max"; case .inbox: "tray.and.arrow.down"; case .tasks: "checklist"; case .databases: "tablecells"22 case .graph: "point.3.filled.connected.trianglepath.dotted"; case .constellation: "sparkle.magnifyingglass"; case .timeline: "clock.arrow.circlepath"; case .health: "stethoscope"; case .search: "magnifyingglass"; case .research: "graduationcap"23 case .canvas: "square.on.square.dashed"; case .ask: "sparkles"24 }25 }26 /// Order of the « Espace de travail » section of the sidebar.27 static let features: [Destination] = [.today, .inbox, .tasks, .databases, .canvas, .graph, .constellation, .timeline, .health, .research, .ask, .search]28}2930/// A place in the knowledge base: a page, and optionally one of its blocks (scrolled to and flashed).31nonisolated struct Route: Hashable, Codable, Sendable {32 var pageId: String33 var blockId: String?34 init(pageId: String, blockId: String? = nil) { self.pageId = pageId.lowercased(); self.blockId = blockId?.isEmpty == false ? blockId : nil }35 /// `spnotes://page/<id>?block=<id>`36 var url: URL { var c = URLComponents(); c.scheme = "spnotes"; c.host = "page"; c.path = "/" + pageId; if let blockId { c.queryItems = [URLQueryItem(name: "block", value: blockId)] }; return c.url! }37}3839/// `spnotes://page/<uuid>?block=<id>` opens a page at a block (2.0); `spnotes://note/<uuid>` and `spnotes://p/<uuid>` open a40/// note (1.x links keep working, with or without `?block=` / `#block`); `spnotes://new?text=…` creates a note from text;41/// `spnotes://go/<destination>` shows a screen. A link never deletes or sends anything.42enum DeepLink: Equatable {43 case page(Route), new(text: String?), destination(Destination)44 /// 1.x spelling, kept for the callers of the old API.45 static func note(_ id: String) -> DeepLink { .page(Route(pageId: id)) }4647 init?(_ url: URL) {48 guard url.scheme?.lowercased() == "spnotes" else { return nil }49 let parts = ([url.host ?? ""] + url.pathComponents).filter { !$0.isEmpty && $0 != "/" }50 let query = URLComponents(url: url, resolvingAgainstBaseURL: false)?.queryItems51 switch parts.first {52 case "note", "p", "page":53 guard parts.count == 2, UUID(uuidString: parts[1]) != nil else { return nil }54 let raw = query?.first { $0.name == "block" }?.value ?? url.fragment55 // a block id is 1 to 64 characters of the id alphabet: anything else is ignored, the page still opens56 let block = raw.flatMap { b in !b.isEmpty && b.count <= 64 && b.allSatisfy({ $0.isLetter || $0.isNumber || $0 == "_" || $0 == "-" }) ? b : nil }57 self = .page(Route(pageId: parts[1], blockId: block))58 case "new": self = .new(text: query?.first { $0.name == "text" }?.value.map { String($0.prefix(100_000)) })59 case "go": guard parts.count == 2, let d = Destination(rawValue: parts[1]) else { return nil }; self = .destination(d)60 default: return nil61 }62 }63}6465/// What a feature screen can ask of the window that hosts it (the `FeatureRouter` of SPEC §8).66@MainActor protocol FeatureRouting: AnyObject {67 func openPage(id: String, blockId: String?)68 func openURL(_ url: URL)69 func presentError(_ message: String)70}7172/// Provider of the feature screens. The shell only knows this protocol; `FeaturesHost` implements it with the screens73/// of the shared SPNotesFeatures library (and this app's own Tasks, Inbox and Search screens).74@MainActor protocol FeatureHosting {75 func isAvailable(_ destination: Destination, env: AppEnvironment) -> Bool76 func screen(for destination: Destination, env: AppEnvironment, window: WindowModel) -> AnyView?77 /// Number shown beside the destination in the sidebar (open tasks, captures to triage).78 func badge(for destination: Destination, env: AppEnvironment) -> Int?79}8081extension SidebarItem {82 /// Stable text form for window restoration.83 var stored: String {84 switch self {85 case .list(let l): "list:" + SidebarItem.name(l)86 case .section(let s): "section:" + s87 case .folder(let id): "folder:" + id88 case .tag(let t): "tag:" + t89 case .unsynced: "unsynced"90 }91 }92 init?(stored: String) {93 if stored == "unsynced" { self = .unsynced; return }94 guard let i = stored.firstIndex(of: ":") else { return nil }95 let kind = stored[..<i], value = String(stored[stored.index(after: i)...])96 switch kind {97 case "list": guard let l = SidebarItem.lists.first(where: { SidebarItem.name($0) == value }) else { return nil }; self = .list(l)98 case "section": self = .section(value)99 case "folder": self = .folder(value)100 case "tag": self = .tag(value)101 default: return nil102 }103 }104 private static let lists: [SmartList] = [.all, .recent, .pinned, .favorites, .inbox, .archived, .trash]105 private static func name(_ l: SmartList) -> String {106 switch l { case .all: "all"; case .recent: "recent"; case .pinned: "pinned"; case .favorites: "favorites"; case .inbox: "inbox"; case .archived: "archived"; case .trash: "trash"; default: "all" }107 }108}109