The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { useEffect, useId, useState } from "react"; |
| 2 | import { T, useGT } from "gt-react"; |
| 3 | import { useNavigate } from "@tanstack/react-router"; |
| 4 | import { create } from "zustand"; |
| 5 | import { |
| 6 | localSnoozeDate, |
| 7 | localSnoozeTime, |
| 8 | resolveCustomSnooze, |
| 9 | type CustomSnoozeInput, |
| 10 | } from "@t3tools/client-runtime/state/thread-settled"; |
| 11 | import { Button } from "./ui/button"; |
| 12 | import { CalendarIcon, Settings2Icon } from "lucide-react"; |
| 13 | import { Calendar } from "./ui/calendar"; |
| 14 | import { weekStartsOn } from "../timestampFormat"; |
| 15 | import { useNowMinute } from "../hooks/useNowMinute"; |
| 16 | import { Popover, PopoverTrigger, PopoverPopup } from "./ui/popover"; |
| 17 | import { Input } from "./ui/input"; |
| 18 | import { Label } from "./ui/label"; |
| 19 | import { Toggle, ToggleGroup } from "./ui/toggle-group"; |
| 20 | import { Select, SelectTrigger, SelectValue, SelectPopup, SelectItem } from "./ui/select"; |
| 21 | import { |
| 22 | NumberField, |
| 23 | NumberFieldGroup, |
| 24 | NumberFieldInput, |
| 25 | NumberFieldDecrement, |
| 26 | NumberFieldIncrement, |
| 27 | } from "./ui/number-field"; |
| 28 | import { |
| 29 | Dialog, |
| 30 | DialogPopup, |
| 31 | DialogHeader, |
| 32 | DialogTitle, |
| 33 | DialogDescription, |
| 34 | DialogPanel, |
| 35 | DialogFooter, |
| 36 | } from "./ui/dialog"; |
| 37 | |
| 38 | type SnoozeChoice = { readonly snoozedUntil: string }; |
| 39 | type Request = { readonly resolve: (choice: SnoozeChoice | null) => void }; |
| 40 | const useRequest = create<{ request: Request | null }>(() => ({ request: null })); |
| 41 | |
| 42 | export function requestCustomSnooze(): Promise<SnoozeChoice | null> { |
| 43 | useRequest.getState().request?.resolve(null); |
| 44 | return new Promise((resolve) => useRequest.setState({ request: { resolve } })); |
| 45 | } |
| 46 | |
| 47 | function finish(choice: SnoozeChoice | null) { |
| 48 | const request = useRequest.getState().request; |
| 49 | useRequest.setState({ request: null }); |
| 50 | request?.resolve(choice); |
| 51 | } |
| 52 | |
| 53 | function startOfLocalDay(ms: number) { |
| 54 | const day = new Date(ms); |
| 55 | day.setHours(0, 0, 0, 0); |
| 56 | return day; |
| 57 | } |
| 58 | |
| 59 | export function CustomSnoozeDialogHost() { |
| 60 | const request = useRequest((state) => state.request); |
| 61 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- resolves any pending snooze request promise with null when the dialog host unmounts |
| 62 | useEffect(() => () => finish(null), []); |
| 63 | return request ? <CustomSnoozeDialog /> : null; |
| 64 | } |
| 65 | |
| 66 | function CustomSnoozeDialog() { |
| 67 | const gt = useGT(); |
| 68 | const id = useId(); |
| 69 | const navigate = useNavigate(); |
| 70 | const [initial] = useState(() => new Date(Date.now() + 3_600_000)); |
| 71 | const nowMinute = useNowMinute(); |
| 72 | // Follows the clock so a dialog left open past midnight stops offering yesterday. |
| 73 | const today = startOfLocalDay(Date.parse(`${nowMinute}Z`)); |
| 74 | const [mode, setMode] = useState<CustomSnoozeInput["mode"]>("date"); |
| 75 | const [date, setDate] = useState(initial); |
| 76 | const [calendarOpen, setCalendarOpen] = useState(false); |
| 77 | const [time, setTime] = useState(localSnoozeTime(initial)); |
| 78 | const [amount, setAmount] = useState("2"); |
| 79 | const [unit, setUnit] = useState<"minutes" | "hours" | "days">("hours"); |
| 80 | const [error, setError] = useState<string | null>(null); |
| 81 | const input: CustomSnoozeInput = |
| 82 | mode === "date" ? { mode, date: localSnoozeDate(date), time } : { mode, amount, unit }; |
| 83 | return ( |
| 84 | <Dialog |
| 85 | open |
| 86 | onOpenChange={(open) => { |
| 87 | if (!open) finish(null); |
| 88 | }} |
| 89 | > |
| 90 | <DialogPopup className="sm:max-w-md"> |
| 91 | <form |
| 92 | className="flex min-h-0 flex-col" |
| 93 | onSubmit={(event) => { |
| 94 | event.preventDefault(); |
| 95 | const snoozedUntil = resolveCustomSnooze(input, new Date()); |
| 96 | if (!snoozedUntil) { |
| 97 | setError( |
| 98 | mode === "date" |
| 99 | ? gt("Choose a valid date and time in the future.") |
| 100 | : gt("Enter a positive duration."), |
| 101 | ); |
| 102 | return; |
| 103 | } |
| 104 | finish({ snoozedUntil }); |
| 105 | }} |
| 106 | > |
| 107 | <DialogHeader> |
| 108 | <DialogTitle> |
| 109 | <T>Custom snooze</T> |
| 110 | </DialogTitle> |
| 111 | <DialogDescription> |
| 112 | <T>Choose when snoozed threads return to your inbox.</T> |
| 113 | </DialogDescription> |
| 114 | </DialogHeader> |
| 115 | <DialogPanel> |
| 116 | <div className="flex flex-col gap-4"> |
| 117 | <ToggleGroup |
| 118 | aria-label={gt("Schedule type")} |
| 119 | className="w-full *:flex-1" |
| 120 | value={[mode]} |
| 121 | onValueChange={(next) => { |
| 122 | const value = next[0]; |
| 123 | if (value === "date" || value === "duration") setMode(value); |
| 124 | setError(null); |
| 125 | }} |
| 126 | > |
| 127 | <Toggle value="date"> |
| 128 | <T>Date and time</T> |
| 129 | </Toggle> |
| 130 | <Toggle value="duration"> |
| 131 | <T>Duration</T> |
| 132 | </Toggle> |
| 133 | </ToggleGroup> |
| 134 | <div className="flex flex-col gap-4"> |
| 135 | {mode === "date" ? ( |
| 136 | <div className="grid grid-cols-1 gap-3 sm:grid-cols-2"> |
| 137 | <div className="flex min-w-0 flex-col gap-2"> |
| 138 | <Label htmlFor={`${id}-date`}> |
| 139 | <T context="calendar date">Date</T> |
| 140 | </Label> |
| 141 | <Popover open={calendarOpen} onOpenChange={setCalendarOpen}> |
| 142 | <PopoverTrigger |
| 143 | render={ |
| 144 | <Button |
| 145 | id={`${id}-date`} |
| 146 | variant="outline" |
| 147 | className="w-full justify-between" |
| 148 | /> |
| 149 | } |
| 150 | > |
| 151 | {date.toLocaleDateString(undefined, { |
| 152 | month: "short", |
| 153 | day: "numeric", |
| 154 | year: "numeric", |
| 155 | })} |
| 156 | <CalendarIcon className="size-4 text-muted-foreground" /> |
| 157 | </PopoverTrigger> |
| 158 | <PopoverPopup align="start" aria-label={gt("Choose snooze date")}> |
| 159 | <Calendar |
| 160 | mode="single" |
| 161 | required |
| 162 | selected={date} |
| 163 | defaultMonth={date} |
| 164 | {...(weekStartsOn === undefined ? {} : { weekStartsOn })} |
| 165 | disabled={{ before: today }} |
| 166 | onSelect={(selected) => { |
| 167 | setDate(selected); |
| 168 | setCalendarOpen(false); |
| 169 | setError(null); |
| 170 | }} |
| 171 | /> |
| 172 | </PopoverPopup> |
| 173 | </Popover> |
| 174 | </div> |
| 175 | <Label className="flex min-w-0 flex-col items-stretch" htmlFor={`${id}-time`}> |
| 176 | <T context="time of day">Time</T> |
| 177 | <Input |
| 178 | nativeInput |
| 179 | id={`${id}-time`} |
| 180 | className="h-9 sm:h-8" |
| 181 | type="time" |
| 182 | required |
| 183 | value={time} |
| 184 | onChange={(event) => { |
| 185 | setTime(event.target.value); |
| 186 | setError(null); |
| 187 | }} |
| 188 | /> |
| 189 | </Label> |
| 190 | </div> |
| 191 | ) : ( |
| 192 | <div className="grid grid-cols-1 gap-3 sm:grid-cols-2"> |
| 193 | <NumberField |
| 194 | id={`${id}-amount`} |
| 195 | min={0} |
| 196 | step="any" |
| 197 | value={amount === "" ? null : Number(amount)} |
| 198 | onValueChange={(value) => { |
| 199 | setAmount(value === null ? "" : String(value)); |
| 200 | setError(null); |
| 201 | }} |
| 202 | > |
| 203 | <Label htmlFor={`${id}-amount`}> |
| 204 | <T>Snooze for</T> |
| 205 | </Label> |
| 206 | <NumberFieldGroup> |
| 207 | <NumberFieldDecrement aria-label={gt("Decrease duration")} /> |
| 208 | <NumberFieldInput required /> |
| 209 | <NumberFieldIncrement aria-label={gt("Increase duration")} /> |
| 210 | </NumberFieldGroup> |
| 211 | </NumberField> |
| 212 | <Label className="flex min-w-0 flex-col items-stretch" htmlFor={`${id}-unit`}> |
| 213 | <T context="unit of time">Unit</T> |
| 214 | <Select |
| 215 | value={unit} |
| 216 | items={{ minutes: gt("Minutes"), hours: gt("Hours"), days: gt("Days") }} |
| 217 | onValueChange={(value) => { |
| 218 | if (value === "minutes" || value === "hours" || value === "days") |
| 219 | setUnit(value); |
| 220 | setError(null); |
| 221 | }} |
| 222 | > |
| 223 | <SelectTrigger id={`${id}-unit`} className="min-w-0"> |
| 224 | <SelectValue /> |
| 225 | </SelectTrigger> |
| 226 | <SelectPopup> |
| 227 | <SelectItem value="minutes"> |
| 228 | <T>Minutes</T> |
| 229 | </SelectItem> |
| 230 | <SelectItem value="hours"> |
| 231 | <T>Hours</T> |
| 232 | </SelectItem> |
| 233 | <SelectItem value="days"> |
| 234 | <T>Days</T> |
| 235 | </SelectItem> |
| 236 | </SelectPopup> |
| 237 | </Select> |
| 238 | </Label> |
| 239 | </div> |
| 240 | )} |
| 241 | </div> |
| 242 | </div> |
| 243 | {error && ( |
| 244 | <p role="alert" className="text-destructive"> |
| 245 | {error} |
| 246 | </p> |
| 247 | )} |
| 248 | </DialogPanel> |
| 249 | <DialogFooter> |
| 250 | <Button |
| 251 | type="button" |
| 252 | variant="ghost-muted" |
| 253 | className="sm:mr-auto" |
| 254 | onClick={() => { |
| 255 | finish(null); |
| 256 | void navigate({ |
| 257 | to: "/settings/threads", |
| 258 | hash: "snooze-options", |
| 259 | }); |
| 260 | }} |
| 261 | > |
| 262 | <Settings2Icon /> |
| 263 | <T>Edit snooze options</T> |
| 264 | </Button> |
| 265 | <Button type="button" variant="outline" onClick={() => finish(null)}> |
| 266 | <T>Cancel</T> |
| 267 | </Button> |
| 268 | <Button type="submit"> |
| 269 | <T context="verb">Snooze</T> |
| 270 | </Button> |
| 271 | </DialogFooter> |
| 272 | </form> |
| 273 | </DialogPopup> |
| 274 | </Dialog> |
| 275 | ); |
| 276 | } |