import { create } from "zustand"; import { createInstance, listInstances, deleteInstance, getInstanceInfo, installInstance, type InstanceInfo, type InstanceRuntime, } from "../api/instance"; interface InstanceState { instances: InstanceInfo[]; currentInstance: InstanceInfo | null; loading: boolean; error: string | null; fetchInstances: (gamePath: string) => Promise; create: ( name: string, gamePath: string, runtime: InstanceRuntime, options?: { author?: string; description?: string; java?: string; minMemory?: number; maxMemory?: number; } ) => Promise; remove: (name: string, gamePath: string) => Promise; select: (name: string, gamePath: string) => Promise; install: (name: string, gamePath: string) => Promise; clearError: () => void; } export const useInstanceStore = create((set) => ({ instances: [], currentInstance: null, loading: false, error: null, fetchInstances: async (gamePath: string) => { set({ loading: true, error: null }); try { const instances = await listInstances(gamePath); set({ instances, loading: false }); } catch (e: any) { set({ error: e.message, loading: false }); } }, create: async (name, gamePath, runtime, options?) => { set({ loading: true, error: null }); try { const instance = await createInstance(name, gamePath, runtime, options); set((state) => ({ instances: [...state.instances, instance], loading: false, })); } catch (e: any) { set({ error: e.message, loading: false }); } }, remove: async (name: string, gamePath: string) => { set({ loading: true, error: null }); try { await deleteInstance(name, gamePath); set((state) => ({ instances: state.instances.filter((i) => i.name !== name), currentInstance: state.currentInstance?.name === name ? null : state.currentInstance, loading: false, })); } catch (e: any) { set({ error: e.message, loading: false }); } }, select: async (name: string, gamePath: string) => { set({ loading: true, error: null }); try { const instance = await getInstanceInfo(name, gamePath); set({ currentInstance: instance, loading: false }); } catch (e: any) { set({ error: e.message, loading: false }); } }, install: async (name: string, gamePath: string) => { set({ loading: true, error: null }); try { const { requestId } = await installInstance(name, gamePath); set({ loading: false }); return requestId; } catch (e: any) { set({ error: e.message, loading: false }); throw e; } }, clearError: () => set({ error: null }), }));