Open History to see the last commit
1import SwiftUI
2
3struct EnvironmentPreferencesView: View {
4 @Bindable var model: FeatureRootModel
5 let environmentID: String
6 @State private var settings: ServerSettingsSnapshot?
7 @State private var busy = false
8 @State private var errorMessage: String?
9 @State private var mismatches: [String] = []
10 @State private var routingPermission = GitHubRoutingPermission.off
11
12 private var environment: FeatureEnvironment? {
13 model.snapshot.environments.first { $0.id == environmentID }
14 }
15
16 private var supportsRestartContinuation: Bool {
17 model.snapshot.preferencesByEnvironment?[environmentID]?.continueThreadsAfterServerUpdate != nil
18 }
19
20 var body: some View {
21 Form {
22 Section {
23 Picker("GitHub sharing", selection: Binding(
24 get: { routingPermission }, set: { saveRoutingPermission($0) }
25 )) {
26 ForEach(GitHubRoutingPermission.allCases, id: \.self) { Text($0.label).tag($0) }
27 }
28 } footer: {
29 Text("Enable both environments to share PR data through the same GitHub account. Write access permits PR changes. Credentials stay on each environment.")
30 }
31 if let settings {
32 if let streamingMode = settings.responseStreamingMode {
33 Section {
34 ResponseStreamingPicker(title: "Streaming", selection: Binding(
35 get: { streamingMode },
36 set: { save(.responseStreamingMode($0)) }
37 ))
38 .accessibilityIdentifier("environment-response-streaming")
39 } header: {
40 Text("Responses")
41 } footer: {
42 if streamingMode == .token {
43 Text("Token streaming updates more often and can use more battery.")
44 }
45 }
46 }
47 if environment?.canCustomizeIcon == true {
48 Section("Environment") {
49 Picker("Icon", selection: Binding(
50 get: { settings.environmentIcon ?? "" },
51 set: { save(.environmentIcon($0.isEmpty ? nil : $0)) }
52 )) {
53 Text("Detected").tag("")
54 Text("Server").tag("server")
55 Text("Cloud").tag("cloud")
56 Text("Desktop").tag("desktop")
57 Text("Laptop").tag("laptop")
58 Text("Mac mini").tag("mac-mini")
59 Text("Mac Studio").tag("mac-studio")
60 }
61 }
62 }
63 if model.snapshot.preferencesByEnvironment?[environmentID]?.automaticSettlement != nil {
64 Section {
65 Picker("New threads", selection: Binding(
66 get: { settings.defaultThreadEnvMode.rawValue },
67 set: { save(.defaultThreadEnvMode($0 == "worktree" ? .worktree : .local)) }
68 )) {
69 Text("Local workspace").tag("local")
70 Text("New worktree").tag("worktree")
71 }
72 Toggle("Start worktrees from origin", isOn: Binding(
73 get: { settings.newWorktreesStartFromOrigin },
74 set: { save(.newWorktreesStartFromOrigin($0)) }
75 ))
76 if supportsRestartContinuation {
77 Toggle("Continue threads after restarts", isOn: Binding(
78 get: { settings.continueThreadsAfterServerUpdate },
79 set: { save(.continueThreadsAfterServerUpdate($0)) }
80 ))
81 }
82 } footer: {
83 Text("These preferences and automatic settlement apply to connected environments that support them. Projects, models and providers remain separate.")
84 }
85 if !mismatches.isEmpty {
86 Section("Different preferences") {
87 ForEach(mismatches, id: \.self) { Text($0) }
88 Button("Use this environment’s preferences") {
89 save(.sharedPreferences(settings.sharedPatch(
90 supportsRestartContinuation: supportsRestartContinuation
91 )))
92 }
93 }
94 }
95 }
96 } else if errorMessage == nil {
97 Text("Loading preferences…")
98 }
99 if let errorMessage {
100 Section {
101 Text(errorMessage)
102 Button("Try again") { Task { await load() } }
103 }
104 }
105 }
106 .disabled(busy)
107 .scrollContentBackground(.hidden)
108 .background(T3Colors.background)
109 .navigationTitle("Preferences")
110 .navigationBarTitleDisplayMode(.inline)
111 .task { await load() }
112 }
113
114 private func load() async {
115 busy = true
116 defer { busy = false }
117 do {
118 routingPermission = try await model.client.gitHubRoutingPermission(environmentID: environmentID)
119 settings = try await model.client.serverPreferences(environmentID: environmentID)
120 mismatches = model.client.sharedPreferenceMismatches(environmentID: environmentID)
121 errorMessage = nil
122 } catch { errorMessage = "Could not load preferences. Check this connection." }
123 }
124
125 private func save(_ change: ServerSettingsChange) {
126 busy = true
127 Task {
128 defer { busy = false }
129 do {
130 try await model.client.updateServerPreferences(environmentID: environmentID, change: change)
131 await load()
132 } catch { errorMessage = "Could not save preferences. Check this connection and try again." }
133 }
134 }
135
136 private func saveRoutingPermission(_ permission: GitHubRoutingPermission) {
137 guard !busy else { return }
138 busy = true
139 Task {
140 defer { busy = false }
141 do {
142 try await model.client.setGitHubRoutingPermission(environmentID: environmentID, permission: permission)
143 routingPermission = permission
144 } catch { errorMessage = "Could not save GitHub sharing." }
145 }
146 }
147}