t2-mapper/src/components/LoadDemoButton.tsx

97 lines
3 KiB
TypeScript
Raw Normal View History

2026-02-20 15:48:15 -08:00
import { useCallback, useRef } from "react";
2026-02-28 17:58:09 -08:00
import { MdOndemandVideo } from "react-icons/md";
import { createLogger } from "../logger";
import { liveConnectionStore } from "../state/liveConnectionStore";
2026-03-09 12:38:40 -07:00
import { usePlaybackActions, useRecording } from "./RecordingProvider";
2026-03-01 09:40:17 -08:00
import styles from "./LoadDemoButton.module.css";
2026-02-20 15:48:15 -08:00
const log = createLogger("LoadDemoButton");
export function LoadDemoButton({
isActive = false,
choosingMap = false,
onCancelChoosingMap,
}: {
isActive?: boolean;
choosingMap?: boolean;
onCancelChoosingMap?: () => void;
}) {
2026-03-09 12:38:40 -07:00
const recording = useRecording();
const isDemoLoaded = recording?.source === "demo";
const { setRecording } = usePlaybackActions();
2026-02-20 15:48:15 -08:00
const inputRef = useRef<HTMLInputElement>(null);
2026-02-28 17:58:09 -08:00
const parseTokenRef = useRef(0);
2026-02-20 15:48:15 -08:00
const handleClick = useCallback(() => {
if (choosingMap && isDemoLoaded) {
onCancelChoosingMap?.();
return;
}
2026-03-09 12:38:40 -07:00
if (isDemoLoaded) {
// Unload the recording/parser but leave entities frozen in the store.
2026-02-28 17:58:09 -08:00
parseTokenRef.current += 1;
2026-02-20 15:48:15 -08:00
setRecording(null);
return;
}
inputRef.current?.click();
}, [isDemoLoaded, choosingMap, onCancelChoosingMap, setRecording]);
2026-02-20 15:48:15 -08:00
const handleFileChange = useCallback(
async (e: React.ChangeEvent<HTMLInputElement>) => {
const file = e.target.files?.[0];
if (!file) return;
// Reset the input so the same file can be re-selected.
e.target.value = "";
try {
const buffer = await file.arrayBuffer();
2026-02-28 17:58:09 -08:00
const parseToken = parseTokenRef.current + 1;
parseTokenRef.current = parseToken;
const { createDemoStreamingRecording } =
await import("../stream/demoStreaming");
2026-02-28 17:58:09 -08:00
const recording = await createDemoStreamingRecording(buffer);
if (parseTokenRef.current !== parseToken) {
return;
}
// Disconnect from any live server before loading the demo.
const liveState = liveConnectionStore.getState();
liveState.disconnectServer();
2026-02-28 17:58:09 -08:00
// Metadata-first: mission/game-mode sync happens immediately.
setRecording(recording);
2026-02-20 15:48:15 -08:00
} catch (err) {
log.error("Failed to load demo: %o", err);
2026-02-20 15:48:15 -08:00
}
},
[setRecording],
);
return (
<>
<input
ref={inputRef}
type="file"
accept=".rec"
style={{ display: "none" }}
onChange={handleFileChange}
/>
<button
type="button"
2026-03-01 09:40:17 -08:00
className={styles.Root}
2026-03-09 12:38:40 -07:00
aria-label={isDemoLoaded ? "Unload demo" : "Load demo (.rec)"}
title={isDemoLoaded ? "Unload demo" : "Load demo (.rec)"}
2026-02-20 15:48:15 -08:00
onClick={handleClick}
data-active={isActive}
2026-02-20 15:48:15 -08:00
>
2026-03-01 09:40:17 -08:00
<MdOndemandVideo className={styles.DemoIcon} />
<span className={styles.ButtonLabel}>Demo</span>
<span className={styles.ButtonHint}>
{choosingMap && isDemoLoaded
? "Return to demo"
: isDemoLoaded
? "Click to unload"
: "Load a .rec file"}
2026-02-20 15:48:15 -08:00
</span>
</button>
</>
);
}