import { useCallback, useEffect, useState } from "react" import { createFileRoute, Link, useNavigate } from "@tanstack/react-router" import type { Archive } from "@/db/schema" import { ArrowLeft, Video, Image, FileText, Lock, Globe, Trash2, Edit3, Share2, } from "lucide-react" export const Route = createFileRoute("/archive/$archiveId")({ ssr: false, component: ArchiveDetailPage, }) function ArchiveDetailPage() { const { archiveId } = Route.useParams() const navigate = useNavigate() const [archive, setArchive] = useState(null) const [loading, setLoading] = useState(true) const [error, setError] = useState(null) const [deleting, setDeleting] = useState(false) const [showDeleteConfirm, setShowDeleteConfirm] = useState(false) const [isEditing, setIsEditing] = useState(false) const loadArchive = useCallback(async () => { setLoading(true) setError(null) try { const response = await fetch(`/api/archives/${archiveId}`) const data = await response.json() if (!response.ok) { throw new Error(data.error || "Failed to load archive") } setArchive(data.archive) } catch (err) { console.error("[archive] failed to load archive", err) setError(err instanceof Error ? err.message : "Failed to load archive") } finally { setLoading(false) } }, [archiveId]) useEffect(() => { void loadArchive() }, [loadArchive]) const handleDelete = async () => { setDeleting(true) try { const response = await fetch(`/api/archives/${archiveId}`, { method: "DELETE", }) if (!response.ok) { const data = await response.json() throw new Error(data.error || "Failed to delete archive") } navigate({ to: "/archive" }) } catch (err) { console.error("[archive] failed to delete", err) setError(err instanceof Error ? err.message : "Failed to delete archive") } finally { setDeleting(false) setShowDeleteConfirm(false) } } const handleToggleVisibility = async () => { if (!archive) return try { const response = await fetch(`/api/archives/${archiveId}`, { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ is_public: !archive.is_public }), }) const data = await response.json() if (!response.ok) { throw new Error(data.error || "Failed to update visibility") } setArchive(data.archive) } catch (err) { console.error("[archive] failed to toggle visibility", err) setError(err instanceof Error ? err.message : "Failed to update") } } if (loading) { return (
) } if (error || !archive) { return (
Back to Archives

{error || "Archive not found"}

) } const TypeIcon = { video: Video, image: Image, text: FileText, }[archive.type] || FileText return (
Back to Archives

{archive.title}

{archive.type} {new Date(archive.created_at).toLocaleDateString(undefined, { year: "numeric", month: "long", day: "numeric", })}
{archive.description && (

{archive.description}

)} {/* Content area */}
{archive.type === "video" && ( )} {archive.type === "image" && ( )} {archive.type === "text" && ( )}
{/* Metadata */} {archive.file_size_bytes && archive.file_size_bytes > 0 && (
Size: {formatBytes(archive.file_size_bytes)} {archive.duration_seconds && ( Duration: {formatDuration(archive.duration_seconds)} )}
)}
{/* Delete confirmation modal */} {showDeleteConfirm && (

Delete Archive?

This action cannot be undone. All content will be permanently deleted.

)}
) } function VideoContent({ archive }: { archive: Archive }) { if (!archive.content_url) { return (
) } return (