The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 304 lines 12.1 KB
Raw
1 import { SegmentedControl } from "../../components/ui/SegmentedControl";
2 import {
3 BasicAlertDialog,
4 Column,
5 DateTimePicker,
6 Host,
7 FilledTonalIconButton,
8 Row,
9 RNHostView,
10 Shape,
11 Surface,
12 Text,
13 TextButton,
14 } from "@expo/ui/jetpack-compose";
15 import {
16 padding,
17 size,
18 width,
19 fillMaxWidth,
20 testID,
21 verticalScroll,
22 } from "@expo/ui/jetpack-compose/modifiers";
23 import {
24 localSnoozeDate,
25 localSnoozeTime,
26 resolveCustomSnooze,
27 type CustomSnoozeInput,
28 } from "@t3tools/client-runtime/state/thread-settled";
29 import { requireNativeModule } from "expo";
30 import { msg, useGT, useMessages } from "gt-react-native";
31 import { useEffect, useState } from "react";
32 import { AppState, useWindowDimensions } from "react-native";
33
34 import { OverlayPortal } from "../../components/ui/OverlayPortal";
35 import { useFormattingLocale } from "../../i18n/I18nProvider";
36 import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
37 import { useScaledTextRole } from "../settings/appearance/useScaledTextRole";
38 import {
39 applySnoozePickerDate,
40 applySnoozePickerTime,
41 snoozeDateToPickerDate,
42 snoozePickerLabels,
43 } from "./customSnoozeDate";
44
45 type Props = {
46 readonly onClose: () => void;
47 readonly onSnooze: (snoozedUntil: string) => void;
48 };
49
50 const roundedCorner = Shape.RoundedCorner;
51
52 const modes = [
53 { value: "date", label: msg("Date and time") },
54 { value: "duration", label: msg("Duration") },
55 ] as const;
56 const units = [
57 { value: "minutes", label: msg("Minutes") },
58 { value: "hours", label: msg("Hours") },
59 { value: "days", label: msg("Days") },
60 ] as const;
61
62 function systemUses24HourClock() {
63 return requireNativeModule<{ is24HourFormat(): boolean }>("T3NativeControls").is24HourFormat();
64 }
65
66 export function CustomSnoozeSheet(props: Props) {
67 const gt = useGT();
68 const m = useMessages();
69 const locale = useFormattingLocale();
70 const { themeAppearance, themeVariables: colors } = useAppearancePreferences();
71 const [is24Hour, setIs24Hour] = useState(systemUses24HourClock);
72 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- subscribes to AppState to re-read the native 24-hour clock setting when the app resumes
73 useEffect(() => {
74 const subscription = AppState.addEventListener("change", (state) => {
75 if (state === "active") setIs24Hour(systemUses24HourClock());
76 });
77 return () => subscription.remove();
78 }, []);
79 const titleTypography = useScaledTextRole("title");
80 const bodyTypography = useScaledTextRole("footnote");
81 const { width: windowWidth } = useWindowDimensions();
82 const [mode, setMode] = useState<CustomSnoozeInput["mode"]>("date");
83 const [date, setDate] = useState(() => new Date(Date.now() + 3_600_000));
84 const [picker, setPicker] = useState<"date" | "time">("date");
85 const [amount, setAmount] = useState(2);
86 const [unit, setUnit] = useState<"minutes" | "hours" | "days">("hours");
87 const [error, setError] = useState<string | null>(null);
88 const pickerLabels = snoozePickerLabels(date, locale, is24Hour);
89 const submit = () => {
90 const input: CustomSnoozeInput =
91 mode === "date"
92 ? { mode, date: localSnoozeDate(date), time: localSnoozeTime(date) }
93 : { mode, amount: String(amount), unit };
94 const snoozedUntil = resolveCustomSnooze(input, new Date());
95 if (!snoozedUntil) {
96 setError(
97 mode === "date"
98 ? gt("Choose a date and time in the future.")
99 : gt("Enter a positive duration."),
100 );
101 return;
102 }
103 props.onSnooze(snoozedUntil);
104 props.onClose();
105 };
106 const pickerColors = {
107 containerColor: colors["--color-card-alt"],
108 titleContentColor: colors["--color-foreground-secondary"],
109 headlineContentColor: colors["--color-foreground"],
110 weekdayContentColor: colors["--color-foreground-secondary"],
111 subheadContentColor: colors["--color-foreground"],
112 navigationContentColor: colors["--color-foreground"],
113 yearContentColor: colors["--color-foreground"],
114 dayContentColor: colors["--color-foreground"],
115 currentYearContentColor: colors["--color-primary"],
116 selectedYearContainerColor: colors["--color-primary"],
117 selectedYearContentColor: colors["--color-primary-foreground"],
118 selectedDayContainerColor: colors["--color-primary"],
119 selectedDayContentColor: colors["--color-primary-foreground"],
120 todayContentColor: colors["--color-primary"],
121 todayDateBorderColor: colors["--color-primary"],
122 dividerColor: colors["--color-border"],
123 clockDialColor: colors["--color-secondary"],
124 clockDialSelectedContentColor: colors["--color-primary-foreground"],
125 clockDialUnselectedContentColor: colors["--color-secondary-foreground"],
126 selectorColor: colors["--color-primary"],
127 periodSelectorSelectedContainerColor: colors["--color-secondary"],
128 periodSelectorSelectedContentColor: colors["--color-secondary-foreground"],
129 periodSelectorUnselectedContentColor: colors["--color-foreground"],
130 timeSelectorSelectedContainerColor: colors["--color-secondary"],
131 timeSelectorSelectedContentColor: colors["--color-secondary-foreground"],
132 timeSelectorUnselectedContainerColor: colors["--color-card"],
133 timeSelectorUnselectedContentColor: colors["--color-foreground"],
134 };
135 return (
136 // Recycled thread rows can detach a zero-sized native dialog host.
137 <OverlayPortal>
138 <Host colorScheme={themeAppearance} style={{ height: 0, width: 0 }}>
139 <BasicAlertDialog
140 onDismissRequest={props.onClose}
141 properties={{ usePlatformDefaultWidth: false }}
142 modifiers={[width(Math.min(360, windowWidth - 32))]}
143 >
144 <Surface
145 color={colors["--color-card-alt"]}
146 contentColor={colors["--color-foreground"]}
147 shape={roundedCorner({
148 cornerRadii: { topStart: 28, topEnd: 28, bottomStart: 28, bottomEnd: 28 },
149 })}
150 >
151 <Column modifiers={[fillMaxWidth(), verticalScroll()]}>
152 <Column
153 verticalArrangement={{ spacedBy: 16 }}
154 modifiers={[fillMaxWidth(), padding(24, 24, 24, 16)]}
155 >
156 <Text style={titleTypography}>{gt("Custom snooze")}</Text>
157 <RNHostView matchContents={{ vertical: true }} modifiers={[fillMaxWidth()]}>
158 <SegmentedControl
159 variant="card"
160 options={modes.map((option) => ({ ...option, label: m(option.label) }))}
161 selected={mode}
162 onSelect={(value) => {
163 setMode(value);
164 setError(null);
165 }}
166 />
167 </RNHostView>
168 {mode === "date" ? (
169 <RNHostView matchContents={{ vertical: true }} modifiers={[fillMaxWidth()]}>
170 <SegmentedControl
171 variant="card"
172 options={[
173 { value: "date", label: pickerLabels.date },
174 { value: "time", label: pickerLabels.time },
175 ]}
176 selected={picker}
177 onSelect={setPicker}
178 />
179 </RNHostView>
180 ) : null}
181 </Column>
182 {mode === "date" ? (
183 <Column horizontalAlignment="center" modifiers={[fillMaxWidth()]}>
184 <SnoozeDateTimePicker
185 key={picker}
186 date={date}
187 picker={picker}
188 is24Hour={is24Hour}
189 colors={pickerColors}
190 onChange={(selected) => {
191 setDate((current) =>
192 picker === "date"
193 ? applySnoozePickerDate(current, selected)
194 : applySnoozePickerTime(current, selected),
195 );
196 setError(null);
197 }}
198 />
199 </Column>
200 ) : (
201 <Column
202 verticalArrangement={{ spacedBy: 16 }}
203 modifiers={[fillMaxWidth(), padding(24, 8, 24, 16)]}
204 >
205 <Row
206 horizontalArrangement="spaceEvenly"
207 verticalAlignment="center"
208 modifiers={[fillMaxWidth()]}
209 >
210 <FilledTonalIconButton
211 enabled={amount > 1}
212 onClick={() => setAmount((current) => Math.max(1, current - 1))}
213 colors={{
214 containerColor: colors["--color-secondary"],
215 contentColor: colors["--color-secondary-foreground"],
216 }}
217 modifiers={[size(48, 48), testID("snooze-decrease-duration")]}
218 >
219 <Text style={titleTypography}>−</Text>
220 </FilledTonalIconButton>
221 <Text style={titleTypography} modifiers={[testID("snooze-duration")]}>
222 {String(amount)}
223 </Text>
224 <FilledTonalIconButton
225 enabled={amount < 99}
226 onClick={() => setAmount((current) => Math.min(99, current + 1))}
227 colors={{
228 containerColor: colors["--color-secondary"],
229 contentColor: colors["--color-secondary-foreground"],
230 }}
231 modifiers={[size(48, 48), testID("snooze-increase-duration")]}
232 >
233 <Text style={titleTypography}>+</Text>
234 </FilledTonalIconButton>
235 </Row>
236 <RNHostView matchContents={{ vertical: true }} modifiers={[fillMaxWidth()]}>
237 <SegmentedControl
238 variant="card"
239 options={units.map((option) => ({ ...option, label: m(option.label) }))}
240 selected={unit}
241 onSelect={setUnit}
242 />
243 </RNHostView>
244 </Column>
245 )}
246 <Column
247 verticalArrangement={{ spacedBy: 16 }}
248 modifiers={[fillMaxWidth(), padding(24, 8, 24, 24)]}
249 >
250 {error ? (
251 <Text style={bodyTypography} color={colors["--color-danger-foreground"]}>
252 {error}
253 </Text>
254 ) : null}
255 <Row horizontalArrangement="end" modifiers={[fillMaxWidth()]}>
256 <TextButton
257 onClick={props.onClose}
258 colors={{ contentColor: colors["--color-foreground"] }}
259 >
260 <Text style={bodyTypography}>{gt("Cancel")}</Text>
261 </TextButton>
262 <TextButton
263 onClick={submit}
264 colors={{ contentColor: colors["--color-foreground"] }}
265 >
266 <Text style={bodyTypography}>
267 {gt("Snooze", { $context: "verb, confirm button" })}
268 </Text>
269 </TextButton>
270 </Row>
271 </Column>
272 </Column>
273 </Surface>
274 </BasicAlertDialog>
275 </Host>
276 </OverlayPortal>
277 );
278 }
279
280 function SnoozeDateTimePicker(props: {
281 readonly date: Date;
282 readonly picker: "date" | "time";
283 readonly is24Hour: boolean;
284 readonly colors: React.ComponentProps<typeof DateTimePicker>["elementColors"];
285 readonly onChange: (date: Date) => void;
286 }) {
287 // Changing initialDate resets Compose's picker state, including its active clock dial.
288 const [initialDate] = useState(() =>
289 props.picker === "date" ? snoozeDateToPickerDate(props.date) : props.date.toISOString(),
290 );
291 return (
292 <DateTimePicker
293 initialDate={initialDate}
294 displayedComponents={props.picker === "date" ? "date" : "hourAndMinute"}
295 is24Hour={props.is24Hour}
296 showVariantToggle={false}
297 elementColors={props.colors}
298 // Null marks incomplete input; keep the last valid date.
299 onDateSelected={(date) => {
300 if (date) props.onChange(date);
301 }}
302 />
303 );
304 }