Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

FeatureSourceControlView.swift

232 lines8.3 KB

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 .task { await load() }
64 }
65​
66 private func statusList(_ status: FeatureSourceControlStatus) -> some View {
67 List {
68 Section("Repository") {
69 LabeledContent("Branch", value: status.branch ?? "Detached HEAD")
70 if let upstream = status.upstream {
71 LabeledContent("Upstream", value: upstream)
72 }
73 HStack {
74 Label("\(status.aheadCount) ahead", systemImage: "arrow.up")
75 Spacer()
76 Label("\(status.behindCount) behind", systemImage: "arrow.down")
77 }
78 .font(T3Typography.supporting)
79 .foregroundStyle(T3Colors.textSecondary)
80 if let pullRequest = status.pullRequest {
81 if let url = pullRequest.url {
82 Link(destination: url) {
83 Label("PR #\(pullRequest.number) · \(pullRequest.title)", systemImage: "arrow.up.right.square")
84 }
85 } else {
86 LabeledContent("Pull Request", value: "#\(pullRequest.number) · \(pullRequest.state)")
87 }
88 }
89 }
90​
91 Section("Actions") {
92 if status.availableActions.isEmpty {
93 Text(status.isBusy ? "Source control operation in progress" : "No actions available")
94 .foregroundStyle(T3Colors.textSecondary)
95 }
96 ForEach(status.availableActions, id: \.self) { action in
97 Button {
98 begin(action)
99 } label: {
100 Label(action.title, systemImage: action.icon)
101 .frame(maxWidth: .infinity, alignment: .leading)
102 }
103 .disabled(isRunningAction)
104 }
105 }
106​
107 Section("\(status.files.count) changed \(status.files.count == 1 ? "file" : "files")") {
108 if status.files.isEmpty {
109 Label("Working tree clean", systemImage: "checkmark.circle")
110 .foregroundStyle(T3Colors.textSecondary)
111 }
112 ForEach(status.files) { file in
113 HStack(spacing: 10) {
114 Text(file.state.shortLabel)
115 .font(.caption2.monospaced().weight(.bold))
116 .foregroundStyle(file.state.color)
117 .frame(width: 18)
118 Text(file.path)
119 .font(T3Typography.threadBody)
120 .lineLimit(1)
121 Spacer()
122 if file.isStaged {
123 Text("STAGED")
124 .font(T3Typography.eyebrow)
125 .foregroundStyle(.green)
126 }
127 }
128 .accessibilityElement(children: .combine)
129 }
130 }
131 }
132 .listStyle(.insetGrouped)
133 .scrollContentBackground(.hidden)
134 .refreshable { await load() }
135 .overlay {
136 if isRunningAction {
137 ProgressView()
138 .padding(12)
139 .background(.ultraThinMaterial, in: RoundedRectangle(cornerRadius: 10))
140 }
141 }
142 }
143​
144 private func begin(_ action: FeatureSourceControlAction) {
145 if action.requiresMessage {
146 commitMessage = ""
147 pendingCommitAction = action
148 } else {
149 Task { await perform(action, message: nil) }
150 }
151 }
152​
153 private func load() async {
154 isLoading = true
155 defer { isLoading = false }
156 do {
157 status = try await client.sourceControlStatus(threadID: threadID)
158 errorMessage = nil
159 } catch {
160 errorMessage = error.localizedDescription
161 }
162 }
163​
164 private func perform(_ action: FeatureSourceControlAction, message: String?) async {
165 isRunningAction = true
166 defer { isRunningAction = false }
167 do {
168 status = try await client.performSourceControlAction(
169 threadID: threadID,
170 action: action,
171 message: message?.trimmingCharacters(in: .whitespacesAndNewlines)
172 )
173 errorMessage = nil
174 } catch {
175 errorMessage = error.localizedDescription
176 }
177 }
179​
180private extension FeatureSourceControlAction {
181 var requiresMessage: Bool {
182 switch self {
183 case .commit, .commitAndPush, .commitPushAndCreatePullRequest: true
184 case .push, .pull, .createPullRequest: false
185 }
186 }
187​
188 var title: String {
189 switch self {
190 case .commit: "Commit changes"
191 case .push: "Push"
192 case .pull: "Pull latest"
193 case .createPullRequest: "Create pull request"
194 case .commitAndPush: "Commit and push"
195 case .commitPushAndCreatePullRequest: "Commit, push, and create PR"
196 }
197 }
198​
199 var icon: String {
200 switch self {
201 case .commit: "checkmark.circle"
202 case .push: "arrow.up.circle"
203 case .pull: "arrow.down.circle"
204 case .createPullRequest: "arrow.triangle.pull"
205 case .commitAndPush: "arrow.up.circle.fill"
206 case .commitPushAndCreatePullRequest: "point.3.connected.trianglepath.dotted"
207 }
208 }
210​
211private extension FeatureSourceControlFileState {
212 var shortLabel: String {
213 switch self {
214 case .added: "A"
215 case .modified: "M"
216 case .deleted: "D"
217 case .renamed: "R"
218 case .untracked: "?"
219 case .conflicted: "!"
220 }
221 }
222​
223 var color: Color {
224 switch self {
225 case .added: .green
226 case .modified: .orange
227 case .deleted, .conflicted: .red
228 case .renamed: .blue
229 case .untracked: .secondary
230 }
231 }