Each format button now shows a little rectangle in its real proportion (tall 9:16, square 1:1, wide 16:9) plus what it's for, so the choice reads at a glance instead of needing a hover tooltip.
229 lines
12 KiB
JavaScript
229 lines
12 KiB
JavaScript
import React, { useState, useEffect, useRef } from 'react';
|
|
import { Link2, Upload, FileVideo, X, Info, Loader2 } from 'lucide-react';
|
|
import { getApiUrl } from '../config';
|
|
|
|
const SUPPORTED_PLATFORMS = [
|
|
'YouTube', 'Vimeo', 'TikTok', 'X / Twitter', 'Twitch',
|
|
'Facebook', 'Instagram', 'Dailymotion', 'Reddit', 'Streamable',
|
|
];
|
|
|
|
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 [acknowledged, setAcknowledged] = useState(false);
|
|
const [outputFormat, setOutputFormat] = useState('vertical'); // vertical | horizontal | square
|
|
const [showInfo, setShowInfo] = useState(false);
|
|
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(() => {
|
|
fetch(getApiUrl('/api/config'))
|
|
.then((r) => r.ok ? r.json() : null)
|
|
.then((cfg) => {
|
|
if (cfg && cfg.youtubeUrlEnabled === false) {
|
|
setYoutubeUrlEnabled(false);
|
|
setMode('file');
|
|
}
|
|
})
|
|
.catch(() => {});
|
|
}, []);
|
|
|
|
const handleSubmit = (e) => {
|
|
e.preventDefault();
|
|
if (!acknowledged) return;
|
|
if (mode === 'url' && url) {
|
|
onProcess({ type: 'url', payload: url, acknowledged: true, outputFormat });
|
|
} else if (mode === 'file' && file) {
|
|
onProcess({ type: 'file', payload: file, acknowledged: true, outputFormat });
|
|
}
|
|
};
|
|
|
|
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">
|
|
<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">
|
|
<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
|
|
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>
|
|
) : (
|
|
<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 up to 500MB</p>
|
|
</label>
|
|
)}
|
|
</div>
|
|
)}
|
|
|
|
{/* Output format selector */}
|
|
<div className="mt-5">
|
|
<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-[10px] leading-tight text-center text-muted">{f.hint}</span>
|
|
</button>
|
|
);
|
|
})}
|
|
</div>
|
|
</div>
|
|
|
|
<label className="flex items-start gap-2 mt-5 text-xs text-muted cursor-pointer select-none">
|
|
<input
|
|
type="checkbox"
|
|
checked={acknowledged}
|
|
onChange={(e) => setAcknowledged(e.target.checked)}
|
|
className="mt-0.5 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="/#legal" target="_blank" rel="noopener noreferrer" className="text-ink2 underline underline-offset-2 hover:text-brass transition-colors" onClick={(e) => e.stopPropagation()}>Terms & Privacy</a>.
|
|
</span>
|
|
</label>
|
|
|
|
<button
|
|
type="submit"
|
|
disabled={isProcessing || !acknowledged || (mode === 'url' && !url) || (mode === 'file' && !file)}
|
|
className="w-full btn-primary mt-4"
|
|
>
|
|
{isProcessing ? (
|
|
<>
|
|
<Loader2 size={16} className="animate-spin" />
|
|
Processing Video...
|
|
</>
|
|
) : (
|
|
<>
|
|
Generate Clips
|
|
</>
|
|
)}
|
|
</button>
|
|
</form>
|
|
</div>
|
|
);
|
|
}
|