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

CustomSnoozeSheet.ios.tsx

279 lines9.8 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​
218/** The popover owns a native navigation bar, just like the app's sheet screens. */
219function SnoozePopoverNavigation(props: {
220 readonly width: number;
221 readonly height: number;
222 readonly onClose: () => void;
223 readonly onSubmit: () => void;
224 readonly children: ReactNode;
225}) {
226 const navigationTheme = useMobileNavigationTheme();
227 const { themeVariables: colors } = useAppearancePreferences();
228​
229 return (
230 <VStack modifiers={[frame({ width: props.width, height: props.height })]}>
231 <RNHostView>
232 <View style={{ width: props.width, height: props.height }}>
233 <NavigationIndependentTree>
234 <NavigationContainer theme={navigationTheme}>
235 <SnoozeStack.Navigator
236 screenOptions={{
237 contentStyle: { backgroundColor: "transparent" },
238 headerShadowVisible: false,
239 headerStyle: {
240 backgroundColor: NATIVE_LIQUID_GLASS_SUPPORTED
241 ? "transparent"
242 : colors["--color-card"],
243 },
244 headerTintColor: colors["--color-foreground"],
245 headerTitleStyle: { fontSize: 17, fontWeight: "700" },
246 headerTransparent: NATIVE_LIQUID_GLASS_SUPPORTED,
247 title: "Custom snooze",
248 }}
249 >
250 <SnoozeStack.Screen name="CustomSnooze">
251 {() => (
252 <>
253 <Toolbar placement="left">
254 <Toolbar.Button
255 accessibilityLabel="Cancel custom snooze"
256 icon="xmark"
257 onPress={props.onClose}
258 />
259 </Toolbar>
260 <Toolbar placement="right">
261 <Toolbar.Button label="Snooze" onPress={props.onSubmit} />
262 </Toolbar>
263 <ScrollView
264 contentInsetAdjustmentBehavior="automatic"
265 showsVerticalScrollIndicator={false}
266 >
267 {props.children}
268 </ScrollView>
269 </>
270 )}
271 </SnoozeStack.Screen>
272 </SnoozeStack.Navigator>
273 </NavigationContainer>
274 </NavigationIndependentTree>
275 </View>
276 </RNHostView>
277 </VStack>
278 );