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
176 lines
5.5 KiB
TypeScript
176 lines
5.5 KiB
TypeScript
import { useEffect, useRef } from "react";
|
|
import { Link } from "react-router-dom";
|
|
import { ChevronDown, ChevronUp, Pencil } from "lucide-react";
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
} from "@/components/ui/dialog";
|
|
import { Button } from "@/components/ui/button";
|
|
import { getVideoStreamUrl } from "@/lib/api";
|
|
import { formatBytes } from "@/lib/format";
|
|
import type { DiscoveryVideo } from "@/types";
|
|
|
|
interface VideoPlayerDialogProps {
|
|
open: boolean;
|
|
onOpenChange: (open: boolean) => void;
|
|
video: DiscoveryVideo | null;
|
|
onPrevious?: () => void;
|
|
onNext?: () => void;
|
|
hasPrevious?: boolean;
|
|
hasNext?: boolean;
|
|
onEdit?: (video: DiscoveryVideo) => void;
|
|
}
|
|
|
|
export function VideoPlayerDialog({
|
|
open,
|
|
onOpenChange,
|
|
video,
|
|
onPrevious,
|
|
onNext,
|
|
hasPrevious,
|
|
hasNext,
|
|
onEdit,
|
|
}: VideoPlayerDialogProps) {
|
|
const videoRef = useRef<HTMLVideoElement>(null);
|
|
|
|
// Autoplay when the dialog opens or the video changes. The play() call may
|
|
// be blocked if the browser considers the gesture consumed — the user can
|
|
// always press play manually.
|
|
useEffect(() => {
|
|
if (open && video) {
|
|
videoRef.current?.play().catch(() => {});
|
|
}
|
|
}, [open, video]);
|
|
|
|
// Arrow-key navigation. ↑/↓ always switch videos (when available) and are
|
|
// captured in the CAPTURE phase with preventDefault, so they never reach
|
|
// the <video> element's native controls or move focus onto the player.
|
|
// ←/→ switch videos too, except when the player itself is focused — there
|
|
// they keep their native seek behaviour.
|
|
useEffect(() => {
|
|
if (!open) return;
|
|
|
|
const onKey = (e: KeyboardEvent) => {
|
|
const isVideoFocused = e.target instanceof HTMLVideoElement;
|
|
|
|
if (e.key === "ArrowUp" || e.key === "ArrowDown") {
|
|
e.preventDefault();
|
|
if (e.key === "ArrowUp" && hasPrevious) onPrevious?.();
|
|
if (e.key === "ArrowDown" && hasNext) onNext?.();
|
|
return;
|
|
}
|
|
|
|
if (
|
|
!isVideoFocused &&
|
|
(e.key === "ArrowLeft" || e.key === "ArrowRight")
|
|
) {
|
|
e.preventDefault();
|
|
if (e.key === "ArrowLeft" && hasPrevious) onPrevious?.();
|
|
if (e.key === "ArrowRight" && hasNext) onNext?.();
|
|
}
|
|
};
|
|
|
|
window.addEventListener("keydown", onKey, true);
|
|
return () => window.removeEventListener("keydown", onKey, true);
|
|
}, [open, hasPrevious, hasNext, onPrevious, onNext]);
|
|
|
|
return (
|
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
|
<DialogContent className="overflow-hidden sm:max-w-4xl">
|
|
<DialogHeader>
|
|
<DialogTitle className="truncate pr-8">{video?.title}</DialogTitle>
|
|
</DialogHeader>
|
|
|
|
{open && video && (
|
|
<div className="space-y-4">
|
|
<div className="relative bg-black">
|
|
{/* tabIndex=-1 keeps the player out of the focus order so the
|
|
dialog doesn't focus it and arrow keys stay ours */}
|
|
<video
|
|
key={video.id}
|
|
ref={videoRef}
|
|
src={getVideoStreamUrl(video.id)}
|
|
controls
|
|
autoPlay
|
|
tabIndex={-1}
|
|
onKeyDown={(e) => {
|
|
if (e.key === "ArrowUp" || e.key === "ArrowDown") {
|
|
e.preventDefault();
|
|
}
|
|
}}
|
|
className="max-h-[65vh] w-full"
|
|
/>
|
|
|
|
{hasPrevious && (
|
|
<Button
|
|
variant="secondary"
|
|
size="icon"
|
|
onClick={onPrevious}
|
|
aria-label="Previous video"
|
|
className="absolute left-2 top-1/2 -translate-y-1/2 opacity-70"
|
|
>
|
|
<ChevronUp />
|
|
</Button>
|
|
)}
|
|
{hasNext && (
|
|
<Button
|
|
variant="secondary"
|
|
size="icon"
|
|
onClick={onNext}
|
|
aria-label="Next video"
|
|
className="absolute right-2 top-1/2 -translate-y-1/2 opacity-70"
|
|
>
|
|
<ChevronDown />
|
|
</Button>
|
|
)}
|
|
</div>
|
|
|
|
<div className="flex flex-wrap items-center justify-between gap-2">
|
|
<span className="text-xs text-muted-foreground">
|
|
{formatBytes(video.size)}
|
|
</span>
|
|
|
|
<div className="flex flex-wrap items-center gap-1.5">
|
|
{video.entries?.map((entry) => (
|
|
<Link
|
|
key={entry.id}
|
|
to={`/entry/${entry.id}`}
|
|
onClick={() => onOpenChange(false)}
|
|
className="rounded-full bg-accent px-2 py-0.5 text-[10px] text-accent-foreground transition-colors hover:bg-accent/70"
|
|
>
|
|
{entry.title}
|
|
</Link>
|
|
))}
|
|
{onEdit && (
|
|
<Button
|
|
variant="ghost"
|
|
size="xs"
|
|
onClick={() => onEdit(video)}
|
|
>
|
|
<Pencil />
|
|
Edit
|
|
</Button>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
{video.description && (
|
|
<p className="text-sm text-muted-foreground">
|
|
{video.description}
|
|
</p>
|
|
)}
|
|
|
|
{(hasPrevious || hasNext) && (
|
|
<p className="text-center text-[10px] text-muted-foreground/60">
|
|
Use ↑ / ↓ to switch videos
|
|
</p>
|
|
)}
|
|
</div>
|
|
)}
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
}
|