import React, { useState } from 'react'; import { Download, Share2, Instagram, Youtube, Video, CheckCircle, AlertCircle, X, Loader2, Copy, Wand2, Type } from 'lucide-react'; import { getApiUrl } from '../config'; import SubtitleModal from './SubtitleModal'; export default function ResultCard({ clip, index, jobId, uploadPostKey, uploadUserId, geminiApiKey, onPlay, onPause }) { const [showModal, setShowModal] = useState(false); const [showSubtitleModal, setShowSubtitleModal] = useState(false); const videoRef = React.useRef(null); const [currentVideoUrl, setCurrentVideoUrl] = useState(getApiUrl(clip.video_url)); const [platforms, setPlatforms] = useState({ tiktok: true, instagram: true, youtube: true }); const [posting, setPosting] = useState(false); const [postResult, setPostResult] = useState(null); const [isEditing, setIsEditing] = useState(false); const [isSubtitling, setIsSubtitling] = useState(false); const [editError, setEditError] = useState(null); const handleAutoEdit = async () => { setIsEditing(true); setEditError(null); try { // Use passed prop or fallback const apiKey = geminiApiKey || localStorage.getItem('gemini_key'); if (!apiKey) { throw new Error("Gemini API Key is missing. Please set it in Settings."); } const res = await fetch(getApiUrl('/api/edit'), { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-Gemini-Key': apiKey }, body: JSON.stringify({ job_id: jobId, clip_index: index, // We send it in header now, but backend might expect it in body too currently. // We will update backend to be flexible. }) }); 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)); // Reload video if (videoRef.current) { videoRef.current.load(); } } } catch (e) { setEditError(e.message); setTimeout(() => setEditError(null), 5000); } finally { setIsEditing(false); } }; const handleSubtitle = async (options) => { setIsSubtitling(true); setEditError(null); try { const res = await fetch(getApiUrl('/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 }) }); if (!res.ok) { const errText = await res.text(); throw new Error(errText); } const data = await res.json(); if (data.new_video_url) { setCurrentVideoUrl(getApiUrl(data.new_video_url)); if (videoRef.current) { videoRef.current.load(); } setShowSubtitleModal(false); } } catch (e) { setEditError(e.message); setTimeout(() => setEditError(null), 5000); } finally { setIsSubtitling(false); } }; const handlePost = async () => { if (!uploadPostKey || !uploadUserId) { setPostResult({ success: false, msg: "Missing API Key or User ID." }); return; } const selectedPlatforms = Object.keys(platforms).filter(k => platforms[k]); if (selectedPlatforms.length === 0) { setPostResult({ success: false, msg: "Select at least one platform." }); return; } setPosting(true); setPostResult(null); try { const res = await fetch(getApiUrl('/api/social/post'), { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ job_id: jobId, clip_index: index, api_key: uploadPostKey, user_id: uploadUserId, platforms: selectedPlatforms }) }); 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: "Posted successfully!" }); setTimeout(() => { setShowModal(false); setPostResult(null); }, 3000); } catch (e) { setPostResult({ success: false, msg: `Failed: ${e.message}` }); } finally { setPosting(false); } }; return (
{/* Left: Video Preview (Responsive Width) */}
{/* Right: Content & Details */}

{clip.video_title_for_youtube_short || "Viral Clip Generated"}

{Math.floor(clip.end - clip.start)}s #shorts #viral
{/* Scrollable Descriptions Area */}
{/* YouTube */}
YouTube Title

{clip.video_title_for_youtube_short || "Viral Short Video"}

{/* TikTok / IG */}

{clip.video_description_for_tiktok || clip.video_description_for_instagram}

{/* Error Message */} {editError && (
{editError}
)} {/* Actions Footer */}
{/* Post Modal */} {showModal && (

Post to Socials

{!uploadPostKey && (
Configure API Key in Settings first.
)}
{postResult && (
{postResult.success ? : }
{postResult.msg}
)}
)} setShowSubtitleModal(false)} onGenerate={handleSubtitle} isProcessing={isSubtitling} videoUrl={currentVideoUrl} />
); }