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

CustomSnoozeSheet.ios.tsx

284 lines9.9 KB

1import { Toolbar } from "../../components/ui/Toolbar";
2import {
3 DatePicker,
4 Host,
5 HStack,
6 Picker,
7 Popover,
8 RNHostView,
9 Spacer,
10 Text,
11 VStack,
12} from "@expo/ui/swift-ui";
13import {
14 accessibilityHidden,
15 clipped,
16 font,
17 datePickerStyle,
18 foregroundStyle,
19 frame,
20 labelsHidden,
21 padding,
22 pickerStyle,
23 tag,
24} from "@expo/ui/swift-ui/modifiers";
25import {
26 localSnoozeDate,
27 localSnoozeTime,
28 resolveCustomSnooze,
29 type CustomSnoozeInput,
30} from "@t3tools/client-runtime/state/thread-settled";
31import { useState, type ReactNode } from "react";
32import { NavigationContainer, NavigationIndependentTree } from "@react-navigation/native";
33import { createV5StackNavigator as createNativeStackNavigator } from "../../native/createV5StackNavigator";
34import { ScrollView, useWindowDimensions, View } from "react-native";
35import { useMobileNavigationTheme } from "../../lib/useMobileNavigationTheme";
36​
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 SnoozeStack = createNativeStackNavigator<{ CustomSnooze: undefined }>();
42const modes = [
43 { value: "date", label: "Date and time" },
44 { value: "duration", label: "Duration" },
45] as const;
46const units = [
47 { value: "minutes", label: "Minutes" },
48 { value: "hours", label: "Hours" },
49 { value: "days", label: "Days" },
50] as const;
51​
52export function CustomSnoozeSheet(props: {
53 readonly onClose: () => void;
54 readonly onSnooze: (snoozedUntil: string) => void;
55}) {
56 const { width, height } = useWindowDimensions();
57 const [mode, setMode] = useState<CustomSnoozeInput["mode"]>("date");
58 const [date, setDate] = useState(() => new Date(Date.now() + 3_600_000));
59 const [amount, setAmount] = useState(2);
60 const [unit, setUnit] = useState<"minutes" | "hours" | "days">("hours");
61 const [error, setError] = useState<string | null>(null);
62 const { themeVariables: colors, themeAppearance } = useAppearancePreferences();
63 const popoverWidth = Math.min(360, width - 32);
64 const popoverHeight = Math.min(error ? 364 : 324, height - 96);
65 const updateDate = (value: Date) => {
66 setDate(value);
67 setError(null);
68 };
69​
70 const submit = () => {
71 const input: CustomSnoozeInput =
72 mode === "date"
73 ? { mode, date: localSnoozeDate(date), time: localSnoozeTime(date) }
74 : { mode, amount: String(amount), unit };
75 const snoozedUntil = resolveCustomSnooze(input, new Date());
76 if (!snoozedUntil) {
77 setError(
78 mode === "date" ? "Choose a date and time in the future." : "Enter a positive duration.",
79 );
80 return;
81 }
82 props.onSnooze(snoozedUntil);
83 props.onClose();
84 };
85​
86 return (
87 <Host
88 style={{ position: "absolute", top: 0, right: 0, bottom: 0, left: 0 }}
89 colorScheme={themeAppearance}
90 seedColor={colors["--color-primary"]}
91 >
92 <Popover
93 isPresented
94 onIsPresentedChange={(presented) => {
95 if (!presented) props.onClose();
96 }}
97 >
98 <Popover.Trigger>
99 <Spacer
100 modifiers={[frame({ maxWidth: Infinity, maxHeight: Infinity }), accessibilityHidden()]}
101 />
102 </Popover.Trigger>
103 <Popover.Content>
104 <SnoozePopoverNavigation
105 width={popoverWidth}
106 height={popoverHeight}
107 onClose={props.onClose}
108 onSubmit={submit}
109 >
110 <Host
111 matchContents={{ vertical: true }}
112 colorScheme={themeAppearance}
113 style={{ width: popoverWidth }}
114 >
115 <VStack
116 spacing={16}
117 modifiers={[padding({ all: 16 }), foregroundStyle(colors["--color-foreground"])]}
118 >
119 <Picker
120 label="Snooze mode"
121 selection={mode}
122 onSelectionChange={(value: CustomSnoozeInput["mode"]) => {
123 setMode(value);
124 setError(null);
125 }}
126 modifiers={[pickerStyle("segmented")]}
127 >
128 {modes.map((option) => (
129 <Text key={option.value} modifiers={[tag(option.value)]}>
130 {option.label}
131 </Text>
132 ))}
133 </Picker>
134 {mode === "date" ? (
135 <DatePicker
136 title="Snooze until"
137 selection={date}
138 displayedComponents={["date", "hourAndMinute"]}
139 onDateChange={updateDate}
140 modifiers={[
141 datePickerStyle("wheel"),
142 labelsHidden(),
143 frame({ maxWidth: Infinity, minHeight: 180, maxHeight: 180 }),
144 ]}
145 />
146 ) : (
147 <HStack spacing={0}>
148 <Picker
149 label="Duration amount"
150 selection={amount}
151 onSelectionChange={(value: number) => {
152 setAmount(value);
153 setError(null);
154 }}
155 modifiers={[
156 pickerStyle("wheel"),
157 labelsHidden(),
158 frame({ minWidth: 0, maxWidth: Infinity, minHeight: 180, maxHeight: 180 }),
159 clipped(),
160 ]}
161 >
162 {durationAmounts.map((value) => (
163 <Text
164 key={value}
165 modifiers={[tag(value), foregroundStyle(colors["--color-foreground"])]}
166 >
167 {String(value)}
168 </Text>
169 ))}
170 </Picker>
171 <Picker
172 label="Duration unit"
173 selection={unit}
174 onSelectionChange={(value: typeof unit) => {
175 setUnit(value);
176 setError(null);
177 }}
178 modifiers={[
179 pickerStyle("wheel"),
180 labelsHidden(),
181 frame({ minWidth: 0, maxWidth: Infinity, minHeight: 180, maxHeight: 180 }),
182 clipped(),
183 ]}
184 >
185 {units.map((option) => (
186 <Text
187 key={option.value}
188 modifiers={[
189 tag(option.value),
190 foregroundStyle(colors["--color-foreground"]),
191 ]}
192 >
193 {option.label}
194 </Text>
195 ))}
196 </Picker>
197 </HStack>
198 )}
199 {error ? (
200 <Text
201 modifiers={[
202 font({ textStyle: "footnote" }),
203 foregroundStyle(colors["--color-danger-foreground"]),
204 ]}
205 >
206 {error}
207 </Text>
208 ) : null}
209 </VStack>
210 </Host>
211 </SnoozePopoverNavigation>
212 </Popover.Content>
213 </Popover>
214 </Host>
215 );
217​
218function SnoozePopoverNavigation(props: {
219 readonly width: number;
220 readonly height: number;
221 readonly onClose: () => void;
222 readonly onSubmit: () => void;
223 readonly children: ReactNode;
224}) {
225 const navigationTheme = useMobileNavigationTheme();
226 const { themeVariables: colors } = useAppearancePreferences();
227​
228 return (
229 <VStack modifiers={[frame({ width: props.width, height: props.height })]}>
230 <RNHostView>
231 <View style={{ width: props.width, height: props.height }}>
232 <NavigationIndependentTree>
233 <NavigationContainer theme={navigationTheme}>
234 <SnoozeStack.Navigator
235 screenOptions={{
236 contentStyle: { backgroundColor: "transparent" },
237 headerShadowVisible: false,
238 headerStyle: {
239 backgroundColor: NATIVE_LIQUID_GLASS_SUPPORTED
240 ? "transparent"
241 : colors["--color-card"],
242 },
243 headerTintColor: colors["--color-foreground"],
244 headerTitleStyle: { fontSize: 17, fontWeight: "700" },
245 headerTransparent: NATIVE_LIQUID_GLASS_SUPPORTED,
246 title: "Custom snooze",
247 }}
248 >
249 <SnoozeStack.Screen name="CustomSnooze">
250 {() => (
251 <>
252 <Toolbar placement="left">
253 <Toolbar.Button
254 accessibilityLabel="Cancel custom snooze"
255 icon="xmark"
256 onPress={props.onClose}
257 />
258 </Toolbar>
259 <Toolbar placement="right">
260 <Toolbar.Button
261 accessibilityLabel="Snooze"
262 icon="checkmark"
263 variant="prominent"
264 tintColor={colors["--color-prominent-action"]}
265 onPress={props.onSubmit}
266 />
267 </Toolbar>
268 <ScrollView
269 contentInsetAdjustmentBehavior="automatic"
270 showsVerticalScrollIndicator={false}
271 >
272 {props.children}
273 </ScrollView>
274 </>
275 )}
276 </SnoozeStack.Screen>
277 </SnoozeStack.Navigator>
278 </NavigationContainer>
279 </NavigationIndependentTree>
280 </View>
281 </RNHostView>
282 </VStack>
283 );