|
| 1 | +import { |
| 2 | + Button, |
| 3 | + DatePicker, |
| 4 | + Host, |
| 5 | + HStack, |
| 6 | + Menu, |
| 7 | + Picker, |
| 8 | + Popover, |
| 9 | + Spacer, |
| 10 | + Text, |
| 11 | + VStack, |
| 12 | +} from "@expo/ui/swift-ui"; |
| 13 | +import { |
| 14 | + background, |
| 15 | + buttonStyle, |
| 16 | + datePickerStyle, |
| 17 | + font, |
| 18 | + foregroundStyle, |
| 19 | + frame, |
| 20 | + padding, |
| 21 | + pickerStyle, |
| 22 | + presentationBackground, |
| 23 | + shapes, |
| 24 | + tag, |
| 25 | +} from "@expo/ui/swift-ui/modifiers"; |
| 26 | +import { |
| 27 | + localSnoozeDate, |
| 28 | + localSnoozeTime, |
| 29 | + resolveCustomSnooze, |
| 30 | + type CustomSnoozeInput, |
| 31 | +} from "@t3tools/client-runtime/state/thread-settled"; |
| 32 | +import { useState } from "react"; |
| 33 | +import { Modal, Pressable, ScrollView, View } from "react-native"; |
| 34 | +import { AppText } from "../../components/AppText"; |
| 35 | +import { SegmentedControl } from "../../components/SegmentedControl"; |
| 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: "Date and time" }, |
| 41 | + { value: "duration", label: "Duration" }, |
| 42 | +] as const; |
| 43 | +const units = [ |
| 44 | + { value: "minutes", label: "Minutes" }, |
| 45 | + { value: "hours", label: "Hours" }, |
| 46 | + { value: "days", label: "Days" }, |
| 47 | +] as const; |
| 48 | + |
| 49 | +export function CustomSnoozeSheet(props: { |
| 50 | + readonly onClose: () => void; |
| 51 | + readonly onSnooze: (snoozedUntil: string) => void; |
| 52 | +}) { |
| 53 | + const [mode, setMode] = useState<CustomSnoozeInput["mode"]>("date"); |
| 54 | + const [date, setDate] = useState(() => new Date(Date.now() + 3_600_000)); |
| 55 | + const [amount, setAmount] = useState(2); |
| 56 | + const [amountOpen, setAmountOpen] = useState(false); |
| 57 | + const [unit, setUnit] = useState<"minutes" | "hours" | "days">("hours"); |
| 58 | + const [error, setError] = useState<string | null>(null); |
| 59 | + const { themeVariables: colors, themeAppearance, appearance } = useAppearancePreferences(); |
| 60 | + const updateDate = (value: Date) => { |
| 61 | + setDate(value); |
| 62 | + setError(null); |
| 63 | + }; |
| 64 | + |
| 65 | + const submit = () => { |
| 66 | + const input: CustomSnoozeInput = |
| 67 | + mode === "date" |
| 68 | + ? { mode, date: localSnoozeDate(date), time: localSnoozeTime(date) } |
| 69 | + : { mode, amount: String(amount), unit }; |
| 70 | + const snoozedUntil = resolveCustomSnooze(input, new Date()); |
| 71 | + if (!snoozedUntil) { |
| 72 | + setError( |
| 73 | + mode === "date" ? "Choose a date and time in the future." : "Enter a positive duration.", |
| 74 | + ); |
| 75 | + return; |
| 76 | + } |
| 77 | + props.onSnooze(snoozedUntil); |
| 78 | + props.onClose(); |
| 79 | + }; |
| 80 | + |
| 81 | + return ( |
| 82 | + <Modal visible transparent animationType="fade" onRequestClose={props.onClose}> |
| 83 | + <View className="flex-1 items-center justify-center bg-black/40 px-6"> |
| 84 | + <ScrollView |
| 85 | + className="max-h-[80%] w-full max-w-sm grow-0 rounded-3xl border border-border bg-sheet-solid" |
| 86 | + contentContainerStyle={{ padding: 20, gap: 16 }} |
| 87 | + > |
| 88 | + <View className="flex-row items-center justify-between gap-2"> |
| 89 | + <Pressable accessibilityRole="button" onPress={props.onClose} hitSlop={10}> |
| 90 | + <AppText className="text-base">Cancel</AppText> |
| 91 | + </Pressable> |
| 92 | + <AppText accessibilityRole="header" className="text-base font-t3-semibold"> |
| 93 | + Custom snooze |
| 94 | + </AppText> |
| 95 | + <Pressable accessibilityRole="button" onPress={submit} hitSlop={10}> |
| 96 | + <AppText className="text-base text-primary">Snooze</AppText> |
| 97 | + </Pressable> |
| 98 | + </View> |
| 99 | + <SegmentedControl |
| 100 | + options={modes} |
| 101 | + selected={mode} |
| 102 | + onSelect={(value) => { |
| 103 | + setMode(value); |
| 104 | + setError(null); |
| 105 | + }} |
| 106 | + role="tab" |
| 107 | + /> |
| 108 | + <Host |
| 109 | + matchContents={{ vertical: true }} |
| 110 | + style={{ width: "100%" }} |
| 111 | + colorScheme={themeAppearance} |
| 112 | + seedColor={colors["--color-primary"]} |
| 113 | + > |
| 114 | + <HStack |
| 115 | + spacing={6} |
| 116 | + modifiers={[ |
| 117 | + frame({ minHeight: 44 }), |
| 118 | + foregroundStyle(colors["--color-foreground"]), |
| 119 | + font({ size: appearance.baseFontSize }), |
| 120 | + ]} |
| 121 | + > |
| 122 | + <Text>{mode === "date" ? "Until" : "Snooze for"}</Text> |
| 123 | + <Spacer /> |
| 124 | + {mode === "date" ? ( |
| 125 | + <> |
| 126 | + <SnoozeDateField date={date} component="date" onChange={updateDate} /> |
| 127 | + <SnoozeDateField date={date} component="hourAndMinute" onChange={updateDate} /> |
| 128 | + </> |
| 129 | + ) : ( |
| 130 | + <> |
| 131 | + <Popover isPresented={amountOpen} onIsPresentedChange={setAmountOpen}> |
| 132 | + <Popover.Trigger> |
| 133 | + <Button |
| 134 | + onPress={() => setAmountOpen(true)} |
| 135 | + modifiers={[buttonStyle("plain")]} |
| 136 | + > |
| 137 | + <FieldLabel label={String(amount)} /> |
| 138 | + </Button> |
| 139 | + </Popover.Trigger> |
| 140 | + <Popover.Content> |
| 141 | + <VStack |
| 142 | + modifiers={[ |
| 143 | + padding({ all: 12 }), |
| 144 | + frame({ width: 180 }), |
| 145 | + presentationBackground(colors["--color-sheet-solid"]), |
| 146 | + ]} |
| 147 | + > |
| 148 | + <Picker |
| 149 | + label="Duration" |
| 150 | + selection={amount} |
| 151 | + onSelectionChange={(value: number) => { |
| 152 | + setAmount(value); |
| 153 | + setError(null); |
| 154 | + }} |
| 155 | + modifiers={[pickerStyle("wheel"), frame({ height: 160 })]} |
| 156 | + > |
| 157 | + {durationAmounts.map((value) => ( |
| 158 | + <Text |
| 159 | + key={value} |
| 160 | + modifiers={[ |
| 161 | + tag(value), |
| 162 | + foregroundStyle(colors["--color-foreground"]), |
| 163 | + ]} |
| 164 | + > |
| 165 | + {String(value)} |
| 166 | + </Text> |
| 167 | + ))} |
| 168 | + </Picker> |
| 169 | + <Button |
| 170 | + label="Done" |
| 171 | + onPress={() => setAmountOpen(false)} |
| 172 | + modifiers={[foregroundStyle(colors["--color-primary"])]} |
| 173 | + /> |
| 174 | + </VStack> |
| 175 | + </Popover.Content> |
| 176 | + </Popover> |
| 177 | + <Menu |
| 178 | + label={ |
| 179 | + <FieldLabel |
| 180 | + label={unit === "minutes" ? "Minutes" : unit === "hours" ? "Hours" : "Days"} |
| 181 | + /> |
| 182 | + } |
| 183 | + > |
| 184 | + {units.map((option) => ( |
| 185 | + <Button |
| 186 | + key={option.value} |
| 187 | + label={option.label} |
| 188 | + onPress={() => { |
| 189 | + setUnit(option.value); |
| 190 | + setError(null); |
| 191 | + }} |
| 192 | + /> |
| 193 | + ))} |
| 194 | + </Menu> |
| 195 | + </> |
| 196 | + )} |
| 197 | + </HStack> |
| 198 | + </Host> |
| 199 | + {error && <AppText className="text-sm text-danger-foreground">{error}</AppText>} |
| 200 | + </ScrollView> |
| 201 | + </View> |
| 202 | + </Modal> |
| 203 | + ); |
| 204 | +} |
| 205 | + |
| 206 | +function FieldLabel(props: { readonly label: string }) { |
| 207 | + const { themeVariables: colors } = useAppearancePreferences(); |
| 208 | + return ( |
| 209 | + <Text |
| 210 | + modifiers={[ |
| 211 | + padding({ horizontal: 12, vertical: 8 }), |
| 212 | + foregroundStyle(colors["--color-foreground"]), |
| 213 | + background(colors["--color-card"], shapes.capsule()), |
| 214 | + ]} |
| 215 | + > |
| 216 | + {props.label} |
| 217 | + </Text> |
| 218 | + ); |
| 219 | +} |
| 220 | + |
| 221 | +function SnoozeDateField(props: { |
| 222 | + readonly date: Date; |
| 223 | + readonly component: "date" | "hourAndMinute"; |
| 224 | + readonly onChange: (date: Date) => void; |
| 225 | +}) { |
| 226 | + const [open, setOpen] = useState(false); |
| 227 | + const { themeVariables: colors } = useAppearancePreferences(); |
| 228 | + return ( |
| 229 | + <Popover isPresented={open} onIsPresentedChange={setOpen}> |
| 230 | + <Popover.Trigger> |
| 231 | + <Button onPress={() => setOpen(true)} modifiers={[buttonStyle("plain")]}> |
| 232 | + <FieldLabel |
| 233 | + label={ |
| 234 | + props.component === "date" |
| 235 | + ? props.date.toLocaleDateString(undefined, { |
| 236 | + month: "short", |
| 237 | + day: "numeric", |
| 238 | + year: "numeric", |
| 239 | + }) |
| 240 | + : props.date.toLocaleTimeString(undefined, { hour: "numeric", minute: "2-digit" }) |
| 241 | + } |
| 242 | + /> |
| 243 | + </Button> |
| 244 | + </Popover.Trigger> |
| 245 | + <Popover.Content> |
| 246 | + <VStack |
| 247 | + modifiers={[ |
| 248 | + padding({ all: 12 }), |
| 249 | + frame({ width: props.component === "date" ? 340 : 300 }), |
| 250 | + presentationBackground(colors["--color-sheet-solid"]), |
| 251 | + ]} |
| 252 | + > |
| 253 | + <DatePicker |
| 254 | + selection={props.date} |
| 255 | + displayedComponents={[props.component]} |
| 256 | + onDateChange={props.onChange} |
| 257 | + modifiers={[datePickerStyle(props.component === "date" ? "graphical" : "wheel")]} |
| 258 | + /> |
| 259 | + <Button |
| 260 | + label="Done" |
| 261 | + onPress={() => setOpen(false)} |
| 262 | + modifiers={[foregroundStyle(colors["--color-primary"])]} |
| 263 | + /> |
| 264 | + </VStack> |
| 265 | + </Popover.Content> |
| 266 | + </Popover> |
| 267 | + ); |
| 268 | +} |
0 commit comments