Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

FeatureRootView.swift

109 lines3.7 KB

1import SwiftUI
2​
3public struct FeatureRootView: View {
4 @State private var model: FeatureRootModel
5 private let navigationRequest: FeatureWorkspaceNavigationRequest?
6 private let onNavigationRequestConsumed: @MainActor (UUID) -> Void
7​
8 public init(client: any FeatureClient) {
9 _model = State(initialValue: FeatureRootModel(client: client))
10 navigationRequest = nil
11 onNavigationRequestConsumed = { _ in }
12 }
13​
14 init(
15 model: FeatureRootModel,
16 navigationRequest: FeatureWorkspaceNavigationRequest? = nil,
17 onNavigationRequestConsumed: @escaping @MainActor (UUID) -> Void = { _ in }
18 ) {
19 _model = State(initialValue: model)
20 self.navigationRequest = navigationRequest
21 self.onNavigationRequestConsumed = onNavigationRequestConsumed
22 }
23​
24 public var body: some View {
25 Group {
26 if model.isLoading {
27 FeatureLoadingView()
28 } else if shouldShowWorkspace {
29 WorkspaceView(
30 model: model,
31 navigationRequest: navigationRequest,
32 onNavigationRequestConsumed: onNavigationRequestConsumed,
33 submitNewTask: { request in
34 await model.startTask(request)
35 },
36 submitMessage: { submission in
37 await model.sendMessage(submission)
38 }
39 )
40 } else {
41 ConnectionOnboardingView(model: model)
42 }
43 }
44 .preferredColorScheme(preferredColorScheme)
45 .tint(T3Colors.accent)
46 .background(T3Colors.background.ignoresSafeArea())
47 .task { await model.start() }
48 .alert(
49 "Something went wrong",
50 isPresented: Binding(
51 get: { model.errorMessage != nil },
52 set: { if !$0 { model.errorMessage = nil } }
53 ),
54 actions: {
55 Button("OK") { model.errorMessage = nil }
56 },
57 message: {
58 Text(model.errorMessage ?? "Unknown error")
59 }
60 )
61 }
62​
63 /// Keep the last-known workspace visible through a degraded connection.
64 /// Connection management also stays mounted while saved servers are being
65 /// removed, so a disconnected fallback cannot destroy its own Settings sheet.
66 private var shouldShowWorkspace: Bool {
67 FeatureRootPresentation.showsWorkspace(
68 snapshot: model.snapshot,
69 isManagingConnections: model.isManagingConnections
70 )
71 }
72​
73 private var preferredColorScheme: ColorScheme? {
74 switch model.snapshot.settings.appearance {
75 case .system: nil
76 case .light: .light
77 case .dark: .dark
78 }
79 }
81​
82enum FeatureRootPresentation {
83 static func showsWorkspace(
84 snapshot: FeatureSnapshot,
85 isManagingConnections: Bool
86 ) -> Bool {
87 isManagingConnections
88 || !snapshot.environments.isEmpty
89 || !snapshot.projects.isEmpty
90 || !snapshot.threads.isEmpty
91 }
93​
94private struct FeatureLoadingView: View {
95 var body: some View {
96 VStack(spacing: 14) {
97 Image(systemName: "chevron.left.forwardslash.chevron.right")
98 .font(.system(size: 30, weight: .semibold))
99 ProgressView()
100 .controlSize(.small)
101 Text("Connecting to T3 Code")
102 .font(T3Typography.supporting)
103 .foregroundStyle(T3Colors.textSecondary)
104 }
105 .frame(maxWidth: .infinity, maxHeight: .infinity)
106 .background(T3Colors.background)
107 .accessibilityElement(children: .combine)
108 }