From 6744a67931979bdd1a0e3ef1841a0a624f36745a Mon Sep 17 00:00:00 2001 From: Leo dev Date: Sat, 7 Feb 2026 23:25:37 +0100 Subject: [PATCH] Preparing for actual live moves --- src/app/runs/[runId]/components/Editor.tsx | 16 ++---- src/modules/live-run/actions.ts | 57 ++-------------------- src/modules/live-run/core.ts | 29 +++++++++++ src/modules/live-run/types.ts | 10 ++++ 4 files changed, 45 insertions(+), 67 deletions(-) diff --git a/src/app/runs/[runId]/components/Editor.tsx b/src/app/runs/[runId]/components/Editor.tsx index 4cc61f5..0232795 100644 --- a/src/app/runs/[runId]/components/Editor.tsx +++ b/src/app/runs/[runId]/components/Editor.tsx @@ -11,7 +11,7 @@ import { java } from "@codemirror/lang-java"; import { useEffect, useState } from "react"; import useAction, { useActionOnce } from "@/hook/use-action"; import { getLiveRunMoves } from "@/modules/live-run/actions"; -import { LiveRun } from "@/modules/live-run/types"; +import { LiveRun, LiveRunMove } from "@/modules/live-run/types"; type Language = | "javascript" @@ -41,26 +41,16 @@ function getLanguageExtension(lang: Language) { } } -type Move = { - latency: number; - cursor: number; - changes: { - from: number; - to: number; - insert: string; - }; -}; - export default function Editor({ run }: { run: LiveRun | null }) { const [editorView, setEditorView] = useState(); - const [moves] = useActionOnce(getLiveRunMoves, [run]); + const [moves] = useActionOnce(getLiveRunMoves, [run]); useEffect(() => { if (!editorView || !moves) return; let cancelled = false; - const executeMove = async (move: Move) => { + const executeMove = async (move: LiveRunMove) => { return new Promise((resolve) => { const timeout = setTimeout(() => { if (!editorView || cancelled) return resolve(); diff --git a/src/modules/live-run/actions.ts b/src/modules/live-run/actions.ts index 518c00e..df15010 100644 --- a/src/modules/live-run/actions.ts +++ b/src/modules/live-run/actions.ts @@ -1,65 +1,14 @@ "use server"; -import { createClient } from "@/lib/supabase/server"; -import { getSession, getSessionRedirect } from "../account/core"; import * as Core from "./core"; import { LiveRun } from "./types"; export async function getLiveRuns(): Promise { - const supabase = await createClient(); - - const _ = await getSession(supabase); - - return [ - { - id: "1", - username: "speedmaster_x", - problem: "fix-auth-callback", - category: "any%", - views: 670, - runsCount: 1, - start: new Date().getTime(), - }, - { - id: "2", - username: "algo_ninja", - views: 3, - problem: "fix-prod-build", - category: "any%", - runsCount: 1, - start: new Date().getTime(), - }, - { - id: "3", - username: "stackattack", - views: 3, - problem: "add-user-profile-endpoint", - category: "any%", - runsCount: 1, - start: new Date().getTime(), - }, - { - id: "4", - username: "fix-prod-build", - views: 3, - problem: "Linked List Rush", - category: "any%", - runsCount: 1, - start: new Date().getTime(), - }, - ]; + return Core.getLiveRuns(); } -export async function getLiveRun(): Promise { - return { - id: "0", - username: "selimaj", - start: 1770497350654, - views: 230, - problem: "fix-auth-callback", - category: "any%", - runsCount: 0, - }; +export async function getLiveRun(id: string): Promise { + return Core.getLiveRun(id); } export async function getLiveRunMoves() { diff --git a/src/modules/live-run/core.ts b/src/modules/live-run/core.ts index a59f71d..247a291 100644 --- a/src/modules/live-run/core.ts +++ b/src/modules/live-run/core.ts @@ -1,3 +1,32 @@ "use server"; import "server-only"; +import { LiveRun, LiveRunMove } from "./types"; + +const liveRuns: LiveRun[] = []; +const liveRunMoves: Record = {}; + +export async function getLiveRuns(): Promise { + return liveRuns; +} + +export async function getLiveRun(id: string): Promise { + return liveRuns.find((r) => r.id === id); +} + +export async function getLiveRunMoves( + id: string, +): Promise { + return liveRunMoves[id]; +} + +export async function addRun(run: LiveRun) { + if (!liveRuns.find((r) => r.id === run.id || r.username === run.username)) + throw "Duplicate run"; + + liveRuns.push(run); +} + +export async function addLiveRunMove(id: string) { + if (!liveRuns.find((r) => r.id === id)) throw "Invalid run"; +} diff --git a/src/modules/live-run/types.ts b/src/modules/live-run/types.ts index f4073a4..9673d12 100644 --- a/src/modules/live-run/types.ts +++ b/src/modules/live-run/types.ts @@ -7,3 +7,13 @@ export interface LiveRun { problem: string; category: "any%" | "100%"; } + +export interface LiveRunMove { + latency: number; + cursor: number; + changes?: { + from: number; + to: number; + insert: string; + }; +}