All checks were successful
CI/CD — Build & Deploy / Build & Deploy (push) Successful in 22s
161 lines
5.1 KiB
TypeScript
161 lines
5.1 KiB
TypeScript
import { useState, useEffect, useMemo } from 'react';
|
|
import { Link } from 'react-router-dom';
|
|
import { apiFetch } from '../lib/api';
|
|
|
|
interface Video {
|
|
id: string;
|
|
title: string;
|
|
description: string;
|
|
requiredLevel: number;
|
|
locked: boolean;
|
|
thumbnailUrl?: string;
|
|
}
|
|
|
|
interface VideosResponse {
|
|
success: boolean;
|
|
data: { videos: Video[] };
|
|
}
|
|
|
|
export default function HomePage() {
|
|
const [videos, setVideos] = useState<Video[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState(false);
|
|
const [query, setQuery] = useState('');
|
|
|
|
useEffect(() => {
|
|
apiFetch<VideosResponse>('/videos')
|
|
.then((res) => setVideos(res.data.videos))
|
|
.catch(() => setError(true))
|
|
.finally(() => setLoading(false));
|
|
}, []);
|
|
|
|
const filtered = useMemo(() => {
|
|
const q = query.trim().toLowerCase();
|
|
if (!q) return videos;
|
|
return videos.filter(
|
|
(v) => v.title.toLowerCase().includes(q) || v.description?.toLowerCase().includes(q),
|
|
);
|
|
}, [videos, query]);
|
|
|
|
const free = filtered.filter((v) => !v.locked);
|
|
const premium = filtered.filter((v) => v.locked);
|
|
|
|
return (
|
|
<div className="flex flex-col gap-10">
|
|
|
|
{/* Hero */}
|
|
<section className="border-b border-od-border pb-8">
|
|
<h1 className="text-2xl font-semibold text-od-text">Vidéos & formations</h1>
|
|
<p className="mt-2 text-sm text-od-muted">
|
|
Contenu libre et premium — connecte-toi pour accéder aux formations complètes.
|
|
</p>
|
|
<input
|
|
type="search"
|
|
value={query}
|
|
onChange={(e) => setQuery(e.target.value)}
|
|
placeholder="Rechercher…"
|
|
className="mt-4 w-full max-w-sm rounded border border-od-border bg-od-surface px-3 py-2 text-sm text-od-text placeholder-od-muted outline-none focus:border-od-accent"
|
|
/>
|
|
</section>
|
|
|
|
{loading && (
|
|
<div className="grid gap-4 sm:grid-cols-2">
|
|
{[...Array(4)].map((_, i) => <VideoCardSkeleton key={i} />)}
|
|
</div>
|
|
)}
|
|
|
|
{error && (
|
|
<p className="text-sm text-od-crit">Impossible de charger les vidéos. Réessaie plus tard.</p>
|
|
)}
|
|
|
|
{!loading && !error && (
|
|
<>
|
|
{free.length > 0 && (
|
|
<section>
|
|
<h2 className="mb-4 font-mono text-xs uppercase tracking-widest text-od-muted">
|
|
Accès libre
|
|
</h2>
|
|
<div className="grid gap-4 sm:grid-cols-2">
|
|
{free.map((v) => <VideoCard key={v.id} video={v} />)}
|
|
</div>
|
|
</section>
|
|
)}
|
|
|
|
{premium.length > 0 && (
|
|
<section>
|
|
<h2 className="mb-4 font-mono text-xs uppercase tracking-widest text-od-accent">
|
|
Premium
|
|
</h2>
|
|
<div className="grid gap-4 sm:grid-cols-2">
|
|
{premium.map((v) => <VideoCard key={v.id} video={v} />)}
|
|
</div>
|
|
</section>
|
|
)}
|
|
|
|
{videos.length === 0 && (
|
|
<p className="text-sm text-od-muted">Aucune vidéo disponible pour l'instant.</p>
|
|
)}
|
|
{videos.length > 0 && filtered.length === 0 && (
|
|
<p className="text-sm text-od-muted">Aucun résultat pour « {query} ».</p>
|
|
)}
|
|
</>
|
|
)}
|
|
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function VideoCard({ video }: { video: Video }) {
|
|
const inner = (
|
|
<div className="flex flex-col gap-3 rounded border border-od-border bg-od-surface p-4 transition-colors hover:border-od-accent/40">
|
|
{/* Thumbnail */}
|
|
<div className="relative h-28 overflow-hidden rounded bg-od-surface-hi">
|
|
{video.thumbnailUrl && (
|
|
<img
|
|
src={video.thumbnailUrl}
|
|
alt={video.title}
|
|
className="h-full w-full object-cover"
|
|
/>
|
|
)}
|
|
{video.locked && (
|
|
<div className="absolute inset-0 flex items-center justify-center bg-od-bg/70">
|
|
<span className="font-mono text-lg text-od-accent">⊘</span>
|
|
</div>
|
|
)}
|
|
</div>
|
|
|
|
<p className="text-sm font-medium text-od-text leading-snug">{video.title}</p>
|
|
|
|
{video.description && (
|
|
<p className="text-xs text-od-muted line-clamp-2">{video.description}</p>
|
|
)}
|
|
|
|
<div className="flex items-center gap-2">
|
|
{video.locked ? (
|
|
<span className="rounded border border-od-accent px-2 py-0.5 font-mono text-xs text-od-accent">
|
|
Premium
|
|
</span>
|
|
) : (
|
|
<span className="rounded border border-od-border px-2 py-0.5 font-mono text-xs text-od-muted">
|
|
Libre
|
|
</span>
|
|
)}
|
|
</div>
|
|
</div>
|
|
);
|
|
|
|
if (video.locked) return <div className="cursor-not-allowed opacity-75">{inner}</div>;
|
|
|
|
return <Link to={`/video/${video.id}`}>{inner}</Link>;
|
|
}
|
|
|
|
function VideoCardSkeleton() {
|
|
return (
|
|
<div className="flex flex-col gap-3 rounded border border-od-border bg-od-surface p-4">
|
|
<div className="h-28 rounded bg-od-surface-hi animate-pulse" />
|
|
<div className="h-3 w-3/4 rounded bg-od-surface-hi animate-pulse" />
|
|
<div className="h-2 w-1/2 rounded bg-od-surface-hi animate-pulse" />
|
|
</div>
|
|
);
|
|
}
|