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

CustomSnoozeSheet.ios.tsx

230 lines7.1 KB

1import {
2 Button,
3 DatePicker,
4 Host,
5 HStack,
6 Picker,
7 Popover,
8 Spacer,
9 Text,
10 VStack,
11} from "@expo/ui/swift-ui";
12import {
13 accessibilityAddTraits,
14 accessibilityHidden,
15 buttonBorderShape,
16 buttonStyle,
17 clipped,
18 controlSize,
19 font,
20 datePickerStyle,
21 foregroundStyle,
22 frame,
23 labelsHidden,
24 labelStyle,
25 padding,
26 pickerStyle,
27 tag,
28} from "@expo/ui/swift-ui/modifiers";
29import {
30 localSnoozeDate,
31 localSnoozeTime,
32 resolveCustomSnooze,
33 type CustomSnoozeInput,
34} from "@t3tools/client-runtime/state/thread-settled";
35import { useState } from "react";
36import { useWindowDimensions } from "react-native";
37import { NATIVE_LIQUID_GLASS_SUPPORTED } from "../../native/native-glass";
38import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
39​
40const durationAmounts = Array.from({ length: 99 }, (_, index) => index + 1);
41const modes = [
42 { value: "date", label: "Date and time" },
43 { value: "duration", label: "Duration" },
44] as const;
45const units = [
46 { value: "minutes", label: "Minutes" },
47 { value: "hours", label: "Hours" },
48 { value: "days", label: "Days" },
49] as const;
50​
51export function CustomSnoozeSheet(props: {
52 readonly onClose: () => void;
53 readonly onSnooze: (snoozedUntil: string) => void;
54}) {
55 const { width } = useWindowDimensions();
56 const [mode, setMode] = useState<CustomSnoozeInput["mode"]>("date");
57 const [date, setDate] = useState(() => new Date(Date.now() + 3_600_000));
58 const [amount, setAmount] = useState(2);
59 const [unit, setUnit] = useState<"minutes" | "hours" | "days">("hours");
60 const [error, setError] = useState<string | null>(null);
61 const { themeVariables: colors, themeAppearance } = useAppearancePreferences();
62 const updateDate = (value: Date) => {
63 setDate(value);
64 setError(null);
65 };
66​
67 const submit = () => {
68 const input: CustomSnoozeInput =
69 mode === "date"
70 ? { mode, date: localSnoozeDate(date), time: localSnoozeTime(date) }
71 : { mode, amount: String(amount), unit };
72 const snoozedUntil = resolveCustomSnooze(input, new Date());
73 if (!snoozedUntil) {
74 setError(
75 mode === "date" ? "Choose a date and time in the future." : "Enter a positive duration.",
76 );
77 return;
78 }
79 props.onSnooze(snoozedUntil);
80 props.onClose();
81 };
82​
83 return (
84 <Host
85 style={{ position: "absolute", top: 0, right: 0, bottom: 0, left: 0 }}
86 colorScheme={themeAppearance}
87 seedColor={colors["--color-primary"]}
88 >
89 <Popover
90 isPresented
91 onIsPresentedChange={(presented) => {
92 if (!presented) props.onClose();
93 }}
94 >
95 <Popover.Trigger>
96 <Spacer
97 modifiers={[frame({ maxWidth: Infinity, maxHeight: Infinity }), accessibilityHidden()]}
98 />
99 </Popover.Trigger>
100 <Popover.Content>
101 <VStack
102 spacing={16}
103 modifiers={[padding({ all: 16 }), frame({ width: Math.min(360, width - 32) })]}
104 >
105 <HStack spacing={12}>
106 <Button
107 label="Cancel"
108 systemImage="xmark"
109 role="cancel"
110 onPress={props.onClose}
111 modifiers={[
112 labelStyle("iconOnly"),
113 buttonStyle(NATIVE_LIQUID_GLASS_SUPPORTED ? "glass" : "bordered"),
114 buttonBorderShape("circle"),
115 controlSize("large"),
116 ]}
117 />
118 <Text
119 modifiers={[
120 font({ textStyle: "headline" }),
121 frame({ maxWidth: Infinity, alignment: "leading" }),
122 accessibilityAddTraits(["isHeader"]),
123 ]}
124 >
125 Custom snooze
126 </Text>
127 <Button
128 label="Snooze"
129 onPress={submit}
130 modifiers={[
131 buttonStyle(
132 NATIVE_LIQUID_GLASS_SUPPORTED ? "glassProminent" : "borderedProminent",
133 ),
134 controlSize("large"),
135 ]}
136 />
137 </HStack>
138 <Picker
139 label="Snooze mode"
140 selection={mode}
141 onSelectionChange={(value: CustomSnoozeInput["mode"]) => {
142 setMode(value);
143 setError(null);
144 }}
145 modifiers={[pickerStyle("segmented")]}
146 >
147 {modes.map((option) => (
148 <Text key={option.value} modifiers={[tag(option.value)]}>
149 {option.label}
150 </Text>
151 ))}
152 </Picker>
153 {mode === "date" ? (
154 <DatePicker
155 title="Snooze until"
156 selection={date}
157 displayedComponents={["date", "hourAndMinute"]}
158 onDateChange={updateDate}
159 modifiers={[
160 datePickerStyle("wheel"),
161 labelsHidden(),
162 frame({ maxWidth: Infinity, height: 180 }),
163 ]}
164 />
165 ) : (
166 <HStack spacing={0}>
167 <Picker
168 label="Duration amount"
169 selection={amount}
170 onSelectionChange={(value: number) => {
171 setAmount(value);
172 setError(null);
173 }}
174 modifiers={[
175 pickerStyle("wheel"),
176 labelsHidden(),
177 frame({ minWidth: 0, maxWidth: Infinity, height: 180 }),
178 clipped(),
179 ]}
180 >
181 {durationAmounts.map((value) => (
182 <Text
183 key={value}
184 modifiers={[tag(value), foregroundStyle(colors["--color-foreground"])]}
185 >
186 {String(value)}
187 </Text>
188 ))}
189 </Picker>
190 <Picker
191 label="Duration unit"
192 selection={unit}
193 onSelectionChange={(value: typeof unit) => {
194 setUnit(value);
195 setError(null);
196 }}
197 modifiers={[
198 pickerStyle("wheel"),
199 labelsHidden(),
200 frame({ minWidth: 0, maxWidth: Infinity, height: 180 }),
201 clipped(),
202 ]}
203 >
204 {units.map((option) => (
205 <Text
206 key={option.value}
207 modifiers={[tag(option.value), foregroundStyle(colors["--color-foreground"])]}
208 >
209 {option.label}
210 </Text>
211 ))}
212 </Picker>
213 </HStack>
214 )}
215 {error ? (
216 <Text
217 modifiers={[
218 font({ textStyle: "footnote" }),
219 foregroundStyle(colors["--color-danger-foreground"]),
220 ]}
221 >
222 {error}
223 </Text>
224 ) : null}
225 </VStack>
226 </Popover.Content>
227 </Popover>
228 </Host>
229 );