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