import React, { useState } from "react"; import { History, X, RotateCcw } from "lucide-react"; import { Document } from "../../types"; interface VersionHistoryPanelProps { activeDoc: Document; editorText: string; setEditorText: (text: string) => void; createHistoryCheckpoint: (id: string, label?: string) => void; setDocuments: React.Dispatch>; syncDocument: (id: string, text: string, flag: boolean) => Promise; addLog: (text: string, type: "success" | "info" | "sync" | "error") => void; setToast: (toast: { show: boolean; message: string; timestamp: string }) => void; setShowVersionHistory: (show: boolean) => void; activeDocId: string; } export default function VersionHistoryPanel({ activeDoc, editorText, setEditorText, createHistoryCheckpoint, setDocuments, syncDocument, addLog, setToast, setShowVersionHistory, activeDocId }: VersionHistoryPanelProps) { const [checkpointLabelInput, setCheckpointLabelInput] = useState(""); const handleSaveCheckpoint = () => { const val = checkpointLabelInput.trim() || "User Checkpoint"; createHistoryCheckpoint(activeDocId, val); setCheckpointLabelInput(""); setToast({ show: true, message: "Manual snapshot saved 💾", timestamp: new Date().toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" }) }); }; const handleRestore = (hist: any) => { // Create safety backup of current content before swapping createHistoryCheckpoint(activeDocId, "Pre-Restore State Backup"); // Swap text setEditorText(hist.content); // Mark document as edited and sync setDocuments((prevDocs) => prevDocs.map((doc) => { if (doc.id === activeDocId) { return { ...doc, content: hist.content, isSynced: false, lastSaved: new Date().toISOString() }; } return doc; }) ); syncDocument(activeDocId, hist.content, false); setToast({ show: true, message: `Restored: ${hist.label || 'prior snapshot'}`, timestamp: new Date().toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" }) }); addLog(`Restored "${activeDoc.title}" to state: "${hist.label}". Safety checkpoint captured.`, "success"); }; const historyList = activeDoc.history || []; return (
History
{/* Save Custom Checkpoint */}
setCheckpointLabelInput(e.target.value)} placeholder="Label (e.g. Pre-Proofread)" className="w-full text-[9px] font-mono px-2 py-1 bg-[#15181E] text-slate-205 border border-slate-800 rounded placeholder:text-slate-650 focus:outline-none focus:border-indigo-500" />
{/* List of past checkpoints */}
{historyList.length === 0 ? (
No snapshots yet.
Your keystrokes are saved automatically.
) : ( historyList.map((hist) => { const dateObj = new Date(hist.timestamp); const dateFormatted = dateObj.toLocaleDateString([], { month: "short", day: "numeric" }); const timeFormatted = dateObj.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit", second: "2-digit" }); return (
{hist.label || "Snapshot"} {dateFormatted}
{timeFormatted}
{/* Action row */}
{(hist.content.length / 1024).toFixed(2)} KB
); }) )}
); }