Repository Public
1//2// SP Chat iOS3// File: SPChat-iOS/Features/Library/PromptLibraryView.swift4// Description: Saved prompts of the account: search, insert into the composer, create, delete.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8//910import SwiftUI1112struct PromptLibraryView: View {13 /// Nil when opened from the settings (management only).14 var onInsert: ((SavedPrompt) -> Void)?15 @Environment(AppEnvironment.self) private var env16 @Environment(\.dismiss) private var dismiss17 @State private var query = ""18 @State private var editor: Editor?1920 struct Editor: Identifiable { var prompt: SavedPrompt?; var id: String { prompt?.id ?? "new" } }2122 var body: some View {23 let store = env.prompts24 let list = store.matching(query)25 NavigationStack {26 List {27 let favorites = list.filter(\.favorite), others = list.filter { !$0.favorite }28 if !favorites.isEmpty { Section("Favoris") { ForEach(favorites) { row($0) } } }29 if !others.isEmpty { Section(favorites.isEmpty ? "" : String(localized: "Tous les prompts")) { ForEach(others) { row($0) } } }30 }31 .overlay {32 if store.isLoading && store.prompts.isEmpty { ProgressView() }33 else if let error = store.error, store.prompts.isEmpty { ContentUnavailableView { Label("Prompts indisponibles", systemImage: "wifi.slash") } description: { Text(error) } }34 else if store.prompts.isEmpty { ContentUnavailableView { Label("Aucun prompt enregistré", systemImage: "text.book.closed") } description: { Text("Enregistre ici les consignes que tu réutilises souvent. Écris {{sujet}} pour créer une variable à remplir à chaque usage.") } actions: { Button("Nouveau prompt") { editor = Editor() } } }35 else if list.isEmpty { ContentUnavailableView.search(text: query) }36 }37 .searchable(text: $query, prompt: Text("Rechercher"))38 .navigationTitle("Prompts").navigationBarTitleDisplayMode(.inline)39 .toolbar {40 ToolbarItem(placement: .cancellationAction) { Button("Fermer") { dismiss() } }41 ToolbarItem(placement: .primaryAction) { Button { editor = Editor() } label: { Image(systemName: "plus") }.accessibilityLabel(String(localized: "Nouveau prompt")).accessibilityIdentifier("prompts.new") }42 }43 .sheet(item: $editor) { e in PromptEditor(prompt: e.prompt) }44 .task { await store.refresh() }45 .refreshable { await store.refresh() }46 }47 }4849 private func row(_ p: SavedPrompt) -> some View {50 Button {51 if let onInsert { env.haptics.selection(); dismiss(); onInsert(p) } else { editor = Editor(prompt: p) }52 } label: {53 VStack(alignment: .leading, spacing: 5) {54 HStack(spacing: 6) {55 Text(p.title).font(.body.weight(.semibold)).foregroundStyle(Color.textPrimary).lineLimit(1)56 if p.favorite { Image(systemName: "star.fill").font(.caption2).foregroundStyle(Color.orange).accessibilityLabel(String(localized: "favori")) }57 Spacer(minLength: 0)58 if p.useCount > 0 { Text("\(p.useCount)×").font(.caption2.monospacedDigit()).foregroundStyle(Color.muted) }59 }60 Text(p.body).font(.footnote).foregroundStyle(Color.textSecondary).lineLimit(2)61 let vars = p.variables62 if !vars.isEmpty {63 ScrollView(.horizontal, showsIndicators: false) {64 HStack(spacing: 6) { ForEach(vars, id: \.self) { v in Text(v).font(.caption2.weight(.medium)).foregroundStyle(Color.accentColor).padding(.horizontal, 7).padding(.vertical, 3).background(Color.accentSoft, in: .capsule) } }65 }66 .accessibilityLabel(String(localized: "Variables : \(vars.joined(separator: ", "))"))67 }68 }69 .contentShape(.rect)70 }71 .buttonStyle(.plain)72 .swipeActions(edge: .leading, allowsFullSwipe: true) {73 Button { env.haptics.soften(); Task { await env.prompts.toggleFavorite(p) } } label: { Label(p.favorite ? "Retirer" : "Favori", systemImage: p.favorite ? "star.slash" : "star.fill") }.tint(.orange)74 }75 .swipeActions(edge: .trailing) {76 Button(role: .destructive) { Task { await env.prompts.delete(p) } } label: { Label("Supprimer", systemImage: "trash") }77 Button { editor = Editor(prompt: p) } label: { Label("Modifier", systemImage: "pencil") }.tint(.gray)78 }79 .accessibilityIdentifier("prompts.row")80 }81}8283struct PromptEditor: View {84 let prompt: SavedPrompt?85 @Environment(AppEnvironment.self) private var env86 @Environment(\.dismiss) private var dismiss87 @State private var title = ""88 @State private var text = ""89 @State private var favorite = false90 @State private var saving = false9192 var body: some View {93 NavigationStack {94 Form {95 Section("Titre") { TextField("Titre", text: $title).accessibilityIdentifier("prompts.title") }96 Section {97 TextEditor(text: $text).frame(minHeight: 180).accessibilityIdentifier("prompts.body")98 } header: { Text("Texte") } footer: {99 let vars = PromptTemplate.variables(in: text)100 Text(vars.isEmpty ? String(localized: "Astuce : {{sujet}} crée une variable, demandée à chaque insertion.") : String(localized: "Variables : \(vars.joined(separator: ", "))"))101 }102 if prompt == nil { Section { Toggle("Favori", isOn: $favorite) } }103 }104 .navigationTitle(prompt == nil ? "Nouveau prompt" : "Modifier le prompt").navigationBarTitleDisplayMode(.inline)105 .toolbar {106 ToolbarItem(placement: .cancellationAction) { Button("Annuler") { dismiss() } }107 ToolbarItem(placement: .confirmationAction) {108 Button("Enregistrer") {109 let t = String(title.trimmingCharacters(in: .whitespacesAndNewlines).prefix(120)), b = text.trimmingCharacters(in: .whitespacesAndNewlines)110 saving = true111 Task {112 let ok = if let prompt { await env.prompts.update(prompt, title: t, body: b) } else { await env.prompts.create(title: t, body: b, favorite: favorite) }113 saving = false114 if ok { dismiss() }115 }116 }117 .disabled(saving || title.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty || text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)118 .accessibilityIdentifier("prompts.save")119 }120 }121 .onAppear { if let prompt { title = prompt.title; text = prompt.body; favorite = prompt.favorite } }122 }123 }124}125