SPB Git

Repository Public

StoreFailureView.swift 3.8 KB 63 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: App/StoreFailureView.swift3// Description: Blocking screen shown when the local database cannot be opened: explains what happened, that nothing was erased, where the file and its safety copy are, and offers to retry or quit. Nothing else of the application is reachable.4import SwiftUI5import SPNotesStore6import SPNotesDesign78struct StoreFailureView: View {9    @Environment(AppEnvironment.self) private var env10    let error: StoreOpenError1112    private var backups: [URL] { ([error.backupURL].compactMap { $0 } + StoreBackup.existingBackups(of: env.storeURL)).reduce(into: [URL]()) { if !$0.contains($1) { $0.append($1) } } }13    private var title: String {14        switch error {15        case .newerSchema: "Cette version de SP Notes est trop ancienne pour tes notes"16        case .migrationFailed, .backupFailed: "La mise à jour de la base locale n'a pas abouti"17        case .corrupt: "La base locale est endommagée"18        default: "La base locale n'a pas pu être ouverte"19        }20    }2122    var body: some View {23        VStack(spacing: NVSpace.lg) {24            Image(systemName: "externaldrive.badge.exclamationmark").font(.system(size: 44, weight: .light)).foregroundStyle(NVColor.danger).accessibilityHidden(true)25            Text(title).nvFont(.h2).multilineTextAlignment(.center)26            Text(error.userMessage).nvFont(.body).foregroundStyle(NVColor.fgMuted).multilineTextAlignment(.center).frame(maxWidth: 520).fixedSize(horizontal: false, vertical: true)27            Text("SP Notes reste fermée tant que la base n'est pas lisible : rien de ce que tu taperais ne pourrait être conservé. Aucune note n'a été effacée ni modifiée.")28                .nvFont(.ui).foregroundStyle(NVColor.fgMuted).multilineTextAlignment(.center).frame(maxWidth: 520).fixedSize(horizontal: false, vertical: true)29            VStack(alignment: .leading, spacing: NVSpace.sm) {30                fileRow("Base locale", env.storeURL)31                ForEach(backups, id: \.self) { fileRow("Copie de sécurité", $0) }32                Text(String(describing: error)).font(NVFont.fixed(.code)).foregroundStyle(NVColor.fgMuted).textSelection(.enabled).lineLimit(3)33            }34            .padding(NVSpace.lg).frame(maxWidth: 560, alignment: .leading).nvCard()35            HStack(spacing: NVSpace.md) {36                Button("Quitter SP Notes") { NSApp.terminate(nil) }.keyboardShortcut("q")37                if error.isTransient || error.backupURL != nil || { if case .migrationFailed = error { true } else { false } }() {38                    Button("Réessayer") { AppRelauncher.relaunch() }.buttonStyle(.borderedProminent).keyboardShortcut(.defaultAction).accessibilityIdentifier("store-failure-retry")39                }40            }.controlSize(.large)41        }42        .padding(NVSpace.xxxl).frame(maxWidth: .infinity, maxHeight: .infinity).background(NVColor.bg)43        .accessibilityIdentifier("store-failure")44    }4546    private func fileRow(_ label: String, _ url: URL) -> some View {47        HStack(spacing: NVSpace.sm) {48            Text(label).nvFont(.uiStrong)49            Text(url.lastPathComponent).nvFont(.ui).foregroundStyle(NVColor.fgMuted).lineLimit(1).truncationMode(.middle)50            Spacer()51            Button("Afficher dans le Finder") { NSWorkspace.shared.activateFileViewerSelecting([url]) }.controlSize(.small).disabled(!FileManager.default.fileExists(atPath: url.path))52        }53    }54}5556/// Starts a fresh instance of the application and quits this one (the dependency graph is built once per process).57enum AppRelauncher {58    @MainActor static func relaunch() {59        let config = NSWorkspace.OpenConfiguration(); config.createsNewApplicationInstance = true60        NSWorkspace.shared.openApplication(at: Bundle.main.bundleURL, configuration: config) { _, _ in Task { @MainActor in NSApp.terminate(nil) } }61    }62}63
Files main
Menu

Theme