type RecordingState = | "WaitingForGame" | "Buffering" | "RecordingSession" | "Clipping" | "Processing" | "StorageLow"; type ClipSource = "Auto Event" | "Manual Hotkey" | "Bookmark" | "Imported"; type UploadState = "Local only" | "Uploaded" | "Failed" | "Queued"; type Clip = { id: string; title: string; game: string; event: string; source: ClipSource; duration: string; createdAt: string; uploadState: UploadState; path: string; tags: string[]; colorClass: string; }; type AutoEvent = { id: string; game: string; event: string; enabled: boolean; }; type DesktopStatus = { recording_state: RecordingState; detected_game: string | null; replay_buffer_seconds: number; mic_enabled: boolean; upload_enabled: boolean; session_recording: boolean; capture_active: boolean; capture_path: string | null; clip_count: number; library_root: string; ffmpeg_available: boolean; ffmpeg_path: string | null; }; type ClipDto = { id: string; title: string; game: string; event: string; source: ClipSource; duration: string; created_at: string; upload_state: UploadState; path: string; tags: string[]; color_class: string; }; type AppState = { activeView: "Library" | "Recording" | "Auto Clip" | "Uploads" | "Settings"; recordingState: RecordingState; detectedGame: string; replayBufferSeconds: number; sessionRecording: boolean; captureActive: boolean; capturePath: string | null; ffmpegAvailable: boolean; ffmpegPath: string | null; micEnabled: boolean; autoUpload: boolean; selectedClipId: string; clips: Clip[]; autoEvents: AutoEvent[]; }; type TauriGlobal = { core?: { invoke?: (command: string, args?: Record) => Promise; }; }; declare global { interface Window { __TAURI__?: TauriGlobal; __TAURI_INTERNALS__?: unknown; } } let tauriInvoke: ((command: string, args?: Record) => Promise) | null = null; const state: AppState = { activeView: "Library", recordingState: "Buffering", detectedGame: "Counter-Strike 2", replayBufferSeconds: 60, sessionRecording: false, captureActive: false, capturePath: null, ffmpegAvailable: false, ffmpegPath: null, micEnabled: false, autoUpload: false, selectedClipId: "clip-1", clips: [ { id: "clip-1", title: "Dust II clutch", game: "Counter-Strike 2", event: "Kill", source: "Auto Event", duration: "0:18", createdAt: "Today 12:08", uploadState: "Local only", path: "", tags: ["clutch", "kill"], colorClass: "kill", }, { id: "clip-2", title: "Baron steal", game: "League of Legends", event: "Objective", source: "Auto Event", duration: "0:22", createdAt: "Today 11:44", uploadState: "Queued", path: "", tags: ["objective"], colorClass: "objective", }, { id: "clip-3", title: "Manual save", game: "Dota 2", event: "Manual", source: "Manual Hotkey", duration: "1:00", createdAt: "Yesterday 22:15", uploadState: "Uploaded", path: "", tags: ["teamfight"], colorClass: "manual", }, ], autoEvents: [ { id: "cs2-kill", game: "Counter-Strike 2", event: "Kill", enabled: true }, { id: "cs2-round", game: "Counter-Strike 2", event: "Round win", enabled: true }, { id: "lol-kill", game: "League of Legends", event: "Champion kill", enabled: true }, { id: "lol-objective", game: "League of Legends", event: "Objective", enabled: true }, { id: "dota-objective", game: "Dota 2", event: "Roshan/objective", enabled: false }, ], }; const root = document.querySelector("#app"); if (!root) { throw new Error("Missing #app root"); } const appRoot = root; function render() { const selectedClip = state.clips.find((clip) => clip.id === state.selectedClipId) ?? state.clips[0]; appRoot.innerHTML = `
${renderRecordingBar()} ${renderActiveView(selectedClip)}
`; bindEvents(); } function renderRecordingBar() { const statusText = state.captureActive ? "Recording to disk" : state.sessionRecording ? "Recording session" : `${state.recordingState} ${state.replayBufferSeconds}s`; return `

Detected game

${state.detectedGame}

${statusText}
`; } function renderActiveView(selectedClip: Clip) { switch (state.activeView) { case "Recording": return renderRecordingView(); case "Auto Clip": return renderAutoClipView(); case "Uploads": return renderUploadsView(); case "Settings": return renderSettingsView(); case "Library": default: return renderLibraryView(selectedClip); } } function renderLibraryView(selectedClip: Clip | undefined) { return `

Local library

Recent Clips

${state.clips.length > 0 ? state.clips.map(renderClipCard).join("") : `
No clips saved yet
`}
${renderClipDetails(selectedClip)}
`; } function clipFromDto(dto: ClipDto): Clip { return { id: dto.id, title: dto.title, game: dto.game, event: dto.event, source: dto.source, duration: dto.duration, createdAt: dto.created_at, uploadState: dto.upload_state, path: dto.path, tags: dto.tags, colorClass: dto.color_class, }; } function renderClipCard(clip: Clip) { return `
${clip.event}

${clip.title}

${clip.game} · ${clip.source} · ${clip.duration}

`; } function renderClipDetails(clip: Clip | undefined) { if (!clip) { return ` `; } return ` `; } function renderRecordingView() { return `

Replay buffer

${state.replayBufferSeconds} seconds

Capture

720p30 Performance

Lightweight desktop capture uses bundled FFmpeg when system FFmpeg is missing.

${state.ffmpegAvailable ? "FFmpeg ready" : "FFmpeg missing"}${state.ffmpegPath ? ` · ${state.ffmpegPath}` : ""}

${state.capturePath ? `Writing ${state.capturePath}` : "No active capture file"}

Audio

System audio on

`; } function renderAutoClipView() { return `

Game events

Auto Clip Rules

Local APIs only
${state.autoEvents .map( (event) => ` `, ) .join("")}
`; } function renderUploadsView() { return `
${["Catbox", "Litterbox", "Custom HTTP", "Lustful"].map((provider) => `

Provider

${provider}

${provider === "Lustful" ? "Stubbed until API details are confirmed." : "Optional; local clips remain available if upload fails."}

`).join("")}
`; } function renderSettingsView() { return `

Privacy

Local-first

Storage

50 GB cap

Old temporary buffer segments are removed automatically; saved clips are kept.

Hotkeys

F8 saves last 60s

Shift+F8 saves 30s · Alt+F7 toggles session recording.

`; } function bindEvents() { appRoot.querySelectorAll("[data-view]").forEach((button) => { button.addEventListener("click", () => { state.activeView = button.dataset.view as AppState["activeView"]; render(); }); }); appRoot.querySelectorAll("[data-clip-id]").forEach((card) => { card.addEventListener("click", () => { state.selectedClipId = card.dataset.clipId ?? state.selectedClipId; render(); }); }); appRoot.querySelector("[data-action='clip']")?.addEventListener("click", () => { void saveClip(); }); appRoot.querySelector("[data-action='toggle-record']")?.addEventListener("click", () => { void toggleRecording(); }); appRoot.querySelector("[data-action='process']")?.addEventListener("click", () => { void trimSelectedClip(); }); appRoot.querySelector("[data-action='reveal']")?.addEventListener("click", () => { void revealSelectedClip(); }); appRoot.querySelector("[data-action='upload']")?.addEventListener("click", () => { void uploadSelectedClip(); }); appRoot.querySelector("[data-action='delete']")?.addEventListener("click", () => { void deleteSelectedClip(); }); appRoot.querySelector("[data-action='buffer-length']")?.addEventListener("input", (event) => { state.replayBufferSeconds = Number((event.target as HTMLInputElement).value); render(); }); appRoot.querySelector("[data-action='toggle-mic']")?.addEventListener("change", (event) => { state.micEnabled = (event.target as HTMLInputElement).checked; render(); }); appRoot.querySelector("[data-action='toggle-auto-upload']")?.addEventListener("change", (event) => { state.autoUpload = (event.target as HTMLInputElement).checked; render(); }); appRoot.querySelectorAll("[data-event-id]").forEach((input) => { input.addEventListener("change", () => { const eventRule = state.autoEvents.find((event) => event.id === input.dataset.eventId); if (eventRule) { eventRule.enabled = input.checked; } }); }); } async function saveClip() { state.recordingState = "Clipping"; render(); if (tauriInvoke) { try { const dto = await tauriInvoke("save_manual_clip", { seconds: state.replayBufferSeconds }); const clip = clipFromDto(dto); state.clips.unshift(clip); state.selectedClipId = clip.id; state.activeView = "Library"; state.recordingState = "Processing"; await refreshClips(); render(); return; } catch (error) { console.error("Could not save desktop clip", error); } } state.clips.unshift({ id: `manual-${Date.now()}`, title: "Manual clip", game: state.detectedGame, event: "Manual", source: "Manual Hotkey", duration: "1:00", createdAt: "Just now", uploadState: "Local only", path: "", tags: ["manual"], colorClass: "manual", }); state.selectedClipId = state.clips[0].id; state.activeView = "Library"; render(); } async function toggleRecording() { if (tauriInvoke) { try { const status = await tauriInvoke(state.captureActive ? "stop_capture" : "start_capture"); applyDesktopStatus(status); await refreshClips(); render(); return; } catch (error) { console.error("Could not toggle desktop recording", error); } } state.sessionRecording = !state.sessionRecording; state.recordingState = state.sessionRecording ? "RecordingSession" : "Buffering"; render(); } function applyDesktopStatus(status: DesktopStatus) { state.recordingState = status.recording_state; state.detectedGame = status.detected_game ?? "Waiting for game"; state.replayBufferSeconds = status.replay_buffer_seconds; state.micEnabled = status.mic_enabled; state.autoUpload = status.upload_enabled; state.sessionRecording = status.session_recording; state.captureActive = status.capture_active; state.capturePath = status.capture_path; state.ffmpegAvailable = status.ffmpeg_available; state.ffmpegPath = status.ffmpeg_path; } async function trimSelectedClip() { if (!tauriInvoke) { return; } const clip = state.clips.find((candidate) => candidate.id === state.selectedClipId); if (!clip) { return; } const startInput = appRoot.querySelector("[data-action='trim-start']"); const endInput = appRoot.querySelector("[data-action='trim-end']"); const startSeconds = parseTimestamp(startInput?.value ?? "0"); const endSeconds = parseTimestamp(endInput?.value ?? clip.duration); try { const dto = await tauriInvoke("trim_clip", { clipId: clip.id, startSeconds, endSeconds, }); const trimmed = clipFromDto(dto); state.clips.unshift(trimmed); state.selectedClipId = trimmed.id; await refreshClips(); render(); } catch (error) { console.error("Could not trim clip", error); } } async function revealSelectedClip() { if (!tauriInvoke || !state.selectedClipId) { return; } try { await tauriInvoke("reveal_clip", { clipId: state.selectedClipId }); } catch (error) { console.error("Could not reveal clip", error); } } async function uploadSelectedClip() { if (!tauriInvoke || !state.selectedClipId) { return; } try { const dto = await tauriInvoke("upload_clip", { clipId: state.selectedClipId }); const updated = clipFromDto(dto); state.clips = state.clips.map((clip) => (clip.id === updated.id ? updated : clip)); render(); } catch (error) { console.error("Could not queue upload", error); } } async function deleteSelectedClip() { if (!tauriInvoke || !state.selectedClipId) { return; } try { const clips = await tauriInvoke("delete_clip", { clipId: state.selectedClipId }); state.clips = clips.map(clipFromDto); state.selectedClipId = state.clips[0]?.id ?? ""; render(); } catch (error) { console.error("Could not delete clip", error); } } async function refreshClips() { if (!tauriInvoke) { return; } const clips = await tauriInvoke("list_clips"); state.clips = clips.map(clipFromDto); if (!state.clips.some((clip) => clip.id === state.selectedClipId)) { state.selectedClipId = state.clips[0]?.id ?? ""; } } function parseTimestamp(value: string) { const parts = value.trim().split(":").map(Number); if (parts.some((part) => Number.isNaN(part))) { return 0; } if (parts.length === 1) { return parts[0]; } if (parts.length === 2) { return parts[0] * 60 + parts[1]; } return parts[0] * 3600 + parts[1] * 60 + parts[2]; } async function loadDesktopBridge() { const hasTauri = "__TAURI_INTERNALS__" in window; if (!hasTauri) { return; } try { tauriInvoke = window.__TAURI__?.core?.invoke ?? null; if (!tauriInvoke) { return; } const invoke = tauriInvoke; const status = await invoke("get_status"); applyDesktopStatus(status); const clips = await invoke("list_clips"); state.clips = clips.map(clipFromDto); state.selectedClipId = state.clips[0]?.id ?? ""; render(); } catch (error) { console.warn("Tauri bridge unavailable", error); } } render(); void loadDesktopBridge();