"use client"; import { useState, useEffect } from "react"; import { motion, AnimatePresence } from "framer-motion"; import { Save, Download, Trash2, X, Camera, HardDrive, Loader2 } from "lucide-react"; import { toJpeg } from 'html-to-image'; export const SaveLoadModal = ({ isOpen, onClose, currentSession, onLoadSession }: any) => { const [slots, setSlots] = useState>({}); const [isLoading, setIsLoading] = useState(false); const [isCapturing, setIsCapturing] = useState(false); const fetchSlots = async () => { setIsLoading(true); try { const res = await fetch("/api/local/slots"); const data = await res.json(); const map: Record = {}; if (data.slots) { data.slots.forEach((meta: any) => { map[meta.slotIndex] = meta; }); } setSlots(map); } catch (e) { console.error("Fetch slots failed", e); } finally { setIsLoading(false); } }; useEffect(() => { if (isOpen) fetchSlots(); }, [isOpen]); const captureScreen = async () => { setIsCapturing(true); await new Promise(r => setTimeout(r, 800)); try { const dataUrl = await toJpeg(document.body, { quality: 0.6, pixelRatio: 0.5, filter: (node) => { if (node instanceof HTMLElement) { const style = window.getComputedStyle(node); if (style.position === 'fixed' && style.zIndex && parseInt(style.zIndex) > 100) { return false; } } return true; } }); return dataUrl; } catch (e) { console.error("Capture failed", e); return null; } finally { setIsCapturing(false); } }; const handleSave = async (index: number) => { if (!currentSession) return; if (slots[index] && !confirm(`⚠️ 覆盖 Slot ${index + 1}?`)) return; const base64 = await captureScreen(); setIsLoading(true); const lastMsg = currentSession.messages[currentSession.messages.length - 1]; let preview = "New Memory"; if (lastMsg) preview = typeof lastMsg.content === 'string' ? lastMsg.content.slice(0, 30) + "..." : "[Image]"; try { const res = await fetch("/api/local/slots", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ slotIndex: index, session: currentSession, meta: { slotIndex: index, name: `Save ${index + 1}`, previewText: preview, }, thumbnailBase64: base64 }) }); if (!res.ok) throw new Error("Save failed"); await fetchSlots(); } catch (e) { alert("Save failed"); } finally { setIsLoading(false); } }; const handleLoad = async (index: number) => { if (!confirm(`读取 Slot ${index + 1}?\n当前未保存的进度将丢失。`)) return; setIsLoading(true); try { const res = await fetch("/api/local/slots/read", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ slotIndex: index }) }); const data = await res.json(); if (data.session) { onLoadSession(data.session); onClose(); } } catch (e) { alert("Load failed"); } finally { setIsLoading(false); } }; const handleDelete = async (e: any, index: number) => { e.stopPropagation(); if (!confirm("永久删除此存档?")) return; setIsLoading(true); await fetch("/api/local/slots", { method: "DELETE", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ slotIndex: index }) }); await fetchSlots(); setIsLoading(false); }; if (isCapturing) return null; return ( {isOpen && (
LOCAL STORAGE
{Array.from({ length: 12 }).map((_, i) => { const slot = slots[i]; return (
!isLoading && handleSave(i)} className={`relative group aspect-video rounded-xl border-2 transition-all cursor-pointer overflow-hidden flex flex-col ${slot ? 'border-emerald-500/30 bg-black' : 'border-white/5 bg-white/5 hover:border-white/20'}`}>
{slot?.thumbnail ? ( ) : ( {i + 1} )}
{slot && } {slot && }
SLOT {i+1} {slot && {new Date(slot.timestamp).toLocaleDateString()}}
{slot &&
{slot.previewText}
}
) })}
)}
); };