@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
tsx 223 lines 7.14 KB
Raw
1 /**
2 * The live presenter: the real Remotion compositions played in the browser,
3 * stepped like the tcut presenter, and hot-reloaded as you edit.
4 *
5 * → / PageDown play the next step (stops at its end)
6 * ← / PageUp play the previous step
7 * Space pause / resume (replays a finished step)
8 * R replay this step
9 * Home / End first / last step
10 * F presentation mode: fullscreen, video only (F or Esc to leave)
11 *
12 * The current step lives in the URL hash, so a reload lands on the same step.
13 * Opens the agent-loop talk (intro/loop.ts); `?deck=talk` opens the original one.
14 */
15 import { Player, type PlayerRef } from "@remotion/player";
16 import { StrictMode, useCallback, useEffect, useRef, useState, type ComponentType } from "react";
17 import { createRoot } from "react-dom/client";
18 import { Intro } from "../remotion/intro/Intro.tsx";
19 import { Scene } from "../remotion/scene/Scene.tsx";
20 import { Slide } from "../remotion/slides/Slide.tsx";
21 import { VIDEO } from "../shared/types.ts";
22 import { loadDeck, type LiveItem, type LiveStep } from "./deck.ts";
23
24 type Deck = { items: LiveItem[]; steps: LiveStep[] };
25
26 const componentFor = (item: LiveItem) =>
27 (item.item.kind === "intro"
28 ? Intro
29 : item.item.kind === "scene"
30 ? Scene
31 : Slide) as unknown as ComponentType<Record<string, unknown>>;
32
33 const stepFromHash = () => Math.max(0, Number(location.hash.slice(1)) - 1 || 0);
34
35 function App() {
36 const [deck, setDeck] = useState<Deck>();
37 const [index, setIndex] = useState(stepFromHash);
38 const [presenting, setPresenting] = useState(false);
39 const [playing, setPlaying] = useState(false);
40 const [ended, setEnded] = useState(false);
41 const player = useRef<PlayerRef>(null);
42 /** Whether the current step should start playing once its player is ready. */
43 const autoplay = useRef(false);
44
45 const reload = useCallback(
46 () => loadDeck(new URLSearchParams(location.search).get("deck") ?? "loop").then(setDeck),
47 [],
48 );
49 useEffect(() => {
50 reload();
51 // intro.json or a scene.json was rebuilt: reload the data, keep the step.
52 import.meta.hot?.on("prezzy:data", reload);
53 }, [reload]);
54
55 const step = deck?.steps[Math.min(index, (deck?.steps.length ?? 1) - 1)];
56 const item = step ? deck!.items[step.item] : undefined;
57
58 useEffect(() => {
59 history.replaceState(null, "", `${location.search}#${index + 1}`);
60 }, [index]);
61
62 // Enter a step: play it from the start if we got here with → / ←; otherwise
63 // (first load, hot reload) show where it ends, which is what you're editing.
64 const settle = useCallback(() => {
65 const p = player.current;
66 if (!p || !step) return;
67 p.pause();
68 p.seekTo(step.to - 1);
69 setEnded(true);
70 }, [step]);
71 useEffect(() => {
72 const p = player.current;
73 if (!p || !step) return;
74 if (autoplay.current) {
75 setEnded(false);
76 p.seekTo(step.from);
77 p.play();
78 } else {
79 settle();
80 }
81 autoplay.current = false;
82 }, [step?.item, step?.from, step?.to, deck]);
83
84 useEffect(() => {
85 const p = player.current;
86 if (!p) return;
87 const onPlay = () => setPlaying(true);
88 const onPause = () => setPlaying(false);
89 // moveToBeginningWhenEnded={false} keeps the last frame; settle() pins it.
90 const onEnded = () => {
91 setPlaying(false);
92 settle();
93 };
94 p.addEventListener("play", onPlay);
95 p.addEventListener("pause", onPause);
96 p.addEventListener("ended", onEnded);
97 return () => {
98 p.removeEventListener("play", onPlay);
99 p.removeEventListener("pause", onPause);
100 p.removeEventListener("ended", onEnded);
101 };
102 }, [item, settle]);
103
104 const go = useCallback(
105 (next: number, play = true) => {
106 if (!deck) return;
107 const clamped = Math.max(0, Math.min(deck.steps.length - 1, next));
108 autoplay.current = play;
109 if (clamped === index) {
110 // Same step (first/last): just replay it.
111 player.current?.seekTo(deck.steps[clamped]!.from);
112 if (play) player.current?.play();
113 }
114 setIndex(clamped);
115 },
116 [deck, index],
117 );
118
119 useEffect(() => {
120 // Esc leaves fullscreen, and with it presentation mode.
121 const onChange = () => {
122 if (!document.fullscreenElement) setPresenting(false);
123 };
124 document.addEventListener("fullscreenchange", onChange);
125 return () => document.removeEventListener("fullscreenchange", onChange);
126 }, []);
127
128 const state = playing ? "playing" : ended ? "end · → next" : "paused";
129
130 const handleKey = useCallback(
131 (key: string) => {
132 const p = player.current;
133 switch (key) {
134 case "ArrowRight":
135 case "PageDown":
136 go(index + 1);
137 break;
138 case "ArrowLeft":
139 case "PageUp":
140 go(index - 1);
141 break;
142 case " ":
143 if (ended || !p) go(index);
144 else if (p.isPlaying()) p.pause();
145 else p.play();
146 break;
147 case "r":
148 go(index);
149 break;
150 case "Home":
151 go(0, false);
152 break;
153 case "End":
154 go(Number.MAX_SAFE_INTEGER, false);
155 break;
156 case "f":
157 // Presentation mode works even where fullscreen is refused (or the browser's own fullscreen is used).
158 if (presenting) {
159 setPresenting(false);
160 if (document.fullscreenElement) document.exitFullscreen();
161 } else {
162 setPresenting(true);
163 document.documentElement.requestFullscreen().catch(() => {});
164 }
165 break;
166 default:
167 return false;
168 }
169 return true;
170 },
171 [go, index, ended, presenting],
172 );
173
174 useEffect(() => {
175 const onKey = (event: KeyboardEvent) => {
176 if (handleKey(event.key.length === 1 ? event.key.toLowerCase() : event.key))
177 event.preventDefault();
178 };
179 window.addEventListener("keydown", onKey);
180 return () => window.removeEventListener("keydown", onKey);
181 }, [handleKey]);
182
183 if (!deck || !step || !item) return <div className="loading">Loading the deck…</div>;
184 return (
185 <div className={presenting ? "stage presenting" : "stage"}>
186 <div className="player">
187 <Player
188 ref={player}
189 key={item.item.id}
190 component={componentFor(item)}
191 inputProps={item.inputProps}
192 durationInFrames={item.durationInFrames}
193 compositionWidth={VIDEO.width}
194 compositionHeight={VIDEO.height}
195 fps={VIDEO.fps}
196 inFrame={step.from}
197 outFrame={step.to - 1}
198 moveToBeginningWhenEnded={false}
199 acknowledgeRemotionLicense
200 // Only the presenter's keys control playback.
201 spaceKeyToPlayOrPause={false}
202 clickToPlay={false}
203 doubleClickToFullscreen={false}
204 style={{ width: "100%", height: "100%" }}
205 />
206 </div>
207 <div className="bar">
208 <span className="count">
209 {index + 1} / {deck.steps.length}
210 </span>
211 <span className="title">{step.title}</span>
212 <span className="state">{state}</span>
213 <span className="hint">F to present</span>
214 </div>
215 </div>
216 );
217 }
218
219 createRoot(document.getElementById("root")!).render(
220 <StrictMode>
221 <App />
222 </StrictMode>,
223 );