2026-06-19 23:50:40 +08:00
|
|
|
import { create } from "zustand";
|
|
|
|
|
import {
|
|
|
|
|
createInstance,
|
|
|
|
|
listInstances,
|
|
|
|
|
deleteInstance,
|
|
|
|
|
getInstanceInfo,
|
2026-07-11 23:33:44 +08:00
|
|
|
installInstance,
|
|
|
|
|
type InstanceInfo,
|
|
|
|
|
type InstanceRuntime,
|
2026-06-19 23:50:40 +08:00
|
|
|
} from "../api/instance";
|
|
|
|
|
|
|
|
|
|
interface InstanceState {
|
|
|
|
|
instances: InstanceInfo[];
|
|
|
|
|
currentInstance: InstanceInfo | null;
|
|
|
|
|
loading: boolean;
|
|
|
|
|
error: string | null;
|
|
|
|
|
|
2026-07-11 23:33:44 +08:00
|
|
|
fetchInstances: (gamePath: string) => Promise<void>;
|
2026-06-19 23:50:40 +08:00
|
|
|
create: (
|
|
|
|
|
name: string,
|
|
|
|
|
gamePath: string,
|
2026-07-11 23:33:44 +08:00
|
|
|
runtime: InstanceRuntime,
|
|
|
|
|
options?: {
|
|
|
|
|
author?: string;
|
|
|
|
|
description?: string;
|
|
|
|
|
java?: string;
|
|
|
|
|
minMemory?: number;
|
|
|
|
|
maxMemory?: number;
|
|
|
|
|
}
|
2026-06-19 23:50:40 +08:00
|
|
|
) => Promise<void>;
|
2026-07-11 23:33:44 +08:00
|
|
|
remove: (name: string, gamePath: string) => Promise<void>;
|
|
|
|
|
select: (name: string, gamePath: string) => Promise<void>;
|
|
|
|
|
install: (name: string, gamePath: string) => Promise<string>;
|
2026-06-19 23:50:40 +08:00
|
|
|
clearError: () => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const useInstanceStore = create<InstanceState>((set) => ({
|
|
|
|
|
instances: [],
|
|
|
|
|
currentInstance: null,
|
|
|
|
|
loading: false,
|
|
|
|
|
error: null,
|
|
|
|
|
|
2026-07-11 23:33:44 +08:00
|
|
|
fetchInstances: async (gamePath: string) => {
|
2026-06-19 23:50:40 +08:00
|
|
|
set({ loading: true, error: null });
|
|
|
|
|
try {
|
2026-07-11 23:33:44 +08:00
|
|
|
const instances = await listInstances(gamePath);
|
2026-06-19 23:50:40 +08:00
|
|
|
set({ instances, loading: false });
|
|
|
|
|
} catch (e: any) {
|
|
|
|
|
set({ error: e.message, loading: false });
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2026-07-11 23:33:44 +08:00
|
|
|
create: async (name, gamePath, runtime, options?) => {
|
2026-06-19 23:50:40 +08:00
|
|
|
set({ loading: true, error: null });
|
|
|
|
|
try {
|
2026-07-11 23:33:44 +08:00
|
|
|
const instance = await createInstance(name, gamePath, runtime, options);
|
2026-06-19 23:50:40 +08:00
|
|
|
set((state) => ({
|
|
|
|
|
instances: [...state.instances, instance],
|
|
|
|
|
loading: false,
|
|
|
|
|
}));
|
|
|
|
|
} catch (e: any) {
|
|
|
|
|
set({ error: e.message, loading: false });
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2026-07-11 23:33:44 +08:00
|
|
|
remove: async (name: string, gamePath: string) => {
|
2026-06-19 23:50:40 +08:00
|
|
|
set({ loading: true, error: null });
|
|
|
|
|
try {
|
2026-07-11 23:33:44 +08:00
|
|
|
await deleteInstance(name, gamePath);
|
2026-06-19 23:50:40 +08:00
|
|
|
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 });
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2026-07-11 23:33:44 +08:00
|
|
|
select: async (name: string, gamePath: string) => {
|
2026-06-19 23:50:40 +08:00
|
|
|
set({ loading: true, error: null });
|
|
|
|
|
try {
|
2026-07-11 23:33:44 +08:00
|
|
|
const instance = await getInstanceInfo(name, gamePath);
|
2026-06-19 23:50:40 +08:00
|
|
|
set({ currentInstance: instance, loading: false });
|
|
|
|
|
} catch (e: any) {
|
|
|
|
|
set({ error: e.message, loading: false });
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2026-07-11 23:33:44 +08:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2026-06-19 23:50:40 +08:00
|
|
|
clearError: () => set({ error: null }),
|
|
|
|
|
}));
|