diff options
Diffstat (limited to 'packages')
| -rw-r--r-- | packages/ui/src/components/instance-creation-modal.tsx | 70 | ||||
| -rw-r--r-- | packages/ui/src/components/instance-editor-modal.tsx | 4 | ||||
| -rw-r--r-- | packages/ui/src/models/instance.ts (renamed from packages/ui/src/models/instances.ts) | 18 | ||||
| -rw-r--r-- | packages/ui/src/pages/index.tsx | 5 | ||||
| -rw-r--r-- | packages/ui/src/pages/instances-view.tsx | 4 |
5 files changed, 46 insertions, 55 deletions
diff --git a/packages/ui/src/components/instance-creation-modal.tsx b/packages/ui/src/components/instance-creation-modal.tsx index 8a2b1b4..7c46d0f 100644 --- a/packages/ui/src/components/instance-creation-modal.tsx +++ b/packages/ui/src/components/instance-creation-modal.tsx @@ -1,7 +1,13 @@ -import { invoke } from "@tauri-apps/api/core"; import { Loader2, Search } from "lucide-react"; import { useCallback, useEffect, useMemo, useState } from "react"; import { toast } from "sonner"; +import { + getFabricLoadersForVersion, + getForgeVersionsForGame, + installFabric, + installForge, + installVersion, +} from "@/client"; import { Button } from "@/components/ui/button"; import { Dialog, @@ -13,12 +19,13 @@ import { } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { ScrollArea } from "@/components/ui/scroll-area"; -import { useInstancesStore } from "@/models/instances"; +import { useInstanceStore } from "@/models/instance"; import { useGameStore } from "@/stores/game-store"; -import type { Version } from "@/types/bindings/manifest"; -import type { FabricLoaderEntry } from "../types/bindings/fabric"; -import type { ForgeVersion as ForgeVersionEntry } from "../types/bindings/forge"; -import type { Instance } from "../types/bindings/instance"; +import type { + FabricLoaderEntry, + ForgeVersion as ForgeVersionEntry, + Version, +} from "@/types"; interface Props { open: boolean; @@ -27,7 +34,7 @@ interface Props { export function InstanceCreationModal({ open, onOpenChange }: Props) { const gameStore = useGameStore(); - const instancesStore = useInstancesStore(); + const instancesStore = useInstanceStore(); // Steps: 1 = name, 2 = version, 3 = mod loader const [step, setStep] = useState<number>(1); @@ -61,12 +68,7 @@ export function InstanceCreationModal({ open, onOpenChange }: Props) { setForgeVersions([]); try { if (modLoaderType === "fabric") { - const loaders = await invoke<FabricLoaderEntry[]>( - "get_fabric_loaders_for_version", - { - gameVersion: selectedVersionUI.id, - }, - ); + const loaders = await getFabricLoadersForVersion(selectedVersionUI.id); setFabricLoaders(loaders || []); if (loaders && loaders.length > 0) { setSelectedFabricLoader(loaders[0].loader.version); @@ -74,12 +76,7 @@ export function InstanceCreationModal({ open, onOpenChange }: Props) { setSelectedFabricLoader(""); } } else if (modLoaderType === "forge") { - const versions = await invoke<ForgeVersionEntry[]>( - "get_forge_versions_for_game", - { - gameVersion: selectedVersionUI.id, - }, - ); + const versions = await getForgeVersionsForGame(selectedVersionUI.id); setForgeVersions(versions || []); if (versions && versions.length > 0) { // Binding `ForgeVersion` uses `version` (not `id`) — use `.version` here. @@ -182,17 +179,12 @@ export function InstanceCreationModal({ open, onOpenChange }: Props) { try { // Step 1: create instance - const instance = await invoke<Instance>("create_instance", { - name: instanceName.trim(), - }); + const instance = await instancesStore.create(instanceName.trim()); // If selectedVersion provided, install it - if (selectedVersionUI) { + if (selectedVersionUI && instance) { try { - await invoke("install_version", { - instanceId: instance.id, - versionId: selectedVersionUI.id, - }); + await installVersion(instance?.id, selectedVersionUI.id); } catch (err) { console.error("Failed to install base version:", err); // continue - instance created but version install failed @@ -203,24 +195,24 @@ export function InstanceCreationModal({ open, onOpenChange }: Props) { } // If mod loader selected, install it - if (modLoaderType === "fabric" && selectedFabricLoader) { + if (modLoaderType === "fabric" && selectedFabricLoader && instance) { try { - await invoke("install_fabric", { - instanceId: instance.id, - gameVersion: selectedVersionUI?.id ?? "", - loaderVersion: selectedFabricLoader, - }); + await installFabric( + instance?.id, + selectedVersionUI?.id ?? "", + selectedFabricLoader, + ); } catch (err) { console.error("Failed to install Fabric:", err); toast.error(`Failed to install Fabric: ${String(err)}`); } - } else if (modLoaderType === "forge" && selectedForgeLoader) { + } else if (modLoaderType === "forge" && selectedForgeLoader && instance) { try { - await invoke("install_forge", { - instanceId: instance.id, - gameVersion: selectedVersionUI?.id ?? "", - installerVersion: selectedForgeLoader, - }); + await installForge( + instance?.id, + selectedVersionUI?.id ?? "", + selectedForgeLoader, + ); } catch (err) { console.error("Failed to install Forge:", err); toast.error(`Failed to install Forge: ${String(err)}`); diff --git a/packages/ui/src/components/instance-editor-modal.tsx b/packages/ui/src/components/instance-editor-modal.tsx index f880c20..d964185 100644 --- a/packages/ui/src/components/instance-editor-modal.tsx +++ b/packages/ui/src/components/instance-editor-modal.tsx @@ -16,7 +16,7 @@ import { Input } from "@/components/ui/input"; import { Textarea } from "@/components/ui/textarea"; import { toNumber } from "@/lib/tsrs-utils"; -import { useInstancesStore } from "@/models/instances"; +import { useInstanceStore } from "@/models/instance"; import { useSettingsStore } from "@/models/settings"; import type { FileInfo } from "../types/bindings/core"; import type { Instance } from "../types/bindings/instance"; @@ -28,7 +28,7 @@ type Props = { }; export function InstanceEditorModal({ open, instance, onOpenChange }: Props) { - const instancesStore = useInstancesStore(); + const instancesStore = useInstanceStore(); const { config } = useSettingsStore(); const [activeTab, setActiveTab] = useState< diff --git a/packages/ui/src/models/instances.ts b/packages/ui/src/models/instance.ts index f434c7c..a3fda3d 100644 --- a/packages/ui/src/models/instances.ts +++ b/packages/ui/src/models/instance.ts @@ -12,39 +12,35 @@ import { } from "@/client"; import type { Instance } from "@/types"; -interface InstancesState { - // State +interface InstanceState { instances: Instance[]; activeInstance: Instance | null; - // Actions refresh: () => Promise<void>; create: (name: string) => Promise<Instance | null>; delete: (id: string) => Promise<void>; update: (instance: Instance) => Promise<void>; setActiveInstance: (instance: Instance) => Promise<void>; duplicate: (id: string, newName: string) => Promise<Instance | null>; - getInstance: (id: string) => Promise<Instance | null>; + get: (id: string) => Promise<Instance | null>; } -export const useInstancesStore = create<InstancesState>((set, get) => ({ - // Initial state +export const useInstanceStore = create<InstanceState>((set, get) => ({ instances: [], activeInstance: null, - // Actions refresh: async () => { const { setActiveInstance } = get(); try { const instances = await listInstances(); - const active = await getActiveInstance(); + const activeInstance = await getActiveInstance(); - if (!active && instances.length > 0) { + if (!activeInstance && instances.length > 0) { // If no active instance but instances exist, set the first one as active await setActiveInstance(instances[0]); } - set({ instances }); + set({ instances, activeInstance }); } catch (e) { console.error("Failed to load instances:", e); toast.error("Error loading instances"); @@ -124,7 +120,7 @@ export const useInstancesStore = create<InstancesState>((set, get) => ({ } }, - getInstance: async (id) => { + get: async (id) => { try { return await getInstance(id); } catch (e) { diff --git a/packages/ui/src/pages/index.tsx b/packages/ui/src/pages/index.tsx index 54cfc1e..093ccb2 100644 --- a/packages/ui/src/pages/index.tsx +++ b/packages/ui/src/pages/index.tsx @@ -3,18 +3,21 @@ import { Outlet, useLocation } from "react-router"; import { ParticleBackground } from "@/components/particle-background"; import { Sidebar } from "@/components/sidebar"; import { useAuthStore } from "@/models/auth"; +import { useInstanceStore } from "@/models/instance"; import { useSettingsStore } from "@/models/settings"; export function IndexPage() { const authStore = useAuthStore(); const settingsStore = useSettingsStore(); + const instanceStore = useInstanceStore(); const location = useLocation(); useEffect(() => { authStore.init(); settingsStore.refresh(); - }, [authStore.init, settingsStore.refresh]); + instanceStore.refresh(); + }, [authStore.init, settingsStore.refresh, instanceStore.refresh]); return ( <div className="relative h-screen w-full overflow-hidden bg-background font-sans"> diff --git a/packages/ui/src/pages/instances-view.tsx b/packages/ui/src/pages/instances-view.tsx index ad6bd38..1634905 100644 --- a/packages/ui/src/pages/instances-view.tsx +++ b/packages/ui/src/pages/instances-view.tsx @@ -13,11 +13,11 @@ import { } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { toNumber } from "@/lib/tsrs-utils"; -import { useInstancesStore } from "@/models/instances"; +import { useInstanceStore } from "@/models/instance"; import type { Instance } from "../types/bindings/instance"; export function InstancesView() { - const instancesStore = useInstancesStore(); + const instancesStore = useInstanceStore(); // Modal / UI state const [showCreateModal, setShowCreateModal] = useState(false); |