clipwright/dashboard/src/components/ResultCard.jsx
victorcavero14 b884131639 feat(editor): clip editor frontend — ClipEditor overlay wired into results
- ClipEditor.jsx: per-clip edit overlay driving the /edl + /rerender backend
- App.jsx: editingClip state, handleClipRerendered syncs results, reopened
  project state and per-clip edit state after a recut (burned layers reset)
- ResultCard.jsx: "edit clip" entry button (Scissors), layout tweak
- recut.py: write absolute paths in the concat list — the demuxer resolves
  relative entries against the list file's dir, not the process cwd

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-05 12:03:15 +02:00

1001 lines
46 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import React, { useState, useEffect } from 'react';
import { Download, Share2, Instagram, Youtube, Video, AlertCircle, Loader2, Copy, Check, Wand2, Type, Calendar, Languages, FileText, Link2, Scissors } from 'lucide-react';
import { getApiUrl } from '../config';
import { apiFetch } from '../lib/api';
import SubtitleModal from './SubtitleModal';
import HookModal from './HookModal';
import TranslateModal from './TranslateModal';
import Modal from './ui/Modal';
import SegmentedControl from './ui/SegmentedControl';
import WatermarkModal, { watermarkNoticeDismissed } from './WatermarkModal';
import { useAuth } from '../contexts/AuthContext';
import { renderInBrowser } from '../lib/renderInBrowser';
const QUIET_BTN = 'group flex flex-col items-center justify-center gap-1 py-2 px-1 rounded-input border border-rule hover:bg-paper3 text-[11px] lowercase text-ink2 whitespace-nowrap transition-colors disabled:opacity-45 disabled:cursor-not-allowed';
const PLATFORM_OPTIONS = [
{ value: 'tiktok', label: 'tiktok', icon: <Video size={16} /> },
{ value: 'instagram', label: 'instagram', icon: <Instagram size={16} /> },
{ value: 'youtube', label: 'youtube', icon: <Youtube size={16} /> },
];
function formatDuration(clip) {
const secs = clip.end && clip.start ? Math.floor(clip.end - clip.start) : NaN;
if (!Number.isFinite(secs) || secs < 0) return null;
return `${String(Math.floor(secs / 60)).padStart(2, '0')}:${String(secs % 60).padStart(2, '0')}`;
}
export default function ResultCard({ clip, index, jobId, durableUrl, uploadPostKey, uploadUserId, geminiApiKey, elevenLabsKey, isManaged, onPlay, onPause, onBulkSubtitle, clipCount = 1, bulkProgress, initialState = null, onStateChange, connectedPlatforms = null, onConnectSocials, onEditClip = null }) {
const [showModal, setShowModal] = useState(false);
const [showDescModal, setShowDescModal] = useState(false);
const [showSubtitleModal, setShowSubtitleModal] = useState(false);
const [showWatermarkModal, setShowWatermarkModal] = useState(false);
const { plan } = useAuth();
const videoRef = React.useRef(null);
// Pristine base clip (no burned subtitles/hook), stable regardless of how
// clip.video_url mutates after server edits. Used as the compositing base
// for the Remotion preview so it never stacks subtitles over an already-
// subtitled file (double-subtitle bug).
const stripBurns = (filename) => {
let f = filename || '', prev;
do { prev = f; f = f.replace(/^subtitled_\d+_/, '').replace(/^hook_/, ''); } while (f !== prev);
return f;
};
const originalVideoUrl = getApiUrl((clip.video_url || '').replace(/[^/]+$/, stripBurns((clip.video_url || '').split('/').pop())));
const [currentVideoUrl, setCurrentVideoUrl] = useState(getApiUrl(clip.video_url));
const downloadClip = async () => {
try {
const response = await fetch(currentVideoUrl);
if (!response.ok) throw new Error('Download failed');
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.style.display = 'none';
a.href = url;
a.download = `clip-${index + 1}.mp4`;
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
} catch (err) {
console.error('Download error:', err);
window.open(currentVideoUrl, '_blank');
}
};
// Latest file that exists ON THE SERVER (blob: previews don't count).
// All server-side operations must chain from this, so burned-in edits
// (subtitles, hooks, effects) never get silently dropped.
// A reopened project seeds it from the persisted project state.
const [serverVideoFile, setServerVideoFile] = useState(initialState?.server_file || (clip.video_url || '').split('/').pop());
const [videoErrored, setVideoErrored] = useState(false);
const [resolution, setResolution] = useState(null);
// If the local video failed and a durable R2 URL is (now) available, use it.
// Handles the race where the video errors before the durable URL has loaded.
useEffect(() => {
if (videoErrored && durableUrl && currentVideoUrl !== durableUrl) {
setCurrentVideoUrl(durableUrl);
setVideoErrored(false);
}
}, [videoErrored, durableUrl, currentVideoUrl]);
// When an external refresh changes this clip's server file (e.g. bulk
// subtitles applied from another card), adopt it so the card shows the
// freshly subtitled video instead of a stale one.
useEffect(() => {
const serverUrl = getApiUrl(clip.video_url);
const serverName = (clip.video_url || '').split('/').pop();
if (serverName && serverName !== serverVideoFile) {
setServerVideoFile(serverName);
setCurrentVideoUrl(serverUrl);
if (videoRef.current) videoRef.current.load();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [clip.video_url]);
const [platforms, setPlatforms] = useState({
tiktok: true,
instagram: true,
youtube: true
});
const [postTitle, setPostTitle] = useState("");
const [postDescription, setPostDescription] = useState("");
const [isScheduling, setIsScheduling] = useState(false);
const [scheduleDate, setScheduleDate] = useState("");
const [posting, setPosting] = useState(false);
const [postResult, setPostResult] = useState(null);
const [copied, setCopied] = useState(null);
const handleCopy = async (field, text) => {
try {
await navigator.clipboard.writeText(text || '');
setCopied(field);
setTimeout(() => setCopied(null), 2000);
} catch {
// clipboard unavailable — silent
}
};
const [isEditing, setIsEditing] = useState(false);
const [isSubtitling, setIsSubtitling] = useState(false);
const [isHooking, setIsHooking] = useState(false);
const [isTranslating, setIsTranslating] = useState(false);
const [showHookModal, setShowHookModal] = useState(false);
const [showTranslateModal, setShowTranslateModal] = useState(false);
const [editError, setEditError] = useState(null);
const [clipDuration, setClipDuration] = useState(clip.end && clip.start ? clip.end - clip.start : 30);
// Accumulate Remotion layers across operations. A reopened project restores
// the layers persisted in its project state, so the next edit composes over
// them instead of silently dropping previous browser-side work.
const [activeLayers, setActiveLayers] = useState(initialState?.active_layers || { subtitles: null, hook: null, effects: null });
// Report edit state upward (debounced sync to the project record). Skip the
// mount run: only user-driven changes are worth persisting.
const stateReported = React.useRef(false);
useEffect(() => {
if (!stateReported.current) { stateReported.current = true; return; }
onStateChange?.(index, { activeLayers, serverVideoFile });
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [activeLayers, serverVideoFile]);
// True when the current server file already carries burned-in content.
// Browser (Remotion) renders compose over the ORIGINAL clip, so using them
// here would silently drop those burns — chain via server FFmpeg instead.
const hasServerBurns = /(^|_)(subtitled|hook)_/.test(serverVideoFile || '');
// Fetch clip duration from transcript endpoint
useEffect(() => {
if (!jobId || index === undefined) return;
apiFetch(`/api/clip/${jobId}/${index}/transcript`)
.then(res => res.ok ? res.json() : null)
.then(data => {
if (data && data.durationSec) setClipDuration(data.durationSec);
})
.catch(() => {});
}, [jobId, index]);
// Which platforms the selected profile actually has linked. `null` means
// unknown (profile list not loaded) — in that case nothing is gated.
const knownConnections = Array.isArray(connectedPlatforms);
const noAccountsConnected = knownConnections && connectedPlatforms.length === 0;
const platformOptions = knownConnections
? PLATFORM_OPTIONS.map((o) => (connectedPlatforms.includes(o.value) ? o : { ...o, disabled: true, hint: 'not connected' }))
: PLATFORM_OPTIONS;
const handleConnectAccounts = () => {
setShowModal(false);
if (onConnectSocials) onConnectSocials();
else window.open('https://app.upload-post.com', '_blank', 'noopener');
};
// Initialize/Reset form when modal opens
useEffect(() => {
if (showModal) {
setPostTitle(clip.video_title_for_youtube_short || "Viral Short");
setPostDescription(clip.video_description_for_instagram || clip.video_description_for_tiktok || "");
setIsScheduling(false);
setScheduleDate("");
setPostResult(null);
// Only preselect platforms the profile can actually publish to.
if (knownConnections) {
setPlatforms({
tiktok: connectedPlatforms.includes('tiktok'),
instagram: connectedPlatforms.includes('instagram'),
youtube: connectedPlatforms.includes('youtube'),
});
}
}
}, [showModal, clip]);
const handleAutoEdit = async () => {
setIsEditing(true);
setEditError(null);
try {
const apiKey = geminiApiKey || localStorage.getItem('gemini_key');
// Managed (paid) users get the Gemini key resolved server-side;
// only BYOK/self-host needs a local key.
if (!apiKey && !isManaged) {
throw new Error("Gemini API Key is missing. Please set it in Settings.");
}
const geminiHeaders = apiKey ? { 'X-Gemini-Key': apiKey } : {};
// Try Remotion effects endpoint first
const effectsRes = hasServerBurns ? null : await apiFetch('/api/effects/generate', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...geminiHeaders
},
body: JSON.stringify({
job_id: jobId,
clip_index: index,
input_filename: serverVideoFile
})
});
if (effectsRes && effectsRes.ok) {
const data = await effectsRes.json();
if (data.effects && data.effects.segments) {
const newLayers = { ...activeLayers, effects: data.effects };
setActiveLayers(newLayers);
const blobUrl = await renderInBrowser({
videoUrl: originalVideoUrl,
durationInSeconds: clipDuration,
subtitles: newLayers.subtitles,
hook: newLayers.hook,
effects: newLayers.effects,
});
setCurrentVideoUrl(blobUrl);
if (videoRef.current) videoRef.current.load();
return;
}
}
// Fallback: legacy FFmpeg edit endpoint
const res = await apiFetch('/api/edit', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...geminiHeaders
},
body: JSON.stringify({
job_id: jobId,
clip_index: index,
input_filename: serverVideoFile
})
});
if (!res.ok) {
const errText = await res.text();
try {
const jsonErr = JSON.parse(errText);
throw new Error(jsonErr.detail || errText);
} catch (e) {
throw new Error(errText);
}
}
const data = await res.json();
if (data.new_video_url) {
setCurrentVideoUrl(getApiUrl(data.new_video_url));
setServerVideoFile(data.new_video_url.split('/').pop());
if (videoRef.current) {
videoRef.current.load();
}
}
} catch (e) {
setEditError(e.message);
setTimeout(() => setEditError(null), 5000);
} finally {
setIsEditing(false);
}
};
// Clips are captioned by default, so "no captions" has to be reachable.
// Nothing is re-encoded: the server still holds the clean file next to the
// captioned one and just points this clip back at it.
const handleRemoveSubtitles = async () => {
setIsSubtitling(true);
setEditError(null);
try {
const res = await apiFetch('/api/subtitle/remove', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
job_id: jobId, clip_index: index, input_filename: serverVideoFile,
}),
});
if (!res.ok) throw new Error(await res.text());
const data = await res.json();
if (data.new_video_url) {
const serverUrl = getApiUrl(data.new_video_url);
setServerVideoFile(data.new_video_url.split('/').pop());
const remaining = { ...activeLayers, subtitles: null };
setActiveLayers(remaining);
if (remaining.hook || remaining.effects) {
setCurrentVideoUrl(await renderInBrowser({
videoUrl: serverUrl,
durationInSeconds: clipDuration,
subtitles: null,
hook: remaining.hook,
effects: remaining.effects,
}));
} else {
setCurrentVideoUrl(serverUrl);
}
if (videoRef.current) videoRef.current.load();
setShowSubtitleModal(false);
}
} catch (e) {
setEditError(e.message);
setTimeout(() => setEditError(null), 5000);
} finally {
setIsSubtitling(false);
}
};
const handleSubtitle = async (options) => {
setIsSubtitling(true);
setEditError(null);
try {
// Karaoke styles are burned server-side (ASS word-highlight render);
// the in-browser Remotion path only handles classic styles, and only
// when the server file has no burned-in content to preserve.
if (options.remotion && options.style !== 'karaoke' && !hasServerBurns) {
// Accumulate layer and render all layers together
const newLayers = { ...activeLayers, subtitles: options.remotion };
setActiveLayers(newLayers);
const blobUrl = await renderInBrowser({
videoUrl: originalVideoUrl,
durationInSeconds: clipDuration,
subtitles: newLayers.subtitles,
hook: newLayers.hook,
effects: newLayers.effects,
});
setCurrentVideoUrl(blobUrl);
if (videoRef.current) videoRef.current.load();
setShowSubtitleModal(false);
return;
}
// Fallback: legacy FFmpeg
const res = await apiFetch('/api/subtitle', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
job_id: jobId,
clip_index: index,
position: options.position,
font_size: options.fontSize,
font_name: options.fontName,
font_color: options.fontColor,
border_color: options.borderColor,
border_width: options.borderWidth,
bg_color: options.bgColor,
bg_opacity: options.bgOpacity,
style: options.style || 'classic',
highlight_color: options.highlightColor || '#FFD700',
effect: options.effect || 'none',
base_opacity: options.baseOpacity ?? 1.0,
uppercase: options.uppercase || false,
input_filename: serverVideoFile
})
});
if (!res.ok) throw new Error(await res.text());
const data = await res.json();
if (data.new_video_url) {
const serverUrl = getApiUrl(data.new_video_url);
setServerVideoFile(data.new_video_url.split('/').pop());
// Subtitles are burned into the server file now — drop the
// browser subtitle layer and re-compose any remaining browser
// layers (hook/effects) over the new file so they aren't lost.
const remaining = { ...activeLayers, subtitles: null };
setActiveLayers(remaining);
if (remaining.hook || remaining.effects) {
const blobUrl = await renderInBrowser({
videoUrl: serverUrl,
durationInSeconds: clipDuration,
subtitles: null,
hook: remaining.hook,
effects: remaining.effects,
});
setCurrentVideoUrl(blobUrl);
} else {
setCurrentVideoUrl(serverUrl);
}
if (videoRef.current) videoRef.current.load();
setShowSubtitleModal(false);
}
} catch (e) {
setEditError(e.message);
setTimeout(() => setEditError(null), 5000);
} finally {
setIsSubtitling(false);
}
};
const handleHook = async (hookData) => {
setIsHooking(true);
setEditError(null);
try {
if (hookData.remotion && !hasServerBurns) {
// Accumulate layer and render all layers together
const newLayers = { ...activeLayers, hook: hookData.remotion };
setActiveLayers(newLayers);
const blobUrl = await renderInBrowser({
videoUrl: originalVideoUrl,
durationInSeconds: clipDuration,
subtitles: newLayers.subtitles,
hook: newLayers.hook,
effects: newLayers.effects,
});
setCurrentVideoUrl(blobUrl);
if (videoRef.current) videoRef.current.load();
setShowHookModal(false);
return;
}
// Fallback: legacy FFmpeg
const payload = typeof hookData === 'string'
? { text: hookData, position: 'top', size: 'M' }
: hookData;
const res = await apiFetch('/api/hook', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
job_id: jobId,
clip_index: index,
text: payload.text,
position: payload.position,
size: payload.size,
style: payload.style || 'classic',
duration_seconds: payload.remotion?.displayDurationSec ?? null,
input_filename: serverVideoFile
})
});
if (!res.ok) throw new Error(await res.text());
const data = await res.json();
if (data.new_video_url) {
setCurrentVideoUrl(getApiUrl(data.new_video_url));
setServerVideoFile(data.new_video_url.split('/').pop());
if (videoRef.current) videoRef.current.load();
setShowHookModal(false);
}
} catch (e) {
setEditError(e.message);
setTimeout(() => setEditError(null), 5000);
} finally {
setIsHooking(false);
}
};
const handleTranslate = async (options) => {
console.log('[Translate] Starting translation with options:', options);
setIsTranslating(true);
setEditError(null);
try {
const apiKey = elevenLabsKey;
console.log('[Translate] API Key available:', !!apiKey);
if (!apiKey) {
throw new Error("ElevenLabs API Key is missing. Please set it in Settings.");
}
const requestBody = {
job_id: jobId,
clip_index: index,
target_language: options.targetLanguage,
input_filename: serverVideoFile
};
console.log('[Translate] Request body:', requestBody);
console.log('[Translate] Sending request to /api/translate');
const res = await apiFetch('/api/translate', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-ElevenLabs-Key': apiKey
},
body: JSON.stringify(requestBody)
});
console.log('[Translate] Response status:', res.status);
if (!res.ok) {
const errText = await res.text();
console.error('[Translate] Error response:', errText);
try {
const jsonErr = JSON.parse(errText);
throw new Error(jsonErr.detail || errText);
} catch (e) {
if (e.message !== errText) throw e;
throw new Error(errText);
}
}
const data = await res.json();
console.log('[Translate] Success response:', data);
if (data.new_video_url) {
setCurrentVideoUrl(getApiUrl(data.new_video_url));
setServerVideoFile(data.new_video_url.split('/').pop());
if (videoRef.current) {
videoRef.current.load();
}
setShowTranslateModal(false);
}
} catch (e) {
console.error('[Translate] Exception:', e);
setEditError(e.message);
setTimeout(() => setEditError(null), 5000);
} finally {
setIsTranslating(false);
}
};
// Managed (cloud plan/trial) users post with the server-side key — no BYOK needed
const canPost = isManaged || (uploadPostKey && uploadUserId);
const handlePost = async () => {
if (!canPost) {
setPostResult({ success: false, msg: "Missing API Key or User ID." });
return;
}
if (noAccountsConnected) {
setPostResult({ success: false, msg: "Connect a social account first." });
return;
}
const selectedPlatforms = Object.keys(platforms).filter(k => platforms[k]);
if (selectedPlatforms.length === 0) {
setPostResult({ success: false, msg: "Select at least one platform." });
return;
}
if (isScheduling && !scheduleDate) {
setPostResult({ success: false, msg: "Please select a date and time." });
return;
}
setPosting(true);
setPostResult(null);
try {
const payload = {
job_id: jobId,
clip_index: index,
api_key: uploadPostKey,
user_id: uploadUserId,
platforms: selectedPlatforms,
title: postTitle,
description: postDescription
};
if (isScheduling && scheduleDate) {
// Convert to ISO-8601
payload.scheduled_date = new Date(scheduleDate).toISOString();
// Optional: pass timezone if needed, backend defaults to UTC or we can send user's timezone
payload.timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
}
const res = await apiFetch('/api/social/post', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
});
if (!res.ok) {
const errText = await res.text();
try {
const jsonErr = JSON.parse(errText);
throw new Error(jsonErr.detail || errText);
} catch (e) {
throw new Error(errText);
}
}
setPostResult({ success: true, msg: isScheduling ? "Scheduled successfully!" : "Posted successfully!" });
setTimeout(() => {
setShowModal(false);
setPostResult(null);
}, 3000);
} catch (e) {
setPostResult({ success: false, msg: `Failed: ${e.message}` });
} finally {
setPosting(false);
}
};
const durationReadout = formatDuration(clip);
return (
<div className="card overflow-hidden flex flex-col md:flex-row group hover:border-rule2 transition-colors animate-fade md:min-h-[420px]" style={{ animationDelay: `${index * 0.1}s` }}>
{/* Left: Video Preview — 9:16 column matching the fixed card height */}
<div className="w-full md:w-[236px] bg-black relative shrink-0 aspect-[9/16] md:aspect-auto group/video">
<video
ref={videoRef}
src={currentVideoUrl}
controls
className="w-full h-full object-contain"
playsInline
onLoadedMetadata={(e) => {
if (e.target.videoWidth) setResolution(`${e.target.videoWidth}×${e.target.videoHeight}`);
}}
onError={() => {
// Local /videos/ file gone (e.g. cleaned up after a reload) →
// fall back to the durable R2 copy for managed users. If the
// durable URL hasn't loaded yet, the effect above retries.
if (durableUrl && currentVideoUrl !== durableUrl) setCurrentVideoUrl(durableUrl);
else setVideoErrored(true);
}}
onPlay={() => {
const currentTime = videoRef.current ? videoRef.current.currentTime : 0;
onPlay && onPlay(clip.start + currentTime);
}}
onPause={() => onPause && onPause()}
onEnded={() => {
if (videoRef.current) {
videoRef.current.currentTime = 0;
videoRef.current.play();
}
}}
/>
<div className="absolute top-3 left-3 flex gap-2">
<span className="bg-black/70 text-ink font-mono text-micro uppercase px-2 py-1 rounded-full">
Clip {index + 1}
</span>
</div>
{/* Auto Edit Overlay if Processing */}
{isEditing && (
<div className="absolute inset-0 bg-black/70 flex flex-col items-center justify-center z-10 p-4 text-center">
<Loader2 size={28} className="text-brass animate-spin mb-3" />
<span className="text-xs text-ink lowercase">ai magic in progress…</span>
<span className="readout mt-1.5">APPLYING VIRAL EDITS · ZOOMS</span>
</div>
)}
</div>
{/* Right: Content & Details */}
<div className="flex-1 p-4 md:p-5 flex flex-col overflow-hidden min-w-0">
<div className="mb-4">
<h3 className="text-base font-medium text-ink leading-tight line-clamp-2 mb-2 break-words" title={clip.video_title_for_youtube_short}>
{clip.video_title_for_youtube_short || "Viral Clip Generated"}
</h3>
<div className="flex flex-wrap gap-1.5">
{durationReadout && <span className="readout bg-paper3 px-2 py-0.5 rounded-full shrink-0">{durationReadout}</span>}
{resolution && <span className="readout bg-paper3 px-2 py-0.5 rounded-full shrink-0">{resolution}</span>}
<span className="readout bg-paper3 px-2 py-0.5 rounded-full shrink-0">#shorts</span>
<span className="readout bg-paper3 px-2 py-0.5 rounded-full shrink-0">#viral</span>
</div>
</div>
{/* Descriptions (compact) — full text lives in the modal */}
<div className="flex-1 min-h-0 space-y-2 mb-4">
<div className="bg-paper rounded-input px-3 py-2 border border-rule flex items-center gap-2 min-w-0">
<span className="eyebrow shrink-0">YOUTUBE</span>
<p className="text-xs text-ink2 truncate flex-1 min-w-0">
{clip.video_title_for_youtube_short || "Viral Short Video"}
</p>
<button
onClick={() => handleCopy('youtube', clip.video_title_for_youtube_short || "Viral Short Video")}
aria-label="copy youtube title"
className="p-1 rounded-full text-muted hover:text-brass transition-colors shrink-0"
>
{copied === 'youtube' ? <Check size={14} className="text-ok" /> : <Copy size={14} />}
</button>
</div>
<div className="bg-paper rounded-input px-3 py-2 border border-rule flex items-center gap-2 min-w-0">
<span className="eyebrow shrink-0">TIKTOK · IG</span>
<p className="text-xs text-ink2 truncate flex-1 min-w-0">
{clip.video_description_for_tiktok || clip.video_description_for_instagram}
</p>
<button
onClick={() => handleCopy('caption', clip.video_description_for_tiktok || clip.video_description_for_instagram)}
aria-label="copy caption"
className="p-1 rounded-full text-muted hover:text-brass transition-colors shrink-0"
>
{copied === 'caption' ? <Check size={14} className="text-ok" /> : <Copy size={14} />}
</button>
</div>
<button
onClick={() => setShowDescModal(true)}
className="w-full flex items-center justify-center gap-2 py-2 rounded-input border border-dashed border-rule text-xs lowercase text-muted hover:text-brass hover:border-rule2 transition-colors"
>
<FileText size={14} /> view descriptions
</button>
</div>
{/* Error Message */}
{editError && (
<div className="mb-3 px-3 py-2 rounded-input text-xs text-danger bg-[color-mix(in_oklab,var(--color-danger)_10%,transparent)] flex items-center gap-2">
<AlertCircle size={14} className="shrink-0" />
{editError}
</div>
)}
{/* Actions Footer */}
<div className="grid grid-cols-2 gap-2 mt-auto pt-4 border-t border-rule">
{onEditClip && (
<button
onClick={() => onEditClip(index)}
className={QUIET_BTN}
>
<Scissors size={16} className="text-muted group-hover:text-brass transition-colors shrink-0" />
edit clip
</button>
)}
<button
onClick={handleAutoEdit}
disabled={isEditing}
className={QUIET_BTN}
>
{isEditing ? <Loader2 size={16} className="animate-spin text-brass shrink-0" /> : <Wand2 size={16} className="text-muted group-hover:text-brass transition-colors shrink-0" />}
{isEditing ? 'editing…' : 'auto edit'}
</button>
<button
onClick={() => setShowSubtitleModal(true)}
disabled={isSubtitling}
className={QUIET_BTN}
>
{isSubtitling ? <Loader2 size={16} className="animate-spin text-brass shrink-0" /> : <Type size={16} className="text-muted group-hover:text-brass transition-colors shrink-0" />}
{isSubtitling ? 'adding…' : 'subtitles'}
</button>
<button
onClick={() => setShowHookModal(true)}
disabled={isHooking}
className={QUIET_BTN}
>
{isHooking ? <Loader2 size={16} className="animate-spin text-brass shrink-0" /> : <Wand2 size={16} className="text-muted group-hover:text-brass transition-colors shrink-0" />}
{isHooking ? 'adding…' : 'viral hook'}
</button>
<button
onClick={() => setShowTranslateModal(true)}
disabled={isTranslating}
className={QUIET_BTN}
>
{isTranslating ? <Loader2 size={16} className="animate-spin text-brass shrink-0" /> : <Languages size={16} className="text-muted group-hover:text-brass transition-colors shrink-0" />}
{isTranslating ? 'translating…' : 'dub voice'}
</button>
<button
onClick={() => setShowModal(true)}
className="btn-primary flex-col gap-1 py-2 px-1 text-[11px] rounded-input whitespace-nowrap"
>
<Share2 size={16} className="shrink-0" /> post
</button>
<button
onClick={(e) => {
e.preventDefault();
// Free clips are watermarked — surface the upsell once
// before the first download, then get out of the way.
if (plan === 'free' && !watermarkNoticeDismissed()) {
setShowWatermarkModal(true);
return;
}
downloadClip();
}}
className={`${QUIET_BTN}${onEditClip ? ' col-span-2' : ''}`}
>
<Download size={16} className="text-muted group-hover:text-brass transition-colors shrink-0" /> download
</button>
</div>
</div>
{/* Descriptions Modal */}
<Modal
isOpen={showDescModal}
onClose={() => setShowDescModal(false)}
eyebrow="GENERATED COPY"
title="descriptions"
size="md"
>
<div className="space-y-4">
<div>
<div className="flex items-center justify-between gap-2 mb-1.5">
<label className="eyebrow">YOUTUBE TITLE</label>
<button
onClick={() => handleCopy('youtube', clip.video_title_for_youtube_short || "Viral Short Video")}
aria-label="copy youtube title"
className="p-1 rounded-full text-muted hover:text-brass transition-colors shrink-0"
>
{copied === 'youtube' ? <Check size={14} className="text-ok" /> : <Copy size={14} />}
</button>
</div>
<p className="text-sm text-ink2 select-all break-words bg-paper rounded-input p-3 border border-rule">
{clip.video_title_for_youtube_short || "Viral Short Video"}
</p>
</div>
<div>
<div className="flex items-center justify-between gap-2 mb-1.5">
<label className="eyebrow">TIKTOK · IG CAPTION</label>
<button
onClick={() => handleCopy('caption', clip.video_description_for_tiktok || clip.video_description_for_instagram)}
aria-label="copy caption"
className="p-1 rounded-full text-muted hover:text-brass transition-colors shrink-0"
>
{copied === 'caption' ? <Check size={14} className="text-ok" /> : <Copy size={14} />}
</button>
</div>
<p className="text-sm text-ink2 select-all break-words bg-paper rounded-input p-3 border border-rule whitespace-pre-wrap">
{clip.video_description_for_tiktok || clip.video_description_for_instagram}
</p>
</div>
</div>
</Modal>
{/* Post Modal */}
<Modal
isOpen={showModal}
onClose={() => setShowModal(false)}
eyebrow="PUBLISH"
title="post clip"
size="md"
footer={
noAccountsConnected ? (
<button onClick={handleConnectAccounts} className="btn-primary w-full">
<Link2 size={16} /> connect accounts
</button>
) : (
<button
onClick={handlePost}
disabled={posting || !canPost}
className="btn-primary w-full"
>
{posting ? <><Loader2 size={16} className="animate-spin" /> {isScheduling ? 'scheduling…' : 'publishing…'}</> : <><Share2 size={16} /> {isScheduling ? 'schedule post' : 'publish now'}</>}
</button>
)
}
>
{!canPost && (
<div className="mb-4 px-3 py-2 rounded-input text-xs text-warn bg-[color-mix(in_oklab,var(--color-warn)_10%,transparent)] flex items-start gap-2">
<AlertCircle size={14} className="mt-0.5 shrink-0" />
<div className="lowercase">configure api key in settings first.</div>
</div>
)}
{noAccountsConnected && (
<div className="mb-4 px-3 py-2 rounded-input text-xs text-warn bg-[color-mix(in_oklab,var(--color-warn)_10%,transparent)] flex items-start gap-2">
<AlertCircle size={14} className="mt-0.5 shrink-0" />
<div className="lowercase">no social accounts connected yet — link tiktok, instagram or youtube to publish this clip.</div>
</div>
)}
{/* TikTok is sent as a draft, so say so before they press publish:
someone expecting a live post and finding nothing on their
profile will read it as a failure. Lead with the upside —
posting from inside the app is what the algorithm rewards. */}
{platforms.tiktok && (
<div className="mb-4 px-3 py-2 rounded-input text-xs text-ink2 bg-paper3 flex items-start gap-2">
<AlertCircle size={14} className="mt-0.5 shrink-0 text-brass" />
<div className="lowercase">
tiktok arrives as a <b className="text-ink">draft</b>, not a live post — you'll
get a notification in the app. finishing it there lets you add trending
sounds, effects and hashtags, which reaches more people than posting
straight from an api.
</div>
</div>
)}
<div className="space-y-4">
{/* Title & Description */}
<div>
<label className="eyebrow block mb-1.5">TITLE</label>
<input
type="text"
value={postTitle}
onChange={(e) => setPostTitle(e.target.value)}
className="input-field"
placeholder="enter a catchy title…"
/>
</div>
<div>
<label className="eyebrow block mb-1.5">CAPTION</label>
<textarea
value={postDescription}
onChange={(e) => setPostDescription(e.target.value)}
rows={4}
className="input-field resize-none"
placeholder="write a caption for your post…"
/>
</div>
{/* Scheduling */}
<div className="p-3 bg-paper rounded-input border border-rule">
<label className="flex items-center justify-between cursor-pointer">
<span className="flex items-center gap-2 text-sm text-ink2 lowercase">
<Calendar size={16} className={isScheduling ? 'text-brass' : 'text-muted'} /> schedule post
</span>
<input
type="checkbox"
checked={isScheduling}
onChange={(e) => setIsScheduling(e.target.checked)}
className="w-4 h-4 accent-brass cursor-pointer"
/>
</label>
{isScheduling && (
<div className="mt-3 animate-fade">
<label className="eyebrow block mb-1.5">DATE · TIME</label>
<input
type="datetime-local"
value={scheduleDate}
onChange={(e) => setScheduleDate(e.target.value)}
className="input-field [color-scheme:dark]"
/>
</div>
)}
</div>
{/* Platforms */}
<div>
<label className="eyebrow block mb-2">PLATFORMS</label>
<SegmentedControl
multi
columns={3}
options={platformOptions}
value={Object.keys(platforms).filter(k => platforms[k])}
onChange={(arr) => setPlatforms({
tiktok: arr.includes('tiktok'),
instagram: arr.includes('instagram'),
youtube: arr.includes('youtube'),
})}
/>
</div>
{postResult && (
<div className={postResult.success ? 'badge-ok' : 'badge-danger'}>
{postResult.success ? <Check size={12} className="shrink-0" /> : <AlertCircle size={12} className="shrink-0" />}
{postResult.msg}
</div>
)}
</div>
</Modal>
<SubtitleModal
isOpen={showSubtitleModal}
onClose={() => setShowSubtitleModal(false)}
onGenerate={handleSubtitle}
onApplyAll={onBulkSubtitle ? async (options) => {
await onBulkSubtitle(options);
setShowSubtitleModal(false);
} : undefined}
onRemove={handleRemoveSubtitles}
bulkCount={clipCount}
bulkProgress={bulkProgress}
isProcessing={isSubtitling || (bulkProgress?.running ?? false)}
videoUrl={originalVideoUrl}
jobId={jobId}
clipIndex={index}
existingHook={activeLayers.hook}
/>
<HookModal
isOpen={showHookModal}
onClose={() => setShowHookModal(false)}
onGenerate={handleHook}
isProcessing={isHooking}
videoUrl={originalVideoUrl}
initialText={clip.viral_hook_text}
durationInSeconds={clip.end && clip.start ? clip.end - clip.start : 30}
existingSubtitles={activeLayers.subtitles}
/>
<TranslateModal
isOpen={showTranslateModal}
onClose={() => setShowTranslateModal(false)}
onTranslate={handleTranslate}
isProcessing={isTranslating}
videoUrl={currentVideoUrl}
hasApiKey={!!elevenLabsKey}
/>
{showWatermarkModal && (
<WatermarkModal
onClose={() => setShowWatermarkModal(false)}
onContinue={downloadClip}
/>
)}
</div>
);
}