summaryrefslogtreecommitdiff
path: root/Hutch/App/RootView.swift
blob: e223766de0eb22362bc42b3f6aa6c4d3d98b2c56 (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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
import SwiftUI
import os

private let rootDeepLinkLogger = Logger(subsystem: "net.cleberg.Hutch", category: "DeepLink")

/// The root view of the app. Shows a TabView when authenticated, or a
/// full-screen sheet for token entry on first launch.
struct RootView: View {
    @Environment(AppState.self) private var appState
    @Environment(\.isAMOLEDTheme) private var isAMOLED
    @State private var homePath = NavigationPath()
    @State private var morePath = NavigationPath()
    @State private var repoPath = NavigationPath()
    @State private var buildsPath = NavigationPath()
    @State private var ticketsPath = NavigationPath()
    @State private var isResolvingDeepLink = false
    @State private var hasValidatedLaunch = false

    var body: some View {
        @Bindable var appState = appState

        Group {
            switch appState.authPhase {
            case .launching:
                ProgressView(appState.authStatusMessage)
                    .task {
                        guard !hasValidatedLaunch else { return }
                        hasValidatedLaunch = true
                        await appState.validateOnLaunch()
                    }

            case .unauthenticated:
                // Full-screen token entry that cannot be dismissed.
                TokenEntryView()

            case .authenticated:
                tabContent
            }
        }
        .onChange(of: appState.pendingDeepLink) { _, newValue in
            consumePendingDeepLinkIfPossible(newValue)
        }
        .onChange(of: appState.authPhase) { _, newPhase in
            handleAuthPhaseChange(newPhase)
        }
        .onChange(of: appState.pendingTabNavigation) { _, newValue in
            consumePendingTabNavigationIfPossible(newValue)
        }
        .alert(
            "Couldn't Open Link",
            isPresented: Binding(
                get: { appState.deepLinkError != nil },
                set: { isPresented in
                    if !isPresented {
                        appState.deepLinkError = nil
                    }
                }
            )
        ) {
            Button("OK") {
                appState.deepLinkError = nil
            }
        } message: {
            Text(appState.deepLinkError ?? "")
        }
    }

    // MARK: - Tab View

    private var tabContent: some View {
        @Bindable var appState = appState

        return TabView(selection: $appState.selectedTab) {
            NavigationStack(path: $homePath) {
                HomeView()
                    .navigationDestination(for: HomeRoute.self) { route in
                        switch route {
                        case .work(let scope):
                            WorkView(initialScope: scope)
                        }
                    }
            }
            .tag(AppState.Tab.home)
            .tabItem {
                Label("Home", systemImage: "house")
            }

            NavigationStack(path: $repoPath) {
                RepositoryListView()
            }
            .tag(AppState.Tab.repositories)
            .tabItem {
                Label("Repositories", systemImage: "book.closed")
            }

            NavigationStack(path: $ticketsPath) {
                TrackerListView()
                    // Deep link destination for jumping straight to a ticket.
                    .navigationDestination(for: TicketDeepLinkTarget.self) { target in
                        TicketDetailView(ownerUsername: target.ownerUsername, trackerName: target.trackerName, trackerId: target.trackerId, trackerRid: target.trackerRid, ticketId: target.ticketId)
                    }
            }
            .tag(AppState.Tab.tickets)
            .tabItem {
                Label("Trackers", systemImage: "checklist")
            }

            NavigationStack(path: $buildsPath) {
                BuildListView()
                    // Int destination used by deep links (hutch://builds/<id>).
                    // JobSummary destination is registered inside BuildListView.
                    .navigationDestination(for: Int.self) { jobId in
                        BuildDetailView(jobId: jobId)
                    }
            }
            .tag(AppState.Tab.builds)
            .tabItem {
                Label("Builds", systemImage: "hammer")
            }

            NavigationStack(path: $morePath) {
                MoreNavigationRoot()
            }
            .tag(AppState.Tab.more)
            .tabItem {
                Label("More", systemImage: "ellipsis.circle")
            }
        }
        .id(appState.sessionIdentity)
        .defaultAppStorage(appState.accountDefaults)
        .modifier(SidebarAdaptableTabStyle())
        .modifier(AMOLEDToolbarStyle(isAMOLED: isAMOLED))
        .modifier(TabKeyboardShortcuts(selectedTab: Binding(
            get: { appState.selectedTab },
            set: { appState.selectedTab = $0 }
        )))
        .safeAreaInset(edge: .bottom) {
            if let message = appState.copyConfirmationMessage {
                CopyConfirmationBadge(message: message)
                    .padding(.bottom, 4)
                    .transition(.move(edge: .bottom).combined(with: .opacity))
            }
        }
        .overlay {
            if isResolvingDeepLink {
                ZStack {
                    Color.black.opacity(0.3)
                        .ignoresSafeArea()
                    ProgressView("Opening link…")
                        .padding()
                        .background(.regularMaterial, in: RoundedRectangle(cornerRadius: 12))
                }
            }
        }
    }

    // MARK: - Deep Link Handling

    private func handleAuthPhaseChange(_ newPhase: AppState.AuthPhase) {
        rootDeepLinkLogger.info("Auth phase changed: \(String(describing: newPhase), privacy: .public); pendingDeepLink=\(String(describing: appState.pendingDeepLink), privacy: .public)")
        switch newPhase {
        case .launching:
            break
        case .unauthenticated:
            homePath = NavigationPath()
            morePath = NavigationPath()
            repoPath = NavigationPath()
            buildsPath = NavigationPath()
            ticketsPath = NavigationPath()
            appState.selectedTab = .home
            isResolvingDeepLink = false
        case .authenticated:
            consumePendingDeepLinkIfPossible(appState.pendingDeepLink)
        }
    }

    private func consumePendingDeepLinkIfPossible(_ link: DeepLink?) {
        rootDeepLinkLogger.info("Attempting to consume pending deep link. authenticated=\(appState.isAuthenticated, privacy: .public), link=\(String(describing: link), privacy: .public)")
        guard appState.isAuthenticated, let link else {
            rootDeepLinkLogger.info("Deferred deep link consumption.")
            return
        }
        handleDeepLink(link)
        appState.pendingDeepLink = nil
    }

    private func consumePendingTabNavigationIfPossible(_ target: AppState.TabNavigationTarget?) {
        guard appState.isAuthenticated, let target else { return }
        handleTabNavigation(target)
        appState.pendingTabNavigation = nil
    }

    private func handleDeepLink(_ link: DeepLink) {
        rootDeepLinkLogger.info("Handling deep link: \(String(describing: link), privacy: .public)")
        guard appState.isAuthenticated else {
            rootDeepLinkLogger.info("Ignoring deep link while unauthenticated: \(String(describing: link), privacy: .public)")
            return
        }

        switch link {
        case .home:
            homePath = NavigationPath()
            appState.selectedTab = .home

        case .recentActivity:
            homePath = NavigationPath()
            appState.selectedTab = .home

        case .repository(let service, let owner, let repo):
            resolveRepositoryLink(service: service, owner: owner, repo: repo)

        case .tracker(let owner, let tracker):
            resolveTrackerLink(owner: owner, tracker: tracker)

        case .build(let jobId):
            buildsPath = NavigationPath()
            appState.selectedTab = .builds
            Task {
                await settleNavigationTransition()
                buildsPath.append(jobId)
            }

        case .ticket(let owner, let tracker, let ticketId):
            resolveTicketLink(owner: owner, tracker: tracker, ticketId: ticketId)

        case .mailingList(let owner, let list):
            resolveMailingListLink(owner: owner, list: list)

        case .userProfile(let owner):
            resolveUserProfileLink(owner: owner)

        case .work:
            navigateToWork(scope: .all)

        case .workQueue(let scope):
            navigateToWork(scope: scope)

        case .projectDashboard(let id, let title):
            morePath = NavigationPath()
            appState.selectedTab = .more
            Task {
                await settleNavigationTransition()
                morePath.append(MoreRoute.projects)
                morePath.append(MoreRoute.projectDashboard(id: id, title: title))
            }

        case .failedBuilds:
            buildsPath = NavigationPath()
            appState.pendingBuildListFilter = .failed
            appState.selectedTab = .builds

        case .search(let query):
            morePath = NavigationPath()
            appState.selectedTab = .more
            Task {
                await settleNavigationTransition()
                morePath.append(MoreRoute.lookup(query: query))
            }

        case .lookup:
            morePath = NavigationPath()
            appState.selectedTab = .more
            Task {
                await settleNavigationTransition()
                morePath.append(MoreRoute.lookup(query: nil))
            }

        case .buildsTab:
            buildsPath = NavigationPath()
            appState.selectedTab = .builds

        case .repositoriesTab:
            repoPath = NavigationPath()
            appState.selectedTab = .repositories

        case .trackersTab:
            ticketsPath = NavigationPath()
            appState.selectedTab = .tickets

        case .systemStatus:
            appState.navigateToSystemStatus()
        }
    }

    private func navigateToWork(scope: HutchWorkQueueScope) {
        homePath = NavigationPath()
        appState.selectedTab = .home
        Task {
            await settleNavigationTransition()
            homePath.append(HomeRoute.work(scope: scope))
        }
    }

    private func handleTabNavigation(_ target: AppState.TabNavigationTarget) {
        switch target {
        case .repository(let repository):
            repoPath = NavigationPath()
            appState.selectedTab = .repositories
            Task {
                await settleNavigationTransition()
                repoPath.append(repository)
            }

        case .tracker(let tracker):
            ticketsPath = NavigationPath()
            appState.selectedTab = .tickets
            Task {
                await settleNavigationTransition()
                ticketsPath.append(tracker)
            }

        case .mailingList(let mailingList):
            morePath = NavigationPath()
            appState.selectedTab = .more
            Task {
                await settleNavigationTransition()
                morePath.append(MoreRoute.lists)
                morePath.append(MoreRoute.mailingList(mailingList))
            }
        case .systemStatus:
            morePath = NavigationPath()
            appState.selectedTab = .more
            Task {
                await settleNavigationTransition()
                morePath.append(MoreRoute.systemStatus)
            }
        case .builds:
            buildsPath = NavigationPath()
            appState.selectedTab = .builds
        }
    }

    private func resolveRepositoryLink(service: SRHTService, owner: String, repo: String) {
        isResolvingDeepLink = true
        Task {
            defer { isResolvingDeepLink = false }
            do {
                let summary = try await appState.resolveRepository(owner: owner, name: repo, service: service)
                repoPath = NavigationPath()
                appState.selectedTab = .repositories
                await settleNavigationTransition()
                repoPath.append(summary)
            } catch {
                appState.presentRepositoryDeepLinkError()
            }
        }
    }

    private func resolveTrackerLink(owner: String, tracker: String) {
        isResolvingDeepLink = true
        Task {
            defer { isResolvingDeepLink = false }
            do {
                let trackerSummary = try await appState.resolveTracker(owner: owner, name: tracker)
                ticketsPath = NavigationPath()
                appState.selectedTab = .tickets
                await settleNavigationTransition()
                ticketsPath.append(trackerSummary)
            } catch {
                appState.presentTicketDeepLinkError()
            }
        }
    }

    private func resolveMailingListLink(owner: String, list: String) {
        isResolvingDeepLink = true
        Task {
            defer { isResolvingDeepLink = false }
            do {
                let mailingList = try await appState.resolveMailingList(owner: owner, name: list)
                morePath = NavigationPath()
                appState.selectedTab = .more
                await settleNavigationTransition()
                morePath.append(MoreRoute.lists)
                morePath.append(MoreRoute.mailingList(mailingList))
            } catch {
                appState.deepLinkError = "The mailing list could not be found or is inaccessible."
            }
        }
    }

    private func resolveUserProfileLink(owner: String) {
        rootDeepLinkLogger.info("Routing user profile deep link for owner=\(owner, privacy: .public)")
        morePath = NavigationPath()
        appState.selectedTab = .more
        Task {
            await settleNavigationTransition()
            rootDeepLinkLogger.info("Appending user profile route for owner=\(owner, privacy: .public)")
            morePath.append(MoreRoute.userProfile(owner))
        }
    }

    private func resolveTicketLink(owner: String, tracker: String, ticketId: Int) {
        isResolvingDeepLink = true
        Task {
            defer { isResolvingDeepLink = false }
            do {
                let trackerSummary = try await appState.resolveTracker(owner: owner, name: tracker)
                ticketsPath = NavigationPath()
                appState.selectedTab = .tickets
                await settleNavigationTransition()
                ticketsPath.append(trackerSummary)
                ticketsPath.append(TicketDeepLinkTarget(
                    ownerUsername: String(trackerSummary.owner.canonicalName.dropFirst()),
                    trackerName: trackerSummary.name,
                    trackerId: trackerSummary.id,
                    trackerRid: trackerSummary.rid,
                    ticketId: ticketId
                ))
            } catch {
                appState.presentTicketDeepLinkError()
            }
        }
    }

    @MainActor
    private func settleNavigationTransition() async {
        await Task.yield()
        await Task.yield()
    }
}

enum MoreDestination: Hashable {
    case lists
    case pastes
    case settings
}

enum MoreRoute: Hashable {
    case lookup(query: String?)
    case projects
    case lists
    case pastes
    case profile
    case systemStatus
    case settings
    case about
    case userProfile(String)
    case projectDashboard(id: String, title: String?)
    case mailingList(InboxMailingListReference)
    case thread(InboxThreadSummary)
    case manPageBrowser
    case manPage(URL)
}

private struct MoreNavigationRoot: View {
    @Environment(AppState.self) private var appState

    var body: some View {
        MoreView()
            .navigationDestination(for: MoreRoute.self) { route in
                switch route {
                case .lookup(let query):
                    LookupView(initialQuery: query ?? "")
                case .projects:
                    ProjectsListView()
                case .lists:
                    MailingListListView()
                case .pastes:
                    PasteListView()
                case .profile:
                    ProfileView()
                case .systemStatus:
                    SystemStatusView()
                case .settings:
                    SettingsView()
                case .about:
                    AboutView()
                case .userProfile(let owner):
                    UserProfileDeepLinkView(owner: owner)
                case .projectDashboard(let id, let title):
                    ProjectDashboardDeepLinkView(projectID: id, title: title)
                case .mailingList(let mailingList):
                    MailingListDetailView(mailingList: mailingList)
                case .thread(let thread):
                    ThreadDetailView(
                        thread: thread,
                        onViewed: {
                            InboxReadStateStore.markViewed(max(Date(), thread.lastActivityAt), for: thread.id, defaults: appState.accountDefaults)
                            NeedsAttentionSnapshotStore.adjustUnreadInboxThreads(by: -1, accountID: appState.activeAccountID)
                        },
                        onMarkRead: {
                            InboxReadStateStore.markViewed(max(Date(), thread.lastActivityAt), for: thread.id, defaults: appState.accountDefaults)
                            NeedsAttentionSnapshotStore.adjustUnreadInboxThreads(by: -1, accountID: appState.activeAccountID)
                        },
                        onMarkUnread: {
                            InboxReadStateStore.markUnread(for: thread.id, defaults: appState.accountDefaults)
                            NeedsAttentionSnapshotStore.adjustUnreadInboxThreads(by: 1, accountID: appState.activeAccountID)
                        }
                    )
                case .manPageBrowser:
                    ManPageBrowserView()
                case .manPage(let url):
                    ManPageDetailView(url: url)
                }
            }
    }
}

struct UserProfileDeepLinkView: View {
    private let logger = Logger(subsystem: "net.cleberg.Hutch", category: "DeepLink")
    @Environment(AppState.self) private var appState
    let owner: String
    @State private var user: User?
    @State private var errorMessage: String?

    var body: some View {
        Group {
            if let user {
                UserProfileView(user: user)
            } else if let errorMessage {
                ContentUnavailableView("Couldn't Open Profile", systemImage: "person.crop.circle.badge.exclamationmark", description: Text(errorMessage))
            } else {
                SRHTLoadingStateView(message: "Loading profile...")
            }
        }
        .navigationTitle(displayOwner)
        .navigationBarTitleDisplayMode(.inline)
        .task(id: owner) {
            await loadProfile()
        }
    }

    private var displayOwner: String {
        owner.hasPrefix("~") ? owner : "~\(owner)"
    }

    @MainActor
    private func loadProfile() async {
        logger.info("Loading user profile for owner=\(owner, privacy: .public)")
        errorMessage = nil
        do {
            user = try await appState.resolveUser(username: owner)
            logger.info("Loaded user profile for owner=\(owner, privacy: .public), canonical=\(user?.canonicalName ?? "nil", privacy: .public)")
        } catch {
            logger.error("Failed loading user profile for owner=\(owner, privacy: .public): \(String(describing: error), privacy: .public)")
            errorMessage = "The user profile could not be found or is inaccessible."
        }
    }
}

struct ProjectDashboardDeepLinkView: View {
    @Environment(AppState.self) private var appState
    let projectID: String
    let title: String?
    @State private var project: Project?
    @State private var errorMessage: String?

    var body: some View {
        Group {
            if let project {
                ProjectDetailView(project: project)
            } else if let errorMessage {
                ContentUnavailableView(
                    "Couldn't Open Project",
                    systemImage: "square.stack.3d.up.slash",
                    description: Text(errorMessage)
                )
            } else {
                SRHTLoadingStateView(message: "Loading project...")
            }
        }
        .navigationTitle(title ?? "Project")
        .navigationBarTitleDisplayMode(.inline)
        .task(id: projectID) {
            await loadProject()
        }
    }

    @MainActor
    private func loadProject() async {
        errorMessage = nil
        do {
            project = try await ProjectService(client: appState.client).fetchProjectDetail(rid: projectID)
        } catch {
            errorMessage = "The project could not be found or is inaccessible."
        }
    }
}

// MARK: - Ticket Deep Link Navigation Target

/// Hashable wrapper to push a ticket detail view from a deep link.
struct TicketDeepLinkTarget: Hashable {
    let ownerUsername: String
    let trackerName: String
    let trackerId: Int
    let trackerRid: String
    let ticketId: Int
}

// MARK: - Keyboard Shortcuts for iPad + Hardware Keyboard

/// Adds Cmd+1 through Cmd+5 keyboard shortcuts for tab switching on iPad.
private struct TabKeyboardShortcuts: ViewModifier {
    @Binding var selectedTab: AppState.Tab

    private static let tabMap: [String: AppState.Tab] = [
        "1": .home,
        "2": .repositories,
        "3": .tickets,
        "4": .builds,
        "5": .more,
    ]

    func body(content: Content) -> some View {
        content
            .onKeyPress(characters: .decimalDigits, phases: .down) { press in
                guard press.modifiers == .command else { return .ignored }
                let key = String(press.characters)
                if let tab = Self.tabMap[key] {
                    selectedTab = tab
                    return .handled
                }
                return .ignored
            }
    }
}

// MARK: - AMOLED Toolbar Styling

/// Applies true-black backgrounds to the tab bar and navigation bar when the AMOLED theme is active.
private struct AMOLEDToolbarStyle: ViewModifier {
    let isAMOLED: Bool

    func body(content: Content) -> some View {
        if isAMOLED {
            content
                .toolbarBackground(Color.black, for: .tabBar)
                .toolbarBackground(.visible, for: .tabBar)
                .toolbarBackground(Color.black, for: .navigationBar)
                .toolbarBackground(.visible, for: .navigationBar)
        } else {
            content
        }
    }
}

// MARK: - iPad Sidebar Adaptable

/// Applies `.tabViewStyle(.sidebarAdaptable)` on iOS 18+ so the tab bar
/// becomes a full sidebar on iPad, while falling back to the standard tab
/// bar on earlier releases.
private struct SidebarAdaptableTabStyle: ViewModifier {
    func body(content: Content) -> some View {
        if #available(iOS 18.0, *) {
            content.tabViewStyle(.sidebarAdaptable)
        } else {
            content
        }
    }
}