Live run updates
This commit is contained in:
@@ -6,13 +6,13 @@ import RunChat, { ChatMessage } from "./components/RunChat";
|
|||||||
import { FileTree } from "./components/FileTree";
|
import { FileTree } from "./components/FileTree";
|
||||||
import { File } from "lucide-react";
|
import { File } from "lucide-react";
|
||||||
import Editor from "./components/Editor";
|
import Editor from "./components/Editor";
|
||||||
import useAction from "@/hook/use-action";
|
import useAction, { useActionInterval } from "@/hook/use-action";
|
||||||
import { getLiveRun } from "@/modules/live-run/actions";
|
import { getLiveRun } from "@/modules/live-run/actions";
|
||||||
import ProblemCard from "../problems/components/ProblemCard";
|
import ProblemCard from "../problems/components/ProblemCard";
|
||||||
import ProfileCard from "./components/ProfileCard";
|
import ProfileCard from "./components/ProfileCard";
|
||||||
|
|
||||||
export default function Run() {
|
export default function Run() {
|
||||||
const [runInfo] = useAction(getLiveRun);
|
const [runInfo] = useActionInterval(getLiveRun, 1000);
|
||||||
|
|
||||||
const chatMessages: ChatMessage[] = [
|
const chatMessages: ChatMessage[] = [
|
||||||
{ user: "Alice", message: "Nice split!", time: "1:53" },
|
{ user: "Alice", message: "Nice split!", time: "1:53" },
|
||||||
|
|||||||
@@ -17,3 +17,29 @@ export default function useAction<T>(
|
|||||||
|
|
||||||
return [value, loaded, error];
|
return [value, loaded, error];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function useActionInterval<T>(
|
||||||
|
action: () => Promise<T>,
|
||||||
|
interval: number,
|
||||||
|
dependencies = [],
|
||||||
|
): [T | null, boolean, Error | null] {
|
||||||
|
const [value, setValue] = useState<T | null>(null);
|
||||||
|
const [loaded, setLoaded] = useState(false);
|
||||||
|
const [error, setError] = useState(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const f = () =>
|
||||||
|
action()
|
||||||
|
.then(setValue)
|
||||||
|
.catch(setError)
|
||||||
|
.finally(() => setLoaded(true));
|
||||||
|
|
||||||
|
f();
|
||||||
|
|
||||||
|
const inv = setInterval(f, interval);
|
||||||
|
|
||||||
|
return () => clearInterval(inv);
|
||||||
|
}, dependencies);
|
||||||
|
|
||||||
|
return [value, loaded, error];
|
||||||
|
}
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ export async function getLiveRun(): Promise<LiveRun> {
|
|||||||
return {
|
return {
|
||||||
id: "0",
|
id: "0",
|
||||||
username: "selimaj",
|
username: "selimaj",
|
||||||
start: new Date().getTime(),
|
start: 1770497350654,
|
||||||
views: 230,
|
views: 230,
|
||||||
problem: "solve",
|
problem: "solve",
|
||||||
category: "any%",
|
category: "any%",
|
||||||
|
|||||||
Reference in New Issue
Block a user