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

SettingsRouteScreen.tsx

256 lines8.8 KB

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