diff --git a/src/app/runs/[runId]/components/Editor.tsx b/src/app/runs/[runId]/components/Editor.tsx index f4114be..52035c4 100644 --- a/src/app/runs/[runId]/components/Editor.tsx +++ b/src/app/runs/[runId]/components/Editor.tsx @@ -48,28 +48,23 @@ export default function Editor({ run }: { run: LiveRun | null | undefined }) { [run], ); - const [lastMoveId, setLastMoveId] = useState(-1); + const [_, setLastMoveId] = useState(-1); const [moves, setMoves] = useState([]); useEffect(() => { - let moves = rawMoves?.filter((move) => move.moveId > lastMoveId) || []; + if (!rawMoves || rawMoves.length === 0) return; - if (moves.length === 0) return; + setLastMoveId((prevLastMoveId) => { + const newMoves = rawMoves.filter((move) => move.moveId > prevLastMoveId); - setMoves(moves); - setLastMoveId(lastMoveId + moves.length); + if (newMoves.length === 0) return prevLastMoveId; - if (rawMoves?.length || 0 > 0) { - console.log("raw", rawMoves); - } + setMoves(newMoves); + + return Math.max(...newMoves.map((m) => m.moveId)); + }); }, [rawMoves]); - useEffect(() => { - if (moves.length > 0) { - console.log("moves", moves); - } - }, [moves]); - useEffect(() => { if (!editorView || !moves) return; @@ -81,7 +76,7 @@ export default function Editor({ run }: { run: LiveRun | null | undefined }) { if (!editorView || cancelled) return resolve(); editorView.dispatch({ - // selection: { anchor: move.cursor }, + selection: { anchor: move.cursor }, scrollIntoView: true, changes: move.changes, });