gitbay/Views/MRs/MRListView.swift
252 lines · 9285 bytes
1import SwiftUI
2
3struct MRListView: View {
4
5 @State private var model: MRListViewModel
6 @State private var createModel: MRCreateViewModel
7 @State private var composing = false
8 @State private var filtering = false
9
10 init(client: GitbayClient, repo: String) {
11 _model = State(initialValue: MRListViewModel(client: client, repoPath: repo))
12 _createModel = State(initialValue: MRCreateViewModel(client: client, repoPath: repo))
13 }
14
15 var body: some View {
16 List {
17 Picker("State", selection: Bindable(model).filter.state) {
18 ForEach(MRFilter.State.allCases) { filter in
19 Text(filter.label).tag(filter)
20 }
21 }
22 .pickerStyle(.segmented)
23 .listRowBackground(Color.clear)
24 .listRowInsets(EdgeInsets())
25
26 ForEach(model.state.value ?? []) { mr in
27 NavigationLink(value: MRRoute.mr(repo: model.repoPath, number: mr.number)) {
28 MRRow(mr: mr)
29 }
30 }
31 PageFooter(list: model.list)
32 }
33 .overlay { LoadStateOverlay(state: model.state) }
34 .searchable(text: Bindable(model).filter.search, prompt: "Search titles and bodies")
35 .navigationTitle("Merge Requests")
36 .navigationBarTitleDisplayMode(.inline)
37 .toolbar {
38 ToolbarItem(placement: .topBarTrailing) {
39 Button {
40 filtering = true
41 } label: {
42 Image(systemName: model.filter.isNarrowed
43 ? "line.3.horizontal.decrease.circle.fill"
44 : "line.3.horizontal.decrease.circle")
45 }
46 .accessibilityIdentifier("mr-filter-button")
47 }
48 ToolbarItem(placement: .topBarTrailing) {
49 Button {
50 composing = true
51 } label: {
52 Image(systemName: "plus")
53 }
54 .accessibilityIdentifier("mr-create-button")
55 }
56 }
57 .sheet(isPresented: $filtering) {
58 FilterSheet(
59 title: "Filter Merge Requests",
60 author: Bindable(model).filter.author,
61 milestone: Bindable(model).filter.milestone,
62 milestones: model.milestones,
63 onClear: {
64 let state = model.filter.state
65 model.filter = MRFilter(state: state)
66 }
67 ) {
68 LabelFilterSection(
69 label: Bindable(model).filter.label,
70 labels: model.labels,
71 colors: model.colors
72 )
73 }
74 .task {
75 await model.loadMilestones()
76 await model.loadLabels()
77 }
78 }
79 .sheet(isPresented: $composing) {
80 MRCreateSheet(model: createModel) {
81 composing = false
82 Task { await model.load() }
83 }
84 }
85 .task { await model.loadIfNeeded() }
86 .refreshable { await model.load() }
87 }
88}
89
90/// Source and target are typed, not picked: no command lists branches
91/// yet (krz/gitbay#45). Target prefills with the default branch.
92private struct MRCreateSheet: View {
93
94 let model: MRCreateViewModel
95 let onCreated: () -> Void
96
97 @Environment(\.dismiss) private var dismiss
98 @State private var source = ""
99 @State private var target = ""
100 @State private var title = ""
101 @State private var bodyText = ""
102 @State private var format = "md"
103
104 var body: some View {
105 NavigationStack {
106 Form {
107 Section("Branches") {
108 TextField("Source branch", text: $source)
109 .autocorrectionDisabled()
110 .textInputAutocapitalization(.never)
111 .accessibilityIdentifier("mr-source")
112 TextField("Target branch", text: $target)
113 .autocorrectionDisabled()
114 .textInputAutocapitalization(.never)
115 .accessibilityIdentifier("mr-target")
116 }
117 Section("Title") {
118 TextField("Title", text: $title, axis: .vertical)
119 .lineLimit(1...3)
120 .autocorrectionDisabled()
121 .accessibilityIdentifier("mr-title")
122 }
123 Section("Body") {
124 TextEditor(text: $bodyText)
125 .frame(minHeight: 120)
126 .autocorrectionDisabled()
127 .accessibilityIdentifier("mr-body")
128 Picker("Format", selection: $format) {
129 Text("Markdown").tag("md")
130 Text("Org").tag("org")
131 }
132 .pickerStyle(.segmented)
133 .accessibilityIdentifier("mr-format")
134 }
135 Section {
136 Toggle("Draft", isOn: Bindable(model).draft)
137 .accessibilityIdentifier("mr-draft")
138 } footer: {
139 Text("A draft merge request is open but not asking for review: it won't merge and won't show up in a review queue.")
140 }
141 if let error = model.errorMessage {
142 Section {
143 GBNotice(error)
144 }
145 }
146 }
147 .navigationTitle("New Merge Request")
148 .navigationBarTitleDisplayMode(.inline)
149 .toolbar {
150 ToolbarItem(placement: .cancellationAction) {
151 Button("Cancel") { dismiss() }
152 }
153 ToolbarItem(placement: .confirmationAction) {
154 if model.working {
155 ProgressView()
156 } else {
157 Button("Create") {
158 model.format = format
159 Task {
160 if await model.create(
161 source: source, target: target,
162 title: title, body: bodyText
163 ) != nil {
164 onCreated()
165 }
166 }
167 }
168 .disabled(
169 source.trimmingCharacters(in: .whitespaces).isEmpty
170 || target.trimmingCharacters(in: .whitespaces).isEmpty
171 || title.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
172 )
173 .accessibilityIdentifier("mr-submit")
174 }
175 }
176 }
177 .interactiveDismissDisabled(model.working)
178 .task {
179 await model.loadDefaultBranch()
180 if target.isEmpty, let branch = model.defaultBranch {
181 target = branch
182 }
183 }
184 }
185 }
186}
187
188struct MRRow: View {
189 let mr: MergeRequest
190
191 var body: some View {
192 VStack(alignment: .leading, spacing: 4) {
193 HStack(alignment: .firstTextBaseline, spacing: 6) {
194 Text("!\(mr.number)")
195 .font(.gbMono(.caption))
196 .foregroundStyle(.secondary)
197 Text(mr.title)
198 .font(.gbSans(.subheadline).weight(.medium))
199 .lineLimit(2)
200 }
201 HStack(spacing: 6) {
202 MRStateBadge(state: mr.state)
203 if mr.draft {
204 GBChip("draft", .secondary)
205 }
206 Text(mr.source.isEmpty ? "(source gone)" : mr.source)
207 .lineLimit(1)
208 Image(systemName: "arrow.right")
209 .font(.gbSans(.caption2))
210 Text(mr.targetRef)
211 Spacer()
212 Text(mr.createdAt, format: .relative(presentation: .named))
213 .foregroundStyle(.secondary)
214 }
215 .font(.gbSans(.caption))
216 .foregroundStyle(.secondary)
217 if let stackedOn = mr.stackedOn {
218 Text("stacked on !\(stackedOn.number)")
219 .font(.gbSans(.caption2))
220 .foregroundStyle(.tertiary)
221 }
222 }
223 .padding(.vertical, 2)
224 }
225}
226
227struct MRStateBadge: View {
228 let state: String
229
230 var body: some View {
231 GBChip(state.replacingOccurrences(of: "_", with: " "), color)
232 }
233
234 /// The web's chip mapping: open=ok, merged=done, closed=bad,
235 /// source_gone=neutral.
236 private var color: Color {
237 switch state {
238 case "open": .gbOK
239 case "merged": .gbDone
240 case "closed": .gbBad
241 default: .secondary
242 }
243 }
244}
245
246/// MR navigation routes, separate from the repo stack's.
247nonisolated enum MRRoute: Hashable {
248 case list(repo: String)
249 case mr(repo: String, number: Int64)
250 case diff(repo: String, number: Int64)
251 case rangeDiff(repo: String, number: Int64, from: String, to: String)
252}