import React, { useEffect, useState } from "react"; import { Archive, CheckCircle2, CloudUpload, Folder, History, Link2, Loader2, RefreshCw, Search, Unlink, X, } from "lucide-react"; import { api } from "../lib/tauri"; import { formatBytes, type BackupEntry, type DiscoveredSaveItem, type SimpleRomSchema, } from "../lib/types"; interface Props { discoveredSaves: DiscoveredSaveItem[]; onRefreshSaves: () => Promise; } export const SavesStatesView: React.FC = ({ discoveredSaves, onRefreshSaves }) => { const [subTab, setSubTab] = useState<"local" | "backups">("local"); const [filter, setFilter] = useState<"all" | "matched" | "unmatched" | "states">("all"); const [linkingItem, setLinkingItem] = useState(null); const [romSearch, setRomSearch] = useState(""); const [romResults, setRomResults] = useState([]); const [slotInput, setSlotInput] = useState("default"); const [backups, setBackups] = useState([]); const [toast, setToast] = useState(null); const [uploadingPath, setUploadingPath] = useState(null); const handleUploadSingle = async (item: DiscoveredSaveItem) => { if (!item.matchedRomId) return; setUploadingPath(item.localPath); setToast(null); try { await api.uploadSingleSave({ localPath: item.localPath, fileName: item.fileName, romId: item.matchedRomId, emulatorId: item.emulatorId, slot: item.slot || "default", isDirectory: item.isDirectory, isState: item.isState, }); setToast(`Uploaded '${item.fileName}' to RomM!`); await onRefreshSaves(); } catch (e) { setToast(`Upload failed: ${e}`); } finally { setUploadingPath(null); } }; useEffect(() => { if (subTab === "backups") { api.listBackups().then(setBackups).catch(console.error); } }, [subTab]); const filtered = discoveredSaves.filter((s) => { if (filter === "matched") return s.matchedRomId !== null; if (filter === "unmatched") return s.matchedRomId === null; if (filter === "states") return s.isState; return true; }); const openLinker = async (item: DiscoveredSaveItem) => { setLinkingItem(item); setSlotInput(item.slot || "default"); setRomSearch(""); const page = await api.fetchRoms({ limit: 30, offset: 0 }); setRomResults(page.items); }; const searchRomsForLink = async (e: React.FormEvent) => { e.preventDefault(); const page = await api.fetchRoms({ searchTerm: romSearch, limit: 30, offset: 0 }); setRomResults(page.items); }; const handleLink = async (rom: SimpleRomSchema) => { if (!linkingItem) return; await api.linkSaveToRom({ localPath: linkingItem.localPath, romId: rom.id, romName: rom.name || rom.fs_name_no_ext, emulatorId: linkingItem.emulatorId, slot: slotInput || "default", isState: linkingItem.isState, }); setLinkingItem(null); await onRefreshSaves(); }; const handleUnlink = async (localPath: string) => { await api.unlinkSave(localPath); await onRefreshSaves(); }; const handleRestoreBackup = async (b: BackupEntry) => { const destFolder = await api.pickFolder("Select Target Folder to Restore Backup Into"); if (!destFolder) return; const isZip = b.fileName.endsWith(".zip"); const targetPath = `${destFolder}/${b.fileName.replace(/^\d{8}T\d{6}Z_/, "").replace(/\.zip$/, "")}`; try { await api.restoreBackup(b.backupPath, targetPath, isZip); setToast(`Restored backup to ${targetPath}`); } catch (e) { setToast(`Restore failed: ${e}`); } }; return (

Saves, States & Backups

Inspect matched save files & save states, manually link Title ID folders, or restore automatic backups.

{toast && (
{toast}
)} {subTab === "local" ? ( <>
{(["all", "matched", "unmatched", "states"] as const).map((f) => ( ))}
{filtered.length === 0 ? (
No saves discovered matching this filter. Make sure at least one emulator rule is enabled in Emulators.
) : ( filtered.map((item) => (
{item.isDirectory ? ( ) : ( )} {item.fileName} {item.isState ? "State" : `Save • ${item.slot}`}
{item.emulatorName} • {formatBytes(item.sizeBytes)} • {item.localPath}
{item.matchedRomId ? (
{item.matchedRomName}
{item.matchedPlatform} • {item.matchSource} ( {Math.round(item.matchConfidence * 100)}%)
) : ( Unmatched )} {item.matchedRomId && ( )} {item.matchSource === "manual" && ( )}
)) )}
) : (
{backups.length === 0 ? (
No local backups yet. Backups are automatically created before any server save overwrites a local file.
) : ( backups.map((b) => (
{b.fileName}
ROM #{b.romId} • {formatBytes(b.sizeBytes)} •{" "} {new Date(b.createdAt).toLocaleString()}
)) )}
)} {/* Manual Link Modal */} {linkingItem && (

Link Save to RomM Game

{linkingItem.fileName}

{!linkingItem.isState && (
setSlotInput(e.target.value)} className="w-full px-3 py-1.5 bg-slate-950 border border-slate-800 rounded-lg text-sm" />
)}
setRomSearch(e.target.value)} placeholder="Search RomM games..." className="w-full pl-9 pr-3 py-2 bg-slate-950 border border-slate-800 rounded-lg text-sm" />
{romResults.map((rom) => ( ))}
)}
); };