summaryrefslogtreecommitdiff
path: root/Hutch/Views/Patchsets/PatchsetDetailView.swift
blob: 7bc5630af45b30fc73f80087b9f8c02c8d6f5b35 (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
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

    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()
            .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) {}
            }
            .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()
            }
        }
    }

    @ViewBuilder
    private func patchesSection(_ patchset: PatchsetDetail) -> some View {
        ForEach(patchset.patches) { patch in
            emailSection(patch, title: patch.seriesLabel.map { "Patch \($0)" } ?? "Patch")
        }
    }

    @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)

                ForEach(Array(email.contentBlocks.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)
                    }
                }
            }
            .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: - 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)
    }
}