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 (
{youtubeUrlEnabled && ( )}
{mode === 'url' ? (
setUrl(e.target.value)} placeholder="https://... paste a video link" className="input-field pr-11" required />
{showInfo && (

Paste a link from

{SUPPORTED_PLATFORMS.map((p) => ( {p} ))}

…and 1,000+ more sites. If a link has a public video, we can usually fetch it.

)}
) : (
e.preventDefault()} onDrop={handleDrop} > {file ? (
{file.name}
) : null} {file && fileTooShort ? (

This video is {Math.round(fileSeconds)}s long. Clip generation needs at least {MIN_SOURCE_SECONDS}s of footage to cut from: it already is a short. Pick a longer video.

) : null} {!file && ( )}
)} {/* Output format selector */}

Output format

{[ { value: 'vertical', label: '9:16', hint: 'Shorts · Reels · TikTok', w: 18, h: 32 }, { value: 'square', label: '1:1', hint: 'Feed posts', w: 28, h: 28 }, { value: 'horizontal', label: '16:9', hint: 'Keep landscape · YouTube', w: 36, h: 20 }, ].map((f) => { const active = outputFormat === f.value; return ( ); })}
{/* Advanced generation controls — collapsed by default; blank = AI decides */}
{showAdvanced && ( /* Stacked on a phone: three number fields side by side leaves ~100px each, which crushes both label and value. */

clips to aim for

setTargetClips(e.target.value)} placeholder="auto" className="input-field" />

min length (s)

setClipMinSeconds(e.target.value)} placeholder="15" className="input-field" />

max length (s)

setClipMaxSeconds(e.target.value)} placeholder="60" className="input-field" />

Targets, not guarantees: the AI returns fewer clips when the material doesn't hold them. Leave blank to let it decide.

vertical layout
{autoHook && ( )} {autoHook && (

You can change each clip's hook text, style, position and size afterwards with its hook button.

)}
)}
); }