Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Features/MyDrive/Inspector.swift3// Description: The contextual inspector: what the selection is (picture, name, kind), where it lives, its state on this Mac, its recent versions, its share links, and the actions that go with it. It follows the selection without ever taking the focus or moving the browser.4import SPDriveCore5import SPDriveNext6import SPDriveSync7import SwiftUI89struct InspectorView: View {10 let model: AppModel11 let nav: Navigator12 let items: [DriveItem]13 var body: some View {14 Group {15 if let item = items.first, items.count == 1 { SingleInspector(model: model, nav: nav, item: item) }16 else if items.isEmpty { SPDEmptyState(symbol: "info.circle", title: "Aucune sélection", message: "Sélectionnez un élément pour voir ses informations, ses versions et ses partages.", width: 220) }17 else { multiple }18 }19 .background(SPDTheme.surface)20 }21 private var multiple: some View {22 let files = items.filter { !$0.isFolder }23 return VStack(spacing: SPDTheme.Space.md) {24 ZStack {25 ForEach(Array(items.prefix(3).enumerated()), id: \.element.id) { i, it in ItemThumbnail(model: model, item: it, side: 72).rotationEffect(.degrees(Double(i - 1) * 7)).offset(x: CGFloat(i - 1) * 14) }26 }.frame(height: 96)27 Text("\(items.count) éléments").font(SPDTheme.Typo.title)28 Text("\(files.count) fichier\(files.count > 1 ? "s" : ""), \(items.count - files.count) dossier\(items.count - files.count > 1 ? "s" : "") · \(Fmt.bytes(files.reduce(0) { $0 + $1.size }))").font(SPDTheme.Typo.callout).foregroundStyle(SPDTheme.textSecondary)29 if !items.contains(where: \.isTrashed) {30 HStack {31 Button("Télécharger", systemImage: "arrow.down.circle") { Task { await model.download(items) } }32 Button("Déplacer…", systemImage: "folder") { nav.sheet = .move(items) }33 }.controlSize(.small)34 }35 }36 .padding(SPDTheme.Space.xl).frame(maxWidth: .infinity, maxHeight: .infinity)37 }38}3940private struct SingleInspector: View {41 let model: AppModel42 let nav: Navigator43 let item: DriveItem44 @State private var details: FileDetails?45 @State private var detailsError: String?4647 private var links: [DriveShare] { model.shares.filter { $0.targetId == item.id } }48 var body: some View {49 ScrollView {50 VStack(alignment: .leading, spacing: SPDTheme.Space.lg) {51 header52 if !item.isTrashed { actions }53 section("Informations") {54 if !item.isFolder { line("Taille", Fmt.bytes(item.size)) }55 line("Emplacement", "Mon Drive" + model.path(to: item.id).dropLast().map { " › " + $0.name }.joined())56 line("Créé", Fmt.date(item.createdAt))57 line("Modifié", Fmt.date(item.isFolder ? item.updatedAt : item.contentAt))58 if let t = item.trashedAt { line("Mis à la corbeille", Fmt.date(t)) }59 }60 section("Sur ce Mac") {61 HStack { Text("État").foregroundStyle(SPDTheme.textSecondary); Spacer(); SyncStateLabel(model: model, item: item) }.font(SPDTheme.Typo.callout)62 if !item.isTrashed {63 Toggle("Garder disponible hors ligne", isOn: Binding(get: { model.isPinnedItself(item.id) }, set: { on in Task { await model.setPinned([item], on) } })).font(SPDTheme.Typo.callout).toggleStyle(.switch).controlSize(.mini)64 if model.isPinned(item.id), !model.isPinnedItself(item.id) { Text("Inclus dans un dossier gardé hors ligne.").font(SPDTheme.Typo.caption).foregroundStyle(SPDTheme.textTertiary) }65 }66 }67 if !item.isTrashed, model.has(.ai), !model.isPending(item.id) {68 Button { nav.sheet = .ask(item) } label: {69 Label(item.isFolder ? "Demander à ce dossier…" : "Demander à ce document…", systemImage: "sparkles").frame(maxWidth: .infinity).padding(.vertical, 3)70 }.buttonStyle(.bordered).tint(SPDTheme.brand).accessibilityIdentifier("inspector-ask")71 }72 if !item.isTrashed { sharing }73 if !item.isFolder { versions; integrity }74 if !item.isTrashed, model.has(.activity), !model.isPending(item.id) { section("Activité") { FileActivitySection(model: model, item: item) } }75 }76 .padding(SPDTheme.Space.lg)77 }78 .task(id: "\(item.id)|\(item.versionID ?? "")|\(model.online)") {79 details = nil; detailsError = nil80 guard !item.isFolder, !model.isPending(item.id), let api = model.api else { return }81 do { details = try await api.fileDetails(item.id) } catch { detailsError = DriveError.wrap(error).isTransient ? "Disponible une fois en ligne." : DriveError.wrap(error).errorDescription }82 }83 }84 private var header: some View {85 VStack(spacing: SPDTheme.Space.sm) {86 ItemThumbnail(model: model, item: item, side: 132)87 .frame(maxWidth: .infinity).padding(.vertical, SPDTheme.Space.sm)88 .background(SPDTheme.sunken, in: RoundedRectangle(cornerRadius: SPDTheme.Radius.md, style: .continuous))89 Text(item.name).font(SPDTheme.Typo.headline).multilineTextAlignment(.center).textSelection(.enabled).lineLimit(4)90 Text(Fmt.kind(item)).font(SPDTheme.Typo.callout).foregroundStyle(SPDTheme.textSecondary)91 HStack(spacing: SPDTheme.Space.xs) {92 if item.starred { SPDPill(text: "Favori", symbol: "star.fill", tint: SPDTheme.warning) }93 if !links.filter(\.isActive).isEmpty { SPDPill(text: "Partagé", symbol: "link", tint: SPDTheme.brand) }94 if item.scan == "infected" { SPDPill(text: "Bloqué", symbol: "exclamationmark.shield.fill", tint: SPDTheme.critical) }95 if item.isTrashed { SPDPill(text: "Corbeille", symbol: "trash") }96 }97 }98 .frame(maxWidth: .infinity)99 }100 private var actions: some View {101 HStack(spacing: SPDTheme.Space.sm) {102 action("Ouvrir", "arrow.up.forward.app") { if item.isFolder { nav.go(to: item.id) } else { Task { await model.open(item) } } }103 action("Télécharger", "arrow.down.circle") { Task { await model.download([item]) } }104 action("Partager", "link") { nav.sheet = .share(item) }105 action(item.starred ? "Retirer" : "Favori", item.starred ? "star.slash" : "star") { model.setStarred([item], !item.starred) }106 }107 }108 private func action(_ title: String, _ symbol: String, _ run: @escaping () -> Void) -> some View {109 Button(action: run) {110 VStack(spacing: 3) { Image(systemName: symbol).font(.system(size: 15)); Text(title).font(SPDTheme.Typo.caption).lineLimit(1).minimumScaleFactor(0.75) }111 .padding(.horizontal, 2)112 .frame(maxWidth: .infinity).padding(.vertical, SPDTheme.Space.sm)113 .background(SPDTheme.surfaceRaised, in: RoundedRectangle(cornerRadius: SPDTheme.Radius.md, style: .continuous))114 .overlay(RoundedRectangle(cornerRadius: SPDTheme.Radius.md, style: .continuous).strokeBorder(SPDTheme.hairline))115 }116 .buttonStyle(.plain).foregroundStyle(SPDTheme.brand).accessibilityLabel(title)117 }118 @ViewBuilder private var sharing: some View {119 section("Partage") {120 let active = links.filter(\.isActive)121 if active.isEmpty { Text("Aucun lien actif : seul votre compte y a accès.").font(SPDTheme.Typo.callout).foregroundStyle(SPDTheme.textSecondary) }122 ForEach(active.prefix(3)) { l in123 HStack(alignment: .firstTextBaseline) {124 Image(systemName: l.hasPassword ? "lock" : "link").foregroundStyle(SPDTheme.brand).font(SPDTheme.Typo.caption)125 VStack(alignment: .leading, spacing: 1) {126 Text(l.label.isEmpty ? "Lien du \(Fmt.day(l.createdAt))" : l.label).font(SPDTheme.Typo.callout).lineLimit(1)127 Text([l.canDownload ? "téléchargement permis" : "consultation seule", l.expiresAt.map { "expire le \(Fmt.day($0))" }, "\(l.viewCount) ouverture\(l.viewCount > 1 ? "s" : "")"].compactMap { $0 }.joined(separator: " · ")).font(SPDTheme.Typo.caption).foregroundStyle(SPDTheme.textTertiary)128 }129 }130 }131 Button(active.isEmpty ? "Créer un lien…" : "Gérer les liens…") { nav.sheet = .share(item) }.buttonStyle(.link).font(SPDTheme.Typo.callout)132 if item.isFolder, model.has(.collab) { Button("Membres et rôles…") { nav.sheet = .members(item) }.buttonStyle(.link).font(SPDTheme.Typo.callout) }133 if item.isFolder { Button("Créer un lien de dépôt…") { nav.sheet = .portal(folder: item) }.buttonStyle(.link).font(SPDTheme.Typo.callout) }134 }135 }136 @ViewBuilder private var versions: some View {137 section("Versions") {138 if let d = details {139 ForEach(d.versions.prefix(4)) { v in140 HStack {141 Text("v\(v.seq)").font(SPDTheme.Typo.mono).foregroundStyle(v.current ? SPDTheme.brand : SPDTheme.textSecondary).frame(width: 30, alignment: .leading)142 Text(Fmt.date(v.createdAt)).font(SPDTheme.Typo.callout)143 Spacer()144 Text(Fmt.bytes(v.size)).font(SPDTheme.Typo.caption).foregroundStyle(SPDTheme.textTertiary).monospacedDigit()145 }146 }147 Button(d.versions.count > 4 ? "Les \(d.versions.count) versions…" : "Historique des versions…") { nav.sheet = .versions(item) }.buttonStyle(.link).font(SPDTheme.Typo.callout)148 } else {149 line("Nombre", "\(item.versionCount)")150 if let detailsError { Text(detailsError).font(SPDTheme.Typo.caption).foregroundStyle(SPDTheme.textTertiary) }151 }152 }153 }154 private var integrity: some View {155 section("Intégrité") {156 line("Antivirus", ["clean": "Sain", "pending": "Analyse en attente", "infected": "Bloqué", "error": "Non analysé"][item.scan] ?? item.scan)157 VStack(alignment: .leading, spacing: 2) {158 Text("SHA-256").font(SPDTheme.Typo.callout).foregroundStyle(SPDTheme.textSecondary)159 Text(item.sha256.isEmpty ? "—" : item.sha256).font(SPDTheme.Typo.mono).foregroundStyle(SPDTheme.textTertiary).textSelection(.enabled).lineLimit(3)160 }161 }162 }163 private func section<C: View>(_ title: String, @ViewBuilder _ content: () -> C) -> some View {164 VStack(alignment: .leading, spacing: SPDTheme.Space.sm) {165 Text(title).spdEyebrow()166 VStack(alignment: .leading, spacing: SPDTheme.Space.sm) { content() }167 .frame(maxWidth: .infinity, alignment: .leading)168 .spdCard(padding: SPDTheme.Space.md, radius: SPDTheme.Radius.md)169 }170 }171 private func line(_ label: String, _ value: String) -> some View {172 HStack(alignment: .firstTextBaseline) { Text(label).foregroundStyle(SPDTheme.textSecondary); Spacer(minLength: SPDTheme.Space.md); Text(value).multilineTextAlignment(.trailing).textSelection(.enabled) }173 .font(SPDTheme.Typo.callout).accessibilityElement(children: .combine)174 }175}176