Preparing for actual live moves
This commit is contained in:
@@ -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<EditorView>();
|
||||
const [moves] = useActionOnce<Move[]>(getLiveRunMoves, [run]);
|
||||
const [moves] = useActionOnce<LiveRunMove[]>(getLiveRunMoves, [run]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!editorView || !moves) return;
|
||||
|
||||
let cancelled = false;
|
||||
|
||||
const executeMove = async (move: Move) => {
|
||||
const executeMove = async (move: LiveRunMove) => {
|
||||
return new Promise<void>((resolve) => {
|
||||
const timeout = setTimeout(() => {
|
||||
if (!editorView || cancelled) return resolve();
|
||||
|
||||
@@ -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<LiveRun[]> {
|
||||
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<LiveRun> {
|
||||
return {
|
||||
id: "0",
|
||||
username: "selimaj",
|
||||
start: 1770497350654,
|
||||
views: 230,
|
||||
problem: "fix-auth-callback",
|
||||
category: "any%",
|
||||
runsCount: 0,
|
||||
};
|
||||
export async function getLiveRun(id: string): Promise<LiveRun | undefined> {
|
||||
return Core.getLiveRun(id);
|
||||
}
|
||||
|
||||
export async function getLiveRunMoves() {
|
||||
|
||||
@@ -1,3 +1,32 @@
|
||||
"use server";
|
||||
|
||||
import "server-only";
|
||||
import { LiveRun, LiveRunMove } from "./types";
|
||||
|
||||
const liveRuns: LiveRun[] = [];
|
||||
const liveRunMoves: Record<string, LiveRunMove[]> = {};
|
||||
|
||||
export async function getLiveRuns(): Promise<LiveRun[]> {
|
||||
return liveRuns;
|
||||
}
|
||||
|
||||
export async function getLiveRun(id: string): Promise<LiveRun | undefined> {
|
||||
return liveRuns.find((r) => r.id === id);
|
||||
}
|
||||
|
||||
export async function getLiveRunMoves(
|
||||
id: string,
|
||||
): Promise<LiveRunMove[] | undefined> {
|
||||
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";
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user