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