clipwright/dashboard/src/components/MediaInput.jsx

480 lines
26 KiB
JavaScript

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'; }
});
// Channel profile (channels/<slug>.json on the server): tunes how the AI
// writes titles, hooks and the description, and how it art-directs the
// thumbnail. Per job and off by default, so nothing changes for anyone who
// does not use profiles. Fetched from GET /api/channels; when that list is
// empty or unreachable the selector simply does not render.
const [channels, setChannels] = useState([]);
const [channel, setChannel] = useState('none');
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(() => {});
}, []);
// Channel profiles available on this install. A missing route, an empty
// directory or a network error all leave the list empty, which keeps the
// selector hidden — profiles are opt-in and never block a job.
useEffect(() => {
let cancelled = false;
fetch(getApiUrl('/api/channels'))
.then((r) => (r.ok ? r.json() : null))
.then((data) => {
if (cancelled || !data) return;
const list = Array.isArray(data.channels) ? data.channels : [];
setChannels(list.filter((c) => c && c.slug));
})
.catch(() => {});
return () => { cancelled = true; };
}, []);
// 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,
// 'none' = no profile; only a real slug travels, so the default
// request stays byte-identical to the pre-feature one.
channel: channel === 'none' ? null : channel,
};
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 (
<div className="card p-4 sm:p-6 animate-fade">
<div className="flex gap-4 sm:gap-6 mb-6 border-b border-rule" data-tutorial="source-tabs">
<button
onClick={() => setMode('file')}
className={`flex items-center gap-2 pb-3 px-1 -mb-px border-b-2 text-sm lowercase whitespace-nowrap transition-colors ${mode === 'file'
? 'text-ink border-brass'
: 'text-muted border-transparent hover:text-ink2'
}`}
>
<Upload size={16} className={`hidden sm:block ${mode === 'file' ? 'text-brass' : ''}`} />
Upload File
</button>
{youtubeUrlEnabled && (
<button
onClick={() => setMode('url')}
className={`flex items-center gap-2 pb-3 px-1 -mb-px border-b-2 text-sm lowercase whitespace-nowrap transition-colors ${mode === 'url'
? 'text-ink border-brass'
: 'text-muted border-transparent hover:text-ink2'
}`}
>
<Link2 size={16} className={`hidden sm:block ${mode === 'url' ? 'text-brass' : ''}`} />
Video URL
</button>
)}
</div>
<form onSubmit={handleSubmit}>
{mode === 'url' ? (
<div className="space-y-4" data-tutorial="drop-zone">
<div className="relative">
<input
type="url"
value={url}
onChange={(e) => setUrl(e.target.value)}
placeholder="https://... paste a video link"
className="input-field pr-11"
required
/>
<div className="absolute inset-y-0 right-2 flex items-center" ref={infoRef}>
<button
type="button"
onClick={() => setShowInfo((v) => !v)}
aria-label="Supported platforms"
className="p-1.5 text-muted hover:text-brass transition-colors"
>
<Info size={16} />
</button>
{showInfo && (
<div className="absolute right-0 top-full mt-2 w-64 z-20 card p-4 text-left animate-fade">
<p className="eyebrow mb-2">Paste a link from</p>
<div className="flex flex-wrap gap-1.5">
{SUPPORTED_PLATFORMS.map((p) => (
<span key={p} className="text-xs px-2 py-0.5 rounded-full bg-paper3 text-ink2">
{p}
</span>
))}
</div>
<p className="text-xs text-muted mt-2.5 leading-relaxed">
…and 1,000+ more sites. If a link has a public video, we can usually fetch it.
</p>
</div>
)}
</div>
</div>
</div>
) : (
<div
data-tutorial="drop-zone"
className={`border-2 border-dashed rounded-card p-6 sm:p-8 text-center transition-colors ${file ? 'border-brass' : 'border-rule2 hover:border-brass'
}`}
onDragOver={(e) => e.preventDefault()}
onDrop={handleDrop}
>
{file ? (
<div className="flex items-center justify-center gap-3 text-ok min-w-0">
<FileVideo size={18} className="shrink-0" />
<span className="font-medium truncate">{file.name}</span>
<button
type="button"
onClick={() => setFile(null)}
className="p-1 text-muted hover:text-ink hover:bg-paper3 rounded-full transition-colors"
>
<X size={16} />
</button>
</div>
) : null}
{file && fileTooShort ? (
<p className="text-danger text-sm mt-3" role="alert">
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.
</p>
) : null}
{!file && (
<label className="cursor-pointer block">
<input
type="file"
accept="video/*"
onChange={(e) => setFile(e.target.files?.[0] || null)}
className="hidden"
/>
<Upload className="mx-auto mb-3 text-muted" size={18} />
<p className="text-ink2 lowercase">Click to upload or drag and drop</p>
<p className="readout mt-2">MP4, MOV · at least {MIN_SOURCE_SECONDS}s long</p>
<p className="text-muted text-xs mt-1 lowercase">file size is limited only by your server and proxy</p>
</label>
)}
</div>
)}
{/* Output format selector */}
<div className="mt-5" data-tutorial="output-format">
<p className="eyebrow mb-2">Output format</p>
<div className="grid grid-cols-3 gap-2">
{[
{ 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 (
<button
key={f.value}
type="button"
onClick={() => setOutputFormat(f.value)}
className={`py-3 px-2 rounded-input border flex flex-col items-center gap-2 transition-colors
${active ? 'border-[color:var(--color-accent)] text-ink' : 'border-rule2 text-muted hover:border-[color:var(--color-accent)]'}`}
>
{/* Aspect-ratio glyph */}
<span
className="rounded-[3px] border-2 transition-colors"
style={{
width: `${f.w}px`,
height: `${f.h}px`,
borderColor: active ? 'var(--color-accent)' : 'var(--color-rule-2)',
backgroundColor: active ? 'color-mix(in srgb, var(--color-accent) 22%, transparent)' : 'transparent',
}}
/>
<span className="block font-mono text-sm leading-none">{f.label}</span>
<span className="block text-[11px] sm:text-[10px] leading-tight text-center text-muted">{f.hint}</span>
</button>
);
})}
</div>
</div>
{/* Channel profile — only rendered when the install has profiles,
so an empty list or a failed fetch leaves no trace. Tunes the
packaging the AI writes for a job: title, hook, description
and thumbnail. Defaults to no profile. */}
{channels.length > 0 && (
<div className="mt-5" data-tutorial="channel-profile">
<div className="flex flex-wrap items-center justify-between gap-2">
<p className="eyebrow">channel profile</p>
<select
value={channel}
onChange={(e) => setChannel(e.target.value)}
className="input-field !w-auto text-xs py-1.5"
aria-label="channel profile"
>
<option value="none">None (generic)</option>
{channels.map((ch) => (
<option key={ch.slug} value={ch.slug}>{ch.name || ch.slug}</option>
))}
</select>
</div>
<p className="text-[11px] leading-relaxed text-muted mt-1.5">
Tailors titles, hooks, description and thumbnail for that channel.
</p>
</div>
)}
{/* Advanced generation controls — collapsed by default; blank = AI decides */}
<div className="mt-4">
<button
type="button"
onClick={() => setShowAdvanced((v) => !v)}
className="flex items-center gap-1.5 text-xs text-muted hover:text-ink2 lowercase transition-colors"
>
<ChevronDown size={14} className={`transition-transform ${showAdvanced ? 'rotate-180' : ''}`} />
advanced options
{(targetClips || clipMinSeconds || clipMaxSeconds || !autoHook) && (
<span className="text-brass">·</span>
)}
</button>
{showAdvanced && (
/* Stacked on a phone: three number fields side by side leaves
~100px each, which crushes both label and value. */
<div className="mt-3 grid grid-cols-1 sm:grid-cols-3 gap-3 sm:gap-2 animate-fade">
<div>
<p className="eyebrow mb-1.5">clips to aim for</p>
<input
type="number" min="1" max="15" step="1"
value={targetClips}
onChange={(e) => setTargetClips(e.target.value)}
placeholder="auto"
className="input-field"
/>
</div>
<div>
<p className="eyebrow mb-1.5">min length (s)</p>
<input
type="number" min="5" max="175" step="1"
value={clipMinSeconds}
onChange={(e) => setClipMinSeconds(e.target.value)}
placeholder="15"
className="input-field"
/>
</div>
<div>
<p className="eyebrow mb-1.5">max length (s)</p>
<input
type="number" min="10" max="180" step="1"
value={clipMaxSeconds}
onChange={(e) => setClipMaxSeconds(e.target.value)}
placeholder="60"
className="input-field"
/>
</div>
<p className="col-span-1 sm:col-span-3 text-[11px] leading-relaxed text-muted">
Targets, not guarantees: the AI returns fewer clips when the
material doesn't hold them. Leave blank to let it decide.
</p>
<div className="col-span-1 sm:col-span-3 flex flex-wrap items-center justify-between gap-3 pt-3 sm:pt-1 border-t border-rule">
<span className="text-xs text-ink2">vertical layout</span>
<select
value={layout}
onChange={(e) => setLayout(e.target.value)}
className="input-field !w-auto text-xs py-1.5"
aria-label="vertical layout"
>
<option value="auto">Auto (AI picks per video)</option>
<option value="split">Two speakers stacked</option>
<option value="screencast">Screen over presenter</option>
<option value="code">Code / terminal (zoom to text)</option>
<option value="none">Single crop only</option>
</select>
</div>
<div className="col-span-1 sm:col-span-3 flex flex-wrap items-center justify-between gap-3 pt-3 sm:pt-1 border-t border-rule">
<label className="flex items-center gap-2 text-xs text-ink2 cursor-pointer select-none">
<input
type="checkbox"
checked={autoHook}
onChange={(e) => setAutoHook(e.target.checked)}
className="w-4 h-4 shrink-0 accent-[var(--color-accent)] cursor-pointer"
/>
auto hook titles on clips
</label>
{autoHook && (
<select
value={autoHookStyle}
onChange={(e) => setAutoHookStyle(e.target.value)}
className="input-field !w-auto text-xs py-1.5"
>
<option value="pill">Pills</option>
<option value="classic">Classic</option>
<option value="dark">Dark</option>
<option value="yellow">Yellow</option>
<option value="red">Red</option>
<option value="outline">Outline</option>
<option value="outline_yellow">Outline+</option>
</select>
)}
{autoHook && (
<p className="w-full text-[11px] leading-relaxed text-muted">
You can change each clip&apos;s hook text, style, position and size
afterwards with its hook button.
</p>
)}
</div>
</div>
)}
</div>
<label className="flex items-start gap-2.5 mt-5 text-left text-[13px] sm:text-xs leading-relaxed text-muted cursor-pointer select-none">
<input
type="checkbox"
checked={acknowledged}
onChange={(e) => setAcknowledged(e.target.checked)}
className="mt-0.5 w-4 h-4 shrink-0 accent-[var(--color-accent)] cursor-pointer"
/>
<span>
I confirm I own this content or have the rights to process it. I am responsible for any content I submit. See our <a href="/terms" target="_blank" rel="noopener noreferrer" className="text-ink2 underline underline-offset-2 hover:text-brass transition-colors" onClick={(e) => e.stopPropagation()}>Terms</a> and <a href="/privacy" target="_blank" rel="noopener noreferrer" className="text-ink2 underline underline-offset-2 hover:text-brass transition-colors" onClick={(e) => e.stopPropagation()}>Privacy Policy</a>.
</span>
</label>
<button
type="submit"
data-tutorial="generate"
disabled={isProcessing || !acknowledged || (mode === 'url' && !url) || (mode === 'file' && (!file || fileTooShort))}
className="w-full btn-primary mt-4"
>
{isProcessing ? (
<>
<Loader2 size={16} className="animate-spin" />
Processing Video...
</>
) : (
<>
Generate Clips
</>
)}
</button>
</form>
</div>
);
}