The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { useEffect, useRef } from "react"; |
| 2 | import { |
| 3 | Freeze, |
| 4 | getRemotionEnvironment, |
| 5 | OffthreadVideo, |
| 6 | staticFile, |
| 7 | useCurrentFrame, |
| 8 | useVideoConfig, |
| 9 | } from "remotion"; |
| 10 | import { |
| 11 | TAB_BAR, |
| 12 | TERMINAL, |
| 13 | TITLE_BAR, |
| 14 | type SceneCapture, |
| 15 | type TerminalTab, |
| 16 | } from "../../shared/types.ts"; |
| 17 | import { sans } from "../fonts.ts"; |
| 18 | import { TrafficLights, Window } from "./Desktop.tsx"; |
| 19 | import type { SceneSchedule } from "./schedule.ts"; |
| 20 | |
| 21 | /** Ghostty's default dark background, matching the recorded clip. */ |
| 22 | const BACKGROUND = "#282c34"; |
| 23 | /** Ghostty's macOS chrome: title bar and native tab bar. */ |
| 24 | const CHROME = "#21252b"; |
| 25 | const TAB_IDLE = "#1b1e23"; |
| 26 | const DIVIDER = "rgba(0,0,0,0.45)"; |
| 27 | |
| 28 | const TABS: TerminalTab[] = ["deploy", "test", "dev"]; |
| 29 | const TAB_TITLES: Record<TerminalTab, string> = { |
| 30 | deploy: "deploy", |
| 31 | test: "test", |
| 32 | dev: "dev", |
| 33 | }; |
| 34 | |
| 35 | /** Position in the terminal clip at `frame`: plays during terminal beats, holds between them. */ |
| 36 | const clipTime = (plan: SceneSchedule, frame: number, fps: number): number => { |
| 37 | let time: number | undefined; |
| 38 | for (const segment of plan.segments) { |
| 39 | const { beat } = segment; |
| 40 | if (beat.kind !== "terminal") continue; |
| 41 | if (segment.from > frame) { |
| 42 | // Before the first terminal beat: its opening frame (a fresh prompt). |
| 43 | time ??= beat.start; |
| 44 | break; |
| 45 | } |
| 46 | const elapsed = Math.max(0, frame - segment.from - segment.switchFrames) / fps; |
| 47 | time = Math.min(beat.start + elapsed, beat.end); |
| 48 | } |
| 49 | return time ?? 0; |
| 50 | }; |
| 51 | |
| 52 | /** The tab on screen at clip time `time`. */ |
| 53 | const tabAt = (capture: SceneCapture, time: number): TerminalTab => { |
| 54 | let active: TerminalTab = capture.terminal?.tabs?.[0]?.tab ?? "deploy"; |
| 55 | for (const change of capture.terminal?.tabs ?? []) { |
| 56 | if (change.at > time) break; |
| 57 | active = change.tab; |
| 58 | } |
| 59 | return active; |
| 60 | }; |
| 61 | |
| 62 | const GhosttyIcon = () => ( |
| 63 | <svg width="15" height="15" viewBox="0 0 24 24" style={{ flex: "none", opacity: 0.8 }}> |
| 64 | <path |
| 65 | d="M12 2C7.6 2 4.5 5.3 4.5 9.6V20c0 .9 1 1.4 1.7.8l1.6-1.3 1.6 1.3c.4.3 1 .3 1.3 0L12 19.5l1.3 1.3c.4.3.9.3 1.3 0l1.6-1.3 1.6 1.3c.7.6 1.7.1 1.7-.8V9.6C19.5 5.3 16.4 2 12 2Z" |
| 66 | fill="#d9d9d9" |
| 67 | /> |
| 68 | <circle cx="9.5" cy="10" r="1.4" fill="#21252b" /> |
| 69 | <circle cx="14.5" cy="10" r="1.4" fill="#21252b" /> |
| 70 | </svg> |
| 71 | ); |
| 72 | |
| 73 | /** Title bar plus Ghostty's native macOS tab bar (equal-width tabs, selected one merges with the terminal). */ |
| 74 | const Chrome = ({ active }: { active: TerminalTab }) => ( |
| 75 | <div style={{ flex: "none", fontFamily: sans }}> |
| 76 | <div |
| 77 | style={{ |
| 78 | height: TITLE_BAR, |
| 79 | display: "flex", |
| 80 | alignItems: "center", |
| 81 | padding: "0 18px", |
| 82 | position: "relative", |
| 83 | background: CHROME, |
| 84 | }} |
| 85 | > |
| 86 | <TrafficLights /> |
| 87 | <div |
| 88 | style={{ |
| 89 | position: "absolute", |
| 90 | inset: 0, |
| 91 | display: "flex", |
| 92 | alignItems: "center", |
| 93 | justifyContent: "center", |
| 94 | gap: 8, |
| 95 | color: "#c8c8c8", |
| 96 | fontSize: 14, |
| 97 | fontWeight: 600, |
| 98 | }} |
| 99 | > |
| 100 | <GhosttyIcon /> |
| 101 | {TAB_TITLES[active]} |
| 102 | </div> |
| 103 | </div> |
| 104 | <div |
| 105 | style={{ |
| 106 | height: TAB_BAR, |
| 107 | display: "flex", |
| 108 | background: TAB_IDLE, |
| 109 | borderTop: `1px solid ${DIVIDER}`, |
| 110 | }} |
| 111 | > |
| 112 | {TABS.map((tab, i) => { |
| 113 | const selected = tab === active; |
| 114 | return ( |
| 115 | <div |
| 116 | key={tab} |
| 117 | style={{ |
| 118 | flex: 1, |
| 119 | display: "flex", |
| 120 | alignItems: "center", |
| 121 | justifyContent: "center", |
| 122 | position: "relative", |
| 123 | background: selected ? BACKGROUND : TAB_IDLE, |
| 124 | borderLeft: i > 0 ? `1px solid ${DIVIDER}` : undefined, |
| 125 | color: selected ? "#f0f0f0" : "#8b8f96", |
| 126 | fontSize: 13.5, |
| 127 | fontWeight: selected ? 600 : 500, |
| 128 | }} |
| 129 | > |
| 130 | {TAB_TITLES[tab]} |
| 131 | <span |
| 132 | style={{ |
| 133 | position: "absolute", |
| 134 | right: 14, |
| 135 | color: "#6b7078", |
| 136 | fontSize: 12, |
| 137 | }} |
| 138 | > |
| 139 | ⌘{i + 1} |
| 140 | </span> |
| 141 | </div> |
| 142 | ); |
| 143 | })} |
| 144 | <div |
| 145 | style={{ |
| 146 | width: 38, |
| 147 | display: "grid", |
| 148 | placeItems: "center", |
| 149 | color: "#8b8f96", |
| 150 | fontSize: 18, |
| 151 | borderLeft: `1px solid ${DIVIDER}`, |
| 152 | }} |
| 153 | > |
| 154 | + |
| 155 | </div> |
| 156 | </div> |
| 157 | </div> |
| 158 | ); |
| 159 | |
| 160 | /** |
| 161 | * In the live presenter, seeking a video to an exact frame on every frame |
| 162 | * stalls, so the terminal plays as a normal <video> kept in step with the |
| 163 | * timeline: it plays while clip time advances and is re-seeked only when it |
| 164 | * drifts or jumps. |
| 165 | */ |
| 166 | const LiveClip = ({ src, time }: { src: string; time: number }) => { |
| 167 | const ref = useRef<HTMLVideoElement>(null); |
| 168 | const frame = useCurrentFrame(); |
| 169 | const { fps } = useVideoConfig(); |
| 170 | const last = useRef({ frame, time }); |
| 171 | const idle = useRef<ReturnType<typeof setTimeout>>(undefined); |
| 172 | useEffect(() => { |
| 173 | const video = ref.current; |
| 174 | if (!video) return; |
| 175 | const advancing = frame === last.current.frame + 1 && time > last.current.time; |
| 176 | last.current = { frame, time }; |
| 177 | if (Math.abs(video.currentTime - time) > 0.25) video.currentTime = time; |
| 178 | if (advancing && video.paused) video.play().catch(() => {}); |
| 179 | if (!advancing && !video.paused) video.pause(); |
| 180 | // The timeline stopped (end of a step, pause): stop the clip on the frame it's showing. |
| 181 | clearTimeout(idle.current); |
| 182 | idle.current = setTimeout( |
| 183 | () => { |
| 184 | video.pause(); |
| 185 | video.currentTime = last.current.time; |
| 186 | }, |
| 187 | (2 * 1000) / fps, |
| 188 | ); |
| 189 | }, [frame, time, fps]); |
| 190 | useEffect(() => () => clearTimeout(idle.current), []); |
| 191 | return ( |
| 192 | <video |
| 193 | ref={ref} |
| 194 | src={src} |
| 195 | muted |
| 196 | playsInline |
| 197 | preload="auto" |
| 198 | style={{ width: TERMINAL.width, height: TERMINAL.height, display: "block" }} |
| 199 | /> |
| 200 | ); |
| 201 | }; |
| 202 | |
| 203 | export const Terminal = ({ |
| 204 | capture, |
| 205 | plan, |
| 206 | frame, |
| 207 | fps, |
| 208 | }: { |
| 209 | capture: SceneCapture; |
| 210 | plan: SceneSchedule; |
| 211 | frame: number; |
| 212 | fps: number; |
| 213 | }) => { |
| 214 | const time = clipTime(plan, frame, fps); |
| 215 | const active = tabAt(capture, time); |
| 216 | // Stay one frame inside the clip so the last frame is always decodable. |
| 217 | const last = capture.terminal ? Math.max(0, Math.floor(capture.terminal.duration * fps) - 1) : 0; |
| 218 | const videoFrame = Math.min(Math.round(time * fps), last); |
| 219 | return ( |
| 220 | <Window background={BACKGROUND} bar={<Chrome active={active} />}> |
| 221 | {capture.terminal && getRemotionEnvironment().isPlayer ? ( |
| 222 | <LiveClip src={staticFile(capture.terminal.clip)} time={time} /> |
| 223 | ) : capture.terminal ? ( |
| 224 | <Freeze frame={videoFrame}> |
| 225 | <OffthreadVideo |
| 226 | src={staticFile(capture.terminal.clip)} |
| 227 | muted |
| 228 | style={{ width: TERMINAL.width, height: TERMINAL.height, display: "block" }} |
| 229 | /> |
| 230 | </Freeze> |
| 231 | ) : null} |
| 232 | </Window> |
| 233 | ); |
| 234 | }; |