The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 252 lines 8.24 KB
Raw
1 import { SheetHeader } from "../../components/ui/SheetHeader";
2 import {
3 DatePicker,
4 Host,
5 HStack,
6 Picker,
7 Popover,
8 RNHostView,
9 Spacer,
10 Text,
11 VStack,
12 } from "@expo/ui/swift-ui";
13 import {
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";
25 import {
26 localSnoozeDate,
27 localSnoozeTime,
28 resolveCustomSnooze,
29 type CustomSnoozeInput,
30 } from "@t3tools/client-runtime/state/thread-settled";
31 import { msg, useGT, useMessages } from "gt-react-native";
32 import { useState, type ReactNode } from "react";
33 import { StandaloneStack } from "../layout/nested-stacks";
34 import { ScrollView, useWindowDimensions, View } from "react-native";
35
36 import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
37
38 const durationAmounts = Array.from({ length: 99 }, (_, index) => index + 1);
39 const modes = [
40 { value: "date", label: msg("Date and time") },
41 { value: "duration", label: msg("Duration") },
42 ] as const;
43 const units = [
44 { value: "minutes", label: msg("Minutes") },
45 { value: "hours", label: msg("Hours") },
46 { value: "days", label: msg("Days") },
47 ] as const;
48
49 export function CustomSnoozeSheet(props: {
50 readonly onClose: () => void;
51 readonly onSnooze: (snoozedUntil: string) => void;
52 }) {
53 const gt = useGT();
54 const m = useMessages();
55 const { width, height } = 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 popoverWidth = Math.min(360, width - 32);
63 const popoverHeight = Math.min(error ? 364 : 324, height - 96);
64 const updateDate = (value: Date) => {
65 setDate(value);
66 setError(null);
67 };
68
69 const submit = () => {
70 const input: CustomSnoozeInput =
71 mode === "date"
72 ? { mode, date: localSnoozeDate(date), time: localSnoozeTime(date) }
73 : { mode, amount: String(amount), unit };
74 const snoozedUntil = resolveCustomSnooze(input, new Date());
75 if (!snoozedUntil) {
76 setError(
77 mode === "date"
78 ? gt("Choose a date and time in the future.")
79 : gt("Enter a positive duration."),
80 );
81 return;
82 }
83 props.onSnooze(snoozedUntil);
84 props.onClose();
85 };
86
87 return (
88 <Host
89 style={{ position: "absolute", top: 0, right: 0, bottom: 0, left: 0 }}
90 colorScheme={themeAppearance}
91 seedColor={colors["--color-primary"]}
92 >
93 <Popover
94 isPresented
95 onIsPresentedChange={(presented) => {
96 if (!presented) props.onClose();
97 }}
98 >
99 <Popover.Trigger>
100 <Spacer
101 modifiers={[frame({ maxWidth: Infinity, maxHeight: Infinity }), accessibilityHidden()]}
102 />
103 </Popover.Trigger>
104 <Popover.Content>
105 <SnoozePopoverNavigation
106 width={popoverWidth}
107 height={popoverHeight}
108 onClose={props.onClose}
109 onSubmit={submit}
110 >
111 <Host
112 matchContents={{ vertical: true }}
113 colorScheme={themeAppearance}
114 style={{ width: popoverWidth }}
115 >
116 <VStack
117 spacing={16}
118 modifiers={[padding({ all: 16 }), foregroundStyle(colors["--color-foreground"])]}
119 >
120 <Picker
121 label={gt("Snooze mode")}
122 selection={mode}
123 onSelectionChange={(value: CustomSnoozeInput["mode"]) => {
124 setMode(value);
125 setError(null);
126 }}
127 modifiers={[pickerStyle("segmented")]}
128 >
129 {modes.map((option) => (
130 <Text key={option.value} modifiers={[tag(option.value)]}>
131 {m(option.label)}
132 </Text>
133 ))}
134 </Picker>
135 {mode === "date" ? (
136 <DatePicker
137 title={gt("Snooze until")}
138 selection={date}
139 displayedComponents={["date", "hourAndMinute"]}
140 onDateChange={updateDate}
141 modifiers={[
142 datePickerStyle("wheel"),
143 labelsHidden(),
144 frame({ maxWidth: Infinity, minHeight: 180, maxHeight: 180 }),
145 ]}
146 />
147 ) : (
148 <HStack spacing={0}>
149 <Picker
150 label={gt("Duration amount")}
151 selection={amount}
152 onSelectionChange={(value: number) => {
153 setAmount(value);
154 setError(null);
155 }}
156 modifiers={[
157 pickerStyle("wheel"),
158 labelsHidden(),
159 frame({ minWidth: 0, maxWidth: Infinity, minHeight: 180, maxHeight: 180 }),
160 clipped(),
161 ]}
162 >
163 {durationAmounts.map((value) => (
164 <Text
165 key={value}
166 modifiers={[tag(value), foregroundStyle(colors["--color-foreground"])]}
167 >
168 {String(value)}
169 </Text>
170 ))}
171 </Picker>
172 <Picker
173 label={gt("Duration unit")}
174 selection={unit}
175 onSelectionChange={(value: typeof unit) => {
176 setUnit(value);
177 setError(null);
178 }}
179 modifiers={[
180 pickerStyle("wheel"),
181 labelsHidden(),
182 frame({ minWidth: 0, maxWidth: Infinity, minHeight: 180, maxHeight: 180 }),
183 clipped(),
184 ]}
185 >
186 {units.map((option) => (
187 <Text
188 key={option.value}
189 modifiers={[
190 tag(option.value),
191 foregroundStyle(colors["--color-foreground"]),
192 ]}
193 >
194 {m(option.label)}
195 </Text>
196 ))}
197 </Picker>
198 </HStack>
199 )}
200 {error ? (
201 <Text
202 modifiers={[
203 font({ textStyle: "footnote" }),
204 foregroundStyle(colors["--color-danger-foreground"]),
205 ]}
206 >
207 {error}
208 </Text>
209 ) : null}
210 </VStack>
211 </Host>
212 </SnoozePopoverNavigation>
213 </Popover.Content>
214 </Popover>
215 </Host>
216 );
217 }
218
219 function 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 gt = useGT();
227 return (
228 <VStack modifiers={[frame({ width: props.width, height: props.height })]}>
229 <RNHostView>
230 <View style={{ width: props.width, height: props.height }}>
231 <StandaloneStack>
232 <SheetHeader
233 title={gt("Custom snooze")}
234 onCancel={props.onClose}
235 accessibilityLabel={gt("Cancel custom snooze")}
236 confirm={{
237 label: gt("Snooze", { $context: "verb, confirm button" }),
238 onPress: props.onSubmit,
239 }}
240 />
241 <ScrollView
242 contentInsetAdjustmentBehavior="automatic"
243 showsVerticalScrollIndicator={false}
244 >
245 {props.children}
246 </ScrollView>
247 </StandaloneStack>
248 </View>
249 </RNHostView>
250 </VStack>
251 );
252 }