import React, { useState, useEffect, useReducer, useRef, useCallback, useMemo, useDeferredValue } from 'react'; import { X, Loader2, Plus, Trash2, ChevronUp, ChevronDown, Scissors, AlertCircle, Undo2, Redo2, ChevronsRight, ChevronsLeft, PanelLeft, PanelLeftClose, Film, } from 'lucide-react'; import { getApiUrl } from '../config'; import { apiFetch, apiJson, QuotaError } from '../lib/api'; import { useAuth } from '../contexts/AuthContext'; // Full-screen clip editor: shows WHICH source segments a clip was cut from, // lets the user trim/extend/split/reorder them (word-snapped), and re-renders // through POST /api/clip/rerender. The recipe (EDL) comes from GET .../edl. const MIN_SEGMENT_SECONDS = 0.5; const SNAP_WINDOW_SECONDS = 0.35; // Hiding the source is a working preference, not a per-clip one, so it sticks. const HIDE_SOURCE_KEY = 'openshorts_editor_hide_source'; // Words per memoised transcript slice. See TranscriptChunk for why this exists. const CHUNK_WORDS = 50; // Segment edges round-trip through 3 decimals, so coverage arithmetic needs a // hair of tolerance before it calls a millisecond sliver "not rendered". const COVERAGE_EPSILON = 0.02; const SEGMENT_COLORS = [ 'oklch(76% .17 50)', // brass 'oklch(70% .12 200)', 'oklch(72% .13 140)', 'oklch(70% .14 300)', 'oklch(74% .13 90)', 'oklch(68% .13 250)', ]; function fmt(t) { if (!Number.isFinite(t)) return '–:––'; const m = Math.floor(t / 60); const s = t - m * 60; return `${m}:${s.toFixed(1).padStart(4, '0')}`; } function totalOf(segments) { return segments.reduce((acc, s) => acc + (s.end - s.start), 0); } function editorReducer(state, action) { switch (action.type) { case 'init': return { segments: action.segments, selected: 0, past: [], future: [], pendingBase: null }; case 'select': return { ...state, selected: action.index }; // Live drag feedback: replaces segments without touching history; the // pre-drag snapshot is kept so the whole drag undoes as ONE step. case 'preview': return { ...state, segments: action.segments, pendingBase: state.pendingBase || state.segments }; case 'commit': { const base = state.pendingBase || state.segments; return { ...state, segments: action.segments, selected: Math.min(action.select ?? state.selected, action.segments.length - 1), past: [...state.past, base], future: [], pendingBase: null, }; } case 'undo': { if (!state.past.length) return state; const prev = state.past[state.past.length - 1]; return { ...state, segments: prev, selected: Math.min(state.selected, prev.length - 1), past: state.past.slice(0, -1), future: [state.segments, ...state.future], pendingBase: null, }; } case 'redo': { if (!state.future.length) return state; const next = state.future[0]; return { ...state, segments: next, selected: Math.min(state.selected, next.length - 1), past: [...state.past, state.segments], future: state.future.slice(1), pendingBase: null, }; } default: return state; } } export default function ClipEditor({ jobId, clipIndex, clipTitle, onClose, onRerendered }) { const { refreshMe } = useAuth(); const [edl, setEdl] = useState(null); const [loadError, setLoadError] = useState(null); const [state, dispatch] = useReducer(editorReducer, { segments: [], selected: 0, past: [], future: [], pendingBase: null }); const { segments, selected } = state; const [snapToWords, setSnapToWords] = useState(true); const [reapplyCaptions, setReapplyCaptions] = useState(true); // Framing override: 'auto' (classifier) | 'full' (whole frame) | 'track'. const [framing, setFraming] = useState('auto'); const [renderedFraming, setRenderedFraming] = useState('auto'); // The recipe of the currently RENDERED preview (playhead maps onto it). const [renderedSegments, setRenderedSegments] = useState(null); const [previewUrl, setPreviewUrl] = useState(null); const [rendering, setRendering] = useState(false); const [renderSeconds, setRenderSeconds] = useState(0); const [renderError, setRenderError] = useState(null); const [confirmClose, setConfirmClose] = useState(false); const [selectedWord, setSelectedWord] = useState(null); const [playhead, setPlayhead] = useState(0); const [showSource, setShowSource] = useState(() => { try { return localStorage.getItem(HIDE_SOURCE_KEY) !== '1'; } catch { return true; } }); // Where the source monitor's playhead is, for the transcript to follow. const [sourceTime, setSourceTime] = useState(0); // Three-point editing, like a Premiere source monitor: mark IN and OUT on // the source, then send that range to the clip. Kept as two independent // numbers rather than a range so either end can be re-marked on its own. const [markIn, setMarkIn] = useState(null); const [markOut, setMarkOut] = useState(null); const videoRef = useRef(null); const sourceRef = useRef(null); const clipTrackRef = useRef(null); const sourceTrackRef = useRef(null); const dragRef = useRef(null); const [ghost, setGhost] = useState(null); // in-progress new segment on the source track const transcriptRef = useRef(null); useEffect(() => { try { localStorage.setItem(HIDE_SOURCE_KEY, showSource ? '0' : '1'); } catch { /* private mode */ } }, [showSource]); // ---- scrubbing the source monitor --------------------------------------- // Dragging on the source track drives this