From 1f969b8ff15037996402ad9cd9bc6a6ee0a08e02 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Sat, 7 Feb 2026 23:11:09 +0100 Subject: [PATCH] A little improvement on move handling --- src/app/runs/[runId]/components/Editor.tsx | 7 ++++--- src/app/runs/[runId]/page.tsx | 2 +- src/hook/use-action.ts | 20 ++++++++++++++++++++ 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/app/runs/[runId]/components/Editor.tsx b/src/app/runs/[runId]/components/Editor.tsx index 7b28440..4cc61f5 100644 --- a/src/app/runs/[runId]/components/Editor.tsx +++ b/src/app/runs/[runId]/components/Editor.tsx @@ -9,8 +9,9 @@ import { json } from "@codemirror/lang-json"; import { python } from "@codemirror/lang-python"; import { java } from "@codemirror/lang-java"; import { useEffect, useState } from "react"; -import useAction from "@/hook/use-action"; +import useAction, { useActionOnce } from "@/hook/use-action"; import { getLiveRunMoves } from "@/modules/live-run/actions"; +import { LiveRun } from "@/modules/live-run/types"; type Language = | "javascript" @@ -50,9 +51,9 @@ type Move = { }; }; -export default function Editor() { +export default function Editor({ run }: { run: LiveRun | null }) { const [editorView, setEditorView] = useState(); - const [moves] = useAction(getLiveRunMoves); + const [moves] = useActionOnce(getLiveRunMoves, [run]); useEffect(() => { if (!editorView || !moves) return; diff --git a/src/app/runs/[runId]/page.tsx b/src/app/runs/[runId]/page.tsx index 7548e97..0899a64 100644 --- a/src/app/runs/[runId]/page.tsx +++ b/src/app/runs/[runId]/page.tsx @@ -82,7 +82,7 @@ export default function Run() { myfile.js
- +
diff --git a/src/hook/use-action.ts b/src/hook/use-action.ts index 1af384a..984d771 100644 --- a/src/hook/use-action.ts +++ b/src/hook/use-action.ts @@ -18,6 +18,26 @@ export default function useAction( return [value, loaded, error]; } +export function useActionOnce( + action: () => Promise, + dependencies: any[] = [], +): [T | null, boolean, Error | null] { + const [value, setValue] = useState(null); + const [loaded, setLoaded] = useState(false); + const [error, setError] = useState(null); + + useEffect(() => { + if (loaded) return; + + action() + .then(setValue) + .catch(setError) + .finally(() => setLoaded(true)); + }, dependencies); + + return [value, loaded, error]; +} + export function useActionInterval( action: () => Promise, interval: number,