From 75042007b7088026ed341eecafb2377c31440412 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Sat, 7 Feb 2026 00:55:23 +0100 Subject: [PATCH] Single liverun type --- src/app/run/components/RunTimer.tsx | 17 ++--------- src/app/runs/components/LiveRunNow.tsx | 30 +++++++++---------- src/app/runs/page.tsx | 8 ++--- src/lib/time-format.ts | 15 ++++++++++ src/modules/live-run/actions.ts | 41 ++++++++++++-------------- src/modules/live-run/types.ts | 11 +------ 6 files changed, 54 insertions(+), 68 deletions(-) create mode 100644 src/lib/time-format.ts diff --git a/src/app/run/components/RunTimer.tsx b/src/app/run/components/RunTimer.tsx index 39f7612..14fd33b 100644 --- a/src/app/run/components/RunTimer.tsx +++ b/src/app/run/components/RunTimer.tsx @@ -1,4 +1,5 @@ import { Card, CardContent } from "@/components/ui/card"; +import formatTime from "@/lib/time-format"; import { LiveRun } from "@/modules/live-run/types"; import { useEffect, useState } from "react"; @@ -7,21 +8,7 @@ export default function RunTimer({ runInfo }: { runInfo: LiveRun }) { useEffect(() => { // Update function calculates elapsed time - const updateTime = () => { - const elapsedMs = Date.now() - runInfo.start; - - const totalSeconds = elapsedMs / 1000; - const hours = String(Math.floor(totalSeconds / 3600)).padStart(2, "0"); - const minutes = String(Math.floor((totalSeconds % 3600) / 60)).padStart( - 2, - "0", - ); - const seconds = (totalSeconds % 60).toFixed(2); - - setTime( - `${totalSeconds / 3600 >= 1 ? hours + ":" : ""}${minutes}:${seconds}`, - ); - }; + const updateTime = () => setTime(formatTime(Date.now() - runInfo.start)); // Update immediately and then every second updateTime(); diff --git a/src/app/runs/components/LiveRunNow.tsx b/src/app/runs/components/LiveRunNow.tsx index 13e9919..6790689 100644 --- a/src/app/runs/components/LiveRunNow.tsx +++ b/src/app/runs/components/LiveRunNow.tsx @@ -5,18 +5,18 @@ import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { TableRow, TableCell } from "@/components/ui/table"; import { cn } from "@/lib/utils"; +import { LiveRun } from "@/modules/live-run/types"; +import formatTime from "@/lib/time-format"; export default function LiveRunRow({ index, - username, - problem, - category, - time, - pace, - status, -}: any) { - const pacePositive = pace.startsWith("+"); - const isTop = status === "pb"; + run, +}: { + index: number; + run: LiveRun; +}) { + const now = new Date().getTime() + 1000; + const isTop = false; return ( - {isTop ? `🔥 ${username}` : username} + {isTop ? `🔥 ${run.username}` : run.username}
- {problem} + {run.problem} - {category} + {run.category}
@@ -45,18 +45,18 @@ export default function LiveRunRow({ - {time} + {formatTime(now - run.start, false)} - {pace} + 0 diff --git a/src/app/runs/page.tsx b/src/app/runs/page.tsx index 9f8b8d3..9ceebb6 100644 --- a/src/app/runs/page.tsx +++ b/src/app/runs/page.tsx @@ -24,11 +24,7 @@ export default function LiveRuns() { const [liveRuns, liveRunsLoaded, liveRunsError] = useAction(getLiveRuns); // Sort top runners (status === 'pb') to the top - const sortedRuns = [...(liveRuns || [])].sort((a, b) => { - if (a.status === "pb" && b.status !== "pb") return -1; - if (a.status !== "pb" && b.status === "pb") return 1; - return 0; - }); + const sortedRuns = [...(liveRuns || [])]; return (
@@ -82,7 +78,7 @@ export default function LiveRuns() { {sortedRuns.map((run, index) => ( - + ))} diff --git a/src/lib/time-format.ts b/src/lib/time-format.ts new file mode 100644 index 0000000..a437040 --- /dev/null +++ b/src/lib/time-format.ts @@ -0,0 +1,15 @@ +export default function formatTime(elapsedMs: number, formatMs = true) { + const totalSeconds = formatMs + ? elapsedMs / 1000 + : Math.floor(elapsedMs / 1000); + const hours = String(Math.floor(totalSeconds / 3600)).padStart(2, "0"); + const minutes = String(Math.floor((totalSeconds % 3600) / 60)).padStart( + 2, + "0", + ); + const seconds = formatMs + ? (totalSeconds % 60).toFixed(2).padStart(2, "0") + : (totalSeconds % 60).toString().padStart(2, "0"); + + return `${totalSeconds / 3600 >= 1 ? hours + ":" : ""}${minutes}:${seconds}`; +} diff --git a/src/modules/live-run/actions.ts b/src/modules/live-run/actions.ts index 01870cf..9b4ebd2 100644 --- a/src/modules/live-run/actions.ts +++ b/src/modules/live-run/actions.ts @@ -3,55 +3,52 @@ import { createClient } from "@/lib/supabase/server"; import { getSession, getSessionRedirect } from "../account/core"; import * as Core from "./core"; -import { LiveRun, Run } from "./types"; +import { LiveRun } from "./types"; -export async function getLiveRuns(): Promise { +export async function getLiveRuns(): Promise { const supabase = await createClient(); const _ = await getSession(supabase); return [ { - id: 1, + id: "1", username: "speedmaster_x", problem: "Two Sum Dash", - category: "Any%", - time: "1:52.34", - pace: "-0:03", - status: "pb", + category: "any%", + runsCount: 1, + start: new Date().getTime(), }, { - id: 2, + id: "2", username: "algo_ninja", problem: "Binary Sprint", - category: "Any%", - time: "2:04.11", - pace: "+0:05", - status: "normal", + category: "any%", + runsCount: 1, + start: new Date().getTime(), }, { - id: 3, + id: "3", username: "stackattack", problem: "String Scramble", - category: "Easy", - time: "0:48.92", - pace: "-0:01", - status: "pb", + category: "any%", + runsCount: 1, + start: new Date().getTime(), }, { - id: 4, + id: "4", username: "reset_king", problem: "Linked List Rush", - category: "Any%", - time: "1:31.77", - pace: "+0:12", - status: "danger", + category: "any%", + runsCount: 1, + start: new Date().getTime(), }, ]; } export async function getLiveRun(): Promise { return { + id: "0", username: "selimaj", start: new Date().getTime(), problem: "solve", diff --git a/src/modules/live-run/types.ts b/src/modules/live-run/types.ts index 894fc84..97154f4 100644 --- a/src/modules/live-run/types.ts +++ b/src/modules/live-run/types.ts @@ -1,17 +1,8 @@ export interface LiveRun { + id: string; username: string; start: number; runsCount: number; problem: string; category: "any%" | "100%"; } - -export interface Run { - id: number; - username: string; - problem: string; - category: string; - time: string; - pace: string; - status: string; -}