Open History to see the last commit
1import { useEffect, useId, useState } from "react";
2import { Tabs } from "@base-ui/react/tabs";
3import { create } from "zustand";
4import {
5 localSnoozeDate,
6 localSnoozeTime,
7 resolveCustomSnooze,
8 type CustomSnoozeInput,
9} from "@t3tools/client-runtime/state/thread-settled";
10import { Button } from "./ui/button";
11import { CalendarIcon } from "lucide-react";
12import { Calendar } from "./ui/calendar";
13import { weekStartsOn } from "../timestampFormat";
14import { Popover, PopoverTrigger, PopoverPopup } from "./ui/popover";
15import { Input } from "./ui/input";
16import { Label } from "./ui/label";
17import { toggleVariants } from "./ui/toggle";
18import { Select, SelectTrigger, SelectValue, SelectPopup, SelectItem } from "./ui/select";
19import {
20 NumberField,
21 NumberFieldGroup,
22 NumberFieldInput,
23 NumberFieldDecrement,
24 NumberFieldIncrement,
25} from "./ui/number-field";
26import {
27 Dialog,
28 DialogPopup,
29 DialogHeader,
30 DialogTitle,
31 DialogDescription,
32 DialogPanel,
33 DialogFooter,
34} from "./ui/dialog";
35
36type SnoozeChoice = { readonly snoozedUntil: string };
37type Request = { readonly resolve: (choice: SnoozeChoice | null) => void };
38const useRequest = create<{ request: Request | null }>(() => ({ request: null }));
39
40export function requestCustomSnooze(): Promise<SnoozeChoice | null> {
41 useRequest.getState().request?.resolve(null);
42 return new Promise((resolve) => useRequest.setState({ request: { resolve } }));
43}
44
45function finish(choice: SnoozeChoice | null) {
46 const request = useRequest.getState().request;
47 useRequest.setState({ request: null });
48 request?.resolve(choice);
49}
50
51export function CustomSnoozeDialogHost() {
52 const request = useRequest((state) => state.request);
53 useEffect(() => () => finish(null), []);
54 return request ? <CustomSnoozeDialog /> : null;
55}
56
57function CustomSnoozeDialog() {
58 const id = useId();
59 const [initial] = useState(() => new Date(Date.now() + 3_600_000));
60 const [mode, setMode] = useState<CustomSnoozeInput["mode"]>("date");
61 const [date, setDate] = useState(initial);
62 const [calendarOpen, setCalendarOpen] = useState(false);
63 const [time, setTime] = useState(localSnoozeTime(initial));
64 const [amount, setAmount] = useState("2");
65 const [unit, setUnit] = useState<"minutes" | "hours" | "days">("hours");
66 const [error, setError] = useState<string | null>(null);
67 const input: CustomSnoozeInput =
68 mode === "date" ? { mode, date: localSnoozeDate(date), time } : { mode, amount, unit };
69 return (
70 <Dialog
71 open
72 onOpenChange={(open) => {
73 if (!open) finish(null);
74 }}
75 >
76 <DialogPopup className="sm:max-w-sm">
77 <form
78 className="flex min-h-0 flex-col"
79 onSubmit={(event) => {
80 event.preventDefault();
81 const snoozedUntil = resolveCustomSnooze(input, new Date());
82 if (!snoozedUntil) {
83 setError(
84 mode === "date"
85 ? "Choose a valid date and time in the future."
86 : "Enter a positive duration.",
87 );
88 return;
89 }
90 finish({ snoozedUntil });
91 }}
92 >
93 <DialogHeader>
94 <DialogTitle>Custom snooze</DialogTitle>
95 <DialogDescription>Choose when snoozed threads return to your inbox.</DialogDescription>
96 </DialogHeader>
97 <DialogPanel className="text-base sm:text-sm">
98 <Tabs.Root
99 value={mode}
100 onValueChange={(value) => {
101 if (value === "date" || value === "duration") setMode(value);
102 setError(null);
103 }}
104 className="flex flex-col gap-4"
105 >
106 <Tabs.List
107 aria-label="Schedule type"
108 className="flex gap-0.5 rounded-lg bg-input/40 p-0.5"
109 >
110 {(["date", "duration"] as const).map((value) => (
111 <Tabs.Tab
112 key={value}
113 value={value}
114 data-pressed={mode === value ? "" : undefined}
115 className={toggleVariants({
116 variant: "segmented",
117 size: "sm",
118 className: "flex-1",
119 })}
120 >
121 {value === "date" ? "Date and time" : "Duration"}
122 </Tabs.Tab>
123 ))}
124 </Tabs.List>
125 <Tabs.Panel value={mode} className="flex flex-col gap-4">
126 {mode === "date" ? (
127 <div className="grid grid-cols-1 gap-3 sm:grid-cols-2">
128 <div className="flex min-w-0 flex-col gap-1.5">
129 <Label htmlFor={`${id}-date`}>Date</Label>
130 <Popover open={calendarOpen} onOpenChange={setCalendarOpen}>
131 <PopoverTrigger
132 render={
133 <Button
134 id={`${id}-date`}
135 variant="outline"
136 className="w-full justify-between font-normal"
137 />
138 }
139 >
140 {date.toLocaleDateString(undefined, {
141 month: "short",
142 day: "numeric",
143 year: "numeric",
144 })}
145 <CalendarIcon className="size-4 text-muted-foreground" />
146 </PopoverTrigger>
147 <PopoverPopup align="start" aria-label="Choose snooze date">
148 <Calendar
149 mode="single"
150 required
151 selected={date}
152 defaultMonth={date}
153 {...(weekStartsOn === undefined ? {} : { weekStartsOn })}
154 disabled={{ before: new Date(new Date().setHours(0, 0, 0, 0)) }}
155 onSelect={(selected) => {
156 setDate(selected);
157 setCalendarOpen(false);
158 setError(null);
159 }}
160 />
161 </PopoverPopup>
162 </Popover>
163 </div>
164 <Label
165 className="flex min-w-0 flex-col items-stretch gap-1.5"
166 htmlFor={`${id}-time`}
167 >
168 Time
169 <Input
170 nativeInput
171 id={`${id}-time`}
172 className="h-9 sm:h-8"
173 type="time"
174 required
175 value={time}
176 onChange={(event) => {
177 setTime(event.target.value);
178 setError(null);
179 }}
180 />
181 </Label>
182 </div>
183 ) : (
184 <div className="grid grid-cols-1 gap-3 sm:grid-cols-2">
185 <NumberField
186 className="gap-1.5"
187 id={`${id}-amount`}
188 min={0}
189 step="any"
190 value={amount === "" ? null : Number(amount)}
191 onValueChange={(value) => {
192 setAmount(value === null ? "" : String(value));
193 setError(null);
194 }}
195 >
196 <Label htmlFor={`${id}-amount`}>Snooze for</Label>
197 <NumberFieldGroup>
198 <NumberFieldDecrement aria-label="Decrease duration" />
199 <NumberFieldInput required />
200 <NumberFieldIncrement aria-label="Increase duration" />
201 </NumberFieldGroup>
202 </NumberField>
203 <Label
204 className="flex min-w-0 flex-col items-stretch gap-1.5"
205 htmlFor={`${id}-unit`}
206 >
207 Unit
208 <Select
209 value={unit}
210 items={{ minutes: "Minutes", hours: "Hours", days: "Days" }}
211 onValueChange={(value) => {
212 if (value === "minutes" || value === "hours" || value === "days")
213 setUnit(value);
214 setError(null);
215 }}
216 >
217 <SelectTrigger id={`${id}-unit`} className="min-w-0">
218 <SelectValue />
219 </SelectTrigger>
220 <SelectPopup>
221 <SelectItem value="minutes">Minutes</SelectItem>
222 <SelectItem value="hours">Hours</SelectItem>
223 <SelectItem value="days">Days</SelectItem>
224 </SelectPopup>
225 </Select>
226 </Label>
227 </div>
228 )}
229 </Tabs.Panel>
230 </Tabs.Root>
231 {error && (
232 <p role="alert" className="text-destructive">
233 {error}
234 </p>
235 )}
236 </DialogPanel>
237 <DialogFooter>
238 <Button type="button" variant="outline" onClick={() => finish(null)}>
239 Cancel
240 </Button>
241 <Button type="submit">Snooze</Button>
242 </DialogFooter>
243 </form>
244 </DialogPopup>
245 </Dialog>
246 );
247}