'use client'; import { useRouter } from 'next/navigation'; import { useState, useTransition } from 'react'; import { Download, ExternalLink, FileText, Loader2, Plus, Trash2 } from 'lucide-react'; import { toast } from 'sonner'; import { addInstructionPdf, addInstructionUrl, deleteInstruction, } from '@/app/(app)/katalog/actions'; import { fileUrl } from '@/lib/images'; import { PdfViewer } from './pdf-viewer'; import { Button } from '@/components/ui/button'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { Field } from '@/components/ui/field'; import { Input } from '@/components/ui/input'; import { NativeSelect } from '@/components/ui/native-select'; export type InstructionRow = { id: string; kind: 'pdf_upload' | 'external_url'; filePath: string | null; url: string | null; label: string | null; fileSizeBytes: number | null; }; function formatSize(bytes: number | null): string { if (!bytes) return ''; const mb = bytes / (1024 * 1024); return mb >= 1 ? `${mb.toLocaleString('de-DE', { maximumFractionDigits: 1 })} MB` : `${Math.round(bytes / 1024)} KB`; } export function InstructionsSection({ setId, instructions, }: { setId: string; instructions: InstructionRow[]; }) { const router = useRouter(); const [pending, startTransition] = useTransition(); const [addOpen, setAddOpen] = useState(false); const [kind, setKind] = useState<'pdf_upload' | 'external_url'>('pdf_upload'); const [viewing, setViewing] = useState(null); function submit(event: React.FormEvent) { event.preventDefault(); const formData = new FormData(event.currentTarget); startTransition(async () => { const result = kind === 'pdf_upload' ? await addInstructionPdf(setId, formData) : await addInstructionUrl(setId, Object.fromEntries(formData.entries())); if (!result.ok) { toast.error(result.error); return; } toast.success('Anleitung hinzugefügt.'); setAddOpen(false); router.refresh(); }); } function remove(id: string) { startTransition(async () => { const result = await deleteInstruction(id); if (!result.ok) { toast.error(result.error); return; } toast.success('Anleitung entfernt.'); router.refresh(); }); } return (
{instructions.length === 0 ? (

Noch keine Anleitung hinterlegt. Lade das PDF hoch oder verlinke es beim Hersteller.

) : (
    {instructions.map((item) => (
  • {item.label ?? (item.kind === 'pdf_upload' ? 'Anleitung' : 'Externe Anleitung')}

    {item.kind === 'pdf_upload' ? `PDF ${formatSize(item.fileSizeBytes)}` : 'Link'}

    {item.kind === 'pdf_upload' && item.filePath ? ( <> ) : item.url ? ( ) : null}
  • ))}
)} Anleitung hinzufügen Ein Set kann mehrere Anleitungen haben – zum Beispiel „Buch 1 von 3“.
setKind(event.target.value as typeof kind)} > {kind === 'pdf_upload' ? ( ) : ( )}
!open && setViewing(null)}> {viewing?.label ?? 'Anleitung'} {viewing?.filePath ? ( ) : null}
); }