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