aboutsummaryrefslogtreecommitdiff
path: root/Hutch/Views/Patchsets/PatchsetDetailView.swift
blob: 91aa417667e5c3076a9854bd8ed37bf9758e2392 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import SwiftUI

struct PatchsetDetailView: View {
    let patchsetID: Int
    let listName: String?

    @Environment(AppState.self) private var appState
    @State private var viewModel: PatchsetDetailViewModel?
    @State private var showStatusPicker = false
    @State private var expandedPatchIDs: Set<Int> = []

    var body: some View {
        Group {
            if let viewModel {
                content(viewModel)
            } else {
                SRHTLoadingStateView(message: "Loading Patchset…")
            }
        }
        .navigationTitle("Patchset")
        .navigationBarTitleDisplayMode(.inline)
        .toolbar {
            if let viewModel, viewModel.patchset != nil {
                ToolbarItem(placement: .topBarTrailing) {
                    actionsMenu(viewModel)
                }
            }
        }
        .task {
            let model = viewModel ?? PatchsetDetailViewModel(patchsetID: patchsetID, client: appState.client)
            viewModel = model
            await model.loadPatchset()
        }
    }

    @ViewBuilder
    private func content(_ viewModel: PatchsetDetailViewModel) -> some View {
        if viewModel.isLoading && viewModel.patchset == nil {
            SRHTLoadingStateView(message: "Loading Patchset…")
        } else if let patchset = viewModel.patchset {
            List {
                headerSection(patchset)
                if let coverLetter = patchset.coverLetter {
                    emailSection(coverLetter, title: "Cover Letter")
                }
                if !patchset.tools.isEmpty {
                    toolsSection(patchset)
                }
                patchesSection(patchset)
            }
            .themedList()
            // Inset grouped lays cells out at a rounded width while the content
            // measures itself at the unrounded one, so a long Text reflows to a
            // different height than the cell was sized for and the two chase each
            // other into a layout loop. ThreadDetailView renders the same bodies
            // through the same DiffView on a plain list without that fight.
            .listStyle(.plain)
            .refreshable { await viewModel.loadPatchset() }
            .overlay {
                if viewModel.isUpdatingStatus {
                    ProgressView()
                }
            }
            .confirmationDialog(
                "Set Status",
                isPresented: $showStatusPicker,
                titleVisibility: .visible
            ) {
                ForEach(PatchsetStatus.assignable, id: \.self) { status in
                    Button(status.displayName) {
                        Task { await viewModel.updateStatus(to: status) }
                    }
                }
                Button("Cancel", role: .cancel) {} // dismisses the dialog; no action needed
            }
            .alert(
                "Couldn't Update Patchset",
                isPresented: .init(
                    get: { viewModel.error != nil },
                    set: { if !$0 { viewModel.error = nil } }
                )
            ) {
                Button("OK", role: .cancel) { viewModel.error = nil }
            } message: {
                Text(viewModel.error ?? "")
            }
        } else if let error = viewModel.error {
            SRHTErrorStateView(
                title: "Couldn't Load Patchset",
                message: error,
                retryAction: { await viewModel.loadPatchset() }
            )
        }
    }

    // MARK: - Sections

    @ViewBuilder
    private func headerSection(_ patchset: PatchsetDetail) -> some View {
        Section {
            VStack(alignment: .leading, spacing: 8) {
                Text(patchset.subject)
                    .font(.headline)

                HStack(spacing: 8) {
                    PatchsetStatusBadge(status: patchset.status)
                    if patchset.version > 1 {
                        Text("v\(patchset.version)")
                            .font(.caption.weight(.medium))
                            .foregroundStyle(.secondary)
                    }
                    Text("\(patchset.patches.count) patch\(patchset.patches.count == 1 ? "" : "es")")
                        .font(.caption)
                        .foregroundStyle(.secondary)
                }

                Text("\(patchset.submitter.canonicalName)\(patchset.updated.relativeDescription)")
                    .font(.caption)
                    .foregroundStyle(.secondary)

                if let listName {
                    Text(listName)
                        .font(.caption)
                        .foregroundStyle(.secondary)
                }
            }
            .padding(.vertical, 2)
            .themedRow()

            // The version chain matters during review: a superseded series should
            // usually be read at its newest version instead.
            //
            // Pushed directly rather than by value, for the same reason as the rows
            // that lead here — this view inherits whatever stack presented it, and
            // not all of them declare a MoreRoute destination.
            if let supersededBy = patchset.supersededBy {
                NavigationLink {
                    PatchsetDetailView(patchsetID: supersededBy, listName: listName)
                } label: {
                    SwiftUI.Label("Superseded by a newer version", systemImage: "arrow.right.circle")
                        .font(.subheadline)
                }
                .themedRow()
            }

            if let supersedes = patchset.supersedes {
                NavigationLink {
                    PatchsetDetailView(patchsetID: supersedes, listName: listName)
                } label: {
                    SwiftUI.Label("Revises an earlier version", systemImage: "arrow.left.circle")
                        .font(.subheadline)
                }
                .themedRow()
            }
        }
    }

    @ViewBuilder
    private func toolsSection(_ patchset: PatchsetDetail) -> some View {
        Section("Checks") {
            ForEach(patchset.tools) { tool in
                HStack(spacing: 8) {
                    Image(systemName: tool.icon.systemImage)
                        .foregroundStyle(tool.icon == .failed ? .red : .secondary)
                    Text(tool.details)
                        .font(.subheadline)
                }
                .themedRow()
            }
        }
    }

    /// Patches start collapsed.
    ///
    /// A diff is tall, and a series is many of them. Rendering every patch expanded
    /// puts a dozen self-sizing diffs in one List, which drives UICollectionView
    /// into a recursive layout loop and wedges the app. The inbox thread view
    /// collapses all but the last message for the same reason.
    @ViewBuilder
    private func patchesSection(_ patchset: PatchsetDetail) -> some View {
        Section("Patches") {
            ForEach(patchset.patches) { patch in
                PatchRow(
                    patch: patch,
                    isExpanded: expandedPatchIDs.contains(patch.id),
                    onToggle: {
                        withAnimation(.easeInOut(duration: 0.2)) {
                            if expandedPatchIDs.contains(patch.id) {
                                expandedPatchIDs.remove(patch.id)
                            } else {
                                expandedPatchIDs.insert(patch.id)
                            }
                        }
                    }
                )
                .themedRow()
            }
        }
    }

    @ViewBuilder
    private func emailSection(_ email: PatchsetEmail, title: String) -> some View {
        Section(title) {
            VStack(alignment: .leading, spacing: 10) {
                Text(email.subject)
                    .font(.subheadline.weight(.semibold))
                    .textSelection(.enabled)

                PatchsetContentBlocks(blocks: email.contentBlocks)
            }
            .padding(.vertical, 4)
            .themedRow()
        }
    }

    // MARK: - Actions

    @ViewBuilder
    private func actionsMenu(_ viewModel: PatchsetDetailViewModel) -> some View {
        Menu {
            Button {
                showStatusPicker = true
            } label: {
                SwiftUI.Label("Set Status", systemImage: "flag")
            }
            .disabled(viewModel.isUpdatingStatus)

            if let mbox = viewModel.patchset?.mbox {
                Divider()
                ShareLink(item: mbox) {
                    SwiftUI.Label("Share mbox", systemImage: "square.and.arrow.up")
                }
                Button {
                    appState.copyToPasteboard(mbox.absoluteString, label: "mbox URL")
                } label: {
                    SwiftUI.Label("Copy mbox URL", systemImage: "doc.on.doc")
                }
            }
        } label: {
            Image(systemName: "ellipsis.circle")
        }
        .accessibilityLabel("Patchset actions")
    }
}

// MARK: - Patch Row

private struct PatchRow: View {
    let patch: PatchsetEmail
    let isExpanded: Bool
    let onToggle: () -> Void

    var body: some View {
        VStack(alignment: .leading, spacing: isExpanded ? 10 : 0) {
            Button(action: onToggle) {
                HStack(alignment: .top, spacing: 12) {
                    Image(systemName: isExpanded ? "chevron.down" : "chevron.right")
                        .font(.caption)
                        .foregroundStyle(.tertiary)
                        .padding(.top, 3)

                    VStack(alignment: .leading, spacing: 2) {
                        Text(patch.subject)
                            .font(.subheadline.weight(.medium))
                            .lineLimit(isExpanded ? nil : 2)
                            .multilineTextAlignment(.leading)
                            .frame(maxWidth: .infinity, alignment: .leading)

                        if let seriesLabel = patch.seriesLabel {
                            Text(seriesLabel)
                                .font(.caption)
                                .foregroundStyle(.secondary)
                        }
                    }
                }
            }
            .buttonStyle(.plain)
            .accessibilityHint(isExpanded ? "Collapses this patch" : "Expands this patch")

            if isExpanded {
                PatchsetContentBlocks(blocks: patch.contentBlocks)
            }
        }
        .padding(.vertical, 4)
    }
}

// MARK: - Content Blocks

private struct PatchsetContentBlocks: View {
    let blocks: [InboxMessageContentBlock]

    var body: some View {
        ForEach(Array(blocks.enumerated()), id: \.offset) { _, block in
            switch block {
            case .plainText(let text):
                Text(text)
                    .font(.body)
                    .textSelection(.enabled)
                    .frame(maxWidth: .infinity, alignment: .leading)
                    .fixedSize(horizontal: false, vertical: true)
            case .diff(let diff):
                DiffView(diff: diff)
                    .textSelection(.enabled)
            }
        }
    }
}

// MARK: - Status Badge

struct PatchsetStatusBadge: View {
    let status: PatchsetStatus

    var body: some View {
        SwiftUI.Label(status.displayName, systemImage: status.systemImage)
            .font(.caption.weight(.medium))
            .padding(.horizontal, 8)
            .padding(.vertical, 3)
            .background(background, in: Capsule())
            .foregroundStyle(foreground)
    }

    private var foreground: Color {
        switch status {
        case .applied, .approved: .green
        case .rejected: .red
        case .needsRevision: .orange
        case .superseded, .unknown, .proposed: .secondary
        }
    }

    private var background: Color {
        foreground.opacity(0.12)
    }
}