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

ThreadSettingsSections.tsx

239 lines9.2 KB

1import { Button } from "../../components/ui/Button";
2import { AuthSettingsWriteScope, type ServerSettingsPatch } from "@t3tools/contracts";
3import { useEnvironmentsWithScope } from "../../state/session";
4import { AutoSettleDaysField } from "./components/AutoSettleDaysField";
5import { ScreenScrollView as ScrollView } from "../../components/ui/ScreenScrollView";
6import { useAtomSet, useAtomValue } from "@effect/atom-react";
7import { AsyncResult } from "effect/reactivity";
8import { View } from "react-native";
9import { useSafeAreaInsets } from "react-native-safe-area-context";
10​
11import { DEFAULT_SERVER_SETTINGS } from "@t3tools/contracts";
12import { supportsSharedSettingsSync } from "@t3tools/client-runtime/state/shared-settings";
13import { AppText as Text } from "../../components/ui/AppText";
14import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../state/preferences";
15import { SettingsSection } from "./components/SettingsSection";
16import { SettingsSwitchRow } from "./components/SettingsSwitchRow";
17import { SettingsScreen } from "./components/SettingsScreen";
18import { planAutoSettleSettingsSync, type AutoSettleSettings } from "./autoSettleSettingsSync";
19import { useSettingsEnvironmentFilter } from "./settings-environment-filter";
20import { resolveMobileSettingsTargets, uniformMobileSetting } from "./settings-scoped-server";
21​
22export function SettingsLabsRouteScreen() {
23 const insets = useSafeAreaInsets();
24​
25 return (
26 <SettingsScreen title="Labs">
27 <ScrollView
28 contentInsetAdjustmentBehavior="automatic"
29 showsVerticalScrollIndicator={false}
30 className="flex-1"
31 contentContainerClassName="gap-6 px-5 pt-4"
32 contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 18) + 18 }}
33 >
34 <LegacySettingsSection />
35 </ScrollView>
36 </SettingsScreen>
37 );
39​
40const AUTO_SETTLE_DEFAULT_DAYS = DEFAULT_SERVER_SETTINGS.sidebarAutoSettleAfterDays ?? 3;
41​
42/**
43 * Usage-limit and auto-settle rows. The Threads page owns their writes, so they share its queue
44 * with the page's other project overrides, and their project override reset.
45 */
46export function AutoSettleSettingsRows(props: {
47 readonly write: (patch: ServerSettingsPatch) => void;
48 readonly pending: boolean;
49}) {
50 const { selectedTargets, projectGroups, selectedProjectKey } = useSettingsEnvironmentFilter();
51 const selectedProject = projectGroups.find((group) => group.key === selectedProjectKey);
52 const writableEnvironments = useEnvironmentsWithScope(selectedTargets, AuthSettingsWriteScope);
53 const projectSelected = selectedProjectKey !== null;
54​
55 const projectMembers = projectSelected
56 ? (selectedProject?.members.map((member) => member.project) ?? [])
57 : null;
58 const displayTargets = resolveMobileSettingsTargets(
59 selectedTargets.filter(supportsSharedSettingsSync),
60 projectMembers,
61 );
62 // The page saves to all its targets, so any read-only one blocks the save.
63 const canWriteSettings =
64 displayTargets.length > 0 &&
65 resolveMobileSettingsTargets(selectedTargets, projectMembers).every((target) =>
66 writableEnvironments.has(target.environment.environmentId),
67 );
68 const reference = displayTargets[0] ?? null;
69 const referenceSettings = reference?.settings ?? null;
70​
71 if (reference === null || referenceSettings === null) {
72 return null;
73 }
74​
75 const writeToAll = (
76 patch: Partial<AutoSettleSettings> & {
77 autoResumeLimitedThreads?: boolean;
78 snoozeLimitedThreads?: boolean;
79 },
80 ) => props.write(patch);
81​
82 const { patch: autoSettlePatch, mismatches } = planAutoSettleSettingsSync(
83 {
84 environmentId: reference.environment.environmentId,
85 projectId: reference.projectId,
86 settings: referenceSettings,
87 },
88 displayTargets.map((target) => ({
89 environmentId: target.environment.environmentId,
90 projectId: target.projectId,
91 label: target.environment.label,
92 settings: target.settings,
93 })),
94 );
95​
96 const supportsProjectOverrides = displayTargets.every(
97 (target) =>
98 target.environment.serverConfig.environment.capabilities.projectSettingsOverrides === true,
99 );
100 const disabled =
101 !canWriteSettings || props.pending || (projectSelected && !supportsProjectOverrides);
102 const afterDays = referenceSettings.sidebarAutoSettleAfterDays;
103​
104 return (
105 <View className="gap-6">
106 {!projectSelected ? (
107 <SettingsSection title="Usage limits">
108 <SettingsSwitchRow
109 icon="clock"
110 label="Resume after usage limits"
111 value={uniformMobileSetting(displayTargets, "autoResumeLimitedThreads")}
112 disabled={disabled}
113 onValueChange={(value) => writeToAll({ autoResumeLimitedThreads: value })}
114 />
115 <SettingsSwitchRow
116 icon="clock"
117 label="Snooze until limits reset"
118 value={uniformMobileSetting(displayTargets, "snoozeLimitedThreads")}
119 disabled={disabled}
120 onValueChange={(value) => writeToAll({ snoozeLimitedThreads: value })}
121 />
122 </SettingsSection>
123 ) : null}
124 <SettingsSection title="Auto-settle">
125 <SettingsSwitchRow
126 icon="arrow.triangle.branch"
127 label="Auto-settle merged threads"
128 value={referenceSettings.sidebarAutoSettleOnMerge}
129 disabled={disabled}
130 onValueChange={(value) => writeToAll({ sidebarAutoSettleOnMerge: value })}
131 />
132 <SettingsSwitchRow
133 icon="clock"
134 label="Auto-settle inactive threads"
135 value={afterDays !== null}
136 disabled={disabled}
137 onValueChange={(value) =>
138 writeToAll({ sidebarAutoSettleAfterDays: value ? AUTO_SETTLE_DEFAULT_DAYS : null })
139 }
140 />
141 {afterDays !== null ? (
142 <View className="flex-row items-center gap-4 px-4 py-4 android:min-h-14 android:py-3">
143 <View className="w-[22px] android:w-6" />
144 <Text className="flex-1 text-foreground text-lg android:text-base">Inactive days</Text>
145 <AutoSettleDaysField
146 value={afterDays}
147 disabled={disabled}
148 onValueChange={(value) => writeToAll({ sidebarAutoSettleAfterDays: value })}
149 />
150 </View>
151 ) : null}
152 </SettingsSection>
153 {!props.pending && mismatches.length > 0 ? (
154 <SettingsSection title="Across environments">
155 <View className="gap-3 p-4">
156 <Text className="text-base text-foreground">Auto-settle defaults differ</Text>
157 <Text className="text-sm text-foreground-muted">
158 {mismatches.map((mismatch) => mismatch.label).join(", ")}
159 </Text>
160 <Button
161 label="Apply auto-settle defaults"
162 tone="secondary"
163 disabled={disabled}
164 onPress={() => writeToAll(autoSettlePatch)}
165 />
166 </View>
167 </SettingsSection>
168 ) : null}
169 </View>
170 );
172​
173export function WorkingSectionSettings() {
174 const savePreferences = useAtomSet(updateMobilePreferencesAtom);
175 const preferences = useAtomValue(mobilePreferencesAtom);
176 const workingShelfEnabled =
177 AsyncResult.isSuccess(preferences) && preferences.value.workingShelfEnabled !== false;
178​
179 return (
180 <View className="gap-3">
181 <SettingsSection title="Sidebar">
182 <SettingsSwitchRow
183 icon="bolt.circle"
184 label="Working section"
185 value={workingShelfEnabled}
186 onValueChange={(value) => savePreferences({ workingShelfEnabled: value })}
187 />
188 </SettingsSection>
189 <Text className="px-2 text-sm text-foreground-muted">
190 Fold working and monitoring threads into a Working section. They return to the top of the
191 list when they need you. While this is on, active threads are ordered by time and cannot be
192 moved.
193 </Text>
194 </View>
195 );
197​
198export function NewThreadSettingsSection() {
199 const savePreferences = useAtomSet(updateMobilePreferencesAtom);
200 const preferences = useAtomValue(mobilePreferencesAtom);
201 const useLastProject =
202 AsyncResult.isSuccess(preferences) && preferences.value.useLastProjectForNewThreads !== false;
203​
204 return (
205 <SettingsSection title="New threads">
206 <SettingsSwitchRow
207 icon="square.and.pencil"
208 label="Use last project"
209 subtitle="Start new threads in the last project you used on this device."
210 value={useLastProject}
211 onValueChange={(value) => savePreferences({ useLastProjectForNewThreads: value })}
212 />
213 </SettingsSection>
214 );
216​
217function LegacySettingsSection() {
218 const savePreferences = useAtomSet(updateMobilePreferencesAtom);
219 const preferences = useAtomValue(mobilePreferencesAtom);
220 const planModeEnabled =
221 AsyncResult.isSuccess(preferences) && preferences.value.planModeEnabled === true;
222​
223 return (
224 <View className="gap-3">
225 <SettingsSection title="Legacy">
226 <SettingsSwitchRow
227 icon="hammer"
228 label="Plan Mode"
229 value={planModeEnabled}
230 onValueChange={(value) => savePreferences({ planModeEnabled: value })}
231 />
232 </SettingsSection>
233 <Text className="px-2 text-sm text-foreground-muted">
234 Opt into retired interfaces kept for compatibility. Plan Mode restores the Build/Plan
235 control; otherwise every task runs in Build mode.
236 </Text>
237 </View>
238 );