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

SettingsRouteScreen.tsx

223 lines7.8 KB

1import { Toolbar } from "../../components/ui/Toolbar";
2import { ScreenScrollView as ScrollView } from "../../components/ui/ScreenScrollView";
3import { useAuth, useUser } from "@clerk/expo";
4import { useNavigation } from "@react-navigation/native";
5import { Platform, View } from "react-native";
6import { deriveProjectGroupLabel } from "@t3tools/client-runtime/state/project-grouping";
7import { useSafeAreaInsets } from "react-native-safe-area-context";
8​
9import { hasCloudPublicConfig } from "../cloud/publicConfig";
10import { useAdaptiveWorkspaceLayout } from "../layout/AdaptiveWorkspaceLayout";
11import { NativeStackScreenOptions } from "../../native/StackHeader";
12import { useSavedRemoteConnections } from "../../state/use-remote-environment-registry";
13import { SettingsRow } from "./components/SettingsRow";
14import { SettingsSection } from "./components/SettingsSection";
15import { SettingsScreen } from "./components/SettingsScreen";
16import {
17 AndroidSettingsEnvironmentFilter,
18 SettingsEnvironmentFilterHeader,
19} from "./components/SettingsEnvironmentFilterHeader";
20import { useSettingsEnvironmentFilter } from "./settings-environment-filter";
21​
22export function SettingsRouteScreen() {
23 const navigation = useNavigation();
24 const { layout } = useAdaptiveWorkspaceLayout();
25 const content = hasCloudPublicConfig() ? (
26 <ConfiguredSettingsRouteScreen />
27 ) : (
28 <LocalSettingsRouteScreen />
29 );
30​
31 return (
32 <>
33 {Platform.OS === "ios" ? (
34 <NativeStackScreenOptions options={{ headerBackVisible: !layout.usesSplitView }} />
35 ) : null}
36 {Platform.OS === "ios" && layout.usesSplitView ? (
37 <>
38 <Toolbar placement="left">
39 <Toolbar.Button
40 accessibilityLabel="Go back"
41 icon="chevron.left"
42 onPress={() => navigation.goBack()}
43 />
44 </Toolbar>
45 </>
46 ) : null}
47 <SettingsEnvironmentFilterHeader closeSettings />
48 {Platform.OS === "android" ? (
49 <SettingsScreen title="Settings" trailing={<AndroidSettingsEnvironmentFilter />}>
50 {content}
51 </SettingsScreen>
52 ) : (
53 content
54 )}
55 </>
56 );
58​
59function ConfiguredSettingsRouteScreen() {
60 const insets = useSafeAreaInsets();
61 const navigation = useNavigation();
62 const { isLoaded, isSignedIn } = useAuth({ treatPendingAsSignedOut: false });
63 const { user } = useUser();
64 const { savedConnectionsById } = useSavedRemoteConnections();
65 const accountLabel = !isLoaded
66 ? "Checking"
67 : !isSignedIn
68 ? "Sign in"
69 : (user?.primaryEmailAddress?.emailAddress ?? "Signed in");
70​
71 return (
72 <View collapsable={false} className="flex-1 bg-sheet">
73 <ScrollView
74 contentInsetAdjustmentBehavior="automatic"
75 showsVerticalScrollIndicator={false}
76 className="flex-1"
77 contentContainerClassName="gap-4 px-5 pt-4"
78 contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 18) + 18 }}
79 >
80 <SettingsSection title="Connections">
81 <SettingsRow
82 icon="person.crop.circle"
83 label="T3 Account"
84 value={accountLabel}
85 disabled={!isLoaded}
86 onPress={() => navigation.navigate("SettingsSheet", { screen: "SettingsAuth" })}
87 />
88 <SettingsRow
89 icon="desktopcomputer"
90 label="Environments"
91 value={`${Object.keys(savedConnectionsById).length}`}
92 valuePosition="trailing"
93 target="SettingsEnvironments"
94 />
95 <SettingsRow icon="bell.badge" label="Notifications" target="SettingsNotifications" />
96 </SettingsSection>
97​
98 <SettingsIndexSections />
99 </ScrollView>
100 </View>
101 );
103​
104function LocalSettingsRouteScreen() {
105 const insets = useSafeAreaInsets();
106 const { savedConnectionsById } = useSavedRemoteConnections();
107 const environmentCount = Object.keys(savedConnectionsById).length;
108​
109 return (
110 <View collapsable={false} className="flex-1 bg-sheet">
111 <ScrollView
112 contentInsetAdjustmentBehavior="automatic"
113 showsVerticalScrollIndicator={false}
114 className="flex-1"
115 contentContainerClassName="gap-4 px-5 pt-4"
116 contentContainerStyle={{
117 paddingBottom: Math.max(insets.bottom, 18) + 18,
118 }}
119 >
120 <SettingsSection title="Connections">
121 <SettingsRow
122 icon="desktopcomputer"
123 label="Environments"
124 value={`${environmentCount}`}
125 valuePosition="trailing"
126 target="SettingsEnvironments"
127 />
128 </SettingsSection>
129​
130 <SettingsIndexSections />
131 </ScrollView>
132 </View>
133 );
135​
136function SettingsIndexSections() {
137 const { selectedTargets, projectGroups, selectedProjectKey } = useSettingsEnvironmentFilter();
138 const noServerTargets = selectedTargets.length === 0;
139 const selectedProject = projectGroups.find((group) => group.key === selectedProjectKey);
140 const scopedProjectMembers =
141 selectedProject?.members
142 .map((member) => member.project)
143 .filter((project) =>
144 selectedTargets.some((target) => target.environmentId === project.environmentId),
145 ) ?? [];
146 const projectLabel =
147 scopedProjectMembers.length > 0
148 ? deriveProjectGroupLabel({
149 representative: scopedProjectMembers[0]!,
150 members: scopedProjectMembers,
151 })
152 : (selectedProject?.label ?? "Unavailable project");
153 return (
154 <>
155 <SettingsSection title="Interface">
156 <SettingsRow icon="paintbrush" label="Appearance" target="SettingsAppearance" />
157 {Platform.OS === "ios" ? (
158 <>
159 <SettingsRow icon="keyboard" label="Keyboard" target="SettingsKeyboard" />
160 <SettingsRow icon="mic" label="Microphone" target="SettingsMicrophone" />
161 </>
162 ) : null}
163 </SettingsSection>
164​
165 <SettingsSection title="Automations">
166 <SettingsRow icon="clock" label="Scheduled tasks" target="SettingsScheduledTasks" />
167 </SettingsSection>
168​
169 <SettingsSection title="Projects & threads">
170 {selectedProjectKey !== null ? (
171 <SettingsRow
172 icon="folder"
173 label="Overview"
174 value={projectLabel}
175 target="SettingsProjectOverview"
176 />
177 ) : null}
178 <SettingsRow icon="folder" label="Organization" target="SettingsOrganization" />
179 <SettingsRow icon="text.bubble" label="Thread behavior" target="SettingsThreads" />
180 <SettingsRow icon="arrow.turn.left.up" label="Follow-ups" target="SettingsFollowUp" />
181 <SettingsRow icon="archivebox" label="Archived Threads" target="SettingsArchive" />
182 </SettingsSection>
183​
184 <SettingsSection title="Server settings">
185 <SettingsRow
186 icon="person.crop.circle"
187 label="Provider accounts"
188 target="SettingsProviderAccounts"
189 disabled={noServerTargets}
190 />
191 <SettingsRow
192 icon="text.bubble"
193 label="New threads"
194 target="SettingsEnvironmentNewThreads"
195 disabled={noServerTargets}
196 />
197 <SettingsRow
198 icon="arrow.triangle.branch"
199 label="Source control"
200 target="SettingsEnvironmentSourceControl"
201 disabled={noServerTargets}
202 />
203 <SettingsRow
204 icon="text.alignleft"
205 label="Agent behavior"
206 target="SettingsEnvironmentAgentBehavior"
207 disabled={noServerTargets}
208 />
209 <SettingsRow
210 icon="arrow.clockwise"
211 label="Maintenance"
212 target="SettingsEnvironmentMaintenance"
213 disabled={noServerTargets}
214 />
215 </SettingsSection>
216​
217 <SettingsSection title="App">
218 <SettingsRow icon="chart.bar.xaxis" label="Usage" target="SettingsUsage" />
219 <SettingsRow icon="info.circle" label="About T3 Code" target="SettingsAbout" />
220 </SettingsSection>
221 </>
222 );