'use client'; import * as React from 'react'; import { createPortal } from 'react-dom'; import { ChevronLeft, ChevronRight, Download, X, ZoomIn } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { MediaImageSkeleton } from '@/components/ui/media-image-skeleton'; import { isImageAttachment, isPdfAttachment } from '@/lib/document-attachments'; import { cn } from '@/lib/utils'; import type { ResolvedDocumentAttachment } from './document-photo-gallery'; interface DocumentAttachmentViewerProps { attachments: ResolvedDocumentAttachment[]; initialIndex?: number; open: boolean; onOpenChange: (open: boolean) => void; } export function DocumentAttachmentViewer({ attachments, initialIndex = 0, open, onOpenChange }: DocumentAttachmentViewerProps) { const [index, setIndex] = React.useState(initialIndex); const [mounted, setMounted] = React.useState(false); React.useEffect(() => { setMounted(true); }, []); React.useEffect(() => { if (open) setIndex(initialIndex); }, [initialIndex, open]); React.useEffect(() => { if (!open) return; function onKeyDown(event: KeyboardEvent) { if (event.key === 'Escape') onOpenChange(false); if (event.key === 'ArrowLeft') setIndex((current) => Math.max(0, current - 1)); if (event.key === 'ArrowRight') setIndex((current) => Math.min(attachments.length - 1, current + 1)); } window.addEventListener('keydown', onKeyDown); return () => window.removeEventListener('keydown', onKeyDown); }, [attachments.length, onOpenChange, open]); React.useEffect(() => { if (!open) return; const previous = document.body.style.overflow; document.body.style.overflow = 'hidden'; return () => { document.body.style.overflow = previous; }; }, [open]); if (!open || attachments.length === 0 || !mounted) return null; const current = attachments[index]; const downloadUrl = `${current.url}${current.url.includes('?') ? '&' : '?'}download=1`; return createPortal(
{current.fileName}
{index + 1} из {attachments.length}
{current.fileName}
Предпросмотр недоступен для этого типа файла