clipwright/dashboard/src/components/HistoryTab.jsx
juancarlos.cavero 0ba4fac47b feat: durable video library on Cloudflare R2 (history + retention)
- cloud/storage.py: R2 (S3-compatible) client — upload, presigned view/download
  URLs, delete-by-prefix.
- cloud/videos.py: archive a completed managed job's clips to R2 under
  users/<uid>/<job>/, GET /api/history (private signed links), and a retention
  sweeper that deletes a user's videos 7 days after their subscription ends.
- models.py: user_videos table (created via create_all).
- app.py: archive clips to R2 on managed-job completion.
- billing.py: email the admin the moment a subscriber hits cancel.
- Frontend: History tab (library grid with per-video view + download).

R2 bucket 'openshorts-videos' + scoped API token created; end-to-end verified:
archive -> presigned GET (200) -> purge after grace -> object 404.
2026-07-14 14:56:40 +02:00

61 lines
2.6 KiB
JavaScript

import React, { useState, useEffect } from 'react';
import { Loader2, Download, History as HistoryIcon, Film } from 'lucide-react';
import { apiJson } from '../lib/api';
// The signed-in user's saved video library (stored in R2). Private, signed links.
export default function HistoryTab() {
const [videos, setVideos] = useState(null);
const [error, setError] = useState('');
useEffect(() => {
apiJson('/api/history')
.then((d) => setVideos(d.videos || []))
.catch(() => setError('Could not load your library.'));
}, []);
const fmtDate = (iso) => (iso ? new Date(iso).toLocaleDateString(undefined, { year: 'numeric', month: 'short', day: 'numeric' }) : '');
if (videos === null && !error) {
return <div className="flex justify-center py-20"><Loader2 className="animate-spin text-primary" /></div>;
}
return (
<div className="h-full overflow-y-auto p-8 max-w-5xl mx-auto animate-[fadeIn_0.3s_ease-out]">
<div className="flex items-center gap-3 mb-2">
<HistoryIcon className="text-primary" />
<h1 className="text-2xl font-bold">Your library</h1>
</div>
<p className="text-zinc-400 text-sm mb-8">
All the shorts you've generated, saved while your plan is active. Kept for 7 days after your plan ends.
</p>
{error && <p className="text-red-400 text-sm">{error}</p>}
{videos && videos.length === 0 && (
<div className="text-center py-20 text-zinc-500">
<Film size={40} className="mx-auto mb-4 opacity-50" />
<p>No videos yet. Generate your first short from the Clip Generator.</p>
</div>
)}
<div className="grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-4 gap-5">
{(videos || []).map((v) => (
<div key={v.id} className="bg-surface border border-white/10 rounded-xl overflow-hidden group">
<div className="aspect-[9/16] bg-black">
<video src={v.view_url} controls preload="metadata" className="w-full h-full object-contain" />
</div>
<div className="p-3">
<p className="text-sm font-medium line-clamp-2 mb-1" title={v.title}>{v.title || 'Short'}</p>
<div className="flex items-center justify-between">
<span className="text-[11px] text-zinc-500">{fmtDate(v.created_at)}</span>
<a href={v.download_url} className="text-primary hover:text-blue-400 flex items-center gap-1 text-xs" title="Download">
<Download size={14} /> Download
</a>
</div>
</div>
</div>
))}
</div>
</div>
);
}