gitbay/Views/MRs/MRListView.swift
253 lines · 9384 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 MarkupPreview(text: bodyText, format: format, identifier: "mr-create-preview")
135 }
136 Section {
137 Toggle("Draft", isOn: Bindable(model).draft)
138 .accessibilityIdentifier("mr-draft")
139 } footer: {
140 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.")
141 }
142 if let error = model.errorMessage {
143 Section {
144 GBNotice(error)
145 }
146 }
147 }
148 .navigationTitle("New Merge Request")
149 .navigationBarTitleDisplayMode(.inline)
150 .toolbar {
151 ToolbarItem(placement: .cancellationAction) {
152 Button("Cancel") { dismiss() }
153 }
154 ToolbarItem(placement: .confirmationAction) {
155 if model.working {
156 ProgressView()
157 } else {
158 Button("Create") {
159 model.format = format
160 Task {
161 if await model.create(
162 source: source, target: target,
163 title: title, body: bodyText
164 ) != nil {
165 onCreated()
166 }
167 }
168 }
169 .disabled(
170 source.trimmingCharacters(in: .whitespaces).isEmpty
171 || target.trimmingCharacters(in: .whitespaces).isEmpty
172 || title.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
173 )
174 .accessibilityIdentifier("mr-submit")
175 }
176 }
177 }
178 .interactiveDismissDisabled(model.working)
179 .task {
180 await model.loadDefaultBranch()
181 if target.isEmpty, let branch = model.defaultBranch {
182 target = branch
183 }
184 }
185 }
186 }
187}
188
189struct MRRow: View {
190 let mr: MergeRequest
191
192 var body: some View {
193 VStack(alignment: .leading, spacing: 4) {
194 HStack(alignment: .firstTextBaseline, spacing: 6) {
195 Text("!\(mr.number)")
196 .font(.gbMono(.caption))
197 .foregroundStyle(.secondary)
198 Text(mr.title)
199 .font(.gbSans(.subheadline).weight(.medium))
200 .lineLimit(2)
201 }
202 HStack(spacing: 6) {
203 MRStateBadge(state: mr.state)
204 if mr.draft {
205 GBChip("draft", .secondary)
206 }
207 Text(mr.source.isEmpty ? "(source gone)" : mr.source)
208 .lineLimit(1)
209 Image(systemName: "arrow.right")
210 .font(.gbSans(.caption2))
211 Text(mr.targetRef)
212 Spacer()
213 Text(mr.createdAt, format: .relative(presentation: .named))
214 .foregroundStyle(.secondary)
215 }
216 .font(.gbSans(.caption))
217 .foregroundStyle(.secondary)
218 if let stackedOn = mr.stackedOn {
219 Text("stacked on !\(stackedOn.number)")
220 .font(.gbSans(.caption2))
221 .foregroundStyle(.tertiary)
222 }
223 }
224 .padding(.vertical, 2)
225 }
226}
227
228struct MRStateBadge: View {
229 let state: String
230
231 var body: some View {
232 GBChip(state.replacingOccurrences(of: "_", with: " "), color)
233 }
234
235 /// The web's chip mapping: open=ok, merged=done, closed=bad,
236 /// source_gone=neutral.
237 private var color: Color {
238 switch state {
239 case "open": .gbOK
240 case "merged": .gbDone
241 case "closed": .gbBad
242 default: .secondary
243 }
244 }
245}
246
247/// MR navigation routes, separate from the repo stack's.
248nonisolated enum MRRoute: Hashable {
249 case list(repo: String)
250 case mr(repo: String, number: Int64)
251 case diff(repo: String, number: Int64)
252 case rangeDiff(repo: String, number: Int64, from: String, to: String)
253}