SPB Git

Repository Public

SidebarView.swift 8.3 KB 139 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Features/Home/SidebarView.swift3// Description: Navigation column: the workspace screens that exist in this build (inbox, tasks, search…), smart lists, spaces, folders (page hierarchy), tags, and the synchronisation status at the bottom. The selection belongs to the window.4import SwiftUI5import SPNotesStore6import SPNotesDesign7import UniformTypeIdentifiers89/// One selectable row of the sidebar: a feature screen or a view of the library.10enum SidebarSelection: Hashable { case feature(Destination), library(SidebarItem) }1112struct SidebarView: View {13    @Environment(AppEnvironment.self) private var env14    @Environment(WindowModel.self) private var window15    @State private var renaming: String?16    @State private var renameText = ""1718    private var selection: Binding<SidebarSelection?> {19        Binding(get: { window.destination == .notes ? .library(window.library.selection) : .feature(window.destination) },20                set: { v in switch v { case .feature(let d)?: window.destination = d; case .library(let item)?: window.library.selection = item; window.destination = .notes; case nil: break } })21    }2223    var body: some View {24        let library = window.library25        List(selection: selection) {26            let features = Destination.features.filter { env.features.isAvailable($0, env: env) }27            if !features.isEmpty {28                Section("Espace de travail") {29                    ForEach(features) { d in30                        row(.feature(d), d.title, d.symbol, count: env.features.badge(for: d, env: env)).accessibilityIdentifier("sidebar-" + d.rawValue)31                    }32                }33            }34            Section("Bibliothèque") {35                row(.library(.list(.all)), "Toutes les notes", "tray.full", count: library.counts.all)36                row(.library(.list(.recent)), "Récentes", "clock")37                row(.library(.list(.favorites)), "Favoris", "star", count: library.counts.favorites)38                row(.library(.list(.pinned)), "Épinglées", "pin", count: library.counts.pinned)39                if library.counts.pending > 0 || library.selection == .unsynced { row(.library(.unsynced), env.account.phase == .local ? "Sur ce Mac seulement" : "À synchroniser", "arrow.triangle.2.circlepath", count: library.counts.pending) }40            }41            Section("Espaces") {42                ForEach(LibraryModel.sections, id: \.id) { s in43                    row(.library(.section(s.id)), s.label, s.symbol).dropDestination(for: String.self) { ids, _ in Task { await library.move(ids, toParent: nil, section: s.id) }; return true }44                }45            }46            Section("Dossiers") {47                if library.folders.isEmpty { Text("Aucun dossier").foregroundStyle(NVColor.fgMuted).nvFont(.ui) }48                OutlineGroup(library.folders, children: \.children) { node in folderRow(node.page) }49            }50            if library.tags.contains(where: { $0.count > 0 }) {51                Section("Tags") {52                    ForEach(library.tags.filter { $0.count > 0 }) { t in53                        Label { HStack { Text(t.name); Spacer(); counter(t.count) } } icon: { Image(systemName: "number").foregroundStyle(Palette.tag(t.color)) }54                            .tag(SidebarSelection.library(.tag(t.name)))55                    }56                }57            }58            Section {59                row(.library(.list(.archived)), "Archives", "archivebox", count: library.counts.archived)60                row(.library(.list(.trash)), "Corbeille", "trash", count: library.counts.trash)61            }62        }63        .listStyle(.sidebar)64        .safeAreaInset(edge: .bottom, spacing: 0) { SyncStatusBar() }65        .toolbar {66            ToolbarItem { Button { window.paletteMode = .commands; window.showPalette = true } label: { Label("Centre de commandes", systemImage: "command") }.help("Centre de commandes (⌘K)") }67        }68        .accessibilityIdentifier("sidebar")69    }7071    private func counter(_ n: Int) -> some View { Text("\(n)").foregroundStyle(NVColor.fgMuted).font(NVFont.fixed(.micro).monospacedDigit()) }72    private func row(_ item: SidebarSelection, _ title: String, _ symbol: String, count: Int? = nil) -> some View {73        Label { HStack { Text(title); Spacer(); if let count, count > 0 { counter(count) } } } icon: { Image(systemName: symbol) }74            .tag(item)75    }7677    @ViewBuilder private func folderRow(_ p: PageSummary) -> some View {78        Group {79            if renaming == p.id {80                TextField("Nom", text: $renameText).onSubmit { let t = renameText; renaming = nil; Task { await env.library.rename(p.id, to: t) } }.onExitCommand { renaming = nil }81            } else {82                Label { Text(Format.title(p.title)).lineLimit(1) } icon: { if let icon = p.icon, !icon.isEmpty { Text(icon) } else { Image(systemName: p.isFolder ? "folder" : "doc.text") } }83            }84        }85        .tag(SidebarSelection.library(.folder(p.id)))86        .dropDestination(for: String.self) { ids, _ in Task { await env.library.move(ids, toParent: p.id) }; return true }87        .draggable(p.id)88        .contextMenu {89            Button("Nouvelle note dans ce dossier") { Task { if let id = await env.library.newNote(parentId: p.id) { window.library.selection = .folder(p.id); await window.library.reloadNotes(); window.open(id); window.focusTitle += 1 } } }90            Button("Nouveau sous-dossier") { Task { _ = await env.library.newNote(parentId: p.id, asFolder: true) } }91            Divider()92            Button("Renommer") { renameText = p.title == "Untitled" ? "" : p.title; renaming = p.id }93            Button("Ouvrir comme note") { window.open(p.id) }94            Button("Placer au premier niveau") { Task { await env.library.move([p.id], toParent: nil) } }95            Divider()96            Button("Placer dans la corbeille", role: .destructive) { Task { await env.library.trash([p.id]) } }97        }98    }99}100101/// Bottom of the sidebar: what the synchronisation is doing, in one line, and the account.102struct SyncStatusBar: View {103    @Environment(AppEnvironment.self) private var env104    @Environment(WindowModel.self) private var window105    var body: some View {106        let (symbol, text, color): (String, String, Color) = {107            switch env.syncMonitor.summary {108            case .localOnly: return ("person.badge.key", "Se connecter pour synchroniser", NVColor.accent)109            case .signedOut: return ("person.crop.circle.badge.exclamationmark", "Session terminée — se reconnecter", NVColor.warning)110            case .offline(let n): return ("wifi.slash", n > 0 ? "Hors ligne · \(n) en attente" : "Hors ligne", NVColor.fgMuted)111            case .syncing: return ("arrow.triangle.2.circlepath", "Synchronisation…", NVColor.fgMuted)112            case .pending(let n): return ("arrow.up.circle", "\(n) à envoyer", NVColor.accent)113            case .conflicts(let n): return ("exclamationmark.triangle", n == 1 ? "1 conflit" : "\(n) conflits", NVColor.warning)114            case .attention(let n): return ("exclamationmark.bubble", n == 1 ? "1 élément à vérifier" : "\(n) éléments à vérifier", NVColor.warning)115            case .failed: return ("exclamationmark.circle", "Synchronisation en échec", NVColor.danger)116            case .upToDate(let d): return ("checkmark.icloud", d.map { "À jour · \(Format.short($0))" } ?? "À jour", NVColor.fgMuted)117            }118        }()119        Button { window.showAccount = true } label: {120            HStack(spacing: NVSpace.sm) {121                Image(systemName: symbol).foregroundStyle(color).symbolEffect(.pulse, isActive: env.syncMonitor.summary == .syncing && !NSWorkspace.shared.accessibilityDisplayShouldReduceMotion)122                Text(text).nvFont(.micro).foregroundStyle(NVColor.fgMuted).lineLimit(1)123                Spacer()124            }125            .padding(.horizontal, NVSpace.lg).padding(.vertical, NVSpace.sm + NVSpace.xxs).contentShape(Rectangle())126        }127        .buttonStyle(.plain)128        .background(.bar)129        .help(helpText)130        .accessibilityIdentifier("sync-status")131        .accessibilityLabel(text)132    }133    private var helpText: String {134        if case .failed(let m) = env.syncMonitor.summary { return m }135        if !env.syncMonitor.attention.isEmpty { return env.syncMonitor.attention.map(\.title).joined(separator: " · ") }136        return "Compte et synchronisation"137    }138}139
Files main
Menu

Theme