summaryrefslogtreecommitdiff
path: root/Sources/BubombUI/Settings/SequenceStepEditorView.swift
blob: 2a063b704ae33975238190b4cc73a8953bd30978 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import SwiftUI
import BubombCore
#if canImport(AppKit)
import AppKit
#endif

/// Sheet or inline editor for an individual Sequence Step.
public struct SequenceStepEditorView: View {
    @Binding var step: SequenceStep
    var onSave: () -> Void
    var onCancel: () -> Void

    @State private var stepTypeIndex: Int = 0
    @State private var appPath: String = ""
    @State private var urlString: String = "https://"
    @State private var commandString: String = ""
    @State private var commandWorkDir: String = ""
    @State private var scriptPath: String = ""
    @State private var scriptArgs: String = ""

    public init(step: Binding<SequenceStep>, onSave: @escaping () -> Void, onCancel: @escaping () -> Void) {
        self._step = step
        self.onSave = onSave
        self.onCancel = onCancel
    }

    public var body: some View {
        VStack(alignment: .leading, spacing: 16) {
            Text("Edit Step")
                .font(.headline)
                .foregroundColor(BubombTheme.textPrimary)

            TextField("Step Name", text: $step.name)
                .textFieldStyle(.roundedBorder)

            Picker("Action Type", selection: $stepTypeIndex) {
                Text("Open Application").tag(0)
                Text("Open URL").tag(1)
                Text("Run Command").tag(2)
                Text("Run Script").tag(3)
            }
            .pickerStyle(.segmented)

            Divider().background(BubombTheme.borderSubtle)

            Group {
                if stepTypeIndex == 0 {
                    VStack(alignment: .leading, spacing: 8) {
                        Text("Application Path:").font(.caption).foregroundColor(BubombTheme.textSecondary)
                        HStack {
                            TextField("/Applications/Safari.app", text: $appPath)
                                .textFieldStyle(.roundedBorder)
                            Button("Choose...") {
                                chooseApplication()
                            }
                        }
                    }
                } else if stepTypeIndex == 1 {
                    VStack(alignment: .leading, spacing: 8) {
                        Text("URL Destination:").font(.caption).foregroundColor(BubombTheme.textSecondary)
                        TextField("https://apple.com", text: $urlString)
                            .textFieldStyle(.roundedBorder)
                    }
                } else if stepTypeIndex == 2 {
                    VStack(alignment: .leading, spacing: 8) {
                        Text("Shell Command:").font(.caption).foregroundColor(BubombTheme.textSecondary)
                        TextField("e.g. npm run build", text: $commandString)
                            .textFieldStyle(.roundedBorder)
                        Text("Working Directory (Optional):").font(.caption).foregroundColor(BubombTheme.textSecondary)
                        TextField("/path/to/project", text: $commandWorkDir)
                            .textFieldStyle(.roundedBorder)
                    }
                } else {
                    VStack(alignment: .leading, spacing: 8) {
                        Text("Script Path:").font(.caption).foregroundColor(BubombTheme.textSecondary)
                        HStack {
                            TextField("/path/to/script.sh", text: $scriptPath)
                                .textFieldStyle(.roundedBorder)
                            Button("Browse...") {
                                chooseScript()
                            }
                        }
                        Text("Arguments (Space separated):").font(.caption).foregroundColor(BubombTheme.textSecondary)
                        TextField("--verbose", text: $scriptArgs)
                            .textFieldStyle(.roundedBorder)
                    }
                }
            }

            Spacer()

            HStack {
                Button("Cancel") {
                    onCancel()
                }
                .keyboardShortcut(.cancelAction)

                Spacer()

                Button("Save Step") {
                    commitStep()
                    onSave()
                }
                .keyboardShortcut(.defaultAction)
                .buttonStyle(.borderedProminent)
                .tint(BubombTheme.accentOrange)
            }
        }
        .padding(20)
        .frame(width: 460, height: 350)
        .background(BubombTheme.surface)
        .onAppear {
            loadFromStep()
        }
    }

    private func loadFromStep() {
        switch step.action {
        case .openApplication(let path):
            stepTypeIndex = 0
            appPath = path
        case .openURL(let url):
            stepTypeIndex = 1
            urlString = url
        case .runCommand(let cmd, let workDir, _):
            stepTypeIndex = 2
            commandString = cmd
            commandWorkDir = workDir ?? ""
        case .runScript(let path, let args, _, _):
            stepTypeIndex = 3
            scriptPath = path
            scriptArgs = args.joined(separator: " ")
        }
    }

    private func commitStep() {
        switch stepTypeIndex {
        case 0:
            step.action = .openApplication(applicationPath: appPath)
        case 1:
            step.action = .openURL(urlString: urlString)
        case 2:
            let dir = commandWorkDir.isEmpty ? nil : commandWorkDir
            step.action = .runCommand(command: commandString, workingDirectory: dir, timeoutSeconds: 30)
        case 3:
            let args = scriptArgs.components(separatedBy: .whitespaces).filter { !$0.isEmpty }
            step.action = .runScript(scriptPath: scriptPath, arguments: args, workingDirectory: nil, timeoutSeconds: 60)
        default:
            break
        }
    }

    private func chooseApplication() {
        #if canImport(AppKit)
        let panel = NSOpenPanel()
        panel.canChooseFiles = true
        panel.canChooseDirectories = false
        panel.allowsMultipleSelection = false
        panel.allowedContentTypes = [.application]
        panel.directoryURL = URL(fileURLWithPath: "/Applications")
        if panel.runModal() == .OK, let url = panel.url {
            appPath = url.path
            if step.name.isEmpty || step.name == "New Step" {
                step.name = url.deletingPathExtension().lastPathComponent
            }
        }
        #endif
    }

    private func chooseScript() {
        #if canImport(AppKit)
        let panel = NSOpenPanel()
        panel.canChooseFiles = true
        panel.canChooseDirectories = false
        panel.allowsMultipleSelection = false
        if panel.runModal() == .OK, let url = panel.url {
            scriptPath = url.path
            if step.name.isEmpty || step.name == "New Step" {
                step.name = url.lastPathComponent
            }
        }
        #endif
    }
}