"use client"; import { useCallback, useEffect, useState } from "react"; import { Clock, RotateCcw } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Sheet, SheetContent, SheetHeader, SheetTitle, SheetTrigger, } from "@/components/ui/sheet"; import { Spinner } from "@/components/ui/spinner"; import { documentsApiService } from "@/lib/apis/documents-api.service"; import { toast } from "sonner"; interface DocumentVersionSummary { version_number: number; title: string; content_hash: string; created_at: string | null; } interface VersionHistoryProps { documentId: number; documentType: string; } export function VersionHistoryButton({ documentId, documentType }: VersionHistoryProps) { const showVersionHistory = documentType === "LOCAL_FOLDER_FILE" || documentType === "OBSIDIAN_CONNECTOR"; if (!showVersionHistory) return null; return ( Version History ); } function VersionHistoryPanel({ documentId }: { documentId: number }) { const [versions, setVersions] = useState([]); const [loading, setLoading] = useState(true); const [selectedVersion, setSelectedVersion] = useState(null); const [versionContent, setVersionContent] = useState(""); const [contentLoading, setContentLoading] = useState(false); const [restoring, setRestoring] = useState(false); const loadVersions = useCallback(async () => { setLoading(true); try { const data = await documentsApiService.listDocumentVersions(documentId); setVersions(data as DocumentVersionSummary[]); } catch { toast.error("Failed to load version history"); } finally { setLoading(false); } }, [documentId]); useEffect(() => { loadVersions(); }, [loadVersions]); const handleSelectVersion = async (versionNumber: number) => { setSelectedVersion(versionNumber); setContentLoading(true); try { const data = (await documentsApiService.getDocumentVersion( documentId, versionNumber )) as { source_markdown: string }; setVersionContent(data.source_markdown || ""); } catch { toast.error("Failed to load version content"); } finally { setContentLoading(false); } }; const handleRestore = async (versionNumber: number) => { setRestoring(true); try { await documentsApiService.restoreDocumentVersion(documentId, versionNumber); toast.success(`Restored version ${versionNumber}`); await loadVersions(); } catch { toast.error("Failed to restore version"); } finally { setRestoring(false); } }; if (loading) { return (
); } if (versions.length === 0) { return (

No version history available yet.

Versions are created when file content changes.

); } return (
{versions.map((v) => (
handleSelectVersion(v.version_number)} >

Version {v.version_number}

{v.created_at && (

{new Date(v.created_at).toLocaleString()}

)} {v.title && (

{v.title}

)}
))}
{selectedVersion !== null && (

Preview — Version {selectedVersion}

{contentLoading ? (
) : (
							{versionContent || "(empty)"}
						
)}
)}
); }