|
| 1 | +import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle } from "@/components/ui/alert-dialog"; |
| 2 | +import { Button } from "@/components/ui/button"; |
| 3 | +import { calculateActiveSessionElapsedTime } from "@/hooks/useTimers"; |
| 4 | +import { formatTimer } from "@/utils/date"; |
| 5 | +import clsx from "clsx"; |
| 6 | +import { isToday } from "date-fns"; |
| 7 | +import { TrashIcon } from "lucide-react"; |
| 8 | +import { useState } from "react"; |
| 9 | +import { useIntl } from "react-intl"; |
| 10 | +import { useInterval } from "usehooks-ts"; |
| 11 | +import { useTimerContext } from "./TimerRoot"; |
| 12 | + |
| 13 | +export const TimerSessions = () => { |
| 14 | + const { formatDateTimeRange } = useIntl(); |
| 15 | + const { timer } = useTimerContext(); |
| 16 | + |
| 17 | + const [removingSessionId, setRemovingSessionId] = useState<string | null>(null); |
| 18 | + |
| 19 | + if (!timer.activeSession && timer.sessions.length === 0) return null; |
| 20 | + |
| 21 | + const visibleSessions = [...(timer.activeSession ? [{ id: "active", start: timer.activeSession.start, end: timer.activeSession.start }] : []), ...timer.sessions.toReversed()]; |
| 22 | + |
| 23 | + return ( |
| 24 | + <> |
| 25 | + <div className="flex flex-col gap-y-0.5"> |
| 26 | + {visibleSessions.map((session) => ( |
| 27 | + <div key={session.id} className="text-muted-foreground flex items-center gap-2"> |
| 28 | + <span className="grow"> |
| 29 | + {formatDateTimeRange(session.start, session.end, { |
| 30 | + dateStyle: isToday(session.start) ? undefined : "short", |
| 31 | + timeStyle: "medium", |
| 32 | + })} |
| 33 | + </span> |
| 34 | + <span |
| 35 | + className={clsx({ |
| 36 | + "font-semibold": session.id === "active", |
| 37 | + })} |
| 38 | + > |
| 39 | + {session.id === "active" ? <ActiveSessionElapsedTime /> : formatTimer(session.end - session.start)} |
| 40 | + </span> |
| 41 | + <Button type="button" variant="ghost" size="icon-xs" className="hover:text-destructive" onClick={() => setRemovingSessionId(session.id)}> |
| 42 | + <TrashIcon className="size-3.5" /> |
| 43 | + </Button> |
| 44 | + </div> |
| 45 | + ))} |
| 46 | + </div> |
| 47 | + |
| 48 | + {removingSessionId && <RemoveSessionDialog sessionId={removingSessionId} onClose={() => setRemovingSessionId(null)} />} |
| 49 | + </> |
| 50 | + ); |
| 51 | +}; |
| 52 | + |
| 53 | +const ActiveSessionElapsedTime = () => { |
| 54 | + const { timer } = useTimerContext(); |
| 55 | + |
| 56 | + const [elapsedTime, setElapsedTime] = useState(() => calculateActiveSessionElapsedTime(timer)); |
| 57 | + useInterval(() => setElapsedTime(calculateActiveSessionElapsedTime(timer)), timer.activeSession ? 1000 : null); |
| 58 | + |
| 59 | + return formatTimer(elapsedTime); |
| 60 | +}; |
| 61 | + |
| 62 | +const RemoveSessionDialog = ({ sessionId, onClose }: { sessionId: string; onClose: () => void }) => { |
| 63 | + const { formatDateTimeRange, formatMessage } = useIntl(); |
| 64 | + const { timer, timerApi, totalElapsedTime } = useTimerContext(); |
| 65 | + |
| 66 | + const session = sessionId === "active" ? (timer.activeSession ? { id: "active", start: timer.activeSession.start, end: Date.now() } : undefined) : timer.sessions.find((s) => s.id === sessionId); |
| 67 | + if (!session) return null; |
| 68 | + |
| 69 | + const duration = session.end - session.start; |
| 70 | + const resultingTimer = Math.max(0, totalElapsedTime - duration); |
| 71 | + |
| 72 | + return ( |
| 73 | + <AlertDialog open onOpenChange={() => onClose()}> |
| 74 | + <AlertDialogContent> |
| 75 | + <AlertDialogHeader> |
| 76 | + <AlertDialogTitle>{formatMessage({ id: "timer.modal.remove-session.title" })}</AlertDialogTitle> |
| 77 | + </AlertDialogHeader> |
| 78 | + |
| 79 | + <div className="bg-muted/40 border-border/60 space-y-2 rounded-md border px-3 py-2 text-sm"> |
| 80 | + <div className="flex items-center justify-between gap-3"> |
| 81 | + <span className="text-muted-foreground">{formatMessage({ id: "timer.modal.remove-session.current" })}</span> |
| 82 | + <span>{formatTimer(totalElapsedTime)}</span> |
| 83 | + </div> |
| 84 | + <div className="flex items-center justify-between gap-3"> |
| 85 | + <span className="text-muted-foreground/80 min-w-0 text-xs"> |
| 86 | + {formatDateTimeRange(session.start, session.end, { |
| 87 | + dateStyle: isToday(session.start) ? undefined : "short", |
| 88 | + timeStyle: "medium", |
| 89 | + })} |
| 90 | + </span> |
| 91 | + <span className="text-destructive">-{formatTimer(duration)}</span> |
| 92 | + </div> |
| 93 | + <div className="border-border/60 flex items-center justify-between gap-3 border-t pt-2"> |
| 94 | + <span className="text-muted-foreground">{formatMessage({ id: "timer.modal.remove-session.result" })}</span> |
| 95 | + <span>{formatTimer(resultingTimer)}</span> |
| 96 | + </div> |
| 97 | + </div> |
| 98 | + |
| 99 | + <AlertDialogFooter> |
| 100 | + <AlertDialogCancel>{formatMessage({ id: "timer.modal.remove-session.cancel" })}</AlertDialogCancel> |
| 101 | + <AlertDialogAction |
| 102 | + variant="destructive" |
| 103 | + onClick={async () => { |
| 104 | + await timerApi.removeTimerSession(timer, sessionId); |
| 105 | + }} |
| 106 | + > |
| 107 | + {formatMessage({ id: "timer.modal.remove-session.submit" })} |
| 108 | + </AlertDialogAction> |
| 109 | + </AlertDialogFooter> |
| 110 | + </AlertDialogContent> |
| 111 | + </AlertDialog> |
| 112 | + ); |
| 113 | +}; |
0 commit comments