summaryrefslogtreecommitdiff
path: root/Hutch/Views/Projects/ProjectDetailView.swift
blob: a04c526ee1254fb7937b30d20fb42831b9fd9ae0 (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
import SwiftUI

struct ProjectDetailView: View {
    let project: Project

    @Environment(AppState.self) private var appState
    @Environment(\.dismiss) private var dismiss
    @State private var detailProject: Project?
    @State private var isLoading = false
    @State private var error: String?
    @State private var pinChangeCount = 0

    private var displayedProject: Project {
        detailProject ?? project
    }

    private var currentUserKey: String? {
        appState.currentUser?.canonicalName
    }

    private var isPinnedToHome: Bool {
        _ = pinChangeCount
        guard let currentUserKey else { return false }
        return ProjectPinStore.isPinned(projectID: displayedProject.id, for: currentUserKey)
    }

    var body: some View {
        List {
            headerSection
            linksSection
            repositoriesSection
            trackersSection
            mailingListsSection
        }
        .themedList()
        .navigationTitle(displayedProject.name)
        .navigationBarTitleDisplayMode(.inline)
        .overlay {
            if isLoading, detailProject == nil, !project.isFullyLoaded {
                SRHTLoadingStateView(message: "Loading project…")
            } else if let error, detailProject == nil, !project.isFullyLoaded {
                SRHTErrorStateView(
                    title: "Couldn't Load Project",
                    message: error,
                    retryAction: { await loadProjectIfNeeded(forceRefresh: true) }
                )
            }
        }
        .toolbar {
            if currentUserKey != nil {
                ToolbarItem(placement: .topBarTrailing) {
                    Button {
                        togglePinnedState()
                    } label: {
                        Image(systemName: isPinnedToHome ? "pin.fill" : "pin")
                    }
                    .accessibilityLabel(isPinnedToHome ? "Unpin from Home" : "Pin to Home")
                }
            }
        }
        .task {
            await loadProjectIfNeeded()
        }
        .refreshable {
            await loadProjectIfNeeded(forceRefresh: true)
        }
        .srhtErrorBanner(error: $error)
    }

    @ViewBuilder
    private var headerSection: some View {
        Section {
            VStack(alignment: .leading, spacing: 10) {
                Text(displayedProject.name)
                    .font(.headline)

                if let description = displayedProject.description, !description.isEmpty {
                    Text(description)
                        .font(.subheadline)
                        .foregroundStyle(.secondary)
                }

                if !displayedProject.tags.isEmpty {
                    ScrollView(.horizontal, showsIndicators: false) {
                        HStack(spacing: 8) {
                            ForEach(displayedProject.tags, id: \.self) { tag in
                                Text(tag)
                                    .font(.caption.weight(.medium))
                                    .padding(.horizontal, 10)
                                    .padding(.vertical, 4)
                                    .background(.quaternary, in: Capsule())
                            }
                        }
                    }
                }

                LabeledContent("Updated", value: displayedProject.updated.relativeDescription)
            }
            .padding(.vertical, 4)
        }
    }

    @ViewBuilder
    private var linksSection: some View {
        let links = projectLinks(for: displayedProject)
        if !links.isEmpty {
            Section("Links") {
                ForEach(links) { link in
                    Link(destination: link.url) {
                        Label(link.title, systemImage: link.systemImage)
                            .font(.subheadline)
                    }
                }
            }
        }
    }

    @ViewBuilder
    private var repositoriesSection: some View {
        if !displayedProject.sources.isEmpty {
            Section("Repositories") {
                ForEach(displayedProject.sources) { source in
                    Button {
                        Task {
                            try? await appState.openProjectSource(source)
                            dismiss()
                        }
                    } label: {
                        ProjectResourceRow(
                            title: source.name,
                            subtitle: source.owner.canonicalName,
                            detail: source.description
                        )
                    }
                    .buttonStyle(.plain)
                }
            }
        }
    }

    @ViewBuilder
    private var trackersSection: some View {
        if !displayedProject.trackers.isEmpty {
            Section("Trackers") {
                ForEach(displayedProject.trackers) { tracker in
                    Button {
                        Task {
                            try? await appState.openProjectTracker(tracker)
                            dismiss()
                        }
                    } label: {
                        ProjectResourceRow(
                            title: tracker.name,
                            subtitle: tracker.owner.canonicalName,
                            detail: tracker.description
                        )
                    }
                    .buttonStyle(.plain)
                }
            }
        }
    }

    @ViewBuilder
    private var mailingListsSection: some View {
        if !displayedProject.mailingLists.isEmpty {
            Section("Mailing Lists") {
                ForEach(displayedProject.mailingLists) { mailingList in
                    Button {
                        appState.openMailingList(mailingList.inboxReference)
                        dismiss()
                    } label: {
                        ProjectResourceRow(
                            title: mailingList.name,
                            subtitle: mailingList.owner.canonicalName,
                            detail: mailingList.description
                        )
                    }
                    .buttonStyle(.plain)
                }
            }
        }
    }

    private func loadProjectIfNeeded(forceRefresh: Bool = false) async {
        guard forceRefresh || !project.isFullyLoaded else {
            detailProject = project
            return
        }
        guard !isLoading else { return }

        isLoading = true
        error = nil
        defer { isLoading = false }

        do {
            let service = ProjectService(client: appState.client)
            detailProject = try await service.fetchProjectDetail(rid: project.id)
        } catch {
            self.error = "Failed to load project"
        }
    }

    private func togglePinnedState() {
        guard let currentUserKey else { return }
        ProjectPinStore.togglePin(projectID: displayedProject.id, for: currentUserKey)
        pinChangeCount += 1
    }

    private func projectLinks(for project: Project) -> [ProjectLink] {
        var links: [ProjectLink] = []

        if let website = project.website?.trimmingCharacters(in: .whitespacesAndNewlines),
           let url = URL(string: website),
           !website.isEmpty {
            links.append(ProjectLink(id: "website", title: website, systemImage: "globe", url: url))
        }

        if let source = project.sources.first,
           let url = sourceURL(for: source) {
            links.append(ProjectLink(id: "primary-repo", title: "\(source.ownerUsername)/\(source.name)", systemImage: "book.closed", url: url))
        }

        if let tracker = project.trackers.first,
           let url = SRHTWebURL.tracker(ownerUsername: tracker.ownerUsername, trackerName: tracker.name) {
            links.append(ProjectLink(id: "primary-tracker", title: "\(tracker.ownerUsername)/\(tracker.name)", systemImage: "checklist", url: url))
        }

        return links
    }

    private func sourceURL(for source: Project.SourceRepo) -> URL? {
        var components = URLComponents()
        components.scheme = "https"
        components.host = "\(source.repoType.service.rawValue).sr.ht"
        components.percentEncodedPath = "/~\(source.ownerUsername)/\(source.name)"
        return components.url
    }
}

private struct ProjectLink: Identifiable {
    let id: String
    let title: String
    let systemImage: String
    let url: URL
}

private struct ProjectResourceRow: View {
    let title: String
    let subtitle: String
    let detail: String?

    var body: some View {
        VStack(alignment: .leading, spacing: 4) {
            Text(title)
                .font(.subheadline.weight(.medium))

            Text(subtitle)
                .font(.caption)
                .foregroundStyle(.secondary)
                .lineLimit(1)

            if let detail, !detail.isEmpty {
                Text(detail)
                    .font(.caption)
                    .foregroundStyle(.tertiary)
                    .lineLimit(2)
            }
        }
        .padding(.vertical, 2)
    }
}