Open History to see the last commit
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 { currentWorkspaceSelections } from "../workspace/workspace-selection-store";
12import { rememberedSettingsPage } from "../workspace/workspace-tab-navigation";
13import { useSelectWorkspaceTab } from "../workspace/useSelectWorkspaceTab";
14import {
15 ScreenScrollView as ScrollView,
16 ScreenScrollViewBottomInset,
17} from "../../components/ui/ScreenScrollView";
18import { useAuth, useUser } from "@clerk/expo";
19import { useNavigation, useNavigationState } from "@react-navigation/native";
20import { use, useMemo } from "react";
21import { Platform, View } from "react-native";
22import { useWorkspaceSurface } from "../workspace/useWorkspaceSurface";
23import { useDeviceType } from "../../native/native-layout-metrics";
24import { useCompactWorkspaceTabs } from "../workspace/useSystemWorkspaceTabBar";
25import { deriveProjectGroupLabel } from "@t3tools/client-runtime/state/project-grouping";
26import { useSafeAreaInsets } from "react-native-safe-area-context";
27
28import { hasCloudPublicConfig } from "../cloud/publicConfig";
29import { useAdaptiveWorkspaceLayout } from "../layout/AdaptiveWorkspaceLayout";
30import { useSavedRemoteConnections } from "../../state/use-remote-environment-registry";
31import { SettingsRow } from "./components/SettingsRow";
32import { SettingsSection, SettingsSidebarContext } from "./components/SettingsSection";
33import { SettingsScreen } from "./components/SettingsScreen";
34import {
35 AndroidSettingsEnvironmentFilter,
36 SettingsEnvironmentFilterHeader,
37} from "./components/SettingsEnvironmentFilterHeader";
38import { useSettingsEnvironmentFilter } from "./settings-environment-filter";
39import { NativePrimaryColumnContext } from "../../native/v5-workspace-context";
40
41export function SettingsRouteScreen() {
42 const surface = useWorkspaceSurface();
43 const deviceType = useDeviceType();
44 const compactTabs = useCompactWorkspaceTabs();
45 const { layout } = useAdaptiveWorkspaceLayout();
46 const inSidebar = use(NativePrimaryColumnContext) !== null && layout.usesSplitView;
47 // The page open beside the list: the route right above the Settings tab's route. While
48 // another tab is open this list stays mounted, hidden, and must keep showing the page Settings
49 // reopens on; otherwise returning shows it unselected for a frame before the page arrives.
50 const selected = useNavigationState((state) => {
51 const list = state.routes.findIndex((route) => route.name === "Settings");
52 return list < 0
53 ? rememberedSettingsPage(currentWorkspaceSelections())
54 : (state.routes[list + 1]?.name ?? null);
55 });
56 const sidebar = useMemo(() => (inSidebar ? { selected } : null), [inSidebar, selected]);
57 const content = hasCloudPublicConfig() ? (
58 <ConfiguredSettingsRouteScreen />
59 ) : (
60 <LocalSettingsRouteScreen />
61 );
62
63 return (
64 <SettingsSidebarContext value={sidebar}>
65 <View className={`flex-1 ${inSidebar ? surface.className : "bg-screen"}`}>
66 <SettingsEnvironmentFilterHeader />
67 <ScreenScrollViewBottomInset value={deviceType === "pad" || compactTabs ? 80 : 0}>
68 {Platform.OS === "android" ? (
69 <SettingsScreen title="Settings" trailing={<AndroidSettingsEnvironmentFilter />}>
70 {content}
71 </SettingsScreen>
72 ) : (
73 content
74 )}
75 </ScreenScrollViewBottomInset>
76 </View>
77 </SettingsSidebarContext>
78 );
79}
80
81function ConfiguredSettingsRouteScreen() {
82 // UIKit's scroll-edge material takes the scroll view's own color; like the chat list, match
83 // the sidebar's surface so the bar doesn't darken over content at rest.
84 // Grouped cards only lift off the screen surface; the drawer is as dark as they are.
85 const listSurface = use(SettingsSidebarContext) ? "bg-drawer" : "bg-screen";
86 const insets = useSafeAreaInsets();
87 const navigation = useNavigation();
88 const { isLoaded, isSignedIn } = useAuth({ treatPendingAsSignedOut: false });
89 const { user } = useUser();
90 const { savedConnectionsById } = useSavedRemoteConnections();
91 const accountLabel = !isLoaded
92 ? "Checking"
93 : !isSignedIn
94 ? "Sign in"
95 : (user?.primaryEmailAddress?.emailAddress ?? "Signed in");
96
97 return (
98 <View collapsable={false} className="flex-1">
99 <ScrollView
100 contentInsetAdjustmentBehavior="automatic"
101 showsVerticalScrollIndicator={false}
102 className={`flex-1 ${listSurface}`}
103 contentContainerClassName="gap-4 px-5 pt-4"
104 contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 18) + 18 }}
105 >
106 <OverflowSection />
107 <SettingsSection title="Connections">
108 <SettingsRow
109 icon="person.crop.circle"
110 label="T3 Account"
111 value={accountLabel}
112 disabled={!isLoaded}
113 onPress={() => navigation.navigate("SettingsAuth")}
114 />
115 <SettingsRow
116 icon="desktopcomputer"
117 label="Environments"
118 value={`${Object.keys(savedConnectionsById).length}`}
119 valuePosition="trailing"
120 target="SettingsEnvironments"
121 />
122 <SettingsRow icon="bell.badge" label="Notifications" target="SettingsNotifications" />
123 </SettingsSection>
124
125 <SettingsIndexSections />
126 </ScrollView>
127 </View>
128 );
129}
130
131function LocalSettingsRouteScreen() {
132 // UIKit's scroll-edge material takes the scroll view's own color; like the chat list, match
133 // the sidebar's surface so the bar doesn't darken over content at rest.
134 // Grouped cards only lift off the screen surface; the drawer is as dark as they are.
135 const listSurface = use(SettingsSidebarContext) ? "bg-drawer" : "bg-screen";
136 const insets = useSafeAreaInsets();
137 const { savedConnectionsById } = useSavedRemoteConnections();
138 const environmentCount = Object.keys(savedConnectionsById).length;
139
140 return (
141 <View collapsable={false} className="flex-1">
142 <ScrollView
143 contentInsetAdjustmentBehavior="automatic"
144 showsVerticalScrollIndicator={false}
145 className={`flex-1 ${listSurface}`}
146 contentContainerClassName="gap-4 px-5 pt-4"
147 contentContainerStyle={{
148 paddingBottom: Math.max(insets.bottom, 18) + 18,
149 }}
150 >
151 <OverflowSection />
152 <SettingsSection title="Connections">
153 <SettingsRow
154 icon="desktopcomputer"
155 label="Environments"
156 value={`${environmentCount}`}
157 valuePosition="trailing"
158 target="SettingsEnvironments"
159 />
160 </SettingsSection>
161
162 <SettingsIndexSections />
163 </ScrollView>
164 </View>
165 );
166}
167
168function SettingsIndexSections() {
169 const { selectedTargets, projectGroups, selectedProjectKey } = useSettingsEnvironmentFilter();
170 const noServerTargets = selectedTargets.length === 0;
171 const selectedProject = projectGroups.find((group) => group.key === selectedProjectKey);
172 const scopedProjectMembers =
173 selectedProject?.members
174 .map((member) => member.project)
175 .filter((project) =>
176 selectedTargets.some((target) => target.environmentId === project.environmentId),
177 ) ?? [];
178 const projectLabel =
179 scopedProjectMembers.length > 0
180 ? deriveProjectGroupLabel({
181 representative: scopedProjectMembers[0]!,
182 members: scopedProjectMembers,
183 })
184 : (selectedProject?.label ?? "Unavailable project");
185 return (
186 <>
187 <SettingsSection title="Interface">
188 <SettingsRow icon="paintbrush" label="Appearance" target="SettingsAppearance" />
189 {navigationCustomizationAvailable() ? (
190 <SettingsRow
191 icon="line.3.horizontal"
192 label="Navigation tabs"
193 target="SettingsNavigation"
194 />
195 ) : null}
196 {Platform.OS === "ios" ? (
197 <>
198 <SettingsRow icon="keyboard" label="Keyboard" target="SettingsKeyboard" />
199 <SettingsRow icon="mic" label="Microphone" target="SettingsMicrophone" />
200 </>
201 ) : null}
202 </SettingsSection>
203
204 <SettingsSection title="Automations">
205 <SettingsRow icon="clock" label="Scheduled tasks" target="SettingsScheduledTasks" />
206 </SettingsSection>
207
208 <SettingsSection title="Projects & threads">
209 {selectedProjectKey !== null ? (
210 <SettingsRow
211 icon="folder"
212 label="Overview"
213 value={projectLabel}
214 target="SettingsProjectOverview"
215 />
216 ) : null}
217 <SettingsRow icon="folder" label="Organization" target="SettingsOrganization" />
218 <SettingsRow icon="text.bubble" label="Thread behavior" target="SettingsThreads" />
219 <SettingsRow icon="arrow.turn.left.up" label="Follow-ups" target="SettingsFollowUp" />
220 <SettingsRow icon="archivebox" label="Archived Threads" target="SettingsArchive" />
221 </SettingsSection>
222
223 <SettingsSection title="Server settings">
224 <SettingsRow
225 icon="person.crop.circle"
226 label="Provider accounts"
227 target="SettingsProviderAccounts"
228 disabled={noServerTargets}
229 />
230 <SettingsRow
231 icon="text.bubble"
232 label="New threads"
233 target="SettingsEnvironmentNewThreads"
234 disabled={noServerTargets}
235 />
236 <SettingsRow
237 icon="arrow.triangle.branch"
238 label="Source control"
239 target="SettingsEnvironmentSourceControl"
240 disabled={noServerTargets}
241 />
242 <SettingsRow
243 icon="text.alignleft"
244 label="Agent behavior"
245 target="SettingsEnvironmentAgentBehavior"
246 disabled={noServerTargets}
247 />
248 <SettingsRow
249 icon="arrow.clockwise"
250 label="Maintenance"
251 target="SettingsEnvironmentMaintenance"
252 disabled={noServerTargets}
253 />
254 </SettingsSection>
255
256 <SettingsSection title="App">
257 <SettingsRow icon="info.circle" label="About T3 Code" target="SettingsAbout" />
258 </SettingsSection>
259 </>
260 );
261}
262
263function OverflowSection() {
264 const order = useNavigationTabOrder();
265 const hidden = useHiddenNavigationTabs();
266 const { layout } = useAdaptiveWorkspaceLayout();
267 const tabs =
268 Platform.OS === "android" && layout.usesSplitView ? [] : overflowNavigationTabs(order, hidden);
269 const selectTab = useSelectWorkspaceTab();
270 return tabs.length ? (
271 <SettingsSection>
272 {tabs.map((tab) => (
273 <SettingsRow
274 key={tab}
275 icon={NAVIGATION_TAB_ICONS[tab]}
276 label={WORKSPACE_TAB_LABELS[tab]}
277 onPress={() => selectTab(tab)}
278 />
279 ))}
280 </SettingsSection>
281 ) : null;
282}