import { useEffect, useMemo, useState, type FormEvent } from "react"; import { ArrowLeft, Download, FileText, Image as ImageIcon, Upload } from "lucide-react"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Textarea } from "@/components/ui/textarea"; import { getStoredToken } from "@/lib/auth"; import { attachmentDownloadURL, getCalendarItem, listAttachments, uploadAttachment, type Attachment, type CalendarItem, } from "@/lib/calendar"; type PostDetailViewProps = { itemId: string; onBack: () => void; }; const statusLabels: Record = { rascunho: "Rascunho", planejado: "Planejado", em_producao: "Em produção", em_revisao: "Em revisão", aprovado: "Aprovado", publicado: "Publicado", cancelado: "Cancelado", }; const dateFormatter = new Intl.DateTimeFormat("pt-BR", { day: "2-digit", month: "long", year: "numeric" }); export function PostDetailView({ itemId, onBack }: PostDetailViewProps) { const [item, setItem] = useState(null); const [attachments, setAttachments] = useState([]); const [previewAttachment, setPreviewAttachment] = useState(null); const [isLoading, setIsLoading] = useState(false); const [isUploading, setIsUploading] = useState(false); const [error, setError] = useState(""); const [attachmentError, setAttachmentError] = useState(""); const imageAttachments = useMemo(() => attachments.filter((attachment) => isImageAttachment(attachment)), [attachments]); const fileAttachments = useMemo(() => attachments.filter((attachment) => !isImageAttachment(attachment)), [attachments]); useEffect(() => { const token = getStoredToken(); if (!token) return; setIsLoading(true); setError(""); Promise.all([getCalendarItem(token, itemId), listAttachments(token, itemId)]) .then(([loadedItem, loadedAttachments]) => { setItem(loadedItem); setAttachments(loadedAttachments); }) .catch((err) => { setError(err instanceof Error ? err.message : "Não foi possível carregar a postagem."); }) .finally(() => setIsLoading(false)); }, [itemId]); async function handleUploadAttachment(event: FormEvent) { event.preventDefault(); const token = getStoredToken(); const fileInput = event.currentTarget.elements.namedItem("attachment") as HTMLInputElement | null; const file = fileInput?.files?.[0]; if (!token || !file) return; setIsUploading(true); setAttachmentError(""); try { const attachment = await uploadAttachment(token, itemId, file); setAttachments((current) => [attachment, ...current]); if (fileInput) fileInput.value = ""; } catch (err) { setAttachmentError(err instanceof Error ? err.message : "Não foi possível enviar o anexo."); } finally { setIsUploading(false); } } if (isLoading && !item) { return

Carregando postagem...

; } if (error) { return (
{error}
); } if (!item) return null; return (

{item.title}

{statusLabels[item.status]}

{item.client_name} · {dateFormatter.format(parseDateValue(item.scheduled_date))}

Detalhes Anexos
{attachmentError && (
{attachmentError}
)} {attachments.length === 0 && (

Nenhum anexo enviado.

)} {imageAttachments.length > 0 && (
{imageAttachments.map((attachment) => ( ))}
)} {fileAttachments.length > 0 && ( )}
Informações
!open && setPreviewAttachment(null)}> {previewAttachment?.original_filename} Visualização do anexo. {previewAttachment && (
{previewAttachment.original_filename}
)}
); } function ReadOnlyField({ label, value, fallback, multiline = false }: { label: string; value: string; fallback: string; multiline?: boolean }) { return (
{multiline ? (