Open History to see the last commit
1import SwiftUI
2
3public struct FeatureSourceControlView: View {
4 let client: any FeatureClient
5 let threadID: String
6
7 @State private var status: FeatureSourceControlStatus?
8 @State private var isLoading = true
9 @State private var isRunningAction = false
10 @State private var errorMessage: String?
11 @State private var commitMessage = ""
12 @State private var pendingCommitAction: FeatureSourceControlAction?
13
14 public init(client: any FeatureClient, threadID: String) {
15 self.client = client
16 self.threadID = threadID
17 }
18
19 public var body: some View {
20 Group {
21 if isLoading, status == nil {
22 ProgressView("Loading repository…")
23 .frame(maxWidth: .infinity, maxHeight: .infinity)
24 } else if let status, status.isRepository {
25 statusList(status)
26 } else {
27 ContentUnavailableView(
28 "Source control unavailable",
29 systemImage: "arrow.triangle.branch",
30 description: Text(
31 errorMessage
32 ?? (status?.isRepository == false
33 ? "This workspace is not a Git repository."
34 : "Repository status could not be loaded.")
35 )
36 )
37 }
38 }
39 .background(T3Colors.background)
40 .navigationTitle("Source Control")
41 .navigationBarTitleDisplayMode(.inline)
42 .toolbar {
43 ToolbarItem(placement: .topBarTrailing) {
44 Button { Task { await load() } } label: { Image(systemName: "arrow.clockwise") }
45 .disabled(isLoading || isRunningAction)
46 .accessibilityLabel("Reload source control")
47 }
48 }
49 .alert("Commit changes", isPresented: Binding(
50 get: { pendingCommitAction != nil },
51 set: { if !$0 { pendingCommitAction = nil } }
52 )) {
53 TextField("Commit message", text: $commitMessage)
54 Button("Cancel", role: .cancel) { pendingCommitAction = nil }
55 Button("Commit") {
56 if let action = pendingCommitAction {
57 Task { await perform(action, message: commitMessage) }
58 }
59 pendingCommitAction = nil
60 }
61 .disabled(commitMessage.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)
62 }
63 .alert("Source control failed", isPresented: Binding(
64 get: { status != nil && errorMessage != nil },
65 set: { if !$0 { errorMessage = nil } }
66 )) {
67 Button("OK") { errorMessage = nil }
68 } message: {
69 Text(errorMessage ?? "The source control action could not be completed.")
70 }
71 .task { await load() }
72 }
73
74 private func statusList(_ status: FeatureSourceControlStatus) -> some View {
75 List {
76 Section("Repository") {
77 LabeledContent("Branch", value: status.branch ?? "Detached HEAD")
78 if let upstream = status.upstream {
79 LabeledContent("Upstream", value: upstream)
80 }
81 HStack {
82 Label("\(status.aheadCount) ahead", systemImage: "arrow.up")
83 Spacer()
84 Label("\(status.behindCount) behind", systemImage: "arrow.down")
85 }
86 .font(T3Typography.supporting)
87 .foregroundStyle(T3Colors.textSecondary)
88 if let pullRequest = status.pullRequest {
89 if let url = pullRequest.url {
90 Link(destination: url) {
91 Label("PR #\(pullRequest.number) · \(pullRequest.title)", systemImage: "arrow.up.right.square")
92 }
93 } else {
94 LabeledContent("Pull Request", value: "#\(pullRequest.number) · \(pullRequest.state)")
95 }
96 }
97 }
98
99 Section("Actions") {
100 if status.availableActions.isEmpty {
101 Text(status.isBusy ? "Source control operation in progress" : "No actions available")
102 .foregroundStyle(T3Colors.textSecondary)
103 }
104 ForEach(status.availableActions, id: \.self) { action in
105 Button {
106 begin(action)
107 } label: {
108 Label(action.title, systemImage: action.icon)
109 .frame(maxWidth: .infinity, alignment: .leading)
110 }
111 .disabled(isRunningAction)
112 }
113 }
114
115 Section("\(status.files.count) changed \(status.files.count == 1 ? "file" : "files")") {
116 if status.files.isEmpty {
117 Label("Working tree clean", systemImage: "checkmark.circle")
118 .foregroundStyle(T3Colors.textSecondary)
119 }
120 ForEach(status.files) { file in
121 HStack(spacing: 10) {
122 Text(file.state.shortLabel)
123 .font(.caption2.monospaced().weight(.bold))
124 .foregroundStyle(file.state.color)
125 .frame(width: 18)
126 Text(file.path)
127 .font(T3Typography.threadBody)
128 .lineLimit(1)
129 Spacer()
130 if file.isStaged {
131 Text("STAGED")
132 .font(T3Typography.eyebrow)
133 .foregroundStyle(.green)
134 }
135 }
136 .accessibilityElement(children: .combine)
137 }
138 }
139 }
140 .listStyle(.insetGrouped)
141 .scrollContentBackground(.hidden)
142 .refreshable { await load() }
143 .overlay {
144 if isRunningAction {
145 ProgressView()
146 .padding(12)
147 .background(.ultraThinMaterial, in: RoundedRectangle(cornerRadius: 10))
148 }
149 }
150 }
151
152 private func begin(_ action: FeatureSourceControlAction) {
153 if action.requiresMessage {
154 commitMessage = ""
155 pendingCommitAction = action
156 } else {
157 Task { await perform(action, message: nil) }
158 }
159 }
160
161 private func load() async {
162 isLoading = true
163 defer { isLoading = false }
164 do {
165 status = try await client.sourceControlStatus(threadID: threadID)
166 errorMessage = nil
167 } catch {
168 errorMessage = error.localizedDescription
169 }
170 }
171
172 private func perform(_ action: FeatureSourceControlAction, message: String?) async {
173 isRunningAction = true
174 defer { isRunningAction = false }
175 do {
176 status = try await client.performSourceControlAction(
177 threadID: threadID,
178 action: action,
179 message: message?.trimmingCharacters(in: .whitespacesAndNewlines)
180 )
181 errorMessage = nil
182 } catch {
183 errorMessage = error.localizedDescription
184 }
185 }
186}
187
188private extension FeatureSourceControlAction {
189 var requiresMessage: Bool {
190 switch self {
191 case .commit, .commitAndPush, .commitPushAndCreatePullRequest: true
192 case .push, .pull, .createPullRequest: false
193 }
194 }
195
196 var title: String {
197 switch self {
198 case .commit: "Commit changes"
199 case .push: "Push"
200 case .pull: "Pull latest"
201 case .createPullRequest: "Create pull request"
202 case .commitAndPush: "Commit and push"
203 case .commitPushAndCreatePullRequest: "Commit, push, and create PR"
204 }
205 }
206
207 var icon: String {
208 switch self {
209 case .commit: "checkmark.circle"
210 case .push: "arrow.up.circle"
211 case .pull: "arrow.down.circle"
212 case .createPullRequest: "arrow.triangle.pull"
213 case .commitAndPush: "arrow.up.circle.fill"
214 case .commitPushAndCreatePullRequest: "point.3.connected.trianglepath.dotted"
215 }
216 }
217}
218
219private extension FeatureSourceControlFileState {
220 var shortLabel: String {
221 switch self {
222 case .added: "A"
223 case .modified: "M"
224 case .deleted: "D"
225 case .renamed: "R"
226 case .untracked: "?"
227 case .conflicted: "!"
228 }
229 }
230
231 var color: Color {
232 switch self {
233 case .added: .green
234 case .modified: .orange
235 case .deleted, .conflicted: .red
236 case .renamed: .blue
237 case .untracked: .secondary
238 }
239 }
240}