SPB Git

Repository Public

FeatureCatalogView.swift 4.6 KB 119 lines swift

1// SP Notes Core2// File: Sources/SPNotesFeaturesCatalog/FeatureCatalogView.swift3// Description: Debug gallery of every feature screen and block view over the fake backend: how the Features team and the4//              app teams look at everything without a server. The apps do not ship it.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import SPNotesDesign9import SPNotesFeatures10import SwiftUI1112/// One screen (or one state of a screen) of the catalogue.13public struct FeatureCatalogEntry: Identifiable {14    public let id: String15    public let title: String16    public let area: String17    public let make: @MainActor (FeatureWorld, any FeatureRouter) -> AnyView1819    public init<V: View>(_ id: String, title: String, area: String, @ViewBuilder make: @escaping @MainActor (FeatureWorld, any FeatureRouter) -> V) {20        self.id = id21        self.title = title22        self.area = area23        self.make = { world, router in AnyView(make(world, router)) }24    }25}2627public enum FeatureCatalog {28    /// Every entry, grouped by area in this order.29    @MainActor public static var entries: [FeatureCatalogEntry] {30        workEntries + knowledgeEntries + assistantEntries + mediaEntries + blocksEntries31    }32}3334/// The gallery: a list of entries on the left (a menu when compact), the chosen screen on the right, with switches for35/// offline, the layout and the colour scheme.36public struct FeatureCatalogView: View {37    @State private var world: FeatureWorld?38    @State private var failure: String?39    @State private var selection: String?40    @State private var offline = false41    @State private var forcedCompact = false42    @State private var dark = false43    @State private var lastCall: String?44    @State private var router = FeatureRouterRecorder()4546    public init() {}4748    public var body: some View {49        Group {50            if let world {51                HStack(spacing: 0) {52                    sidebar53                    Divider()54                    detail(world)55                }56            } else if let failure {57                NVEmptyState(systemImage: "exclamationmark.triangle", title: "Catalogue indisponible", message: failure)58            } else {59                NVProgressDots()60            }61        }62        .frame(maxWidth: .infinity, maxHeight: .infinity)63        .background(NVColor.bg)64        .preferredColorScheme(dark ? .dark : .light)65        .task {66            guard world == nil else { return }67            do {68                world = try await FeatureWorld()69                selection = FeatureCatalog.entries.first?.id70                router.onCall = { call in lastCall = String(describing: call) }71            } catch { failure = String(describing: error) }72        }73    }7475    private var sidebar: some View {76        ScrollView {77            VStack(alignment: .leading, spacing: NVSpace.xs) {78                Toggle("Hors ligne", isOn: $offline)79                Toggle("Compact", isOn: $forcedCompact)80                Toggle("Sombre", isOn: $dark)81                if let lastCall {82                    Text("Routeur : \(lastCall)").nvFont(.micro).foregroundStyle(NVColor.fgMuted)83                }84                let entries = FeatureCatalog.entries85                ForEach(Array(Set(entries.map(\.area))).sorted(), id: \.self) { area in86                    NVSectionHeader(area)87                    ForEach(entries.filter { $0.area == area }) { entry in88                        Button { selection = entry.id } label: {89                            Text(entry.title)90                                .nvFont(.ui)91                                .foregroundStyle(selection == entry.id ? NVColor.accent : NVColor.fg)92                                .frame(maxWidth: .infinity, alignment: .leading)93                                .padding(.vertical, NVSpace.xs)94                                .contentShape(Rectangle())95                        }96                        .buttonStyle(.plain)97                    }98                }99            }100            .padding(NVSpace.md)101        }102        .frame(width: 220)103        .background(NVColor.bgSidebar)104        .onChange(of: offline) { _, value in world?.setOffline(value) }105    }106107    @ViewBuilder108    private func detail(_ world: FeatureWorld) -> some View {109        if let entry = FeatureCatalog.entries.first(where: { $0.id == selection }) {110            entry.make(world, router)111                .id(entry.id + (offline ? "-off" : ""))112                .featureSizeClass(forcedCompact ? .compact : nil)113                .frame(maxWidth: forcedCompact ? 390 : .infinity, maxHeight: .infinity)114        } else {115            NVEmptyState(systemImage: "square.grid.2x2", title: "Choisissez un écran")116        }117    }118}119
Files main
Menu

Theme