Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Features/Next/SuggestionsSheet.swift3// Description: AI organisation suggestions: for the selected files, the model proposes a clearer name, a folder, tags and a category, with its reason. This is a review list — every proposal is applied one at a time, by the person, with the ordinary actions of the app (rename, move: journaled, undoable by hand). Nothing is applied without that confirmation, and the server itself applies nothing.4import SPDriveCore5import SPDriveNext6import SwiftUI78struct SuggestionsSheet: View {9 let model: AppModel10 let nav: Navigator11 let items: [DriveItem]12 @Environment(\.dismiss) private var dismiss13 @State private var state: Loadable<AISuggestions> = .loading14 /// What was applied in this sheet: "<fileId>|name", "<fileId>|folder".15 @State private var applied: Set<String> = []16 @State private var dismissed: Set<String> = []1718 private var files: [DriveItem] { items.filter { !$0.isFolder && !$0.isTrashed }.prefix(20).map { $0 } }19 var body: some View {20 VStack(alignment: .leading, spacing: 0) {21 HStack(spacing: SPDTheme.Space.md) {22 Image(systemName: "sparkles").font(.title2).foregroundStyle(SPDTheme.brand)23 VStack(alignment: .leading, spacing: 0) {24 Text("Suggestions de rangement").font(SPDTheme.Typo.title)25 Text("Des propositions, pas des changements : rien n’est modifié tant que vous n’appliquez pas une ligne.").font(SPDTheme.Typo.callout).foregroundStyle(SPDTheme.textSecondary)26 }27 Spacer()28 Button("Terminé") { dismiss() }.keyboardShortcut(.cancelAction)29 }.padding(SPDTheme.Space.lg)30 Rectangle().fill(SPDTheme.hairline).frame(height: 1)31 switch state {32 case .loading:33 VStack(spacing: SPDTheme.Space.md) { ProgressView(); Text("Le modèle lit le début de \(files.count) document\(files.count > 1 ? "s" : "")…").font(SPDTheme.Typo.callout).foregroundStyle(SPDTheme.textSecondary) }.frame(maxWidth: .infinity, maxHeight: .infinity)34 case .failed(let m):35 SPDEmptyState(symbol: "sparkles", title: "Pas de suggestion", message: m, width: 420) { Button("Réessayer") { Task { await load() } } }36 case .ready(let r):37 let shown = r.suggestions.filter { !dismissed.contains($0.fileId) }38 if shown.isEmpty {39 SPDEmptyState(symbol: "checkmark.circle", title: r.suggestions.isEmpty ? "Rien à proposer" : "Tout est traité", message: r.suggestions.isEmpty ? "Le modèle n’a rien proposé pour ces fichiers (texte non indexé, ou rien de mieux à suggérer)." : "Vous avez appliqué ou écarté chaque proposition.", width: 420)40 } else {41 ScrollView {42 VStack(alignment: .leading, spacing: SPDTheme.Space.md) {43 ForEach(shown) { s in card(s) }44 Text("Proposé par \(signature(r)). Une proposition de modèle peut se tromper : lisez-la avant de l’appliquer.").font(SPDTheme.Typo.caption).foregroundStyle(SPDTheme.textTertiary)45 }.padding(SPDTheme.Space.lg)46 }47 }48 }49 }50 .frame(width: 680, height: 600).background(SPDTheme.surface)51 .task { await load() }52 .accessibilityIdentifier("suggestions-sheet")53 }54 private func signature(_ r: AISuggestions) -> String { [r.model, r.provider.map { "via \($0)" }].compactMap { $0 }.joined(separator: " ") }55 @ViewBuilder private func card(_ s: AISuggestion) -> some View {56 if let item = model.item(s.fileId) {57 VStack(alignment: .leading, spacing: SPDTheme.Space.sm) {58 HStack(spacing: SPDTheme.Space.sm) {59 ItemThumbnail(model: model, item: item, side: 24)60 Text(item.name).font(SPDTheme.Typo.bodyStrong).lineLimit(1).truncationMode(.middle)61 if let c = s.category { SPDPill(text: c, symbol: "tag") }62 Spacer()63 Button("Écarter") { dismissed.insert(s.fileId) }.buttonStyle(.link).help("Ne rien faire pour ce fichier")64 }65 if let reason = s.reason, !reason.isEmpty { Text(reason).font(SPDTheme.Typo.callout).foregroundStyle(SPDTheme.textSecondary) }66 if let name = s.name, name != item.name {67 proposal("pencil", "Renommer en", "« \(name) »", done: applied.contains("\(s.fileId)|name"), id: "apply-name") { model.rename(item, to: name); applied.insert("\(s.fileId)|name") }68 }69 if let f = s.folder, f.id != item.parentID {70 let known = model.item(f.id) != nil71 proposal("folder", "Déplacer dans", f.path, done: applied.contains("\(s.fileId)|folder"), enabled: known, id: "apply-folder") { model.move([model.item(s.fileId) ?? item], to: f.id); applied.insert("\(s.fileId)|folder") }72 if !known { Text("Ce dossier n’est pas encore dans la copie locale de votre Drive.").font(SPDTheme.Typo.caption).foregroundStyle(SPDTheme.textTertiary) }73 }74 if let tags = s.tags, !tags.isEmpty {75 HStack(alignment: .firstTextBaseline, spacing: SPDTheme.Space.sm) {76 Image(systemName: "tag").foregroundStyle(SPDTheme.textSecondary).frame(width: 18)77 Text("Étiquettes proposées : \(tags.joined(separator: ", "))").font(SPDTheme.Typo.callout)78 Spacer()79 Text("à poser depuis le site (l’app macOS ne gère pas les étiquettes)").font(SPDTheme.Typo.caption).foregroundStyle(SPDTheme.textTertiary)80 }81 }82 }83 .frame(maxWidth: .infinity, alignment: .leading).spdCard(padding: SPDTheme.Space.md, radius: SPDTheme.Radius.md)84 }85 }86 private func proposal(_ symbol: String, _ verb: String, _ value: String, done: Bool, enabled: Bool = true, id: String, _ apply: @escaping () -> Void) -> some View {87 HStack(alignment: .firstTextBaseline, spacing: SPDTheme.Space.sm) {88 Image(systemName: symbol).foregroundStyle(SPDTheme.brand).frame(width: 18)89 Text(verb).font(SPDTheme.Typo.callout).foregroundStyle(SPDTheme.textSecondary)90 Text(value).font(SPDTheme.Typo.callout.weight(.medium)).lineLimit(2).textSelection(.enabled)91 Spacer()92 if done { Label("Appliqué", systemImage: "checkmark.circle.fill").font(SPDTheme.Typo.caption).foregroundStyle(SPDTheme.positive) }93 else { Button("Appliquer", action: apply).controlSize(.small).disabled(!enabled).accessibilityIdentifier(id) }94 }95 }96 private func load() async {97 guard let next = model.next else { return }98 guard !files.isEmpty else { state = .failed("Sélectionnez des fichiers (pas des dossiers) pour obtenir des suggestions."); return }99 state = .loading100 do { state = .ready(try await next.aiSuggest(fileIDs: files.map(\.id))) } catch { state = .failed(NextText.error(error)) }101 }102}103