383 lines
15 KiB
TypeScript
383 lines
15 KiB
TypeScript
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<void>;
|
|
}
|
|
|
|
export const SavesStatesView: React.FC<Props> = ({ discoveredSaves, onRefreshSaves }) => {
|
|
const [subTab, setSubTab] = useState<"local" | "backups">("local");
|
|
const [filter, setFilter] = useState<"all" | "matched" | "unmatched" | "states">("all");
|
|
const [linkingItem, setLinkingItem] = useState<DiscoveredSaveItem | null>(null);
|
|
const [romSearch, setRomSearch] = useState("");
|
|
const [romResults, setRomResults] = useState<SimpleRomSchema[]>([]);
|
|
const [slotInput, setSlotInput] = useState("default");
|
|
|
|
const [backups, setBackups] = useState<BackupEntry[]>([]);
|
|
const [toast, setToast] = useState<string | null>(null);
|
|
const [uploadingPath, setUploadingPath] = useState<string | null>(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 (
|
|
<div className="space-y-5">
|
|
<div className="flex flex-wrap items-center justify-between gap-4">
|
|
<div>
|
|
<h2 className="text-2xl font-bold tracking-tight">Saves, States & Backups</h2>
|
|
<p className="text-sm text-slate-400">
|
|
Inspect matched save files & save states, manually link Title ID folders, or restore
|
|
automatic backups.
|
|
</p>
|
|
</div>
|
|
|
|
<div className="flex items-center gap-2">
|
|
<div className="flex gap-1 p-1 bg-slate-900 border border-slate-800 rounded-xl">
|
|
<button
|
|
onClick={() => setSubTab("local")}
|
|
className={`px-3 py-1.5 rounded-lg text-xs font-medium cursor-pointer ${
|
|
subTab === "local" ? "bg-indigo-600 text-white" : "text-slate-400 hover:text-white"
|
|
}`}
|
|
>
|
|
Local Saves ({discoveredSaves.length})
|
|
</button>
|
|
<button
|
|
onClick={() => setSubTab("backups")}
|
|
className={`px-3 py-1.5 rounded-lg text-xs font-medium flex items-center gap-1 cursor-pointer ${
|
|
subTab === "backups"
|
|
? "bg-indigo-600 text-white"
|
|
: "text-slate-400 hover:text-white"
|
|
}`}
|
|
>
|
|
<History className="w-3.5 h-3.5" /> Backups
|
|
</button>
|
|
</div>
|
|
<button
|
|
onClick={onRefreshSaves}
|
|
className="p-2 rounded-lg bg-slate-800 hover:bg-slate-700 border border-slate-700 cursor-pointer"
|
|
title="Rescan local save folders"
|
|
>
|
|
<RefreshCw className="w-4 h-4" />
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
{toast && (
|
|
<div className="p-3 rounded-lg bg-slate-900 border border-indigo-500/40 text-xs text-indigo-200">
|
|
{toast}
|
|
</div>
|
|
)}
|
|
|
|
{subTab === "local" ? (
|
|
<>
|
|
<div className="flex flex-wrap gap-2">
|
|
{(["all", "matched", "unmatched", "states"] as const).map((f) => (
|
|
<button
|
|
key={f}
|
|
onClick={() => setFilter(f)}
|
|
className={`px-3 py-1.5 rounded-lg text-xs font-medium capitalize cursor-pointer border ${
|
|
filter === f
|
|
? "bg-indigo-600/20 border-indigo-500/50 text-indigo-300"
|
|
: "bg-slate-900 border-slate-800 text-slate-400 hover:text-slate-200"
|
|
}`}
|
|
>
|
|
{f}
|
|
</button>
|
|
))}
|
|
</div>
|
|
|
|
<div className="rounded-xl bg-slate-900/80 border border-slate-800 divide-y divide-slate-800/80">
|
|
{filtered.length === 0 ? (
|
|
<div className="p-8 text-center text-sm text-slate-500">
|
|
No saves discovered matching this filter. Make sure at least one emulator rule is
|
|
enabled in <strong>Emulators</strong>.
|
|
</div>
|
|
) : (
|
|
filtered.map((item) => (
|
|
<div
|
|
key={item.localPath}
|
|
className="px-4 py-3 flex flex-wrap items-center justify-between gap-4"
|
|
>
|
|
<div className="min-w-0 space-y-1">
|
|
<div className="flex items-center gap-2">
|
|
{item.isDirectory ? (
|
|
<Folder className="w-4 h-4 text-amber-400 shrink-0" />
|
|
) : (
|
|
<Archive className="w-4 h-4 text-indigo-400 shrink-0" />
|
|
)}
|
|
<span className="font-medium text-sm truncate">{item.fileName}</span>
|
|
<span
|
|
className={`text-[10px] uppercase px-1.5 py-0.5 rounded font-medium ${
|
|
item.isState
|
|
? "bg-purple-950 text-purple-300"
|
|
: "bg-slate-800 text-slate-300"
|
|
}`}
|
|
>
|
|
{item.isState ? "State" : `Save • ${item.slot}`}
|
|
</span>
|
|
</div>
|
|
<div className="text-xs text-slate-500 font-mono truncate max-w-2xl">
|
|
{item.emulatorName} • {formatBytes(item.sizeBytes)} • {item.localPath}
|
|
</div>
|
|
</div>
|
|
|
|
<div className="flex items-center gap-2.5 shrink-0">
|
|
{item.matchedRomId ? (
|
|
<div className="text-right">
|
|
<div className="text-xs font-medium text-emerald-400 flex items-center gap-1 justify-end">
|
|
<CheckCircle2 className="w-3.5 h-3.5" />
|
|
{item.matchedRomName}
|
|
</div>
|
|
<div className="text-[11px] text-slate-500">
|
|
{item.matchedPlatform} • {item.matchSource} (
|
|
{Math.round(item.matchConfidence * 100)}%)
|
|
</div>
|
|
</div>
|
|
) : (
|
|
<span className="text-xs text-amber-400">Unmatched</span>
|
|
)}
|
|
|
|
{item.matchedRomId && (
|
|
<button
|
|
onClick={() => handleUploadSingle(item)}
|
|
disabled={uploadingPath !== null}
|
|
className="px-2.5 py-1.5 rounded-lg bg-indigo-600/20 hover:bg-indigo-600 text-indigo-200 hover:text-white border border-indigo-500/30 text-xs font-medium flex items-center gap-1 cursor-pointer disabled:opacity-50 transition"
|
|
title="Push this save directly to RomM"
|
|
>
|
|
{uploadingPath === item.localPath ? (
|
|
<Loader2 className="w-3.5 h-3.5 animate-spin" />
|
|
) : (
|
|
<CloudUpload className="w-3.5 h-3.5" />
|
|
)}
|
|
Push
|
|
</button>
|
|
)}
|
|
|
|
<button
|
|
onClick={() => openLinker(item)}
|
|
className="px-2.5 py-1.5 rounded-lg bg-slate-800 hover:bg-slate-700 text-xs font-medium flex items-center gap-1 cursor-pointer"
|
|
>
|
|
<Link2 className="w-3.5 h-3.5" /> {item.matchedRomId ? "Relink" : "Link ROM"}
|
|
</button>
|
|
{item.matchSource === "manual" && (
|
|
<button
|
|
onClick={() => handleUnlink(item.localPath)}
|
|
className="p-1.5 rounded-lg bg-slate-800 hover:bg-rose-950 text-slate-400 hover:text-rose-300 cursor-pointer"
|
|
title="Remove manual link"
|
|
>
|
|
<Unlink className="w-3.5 h-3.5" />
|
|
</button>
|
|
)}
|
|
</div>
|
|
</div>
|
|
))
|
|
)}
|
|
</div>
|
|
</>
|
|
) : (
|
|
<div className="rounded-xl bg-slate-900/80 border border-slate-800 divide-y divide-slate-800/80">
|
|
{backups.length === 0 ? (
|
|
<div className="p-8 text-center text-sm text-slate-500">
|
|
No local backups yet. Backups are automatically created before any server save
|
|
overwrites a local file.
|
|
</div>
|
|
) : (
|
|
backups.map((b) => (
|
|
<div
|
|
key={b.backupPath}
|
|
className="px-4 py-3 flex items-center justify-between gap-4"
|
|
>
|
|
<div>
|
|
<div className="text-sm font-medium">{b.fileName}</div>
|
|
<div className="text-xs text-slate-500">
|
|
ROM #{b.romId} • {formatBytes(b.sizeBytes)} •{" "}
|
|
{new Date(b.createdAt).toLocaleString()}
|
|
</div>
|
|
</div>
|
|
<button
|
|
onClick={() => handleRestoreBackup(b)}
|
|
className="px-3 py-1.5 rounded-lg bg-slate-800 hover:bg-indigo-600 text-xs font-medium cursor-pointer transition"
|
|
>
|
|
Restore to Folder
|
|
</button>
|
|
</div>
|
|
))
|
|
)}
|
|
</div>
|
|
)}
|
|
|
|
{/* Manual Link Modal */}
|
|
{linkingItem && (
|
|
<div className="fixed inset-0 z-50 bg-black/70 backdrop-blur-sm flex items-center justify-center p-4">
|
|
<div className="w-full max-w-lg bg-slate-900 border border-slate-800 rounded-2xl p-6 space-y-4 shadow-2xl">
|
|
<div className="flex items-center justify-between">
|
|
<div>
|
|
<h3 className="text-base font-semibold">Link Save to RomM Game</h3>
|
|
<p className="text-xs text-slate-400 font-mono truncate max-w-md">
|
|
{linkingItem.fileName}
|
|
</p>
|
|
</div>
|
|
<button
|
|
onClick={() => setLinkingItem(null)}
|
|
className="text-slate-400 hover:text-white cursor-pointer"
|
|
>
|
|
<X className="w-5 h-5" />
|
|
</button>
|
|
</div>
|
|
|
|
{!linkingItem.isState && (
|
|
<div>
|
|
<label className="block text-xs text-slate-400 mb-1">
|
|
RomM Save Slot Name (kept consistent across syncs)
|
|
</label>
|
|
<input
|
|
value={slotInput}
|
|
onChange={(e) => setSlotInput(e.target.value)}
|
|
className="w-full px-3 py-1.5 bg-slate-950 border border-slate-800 rounded-lg text-sm"
|
|
/>
|
|
</div>
|
|
)}
|
|
|
|
<form onSubmit={searchRomsForLink} className="flex gap-2">
|
|
<div className="relative flex-1">
|
|
<Search className="w-4 h-4 text-slate-500 absolute left-3 top-1/2 -translate-y-1/2" />
|
|
<input
|
|
value={romSearch}
|
|
onChange={(e) => 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"
|
|
/>
|
|
</div>
|
|
<button
|
|
type="submit"
|
|
className="px-3.5 py-2 rounded-lg bg-indigo-600 hover:bg-indigo-500 text-xs font-medium cursor-pointer"
|
|
>
|
|
Search
|
|
</button>
|
|
</form>
|
|
|
|
<div className="max-h-64 overflow-y-auto divide-y divide-slate-800 border border-slate-800 rounded-xl">
|
|
{romResults.map((rom) => (
|
|
<button
|
|
key={rom.id}
|
|
onClick={() => handleLink(rom)}
|
|
className="w-full text-left px-3.5 py-2.5 hover:bg-slate-800/70 flex items-center justify-between gap-2 cursor-pointer"
|
|
>
|
|
<div className="min-w-0">
|
|
<div className="text-sm font-medium truncate">
|
|
{rom.name || rom.fs_name_no_ext}
|
|
</div>
|
|
<div className="text-xs text-slate-500 truncate">{rom.fs_name}</div>
|
|
</div>
|
|
<span className="text-[11px] px-2 py-0.5 rounded bg-indigo-950 text-indigo-300 shrink-0">
|
|
{rom.platform_display_name}
|
|
</span>
|
|
</button>
|
|
))}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
};
|