Repository Public
1//2// SP Chat iOS3// File: SPChat-iOS/Features/Settings/InstructionsView.swift4// Description: The owner's standing instructions (settings key `instructions`): applied by the server to every5// conversation, shared with the web app and the Mac.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9//1011import SwiftUI1213struct InstructionsView: View {14 @Environment(AppEnvironment.self) private var env15 @State private var text = ""16 @State private var enabled = false17 @State private var loaded = false18 @State private var saving = false19 @State private var message: String?20 @State private var failed = false2122 private var dirty: Bool { text != env.settings.instructionsText || enabled != env.settings.instructionsEnabled }2324 var body: some View {25 Form {26 Section {27 Toggle("Appliquer à chaque conversation", isOn: $enabled).accessibilityIdentifier("instructions.enabled")28 } footer: { Text("Quand l’option est active, le serveur ajoute ces instructions au début de toutes les conversations, quel que soit le modèle.") }29 Section {30 TextEditor(text: $text).frame(minHeight: 220).accessibilityIdentifier("instructions.text")31 } header: { Text("Instructions") } footer: { Text("\(text.count) / 20 000 caractères") }32 if let message { Section { Label(message, systemImage: failed ? "exclamationmark.triangle" : "checkmark.circle").font(.footnote).foregroundStyle(failed ? Color.danger : Color.success) } }33 }34 .navigationTitle("Instructions permanentes").navigationBarTitleDisplayMode(.inline)35 .toolbar {36 ToolbarItem(placement: .confirmationAction) {37 Button {38 saving = true39 Task {40 let e = await env.settings.saveInstructions(text: text, enabled: enabled)41 saving = false; failed = e != nil; message = e ?? String(localized: "Enregistré sur le serveur.")42 }43 } label: { if saving { ProgressView() } else { Text("Enregistrer").fontWeight(.semibold) } }44 .disabled(saving || !dirty || text.count > 20_000)45 .accessibilityIdentifier("instructions.save")46 }47 }48 .task {49 await env.settings.refresh()50 if !loaded { text = env.settings.instructionsText; enabled = env.settings.instructionsEnabled; loaded = true }51 }52 }53}54