235 lines
7.8 KiB
TypeScript
235 lines
7.8 KiB
TypeScript
import React, { useCallback, useEffect, useState } from "react";
|
|
import { listen } from "@tauri-apps/api/event";
|
|
import {
|
|
FolderSync,
|
|
Gamepad2,
|
|
HardDrive,
|
|
LayoutDashboard,
|
|
Library,
|
|
Settings,
|
|
Usb,
|
|
} from "lucide-react";
|
|
import { api } from "./lib/tauri";
|
|
import type { AppStateSnapshot, DiscoveredSaveItem, SyncRunSummary } from "./lib/types";
|
|
import { OnboardingConnect } from "./components/OnboardingConnect";
|
|
import { Dashboard } from "./components/Dashboard";
|
|
import { EmulatorsView } from "./components/EmulatorsView";
|
|
import { LibraryView } from "./components/LibraryView";
|
|
import { SavesStatesView } from "./components/SavesStatesView";
|
|
import { HandheldView } from "./components/HandheldView";
|
|
import { SettingsView } from "./components/SettingsView";
|
|
|
|
type NavPage = "dashboard" | "emulators" | "handheld" | "library" | "saves" | "settings";
|
|
|
|
export default function App() {
|
|
const [appState, setAppState] = useState<AppStateSnapshot | null>(null);
|
|
const [page, setPage] = useState<NavPage>("dashboard");
|
|
const [discoveredSaves, setDiscoveredSaves] = useState<DiscoveredSaveItem[]>([]);
|
|
const [syncing, setSyncing] = useState(false);
|
|
const [lastSummary, setLastSummary] = useState<SyncRunSummary | null>(null);
|
|
|
|
const refreshAll = useCallback(async () => {
|
|
const snap = await api.getAppState();
|
|
setAppState(snap);
|
|
if (snap.connected) {
|
|
const saves = await api.discoverLocalSaves();
|
|
setDiscoveredSaves(saves);
|
|
}
|
|
}, []);
|
|
|
|
const handleSyncNow = useCallback(async () => {
|
|
setSyncing(true);
|
|
try {
|
|
const summary = await api.runFullSync();
|
|
setLastSummary(summary);
|
|
await refreshAll();
|
|
} catch (e) {
|
|
console.error("Sync failed:", e);
|
|
} finally {
|
|
setSyncing(false);
|
|
}
|
|
}, [refreshAll]);
|
|
|
|
useEffect(() => {
|
|
refreshAll();
|
|
}, [refreshAll]);
|
|
|
|
// Listen to file watcher & system tray events from Rust backend
|
|
useEffect(() => {
|
|
const unlistenWatcher = listen<string[]>("watcher:change", async () => {
|
|
const snap = await api.getAppState();
|
|
setAppState(snap);
|
|
if (snap.connected && snap.settings.autoSyncOnChange) {
|
|
await handleSyncNow();
|
|
} else if (snap.connected) {
|
|
const saves = await api.discoverLocalSaves();
|
|
setDiscoveredSaves(saves);
|
|
}
|
|
});
|
|
|
|
const unlistenTray = listen("tray:sync-now", async () => {
|
|
await handleSyncNow();
|
|
});
|
|
|
|
return () => {
|
|
unlistenWatcher.then((u) => u());
|
|
unlistenTray.then((u) => u());
|
|
};
|
|
}, [handleSyncNow]);
|
|
|
|
const handleConnected = async () => {
|
|
await api.refreshRomCache().catch(() => {});
|
|
await refreshAll();
|
|
};
|
|
|
|
const handleAutoDetect = async () => {
|
|
await api.autoPopulateDetectedEmulators();
|
|
await refreshAll();
|
|
};
|
|
|
|
const handleResolveConflict = async (
|
|
id: string,
|
|
resolution: "keep_local" | "keep_server"
|
|
) => {
|
|
await api.resolveSyncConflict(id, resolution);
|
|
await refreshAll();
|
|
};
|
|
|
|
const handleClearLogs = async () => {
|
|
await api.clearSyncLogs();
|
|
await refreshAll();
|
|
};
|
|
|
|
const handleDisconnect = async () => {
|
|
const snap = await api.disconnect();
|
|
setAppState(snap);
|
|
setDiscoveredSaves([]);
|
|
setLastSummary(null);
|
|
};
|
|
|
|
if (!appState) {
|
|
return (
|
|
<div className="min-h-screen flex items-center justify-center bg-slate-950 text-slate-400 text-sm">
|
|
Loading RomM Companion...
|
|
</div>
|
|
);
|
|
}
|
|
|
|
if (!appState.connected) {
|
|
return (
|
|
<OnboardingConnect
|
|
initialServerUrl={appState.serverUrl}
|
|
initialDeviceName={appState.deviceName}
|
|
onConnected={handleConnected}
|
|
/>
|
|
);
|
|
}
|
|
|
|
const navItems: { id: NavPage; label: string; icon: React.ComponentType<{ className?: string }> }[] =
|
|
[
|
|
{ id: "dashboard", label: "Dashboard", icon: LayoutDashboard },
|
|
{ id: "emulators", label: "Emulators", icon: HardDrive },
|
|
{ id: "handheld", label: "NextUI USB / SD", icon: Usb },
|
|
{ id: "saves", label: "Saves & States", icon: FolderSync },
|
|
{ id: "library", label: "ROM Library", icon: Library },
|
|
{ id: "settings", label: "Settings", icon: Settings },
|
|
];
|
|
|
|
return (
|
|
<div className="min-h-screen flex bg-slate-950 text-slate-100">
|
|
{/* Sidebar */}
|
|
<aside className="w-60 shrink-0 border-r border-slate-800/90 bg-slate-900/50 p-4 flex flex-col justify-between">
|
|
<div className="space-y-6">
|
|
<div className="flex items-center gap-2.5 px-2">
|
|
<div className="w-9 h-9 rounded-xl bg-indigo-600/20 border border-indigo-500/40 flex items-center justify-center text-indigo-400">
|
|
<Gamepad2 className="w-5 h-5" />
|
|
</div>
|
|
<div className="min-w-0">
|
|
<div className="font-bold text-sm tracking-tight">RomM Companion</div>
|
|
<div className="text-[11px] text-slate-500 truncate">RomM 5.1.0 Sync</div>
|
|
</div>
|
|
</div>
|
|
|
|
<nav className="space-y-1">
|
|
{navItems.map((item) => {
|
|
const Icon = item.icon;
|
|
const active = page === item.id;
|
|
return (
|
|
<button
|
|
key={item.id}
|
|
onClick={() => setPage(item.id)}
|
|
className={`w-full flex items-center gap-2.5 px-3 py-2 rounded-lg text-sm font-medium transition cursor-pointer ${
|
|
active
|
|
? "bg-indigo-600 text-white shadow-sm"
|
|
: "text-slate-400 hover:text-slate-100 hover:bg-slate-800/60"
|
|
}`}
|
|
>
|
|
<Icon className="w-4 h-4" />
|
|
<span>{item.label}</span>
|
|
{item.id === "dashboard" && appState.conflicts.length > 0 && (
|
|
<span className="ml-auto text-[10px] px-1.5 py-0.5 rounded-full bg-amber-500 text-slate-950 font-bold">
|
|
{appState.conflicts.length}
|
|
</span>
|
|
)}
|
|
</button>
|
|
);
|
|
})}
|
|
</nav>
|
|
</div>
|
|
|
|
<div className="p-3 rounded-xl bg-slate-900 border border-slate-800 text-xs space-y-1">
|
|
<div className="flex items-center gap-1.5">
|
|
<span
|
|
className={`w-2 h-2 rounded-full ${
|
|
appState.watcherActive ? "bg-emerald-400" : "bg-slate-500"
|
|
}`}
|
|
/>
|
|
<span className="font-medium text-slate-200 truncate">
|
|
{appState.username || "Connected"}
|
|
</span>
|
|
</div>
|
|
<div className="text-[11px] text-slate-500 truncate">{appState.serverUrl}</div>
|
|
</div>
|
|
</aside>
|
|
|
|
{/* Main Content */}
|
|
<main className="flex-1 p-7 overflow-y-auto">
|
|
{page === "dashboard" && (
|
|
<Dashboard
|
|
appState={appState}
|
|
discoveredSaves={discoveredSaves}
|
|
syncing={syncing}
|
|
lastSummary={lastSummary}
|
|
onSyncNow={handleSyncNow}
|
|
onResolveConflict={handleResolveConflict}
|
|
onClearLogs={handleClearLogs}
|
|
onAutoDetect={handleAutoDetect}
|
|
/>
|
|
)}
|
|
{page === "emulators" && (
|
|
<EmulatorsView emulators={appState.emulators} onRefreshState={refreshAll} />
|
|
)}
|
|
{page === "handheld" && <HandheldView onRefreshState={refreshAll} />}
|
|
{page === "saves" && (
|
|
<SavesStatesView discoveredSaves={discoveredSaves} onRefreshSaves={refreshAll} />
|
|
)}
|
|
{page === "library" && (
|
|
<LibraryView
|
|
defaultDownloadDir={appState.settings.defaultRomDownloadDir}
|
|
emulators={appState.emulators}
|
|
discoveredSaves={discoveredSaves}
|
|
onRefreshAll={refreshAll}
|
|
/>
|
|
)}
|
|
{page === "settings" && (
|
|
<SettingsView
|
|
appState={appState}
|
|
onRefreshState={refreshAll}
|
|
onDisconnect={handleDisconnect}
|
|
/>
|
|
)}
|
|
</main>
|
|
</div>
|
|
);
|
|
}
|