Let users upload videos, tag them with one or more entries, and watch them from a new Discovery Videos page. Backend: - Add Video and VideoEntry models and relations - Add video controller and /video routes - Add video thumbnail generation service (ffmpeg-static) - Add video upload storage location and config Frontend: - Add DiscoveryVideosPage with sidebar link and route - Add AddVideoDialog, VideoDropzone, EntryMultiSelect, VideoCard and VideoPlayerDialog components - Add discoveryVideos API client and useDiscoveryVideos hook - Add format helpers, video types and normalizers - Add skeleton for the page Docs: - Document the feature in docs-vault
207 lines
6.6 KiB
TypeScript
207 lines
6.6 KiB
TypeScript
import { useCallback, useEffect, useRef, useState } from "react";
|
|
import { Clapperboard, Loader2, Plus } from "lucide-react";
|
|
import { Card } from "@/components/ui/card";
|
|
import { Skeleton } from "@/components/ui/skeleton";
|
|
import { Button } from "@/components/ui/button";
|
|
import { VideoCard } from "@/components/discovery-videos/VideoCard";
|
|
import { VideoPlayerDialog } from "@/components/discovery-videos/VideoPlayerDialog";
|
|
import { AddVideoDialog } from "@/components/discovery-videos/AddVideoDialog";
|
|
import { ConfirmDialog } from "@/components/dialogs/ConfirmDialog";
|
|
import { useLibrary } from "@/hooks/useEntries";
|
|
import { useDiscoveryVideos, useDeleteVideo } from "@/hooks/useDiscoveryVideos";
|
|
import type { DiscoveryVideo } from "@/types";
|
|
|
|
function VideoCardSkeleton() {
|
|
return (
|
|
<Card className="flex h-full flex-col overflow-hidden">
|
|
<div className="h-44 w-full bg-muted" />
|
|
{/* Mirror the fixed body heights of VideoCard */}
|
|
<div className="flex flex-col gap-2 p-3">
|
|
<Skeleton className="h-5 w-3/4" />
|
|
<Skeleton className="h-8 w-full" />
|
|
<Skeleton className="h-5 w-1/2" />
|
|
</div>
|
|
</Card>
|
|
);
|
|
}
|
|
|
|
export function DiscoveryVideosPage() {
|
|
useLibrary(); // warm the library store for the entry picker
|
|
|
|
const [addOpen, setAddOpen] = useState(false);
|
|
const [editingVideo, setEditingVideo] = useState<DiscoveryVideo | null>(null);
|
|
const [deletingVideo, setDeletingVideo] = useState<DiscoveryVideo | null>(
|
|
null,
|
|
);
|
|
const [playerIndex, setPlayerIndex] = useState<number | null>(null);
|
|
|
|
const query = useDiscoveryVideos();
|
|
const deleteVideo = useDeleteVideo();
|
|
|
|
// Flatten + dedupe pages
|
|
const videos = (() => {
|
|
const seen = new Set<number>();
|
|
return (query.data?.pages.flatMap((p) => p.videos) ?? []).filter((v) => {
|
|
if (seen.has(v.id)) return false;
|
|
seen.add(v.id);
|
|
return true;
|
|
});
|
|
})();
|
|
|
|
// Infinite scroll via IntersectionObserver
|
|
const sentinelRef = useRef<HTMLDivElement>(null);
|
|
const { hasNextPage, isFetchingNextPage, fetchNextPage } = query;
|
|
const handleIntersect = useCallback(
|
|
(entries: IntersectionObserverEntry[]) => {
|
|
if (
|
|
entries[0]?.isIntersecting &&
|
|
hasNextPage &&
|
|
!isFetchingNextPage
|
|
) {
|
|
void fetchNextPage();
|
|
}
|
|
},
|
|
[hasNextPage, isFetchingNextPage, fetchNextPage],
|
|
);
|
|
|
|
useEffect(() => {
|
|
const el = sentinelRef.current;
|
|
if (!el) return;
|
|
const observer = new IntersectionObserver(handleIntersect, {
|
|
rootMargin: "400px",
|
|
});
|
|
observer.observe(el);
|
|
return () => observer.disconnect();
|
|
}, [handleIntersect]);
|
|
|
|
const handleConfirmDelete = () => {
|
|
if (!deletingVideo) return;
|
|
deleteVideo.mutate(deletingVideo.id, {
|
|
onSuccess: () => setDeletingVideo(null),
|
|
});
|
|
};
|
|
|
|
const openEdit = (video: DiscoveryVideo) => {
|
|
setEditingVideo(video);
|
|
setAddOpen(true);
|
|
};
|
|
|
|
const openPlayer = (video: DiscoveryVideo) => {
|
|
const index = videos.findIndex((v) => v.id === video.id);
|
|
if (index !== -1) setPlayerIndex(index);
|
|
};
|
|
|
|
const playerVideo = playerIndex !== null ? videos[playerIndex] : null;
|
|
|
|
const editFromPlayer = (video: DiscoveryVideo) => {
|
|
setPlayerIndex(null);
|
|
openEdit(video);
|
|
};
|
|
|
|
const closeAdd = (open: boolean) => {
|
|
setAddOpen(open);
|
|
if (!open) setEditingVideo(null);
|
|
};
|
|
|
|
return (
|
|
<div className="animate-page-enter space-y-6">
|
|
<div className="flex items-center justify-between gap-4">
|
|
<div>
|
|
<h1 className="text-2xl font-bold">Discover</h1>
|
|
<p className="text-sm text-muted-foreground">
|
|
Your saved videos — recommendations, and more.
|
|
</p>
|
|
</div>
|
|
<Button onClick={() => setAddOpen(true)}>
|
|
<Plus />
|
|
Add media
|
|
</Button>
|
|
</div>
|
|
|
|
{query.isLoading ? (
|
|
<div className="grid grid-cols-[repeat(auto-fill,minmax(240px,1fr))] items-start gap-4">
|
|
{Array.from({ length: 6 }).map((_, i) => (
|
|
<VideoCardSkeleton key={i} />
|
|
))}
|
|
</div>
|
|
) : videos.length === 0 ? (
|
|
<div className="flex flex-col items-center gap-3 rounded-xl border border-dashed p-12 text-center">
|
|
<Clapperboard className="h-10 w-10 text-muted-foreground" />
|
|
<p className="max-w-sm text-sm text-muted-foreground">
|
|
No videos yet. Drop your first video and start building your watch
|
|
pile.
|
|
</p>
|
|
<Button onClick={() => setAddOpen(true)}>
|
|
<Plus />
|
|
Add media
|
|
</Button>
|
|
</div>
|
|
) : (
|
|
/* items-start: cards keep their own fixed size — a row never
|
|
stretches smaller cards to follow the biggest one */
|
|
<div className="grid grid-cols-[repeat(auto-fill,minmax(240px,1fr))] items-start gap-4">
|
|
{videos.map((video, index) => (
|
|
<div
|
|
key={video.id}
|
|
className="animate-card-enter"
|
|
style={{ animationDelay: `${Math.min(index * 20, 300)}ms` }}
|
|
>
|
|
<VideoCard
|
|
video={video}
|
|
onPlay={openPlayer}
|
|
onEdit={openEdit}
|
|
onDelete={setDeletingVideo}
|
|
/>
|
|
</div>
|
|
))}
|
|
</div>
|
|
)}
|
|
|
|
{/* Infinite scroll sentinel */}
|
|
<div ref={sentinelRef} className="flex justify-center py-4">
|
|
{query.isFetchingNextPage && (
|
|
<Loader2 className="h-6 w-6 animate-spin text-muted-foreground" />
|
|
)}
|
|
</div>
|
|
|
|
<AddVideoDialog open={addOpen} onOpenChange={closeAdd} video={editingVideo} />
|
|
|
|
<VideoPlayerDialog
|
|
open={playerIndex !== null}
|
|
onOpenChange={(open) => {
|
|
if (!open) setPlayerIndex(null);
|
|
}}
|
|
video={playerVideo}
|
|
hasPrevious={playerIndex !== null && playerIndex > 0}
|
|
hasNext={playerIndex !== null && playerIndex < videos.length - 1}
|
|
onPrevious={() =>
|
|
setPlayerIndex((i) => (i !== null ? Math.max(0, i - 1) : i))
|
|
}
|
|
onNext={() =>
|
|
setPlayerIndex((i) =>
|
|
i !== null ? Math.min(videos.length - 1, i + 1) : i,
|
|
)
|
|
}
|
|
onEdit={editFromPlayer}
|
|
/>
|
|
|
|
<ConfirmDialog
|
|
open={!!deletingVideo}
|
|
onOpenChange={(open) => {
|
|
if (!open) setDeletingVideo(null);
|
|
}}
|
|
title="Delete video?"
|
|
description={
|
|
deletingVideo
|
|
? `"${deletingVideo.title}" will be permanently removed from disk.`
|
|
: ""
|
|
}
|
|
confirmLabel="Delete"
|
|
variant="destructive"
|
|
isLoading={deleteVideo.isPending}
|
|
onConfirm={handleConfirmDelete}
|
|
/>
|
|
</div>
|
|
);
|
|
}
|