forge
Explore
Log in

Don't you dare go Hollow

✨ add formatDurationClock function and update duration display in Pomodoro pages

Browse files at this commit

7f98642ec9e5d81f1b1d905742ddc6b591ccbcc2Burak BoduroğluParent: a6eef00
  • feat
    • ✨ add formatDurationClock function and update duration display in Pomodoro pages
Show full message
feat: ✨ add formatDurationClock function and update duration display in Pomodoro pages
src/components/ui/date-picker.tsxmodified+37 -16
@@ -1,6 +1,5 @@
1 import { For, Show, createEffect, createMemo, createSignal, onCleanup } from "solid-js";1 import { For, Show, createEffect, createMemo, createSignal, onCleanup } from "solid-js";
2 import { Portal } from "solid-js/web";2 import { Portal } from "solid-js/web";
3-import { Button } from "@/components/ui/button";
4 import { IconCalendar, IconChevronLeft, IconChevronRight } from "@/components/ui/icons";3 import { IconCalendar, IconChevronLeft, IconChevronRight } from "@/components/ui/icons";
5 import { Input } from "@/components/ui/input";4 import { Input } from "@/components/ui/input";
6 import { cn } from "@/lib/cn";5 import { cn } from "@/lib/cn";
@@ -30,6 +29,10 @@ function sameDay(a: Date | null, b: Date): boolean {
30 return !!a && a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth() && a.getDate() === b.getDate();29 return !!a && a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth() && a.getDate() === b.getDate();
31 }30 }
32 31
32+function isSameDay(a: Date, b: Date): boolean {
33+ return a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth() && a.getDate() === b.getDate();
34+}
35+
33 function monthDays(month: Date): Array<Date | null> {36 function monthDays(month: Date): Array<Date | null> {
34 const year = month.getFullYear();37 const year = month.getFullYear();
35 const monthIndex = month.getMonth();38 const monthIndex = month.getMonth();
@@ -56,6 +59,7 @@ export function DatePicker(props: {
56 const [position, setPosition] = createSignal({ left: 0, top: 0, width: 288 });59 const [position, setPosition] = createSignal({ left: 0, top: 0, width: 288 });
57 const selected = createMemo(() => parseDate(props.value));60 const selected = createMemo(() => parseDate(props.value));
58 const [month, setMonth] = createSignal(selected() ?? new Date());61 const [month, setMonth] = createSignal(selected() ?? new Date());
62+ const today = new Date();
59 const days = createMemo(() => monthDays(month()));63 const days = createMemo(() => monthDays(month()));
60 const monthLabel = createMemo(() =>64 const monthLabel = createMemo(() =>
61 new Intl.DateTimeFormat(locale(), { month: "long", year: "numeric" }).format(month()),65 new Intl.DateTimeFormat(locale(), { month: "long", year: "numeric" }).format(month()),
@@ -64,6 +68,7 @@ export function DatePicker(props: {
64 const date = selected();68 const date = selected();
65 return date ? new Intl.DateTimeFormat(locale(), { weekday: "long" }).format(date) : "";69 return date ? new Intl.DateTimeFormat(locale(), { weekday: "long" }).format(date) : "";
66 });70 });
71+ const todayLabel = createMemo(() => locale().startsWith("tr") ? "Bugün" : "Today");
67 const dayHeaders = createMemo(() => {72 const dayHeaders = createMemo(() => {
68 const base = new Date(2024, 0, 1);73 const base = new Date(2024, 0, 1);
69 return Array.from({ length: 7 }, (_, i) => {74 return Array.from({ length: 7 }, (_, i) => {
@@ -96,17 +101,16 @@ export function DatePicker(props: {
96 setPosition({ left, top, width: panelWidth });101 setPosition({ left, top, width: panelWidth });
97 };102 };
98 updatePosition();103 updatePosition();
99- const closeOnOutside = (event: MouseEvent) => {104+ const closeOnOutside = (event: PointerEvent) => {
100- const target = event.target as Node;105+ if (root && !root.contains(event.target as Node) && panel && !panel.contains(event.target as Node)) setOpen(false);
101- if (root && !root.contains(target) && panel && !panel.contains(target)) setOpen(false);
102 };106 };
103 window.addEventListener("resize", updatePosition);107 window.addEventListener("resize", updatePosition);
104 window.addEventListener("scroll", updatePosition, true);108 window.addEventListener("scroll", updatePosition, true);
105- document.addEventListener("mousedown", closeOnOutside);109+ document.addEventListener("pointerdown", closeOnOutside);
106 onCleanup(() => {110 onCleanup(() => {
107 window.removeEventListener("resize", updatePosition);111 window.removeEventListener("resize", updatePosition);
108 window.removeEventListener("scroll", updatePosition, true);112 window.removeEventListener("scroll", updatePosition, true);
109- document.removeEventListener("mousedown", closeOnOutside);113+ document.removeEventListener("pointerdown", closeOnOutside);
110 });114 });
111 });115 });
112 116
@@ -143,17 +147,19 @@ export function DatePicker(props: {
143 <Portal>147 <Portal>
144 <div148 <div
145 ref={panel}149 ref={panel}
150+ data-kb-top-layer=""
146 class="fixed z-[80] rounded-xl border border-border/80 bg-popover p-2.5 text-popover-foreground shadow-xl"151 class="fixed z-[80] rounded-xl border border-border/80 bg-popover p-2.5 text-popover-foreground shadow-xl"
147 style={{ left: `${position().left}px`, top: `${position().top}px`, width: `${position().width}px` }}152 style={{ left: `${position().left}px`, top: `${position().top}px`, width: `${position().width}px` }}
153+ on:pointerdown={(e) => e.stopPropagation()}
148 >154 >
149 <div class="mb-3 flex items-center justify-between gap-2">155 <div class="mb-3 flex items-center justify-between gap-2">
150- <Button type="button" variant="ghost" size="sm" class="h-8 w-8 rounded-lg p-0" onClick={() => moveMonth(-1)}>156+ <button type="button" class="inline-flex h-8 w-8 items-center justify-center rounded-lg text-muted-foreground transition-colors hover:bg-accent hover:text-accent-foreground" onClick={() => moveMonth(-1)}>
151 <IconChevronLeft class="h-4 w-4" />157 <IconChevronLeft class="h-4 w-4" />
152- </Button>158+ </button>
153 <p class="min-w-0 truncate px-2 text-sm font-semibold capitalize">{monthLabel()}</p>159 <p class="min-w-0 truncate px-2 text-sm font-semibold capitalize">{monthLabel()}</p>
154- <Button type="button" variant="ghost" size="sm" class="h-8 w-8 rounded-lg p-0" onClick={() => moveMonth(1)}>160+ <button type="button" class="inline-flex h-8 w-8 items-center justify-center rounded-lg text-muted-foreground transition-colors hover:bg-accent hover:text-accent-foreground" onClick={() => moveMonth(1)}>
155 <IconChevronRight class="h-4 w-4" />161 <IconChevronRight class="h-4 w-4" />
156- </Button>162+ </button>
157 </div>163 </div>
158 <div class="grid grid-cols-7 gap-1 text-center text-[11px] font-medium text-muted-foreground">164 <div class="grid grid-cols-7 gap-1 text-center text-[11px] font-medium text-muted-foreground">
159 <For each={dayHeaders()}>{(day) => <span>{day}</span>}</For>165 <For each={dayHeaders()}>{(day) => <span>{day}</span>}</For>
@@ -163,20 +169,35 @@ export function DatePicker(props: {
163 {(date) => (169 {(date) => (
164 <Show when={date} fallback={<span class="h-8" />}>170 <Show when={date} fallback={<span class="h-8" />}>
165 {(day) => (171 {(day) => (
166- <Button172+ <button
167 type="button"173 type="button"
168- variant={sameDay(selected(), day()) ? "default" : "ghost"}174+ class={cn(
169- size="sm"175+ "inline-flex h-8 items-center justify-center rounded-lg border border-transparent p-0 text-xs font-medium transition-colors hover:bg-accent hover:text-accent-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring active:scale-[0.98]",
170- class="h-8 rounded-lg p-0 text-xs"176+ isSameDay(today, day()) && "border-primary/50 text-primary",
171- onClick={() => pick(day())}177+ sameDay(selected(), day()) && "border-primary bg-primary text-primary-foreground shadow-sm hover:bg-primary/90 hover:text-primary-foreground",
178+ )}
179+ onClick={(event) => {
180+ event.stopPropagation();
181+ pick(day());
182+ }}
172 >183 >
173 {day().getDate()}184 {day().getDate()}
174- </Button>185+ </button>
175 )}186 )}
176 </Show>187 </Show>
177 )}188 )}
178 </For>189 </For>
179 </div>190 </div>
191+ <button
192+ type="button"
193+ class="mt-2 inline-flex h-8 w-full items-center justify-center rounded-lg border bg-background/80 px-3 text-xs font-medium text-muted-foreground transition-colors hover:bg-accent hover:text-accent-foreground"
194+ onClick={(event) => {
195+ event.stopPropagation();
196+ pick(today);
197+ }}
198+ >
199+ {todayLabel()}
200+ </button>
180 </div>201 </div>
181 </Portal>202 </Portal>
182 </Show>203 </Show>
src/lib/format.tsmodified+8 -0
@@ -28,6 +28,14 @@ export function formatDurationMinutes(ms: number | null | undefined, locale: Loc
28 return locale === "tr" ? `${Math.round(ms / 60_000)} dk` : `${Math.round(ms / 60_000)} min`;28 return locale === "tr" ? `${Math.round(ms / 60_000)} dk` : `${Math.round(ms / 60_000)} min`;
29 }29 }
30 30
31+export function formatDurationClock(ms: number | null | undefined): string {
32+ if (ms == null) return "--:--";
33+ const totalSeconds = Math.max(0, Math.floor(ms / 1000));
34+ const minutes = Math.floor(totalSeconds / 60);
35+ const seconds = totalSeconds % 60;
36+ return `${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`;
37+}
38+
31 export function examDurationMs(durationMs: number | null | undefined, startsAt: number | null, endsAt: number | null): number | null {39 export function examDurationMs(durationMs: number | null | undefined, startsAt: number | null, endsAt: number | null): number | null {
32 if (durationMs != null) return durationMs;40 if (durationMs != null) return durationMs;
33 if (startsAt == null || endsAt == null || endsAt <= startsAt) return null;41 if (startsAt == null || endsAt == null || endsAt <= startsAt) return null;
src/pages/pomodoro-page.tsxmodified+28 -6
@@ -10,9 +10,11 @@ import { Alert } from "@/components/ui/alert";
10 import { Badge } from "@/components/ui/badge";10 import { Badge } from "@/components/ui/badge";
11 import { Button } from "@/components/ui/button";11 import { Button } from "@/components/ui/button";
12 import { DataTable } from "@/components/ui/data-table";12 import { DataTable } from "@/components/ui/data-table";
13+import { IconClock } from "@/components/ui/icons";
13 import { PageSpinner } from "@/components/ui/page-spinner";14 import { PageSpinner } from "@/components/ui/page-spinner";
14 import { createFlash } from "@/lib/flash";15 import { createFlash } from "@/lib/flash";
15-import { formatDateTime, formatDurationMinutes } from "@/lib/format";16+import { createNow } from "@/lib/create-now";
17+import { formatDateTime, formatDurationClock } from "@/lib/format";
16 import { usePreferences, useT } from "@/stores/preferences-context";18 import { usePreferences, useT } from "@/stores/preferences-context";
17 19
18 export default function PomodoroPage() {20 export default function PomodoroPage() {
@@ -29,8 +31,13 @@ function PomodoroContent() {
29 const [error, setError] = createSignal("");31 const [error, setError] = createSignal("");
30 const [pending, setPending] = createSignal(false);32 const [pending, setPending] = createSignal(false);
31 const [flash, setFlash] = createFlash();33 const [flash, setFlash] = createFlash();
34+ const now = createNow(1000);
32 const [log, { refetch }] = createResource(() => getPomodoroMe({ limit: 20 }));35 const [log, { refetch }] = createResource(() => getPomodoroMe({ limit: 20 }));
33 const running = createMemo(() => log()?.items.find((item) => item.finished_at == null) ?? null);36 const running = createMemo(() => log()?.items.find((item) => item.finished_at == null) ?? null);
37+ const runningDuration = createMemo(() => {
38+ const current = running();
39+ return current ? now() - current.started_at : null;
40+ });
34 const columns = createMemo<ColumnDef<NonNullable<ReturnType<typeof log>>["items"][number]>[]>(() => [41 const columns = createMemo<ColumnDef<NonNullable<ReturnType<typeof log>>["items"][number]>[]>(() => [
35 {42 {
36 accessorKey: "started_at",43 accessorKey: "started_at",
@@ -45,7 +52,7 @@ function PomodoroContent() {
45 {52 {
46 accessorKey: "duration_ms",53 accessorKey: "duration_ms",
47 header: t("pomodoro.duration"),54 header: t("pomodoro.duration"),
48- cell: (cell) => formatDurationMinutes(cell.row.original.duration_ms, locale()),55+ cell: (cell) => <span class="mono tabular-nums">{formatDurationClock(cell.row.original.duration_ms)}</span>,
49 },56 },
50 ]);57 ]);
51 58
@@ -92,10 +99,25 @@ function PomodoroContent() {
92 {error() && <Alert variant="destructive">{error()}</Alert>}99 {error() && <Alert variant="destructive">{error()}</Alert>}
93 100
94 <section class="grid gap-3 sm:grid-cols-2">101 <section class="grid gap-3 sm:grid-cols-2">
95- <div class="detail-metric-card">102+ <Show
96- <p class="text-xs font-medium uppercase tracking-[0.08em] text-muted-foreground">{t("pomodoro.total")}</p>103+ when={running()}
97- <p class="mono mt-2 text-3xl font-semibold tabular-nums">{formatDurationMinutes(log()?.total_focus_ms, locale())}</p>104+ fallback={
98- </div>105+ <div class="detail-metric-card">
106+ <p class="text-xs font-medium uppercase tracking-[0.08em] text-muted-foreground">{t("pomodoro.total")}</p>
107+ <p class="mono mt-2 text-3xl font-semibold tabular-nums">{formatDurationClock(log()?.total_focus_ms)}</p>
108+ </div>
109+ }
110+ >
111+ <div class="detail-metric-card overflow-hidden border-emerald-500/25 bg-[radial-gradient(circle_at_top_right,rgba(16,185,129,0.16),transparent_38%)]">
112+ <div class="flex items-center gap-2 text-xs font-medium uppercase tracking-[0.08em] text-muted-foreground">
113+ <span class="flex h-7 w-7 items-center justify-center rounded-full border border-emerald-500/30 bg-emerald-500/10 text-emerald-700 dark:text-emerald-300">
114+ <IconClock class="h-3.5 w-3.5" />
115+ </span>
116+ {t("pomodoro.running")}
117+ </div>
118+ <p class="mono mt-4 text-5xl font-semibold leading-none tracking-tight tabular-nums sm:text-6xl">{formatDurationClock(runningDuration())}</p>
119+ </div>
120+ </Show>
99 <div class="detail-metric-card">121 <div class="detail-metric-card">
100 <p class="text-xs font-medium uppercase tracking-[0.08em] text-muted-foreground">{t("attempt.status")}</p>122 <p class="text-xs font-medium uppercase tracking-[0.08em] text-muted-foreground">{t("attempt.status")}</p>
101 <div class="mt-2 flex flex-wrap items-center gap-2">123 <div class="mt-2 flex flex-wrap items-center gap-2">
src/pages/student-pomodoro-page.tsxmodified+3 -3
@@ -12,7 +12,7 @@ import { ErrorAlert } from "@/components/ui/error-alert";
12 import { IconEye } from "@/components/ui/icons";12 import { IconEye } from "@/components/ui/icons";
13 import { SidePanel } from "@/components/ui/side-panel";13 import { SidePanel } from "@/components/ui/side-panel";
14 import { TableRowActions } from "@/components/ui/table-row-actions";14 import { TableRowActions } from "@/components/ui/table-row-actions";
15-import { formatDateTime, formatDurationMinutes } from "@/lib/format";15+import { formatDateTime, formatDurationClock } from "@/lib/format";
16 import { personLabel } from "@/lib/person";16 import { personLabel } from "@/lib/person";
17 import { usePreferences, useT } from "@/stores/preferences-context";17 import { usePreferences, useT } from "@/stores/preferences-context";
18 18
@@ -118,7 +118,7 @@ function StudentPomodoroContent() {
118 {118 {
119 accessorKey: "duration_ms",119 accessorKey: "duration_ms",
120 header: t("pomodoro.duration"),120 header: t("pomodoro.duration"),
121- cell: (cell) => formatDurationMinutes(cell.row.original.duration_ms, locale()),121+ cell: (cell) => <span class="mono tabular-nums">{formatDurationClock(cell.row.original.duration_ms)}</span>,
122 },122 },
123 ]);123 ]);
124 124
@@ -177,7 +177,7 @@ function StudentPomodoroContent() {
177 <div class="space-y-4">177 <div class="space-y-4">
178 <div class="detail-metric-card">178 <div class="detail-metric-card">
179 <p class="text-xs font-medium uppercase tracking-[0.08em] text-muted-foreground">{t("pomodoro.total")}</p>179 <p class="text-xs font-medium uppercase tracking-[0.08em] text-muted-foreground">{t("pomodoro.total")}</p>
180- <p class="mono mt-2 text-3xl font-semibold tabular-nums">{formatDurationMinutes(p().total_focus_ms, locale())}</p>180+ <p class="mono mt-2 text-3xl font-semibold tabular-nums">{formatDurationClock(p().total_focus_ms)}</p>
181 </div>181 </div>
182 <DataTable columns={logColumns()} data={p().items} empty={t("pomodoro.empty")} enablePagination pageSize={10} />182 <DataTable columns={logColumns()} data={p().items} empty={t("pomodoro.empty")} enablePagination pageSize={10} />
183 </div>183 </div>