|
| 1 | +import { DateTimePicker } from "@expo/ui/community/datetime-picker"; |
| 2 | +import { |
| 3 | + localSnoozeDate, |
| 4 | + localSnoozeTime, |
| 5 | + resolveCustomSnooze, |
| 6 | + type CustomSnoozeInput, |
| 7 | +} from "@t3tools/client-runtime/state/thread-settled"; |
| 8 | +import { useState } from "react"; |
| 9 | +import { |
| 10 | + KeyboardAvoidingView, |
| 11 | + Modal, |
| 12 | + Platform, |
| 13 | + Pressable, |
| 14 | + ScrollView, |
| 15 | + TextInput, |
| 16 | + View, |
| 17 | +} from "react-native"; |
| 18 | +import { useSafeAreaInsets } from "react-native-safe-area-context"; |
| 19 | +import { AppText } from "../../components/AppText"; |
| 20 | + |
| 21 | +export function CustomSnoozeSheet(props: { |
| 22 | + readonly onClose: () => void; |
| 23 | + readonly onSnooze: (snoozedUntil: string) => void; |
| 24 | +}) { |
| 25 | + const insets = useSafeAreaInsets(); |
| 26 | + const [mode, setMode] = useState<CustomSnoozeInput["mode"]>("date"); |
| 27 | + const [date, setDate] = useState(() => new Date(Date.now() + 3_600_000)); |
| 28 | + const [picker, setPicker] = useState<"date" | "time" | null>(null); |
| 29 | + const [amount, setAmount] = useState("2"); |
| 30 | + const [unit, setUnit] = useState<"minutes" | "hours" | "days">("hours"); |
| 31 | + const [error, setError] = useState<string | null>(null); |
| 32 | + return ( |
| 33 | + <Modal |
| 34 | + visible |
| 35 | + animationType="slide" |
| 36 | + presentationStyle="pageSheet" |
| 37 | + onRequestClose={props.onClose} |
| 38 | + > |
| 39 | + <KeyboardAvoidingView |
| 40 | + className="flex-1 bg-screen" |
| 41 | + behavior={Platform.OS === "ios" ? "padding" : undefined} |
| 42 | + > |
| 43 | + <ScrollView |
| 44 | + keyboardShouldPersistTaps="handled" |
| 45 | + contentInsetAdjustmentBehavior="automatic" |
| 46 | + contentContainerStyle={{ padding: 24, paddingBottom: insets.bottom + 24, gap: 20 }} |
| 47 | + > |
| 48 | + <AppText accessibilityRole="header" className="text-xl font-t3-semibold"> |
| 49 | + Custom snooze |
| 50 | + </AppText> |
| 51 | + <AppText className="text-base text-foreground-secondary"> |
| 52 | + Choose when snoozed threads return to your inbox. |
| 53 | + </AppText> |
| 54 | + <View className="flex-row gap-2"> |
| 55 | + {(["date", "duration"] as const).map((value) => ( |
| 56 | + <Pressable |
| 57 | + key={value} |
| 58 | + accessibilityRole="radio" |
| 59 | + accessibilityState={{ checked: mode === value }} |
| 60 | + className={ |
| 61 | + mode === value |
| 62 | + ? "min-h-12 flex-1 items-center justify-center rounded-xl bg-subtle px-3" |
| 63 | + : "min-h-12 flex-1 items-center justify-center rounded-xl px-3" |
| 64 | + } |
| 65 | + onPress={() => { |
| 66 | + setMode(value); |
| 67 | + setPicker(null); |
| 68 | + setError(null); |
| 69 | + }} |
| 70 | + > |
| 71 | + <AppText>{value === "date" ? "Date and time" : "Duration"}</AppText> |
| 72 | + </Pressable> |
| 73 | + ))} |
| 74 | + </View> |
| 75 | + {mode === "date" ? ( |
| 76 | + <View className="gap-3"> |
| 77 | + {(["date", "time"] as const).map((value) => ( |
| 78 | + <Pressable |
| 79 | + key={value} |
| 80 | + accessibilityRole="button" |
| 81 | + accessibilityLabel={value === "date" ? "Choose date" : "Choose time"} |
| 82 | + className="min-h-12 flex-row items-center justify-between rounded-xl bg-subtle px-3" |
| 83 | + onPress={() => setPicker(value)} |
| 84 | + > |
| 85 | + <AppText>{value === "date" ? "Date" : "Time"}</AppText> |
| 86 | + <AppText> |
| 87 | + {value === "date" |
| 88 | + ? date.toLocaleDateString() |
| 89 | + : date.toLocaleTimeString([], { hour: "numeric", minute: "2-digit" })} |
| 90 | + </AppText> |
| 91 | + </Pressable> |
| 92 | + ))} |
| 93 | + {picker && ( |
| 94 | + <DateTimePicker |
| 95 | + value={date} |
| 96 | + mode={picker} |
| 97 | + display={Platform.OS === "ios" ? "spinner" : "default"} |
| 98 | + onDismiss={() => setPicker(null)} |
| 99 | + onValueChange={(_, selected) => { |
| 100 | + const next = new Date(date); |
| 101 | + if (picker === "date") |
| 102 | + next.setFullYear( |
| 103 | + selected.getFullYear(), |
| 104 | + selected.getMonth(), |
| 105 | + selected.getDate(), |
| 106 | + ); |
| 107 | + else next.setHours(selected.getHours(), selected.getMinutes(), 0, 0); |
| 108 | + setDate(next); |
| 109 | + setError(null); |
| 110 | + if (Platform.OS === "android") setPicker(null); |
| 111 | + }} |
| 112 | + /> |
| 113 | + )} |
| 114 | + <AppText className="text-base text-foreground-secondary"> |
| 115 | + Your time zone: {new Intl.DateTimeFormat().resolvedOptions().timeZone}. |
| 116 | + </AppText> |
| 117 | + </View> |
| 118 | + ) : ( |
| 119 | + <View className="gap-3"> |
| 120 | + <AppText>Snooze for</AppText> |
| 121 | + <TextInput |
| 122 | + accessibilityLabel="Duration" |
| 123 | + className="min-h-12 rounded-xl bg-subtle px-3 text-base text-foreground" |
| 124 | + keyboardType="decimal-pad" |
| 125 | + value={amount} |
| 126 | + onChangeText={(value) => { |
| 127 | + setAmount(value); |
| 128 | + setError(null); |
| 129 | + }} |
| 130 | + /> |
| 131 | + <View className="flex-row gap-2"> |
| 132 | + {(["minutes", "hours", "days"] as const).map((value) => ( |
| 133 | + <Pressable |
| 134 | + key={value} |
| 135 | + accessibilityRole="radio" |
| 136 | + accessibilityState={{ checked: unit === value }} |
| 137 | + className={ |
| 138 | + unit === value |
| 139 | + ? "min-h-12 flex-1 items-center justify-center rounded-xl bg-subtle" |
| 140 | + : "min-h-12 flex-1 items-center justify-center rounded-xl" |
| 141 | + } |
| 142 | + onPress={() => { |
| 143 | + setUnit(value); |
| 144 | + setError(null); |
| 145 | + }} |
| 146 | + > |
| 147 | + <AppText> |
| 148 | + {value === "minutes" ? "Minutes" : value === "hours" ? "Hours" : "Days"} |
| 149 | + </AppText> |
| 150 | + </Pressable> |
| 151 | + ))} |
| 152 | + </View> |
| 153 | + <AppText className="text-base text-foreground-secondary"> |
| 154 | + Starts when you press Snooze. One day is 24 hours. |
| 155 | + </AppText> |
| 156 | + </View> |
| 157 | + )} |
| 158 | + {error && ( |
| 159 | + <AppText accessibilityRole="alert" className="text-danger-foreground"> |
| 160 | + {error} |
| 161 | + </AppText> |
| 162 | + )} |
| 163 | + <View className="flex-row justify-end gap-3"> |
| 164 | + <Pressable |
| 165 | + accessibilityRole="button" |
| 166 | + className="min-h-12 justify-center px-3" |
| 167 | + onPress={props.onClose} |
| 168 | + > |
| 169 | + <AppText>Cancel</AppText> |
| 170 | + </Pressable> |
| 171 | + <Pressable |
| 172 | + accessibilityRole="button" |
| 173 | + className="min-h-12 justify-center rounded-xl bg-primary px-3" |
| 174 | + onPress={() => { |
| 175 | + const input: CustomSnoozeInput = |
| 176 | + mode === "date" |
| 177 | + ? { mode, date: localSnoozeDate(date), time: localSnoozeTime(date) } |
| 178 | + : { mode, amount: amount.replace(",", "."), unit }; |
| 179 | + const snoozedUntil = resolveCustomSnooze(input, new Date()); |
| 180 | + if (!snoozedUntil) { |
| 181 | + setError( |
| 182 | + mode === "date" |
| 183 | + ? "Choose a date and time in the future." |
| 184 | + : "Enter a positive duration.", |
| 185 | + ); |
| 186 | + return; |
| 187 | + } |
| 188 | + props.onSnooze(snoozedUntil); |
| 189 | + props.onClose(); |
| 190 | + }} |
| 191 | + > |
| 192 | + <AppText className="text-primary-foreground">Snooze</AppText> |
| 193 | + </Pressable> |
| 194 | + </View> |
| 195 | + </ScrollView> |
| 196 | + </KeyboardAvoidingView> |
| 197 | + </Modal> |
| 198 | + ); |
| 199 | +} |
0 commit comments