import React, { useState, useEffect, useRef } from 'react'; import { Link2, Upload, FileVideo, X, Info, Loader2, ChevronDown } from 'lucide-react'; import { track } from '../lib/analytics'; import { getApiUrl } from '../config'; const SUPPORTED_PLATFORMS = [ 'YouTube', 'Vimeo', 'TikTok', 'X / Twitter', 'Twitch', 'Facebook', 'Instagram', 'Dailymotion', 'Reddit', 'Streamable', ]; // Mirrors the server's MIN_SOURCE_SECONDS: shorter sources are rejected with a // 400 after the whole file was uploaded. Checking the duration in the browser // says so the moment the file is picked (a 27 s upload used to end in a bare // "That run failed"). const MIN_SOURCE_SECONDS = 45; // Duration of a local video file in seconds, or null when the browser cannot // read it (unsupported codec): then the server stays the judge. const readVideoDuration = (file) => new Promise((resolve) => { try { const url = URL.createObjectURL(file); const v = document.createElement('video'); v.preload = 'metadata'; const done = (d) => { URL.revokeObjectURL(url); resolve(d); }; v.onloadedmetadata = () => done(Number.isFinite(v.duration) ? v.duration : null); v.onerror = () => done(null); setTimeout(() => done(null), 8000); v.src = url; } catch { resolve(null); } }); export default function MediaInput({ onProcess, isProcessing }) { const [youtubeUrlEnabled, setYoutubeUrlEnabled] = useState(true); // File upload is the primary path; the link is secondary. const [mode, setMode] = useState('file'); // 'file' | 'url' const [url, setUrl] = useState(''); const [file, setFile] = useState(null); const [fileSeconds, setFileSeconds] = useState(null); const fileTooShort = fileSeconds != null && fileSeconds < MIN_SOURCE_SECONDS; const [acknowledged, setAcknowledged] = useState(false); const [outputFormat, setOutputFormat] = useState('vertical'); // vertical | horizontal | square const [showInfo, setShowInfo] = useState(false); // Advanced generation controls — empty string means "let the AI decide", // which keeps the default pipeline behavior untouched. const [showAdvanced, setShowAdvanced] = useState(false); const [targetClips, setTargetClips] = useState(''); const [clipMinSeconds, setClipMinSeconds] = useState(''); const [clipMaxSeconds, setClipMaxSeconds] = useState(''); // Auto-hook: burn the AI hook text into every clip. On by default; the // choice persists so turning it off sticks across sessions. const [autoHook, setAutoHook] = useState(() => { try { return localStorage.getItem('cw_auto_hook') !== '0'; } catch { return true; } }); const [autoHookStyle, setAutoHookStyle] = useState(() => { // v2 key: the default became 'pill'; an old saved 'classic' was just the old default. try { return localStorage.getItem('cw_auto_hook_style_v2') || 'pill'; } catch { return 'pill'; } }); // Layout: 'auto' lets the AI pick per video (server default); the others // force one on so a podcast host who knows what they uploaded doesn't // depend on the detector, and 'none' keeps the plain single crop. const [layout, setLayout] = useState(() => { try { return localStorage.getItem('cw_layout') || 'auto'; } catch { return 'auto'; } }); const infoRef = useRef(null); // Close the compatibility popover on any outside click. useEffect(() => { if (!showInfo) return; const onClick = (e) => { if (infoRef.current && !infoRef.current.contains(e.target)) setShowInfo(false); }; document.addEventListener('mousedown', onClick); return () => document.removeEventListener('mousedown', onClick); }, [showInfo]); useEffect(() => { let cancelled = false; setFileSeconds(null); if (!file) return undefined; readVideoDuration(file).then((d) => { if (cancelled) return; setFileSeconds(d); if (d != null && d < MIN_SOURCE_SECONDS) { track('SourceTooShort', { props: { seconds: String(Math.round(d)) } }); } }); return () => { cancelled = true; }; }, [file]); useEffect(() => { fetch(getApiUrl('/api/config')) .then((r) => r.ok ? r.json() : null) .then((cfg) => { if (cfg && cfg.youtubeUrlEnabled === false) { setYoutubeUrlEnabled(false); setMode('file'); } }) .catch(() => {}); }, []); // A link pasted in the landing hero: preload it here so the user picks up // where they left off. Not auto-submitted — the rights attestation below // has to be ticked by the user. useEffect(() => { let pending = null; try { pending = localStorage.getItem('cw_pending_url'); if (pending) localStorage.removeItem('cw_pending_url'); } catch { /* ignore */ } if (pending) { setMode('url'); setUrl(pending); } }, []); const handleSubmit = (e) => { e.preventDefault(); if (!acknowledged) return; const advanced = { targetClips: targetClips || null, clipMinSeconds: clipMinSeconds || null, clipMaxSeconds: clipMaxSeconds || null, autoHook, autoHookStyle, layout, }; try { localStorage.setItem('cw_auto_hook', autoHook ? '1' : '0'); localStorage.setItem('cw_auto_hook_style_v2', autoHookStyle); localStorage.setItem('cw_layout', layout); } catch { /* ignore */ } if (mode === 'url' && url) { onProcess({ type: 'url', payload: url, acknowledged: true, outputFormat, ...advanced }); } else if (mode === 'file' && file && !fileTooShort) { onProcess({ type: 'file', payload: file, acknowledged: true, outputFormat, ...advanced }); } }; const handleDrop = (e) => { e.preventDefault(); if (e.dataTransfer.files && e.dataTransfer.files[0]) { setFile(e.dataTransfer.files[0]); setMode('file'); } }; return (