'use client';
import { memo } from 'react';
import { ChevronDown, Download, Loader2 } from 'lucide-react';
import { Button } from '@/components/ui/button';
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu';
import { cn } from '@/lib/utils';
import type {
BunnyDownloadPreference,
DownloadTarget,
Version,
} from '@/components/video-page/types';
interface DownloadControlsProps {
activeVersion: Version | null | undefined;
videoCanDownload: boolean;
isDownloading: boolean;
activeDownloadTarget: DownloadTarget | null;
onDownload: (preference?: BunnyDownloadPreference) => void;
compact?: boolean;
}
interface DownloadMenuItemsProps {
activeVersion: Version | null | undefined;
videoCanDownload: boolean;
isDownloading: boolean;
activeDownloadTarget: DownloadTarget | null;
onDownload: (preference?: BunnyDownloadPreference) => void;
}
export const DownloadControls = memo(function DownloadControls({
activeVersion,
videoCanDownload,
isDownloading,
activeDownloadTarget,
onDownload,
compact = false,
}: DownloadControlsProps) {
if (!activeVersion) return null;
const isVideoDownloadAvailable =
videoCanDownload &&
(activeVersion.providerId === 'bunny' || activeVersion.providerId === 'direct');
if (activeVersion.providerId === 'bunny') {
return (
{
event.preventDefault();
onDownload('original');
}}
disabled={!isVideoDownloadAvailable || isDownloading}
>
{activeDownloadTarget === 'original' ? (
) : (
)}
Download Original
{
event.preventDefault();
onDownload('compressed');
}}
disabled={!isVideoDownloadAvailable || isDownloading}
>
{activeDownloadTarget === 'compressed' ? (
) : (
)}
Download Compressed
);
}
if (compact) {
return (
{
event.preventDefault();
onDownload();
}}
disabled={!isVideoDownloadAvailable || isDownloading}
>
{isDownloading ? (
) : (
)}
Download
);
}
return (
);
});
export const DownloadMenuItems = memo(function DownloadMenuItems({
activeVersion,
videoCanDownload,
isDownloading,
activeDownloadTarget,
onDownload,
}: DownloadMenuItemsProps) {
if (!activeVersion) return null;
const isVideoDownloadAvailable =
videoCanDownload &&
(activeVersion.providerId === 'bunny' || activeVersion.providerId === 'direct');
if (activeVersion.providerId === 'bunny') {
return (
<>
{
event.preventDefault();
onDownload('original');
}}
disabled={!isVideoDownloadAvailable || isDownloading}
>
{activeDownloadTarget === 'original' ? (
) : (
)}
Download Original
{
event.preventDefault();
onDownload('compressed');
}}
disabled={!isVideoDownloadAvailable || isDownloading}
>
{activeDownloadTarget === 'compressed' ? (
) : (
)}
Download Compressed
>
);
}
return (
{
event.preventDefault();
onDownload();
}}
disabled={!isVideoDownloadAvailable || isDownloading}
>
{isDownloading ? (
) : (
)}
Download
);
});