145 lines
4.9 KiB
TypeScript
145 lines
4.9 KiB
TypeScript
import { useState } from 'react';
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
} from '@/components/ui/dialog';
|
|
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
|
|
import { Button } from '@/components/ui/button';
|
|
import { Input } from '@/components/ui/input';
|
|
import { ScrollArea } from '@/components/ui/scroll-area';
|
|
import { Check, Trash2, Upload } from 'lucide-react';
|
|
import { getMediaUrl } from '@/lib/api';
|
|
import { entriesApi } from '@/api/entries';
|
|
import { useUploadMedia } from '@/hooks/useEntries';
|
|
import type { Media } from '@/types';
|
|
|
|
interface EntryMediaDialogProps {
|
|
open: boolean;
|
|
onOpenChange: (open: boolean) => void;
|
|
entryId: number;
|
|
medias: Media[];
|
|
}
|
|
|
|
export function EntryMediaDialog({
|
|
open,
|
|
onOpenChange,
|
|
entryId,
|
|
medias,
|
|
}: EntryMediaDialogProps) {
|
|
const [activeTab, setActiveTab] = useState<string>('poster');
|
|
const uploadMedia = useUploadMedia();
|
|
|
|
const filteredMedias = medias.filter((m) => m.type === activeTab);
|
|
|
|
const handleUpload = (type: string) => {
|
|
const input = document.createElement('input');
|
|
input.type = 'file';
|
|
input.accept = 'image/*';
|
|
input.onchange = async (e) => {
|
|
const file = (e.target as HTMLInputElement).files?.[0];
|
|
if (!file) return;
|
|
const formData = new FormData();
|
|
formData.append(type, file);
|
|
uploadMedia.mutate({ id: entryId, formData });
|
|
};
|
|
input.click();
|
|
};
|
|
|
|
const handleSelect = async (mediaId: number) => {
|
|
await entriesApi.setMediaSelected(entryId, mediaId);
|
|
};
|
|
|
|
const handleDelete = async (mediaId: number) => {
|
|
await entriesApi.deleteMedia(entryId, mediaId);
|
|
};
|
|
|
|
return (
|
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
|
<DialogContent className="sm:max-w-2xl">
|
|
<DialogHeader>
|
|
<DialogTitle>Entry Media</DialogTitle>
|
|
</DialogHeader>
|
|
|
|
<Tabs value={activeTab} onValueChange={setActiveTab}>
|
|
<TabsList>
|
|
<TabsTrigger value="poster">Posters</TabsTrigger>
|
|
<TabsTrigger value="backdrop">Backdrops</TabsTrigger>
|
|
<TabsTrigger value="logo">Logos</TabsTrigger>
|
|
</TabsList>
|
|
|
|
<TabsContent value={activeTab} className="mt-4">
|
|
<div className="flex justify-end mb-3">
|
|
<Button
|
|
size="sm"
|
|
onClick={() => handleUpload(activeTab)}
|
|
disabled={uploadMedia.isPending}
|
|
>
|
|
<Upload className="mr-2 h-4 w-4" />
|
|
Upload {activeTab}
|
|
</Button>
|
|
</div>
|
|
|
|
<ScrollArea className="h-80">
|
|
{filteredMedias.length === 0 ? (
|
|
<p className="py-8 text-center text-sm text-muted-foreground">
|
|
No {activeTab}s found.
|
|
</p>
|
|
) : (
|
|
<div className="grid grid-cols-3 gap-3">
|
|
{filteredMedias.map((media) => (
|
|
<div
|
|
key={media.id}
|
|
className={`group relative overflow-hidden rounded-lg border-2 ${
|
|
media.is_selected
|
|
? 'border-primary'
|
|
: 'border-transparent'
|
|
}`}
|
|
>
|
|
<img
|
|
src={getMediaUrl(
|
|
'entries',
|
|
`${activeTab}s`,
|
|
'preview',
|
|
media.path
|
|
)}
|
|
alt={activeTab}
|
|
className="w-full object-cover"
|
|
loading="lazy"
|
|
/>
|
|
<div className="absolute inset-0 flex items-center justify-center gap-2 bg-black/60 opacity-0 transition-opacity group-hover:opacity-100">
|
|
<Button
|
|
size="icon"
|
|
variant="secondary"
|
|
className="h-8 w-8"
|
|
onClick={() => handleSelect(media.id)}
|
|
>
|
|
<Check className="h-4 w-4" />
|
|
</Button>
|
|
<Button
|
|
size="icon"
|
|
variant="destructive"
|
|
className="h-8 w-8"
|
|
onClick={() => handleDelete(media.id)}
|
|
>
|
|
<Trash2 className="h-4 w-4" />
|
|
</Button>
|
|
</div>
|
|
{media.is_selected && (
|
|
<div className="absolute right-1 top-1 rounded-full bg-primary p-1">
|
|
<Check className="h-3 w-3 text-primary-foreground" />
|
|
</div>
|
|
)}
|
|
</div>
|
|
))}
|
|
</div>
|
|
)}
|
|
</ScrollArea>
|
|
</TabsContent>
|
|
</Tabs>
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
}
|