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

SettingsRouteScreen.tsx

203 lines7.0 KB

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