|
| 1 | +import ComposableArchitecture |
| 2 | +import SupacodeSettingsShared |
| 3 | +import SwiftUI |
| 4 | + |
| 5 | +/// Settings sub-section for managing on-demand and lifecycle scripts. |
| 6 | +public struct RepositoryScriptsSettingsView: View { |
| 7 | + @Bindable var store: StoreOf<RepositorySettingsFeature> |
| 8 | + |
| 9 | + public init(store: StoreOf<RepositorySettingsFeature>) { |
| 10 | + self.store = store |
| 11 | + } |
| 12 | + |
| 13 | + public var body: some View { |
| 14 | + Form { |
| 15 | + // Lifecycle scripts. |
| 16 | + LifecycleScriptSection( |
| 17 | + text: $store.settings.setupScript, |
| 18 | + title: "Setup Script", |
| 19 | + subtitle: "Runs once after worktree creation.", |
| 20 | + icon: "truck.box.badge.clock", |
| 21 | + iconColor: .blue, |
| 22 | + footerExample: "pnpm install" |
| 23 | + ) |
| 24 | + LifecycleScriptSection( |
| 25 | + text: $store.settings.archiveScript, |
| 26 | + title: "Archive Script", |
| 27 | + subtitle: "Runs before a worktree is archived.", |
| 28 | + icon: "archivebox", |
| 29 | + iconColor: .orange, |
| 30 | + footerExample: "docker compose down" |
| 31 | + ) |
| 32 | + LifecycleScriptSection( |
| 33 | + text: $store.settings.deleteScript, |
| 34 | + title: "Delete Script", |
| 35 | + subtitle: "Runs before a worktree is deleted.", |
| 36 | + icon: "trash", |
| 37 | + iconColor: .red, |
| 38 | + footerExample: "docker compose down" |
| 39 | + ) |
| 40 | + |
| 41 | + // User-defined scripts, each in its own section. |
| 42 | + ForEach($store.settings.scripts) { $script in |
| 43 | + Section { |
| 44 | + if script.kind == .custom { |
| 45 | + TextField("Name", text: $script.name) |
| 46 | + } |
| 47 | + ScriptCommandEditor(text: $script.command, label: script.displayName) |
| 48 | + Button("Remove Script…", role: .destructive) { |
| 49 | + store.send(.removeScript(script.id)) |
| 50 | + } |
| 51 | + .buttonStyle(.plain) |
| 52 | + .foregroundStyle(.red) |
| 53 | + .help("Remove this script.") |
| 54 | + } header: { |
| 55 | + Label { |
| 56 | + Text("\(script.displayName) Script") |
| 57 | + .font(.body) |
| 58 | + .bold() |
| 59 | + } icon: { |
| 60 | + Image(systemName: script.resolvedSystemImage).foregroundStyle(script.resolvedTintColor.color) |
| 61 | + .accessibilityHidden(true) |
| 62 | + }.labelStyle(.verticallyCentered) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + } |
| 67 | + .alert($store.scope(state: \.alert, action: \.alert)) |
| 68 | + .formStyle(.grouped) |
| 69 | + .padding(.top, -20) |
| 70 | + .padding(.leading, -8) |
| 71 | + .padding(.trailing, -6) |
| 72 | + .toolbar { |
| 73 | + ToolbarItem(placement: .primaryAction) { |
| 74 | + let usedKinds = Set(store.settings.scripts.map(\.kind)) |
| 75 | + Menu { |
| 76 | + ForEach(ScriptKind.allCases, id: \.self) { kind in |
| 77 | + if kind == .custom || !usedKinds.contains(kind) { |
| 78 | + Button { |
| 79 | + store.send(.addScript(kind)) |
| 80 | + } label: { |
| 81 | + Label { |
| 82 | + Text("\(kind.defaultName) Script") |
| 83 | + } icon: { |
| 84 | + Image.tintedSymbol(kind.defaultSystemImage, color: kind.defaultTintColor.nsColor) |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } label: { |
| 90 | + Image(systemName: "plus") |
| 91 | + .accessibilityLabel("Add Script") |
| 92 | + } |
| 93 | + .help("Add a new script.") |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +/// Reusable section for lifecycle scripts (setup, archive, delete). |
| 100 | +private struct LifecycleScriptSection: View { |
| 101 | + @Binding var text: String |
| 102 | + let title: String |
| 103 | + let subtitle: String |
| 104 | + let icon: String |
| 105 | + let iconColor: Color |
| 106 | + let footerExample: String |
| 107 | + |
| 108 | + var body: some View { |
| 109 | + Section { |
| 110 | + ScriptCommandEditor(text: $text, label: title) |
| 111 | + } header: { |
| 112 | + Label { |
| 113 | + VStack(alignment: .leading, spacing: 0) { |
| 114 | + Text(title) |
| 115 | + .font(.body) |
| 116 | + .bold() |
| 117 | + .lineLimit(1) |
| 118 | + Text(subtitle) |
| 119 | + .font(.footnote) |
| 120 | + .foregroundStyle(.secondary) |
| 121 | + .lineLimit(1) |
| 122 | + } |
| 123 | + } icon: { |
| 124 | + Image(systemName: icon).foregroundStyle(iconColor).accessibilityHidden(true) |
| 125 | + }.labelStyle(.verticallyCentered) |
| 126 | + } footer: { |
| 127 | + Text("e.g., `\(footerExample)`") |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +/// Monospaced text editor for script commands. |
| 133 | +private struct ScriptCommandEditor: View { |
| 134 | + @Binding var text: String |
| 135 | + let label: String |
| 136 | + |
| 137 | + var body: some View { |
| 138 | + TextEditor(text: $text) |
| 139 | + .monospaced() |
| 140 | + .textEditorStyle(.plain) |
| 141 | + .autocorrectionDisabled() |
| 142 | + .frame(height: 90) |
| 143 | + .accessibilityLabel(label) |
| 144 | + } |
| 145 | +} |
0 commit comments