import React, { useState, useEffect, useMemo } from 'react'; import { Loader2, Download, Film, FolderOpen } from 'lucide-react'; import { apiJson } from '../lib/api'; // The signed-in user's saved video library (stored in R2). Private, signed links. // Videos are grouped by project (job); re-openable projects get a "reopen" // action that restores the whole job for further editing in the Clip Generator. export default function HistoryTab({ onReopenProject }) { const [videos, setVideos] = useState(null); const [projects, setProjects] = useState({}); const [reopening, setReopening] = useState(null); const [reopenError, setReopenError] = useState(''); const [error, setError] = useState(''); useEffect(() => { apiJson('/api/history') .then((d) => setVideos(d.videos || [])) .catch(() => setError('Could not load your library.')); apiJson('/api/projects') .then((d) => { const map = {}; for (const p of d.projects || []) map[p.job_id] = p; setProjects(map); }) .catch(() => {}); }, []); // Group videos by job, preserving the newest-first order of /api/history. const groups = useMemo(() => { const byJob = new Map(); for (const v of videos || []) { const key = v.job_id || v.id; if (!byJob.has(key)) byJob.set(key, []); byJob.get(key).push(v); } return [...byJob.entries()]; }, [videos]); const handleReopen = async (jobId) => { if (!onReopenProject || reopening) return; setReopening(jobId); setReopenError(''); try { await onReopenProject(jobId); } catch (e) { setReopenError('Could not reopen this project. Please try again.'); setReopening(null); } }; const fmtDate = (iso) => (iso ? new Date(iso).toLocaleDateString(undefined, { year: 'numeric', month: 'short', day: 'numeric' }) : ''); if (videos === null && !error) { return
06 · HISTORY
All the shorts you've generated, saved while your plan is active. Kept for 7 days after your plan ends. Reopen a project to keep editing its clips.
{error &&{error}
} {reopenError &&{reopenError}
} {videos && videos.length === 0 && (No videos yet. Generate your first short from the Clip Generator.
{project?.title || vids[0]?.title || 'Project'}
{fmtDate(vids[0]?.created_at)} · {vids.length} clip{vids.length === 1 ? '' : 's'}
{v.title || 'Short'}