Files
koring-launcher/src/stores/configStore.ts
T

152 lines
4.4 KiB
TypeScript
Raw Normal View History

2026-06-28 03:37:07 +08:00
import { create } from "zustand";
import {
getConfig,
saveConfig,
type AppConfig,
type ThemeConfig,
type A11yConfig,
type BackgroundConfig,
type GameConfig,
type JavaConfig,
type AdvancedConfig,
type DownloadConfig,
type NetworkConfig,
type InstanceMeta,
2026-06-28 03:37:07 +08:00
} from "@/api/config";
import { DEFAULT_BG } from "@/lib/mode";
let saveTimer: ReturnType<typeof setTimeout> | null = null;
function debouncedSave(config: AppConfig) {
if (saveTimer) clearTimeout(saveTimer);
saveTimer = setTimeout(() => {
saveConfig(config).catch((e) => {
console.error("[config] save failed:", e);
});
}, 300);
}
2026-06-28 03:37:07 +08:00
interface ConfigState {
config: AppConfig;
loaded: boolean;
isFirstLaunch: boolean;
2026-06-28 03:37:07 +08:00
init: () => Promise<void>;
applyPreloaded: (config: AppConfig, isFirstLaunch: boolean) => void;
2026-06-28 03:37:07 +08:00
setTheme: (patch: Partial<ThemeConfig>) => void;
setA11y: (patch: Partial<A11yConfig>) => void;
setBackground: (patch: Partial<BackgroundConfig>) => void;
setGame: (patch: Partial<GameConfig>) => void;
setJava: (patch: Partial<JavaConfig>) => void;
setAdvanced: (patch: Partial<AdvancedConfig>) => void;
setDownload: (patch: Partial<DownloadConfig>) => void;
setNetwork: (patch: Partial<NetworkConfig>) => void;
setInstances: (instances: InstanceMeta[]) => void;
setOobe: (value: boolean) => void;
2026-06-28 03:37:07 +08:00
}
const DEFAULT_CONFIG: AppConfig = {
version: 1,
oobe: true,
2026-06-28 03:37:07 +08:00
theme: { darkMode: "auto", parallax: true },
a11y: { reduceMotion: false, reduceTransparency: false, highContrast: false, contentBlurOpacity: 50 },
background: { bgType: "image", image: DEFAULT_BG, blur: 0, opacity: 100 },
2026-08-04 01:28:16 +08:00
game: { gameDir: ".minecraft", resourceDir: "", savesDir: "", instancesDir: ".minecraft/instances", gameDirs: [] },
2026-06-28 03:37:07 +08:00
java: { javaPath: "", memMode: "auto", memGB: 4, gc: "auto", jvmArgs: "" },
advanced: { afterLaunch: "close", winMode: "default", customWidth: 854, customHeight: 480, gameArgs: "", preLaunchCmd: "", debugMode: false },
download: { fileSource: "mirror", versionSource: "mirror", threads: 16, speedLimit: 0 },
network: { securityId: { enabled: false, authUrl: "" } },
instances: [],
2026-06-28 03:37:07 +08:00
};
export const useConfigStore = create<ConfigState>((set, get) => ({
config: DEFAULT_CONFIG,
loaded: false,
isFirstLaunch: false,
applyPreloaded: (config, isFirstLaunch) => {
set({ config, isFirstLaunch, loaded: true });
},
2026-06-28 03:37:07 +08:00
init: async () => {
// If already preloaded, skip IPC call
if (get().loaded) return;
try {
const config = await getConfig();
set({ config, loaded: true });
} catch (e) {
console.error("[config] init failed, using defaults:", e);
set({ loaded: true });
}
2026-06-28 03:37:07 +08:00
},
setTheme: (patch) => {
const { config } = get();
const next = { ...config, theme: { ...config.theme, ...patch } };
set({ config: next });
debouncedSave(next);
2026-06-28 03:37:07 +08:00
},
setA11y: (patch) => {
const { config } = get();
const next = { ...config, a11y: { ...config.a11y, ...patch } };
set({ config: next });
debouncedSave(next);
2026-06-28 03:37:07 +08:00
},
setBackground: (patch) => {
const { config } = get();
const next = { ...config, background: { ...config.background, ...patch } };
set({ config: next });
debouncedSave(next);
2026-06-28 03:37:07 +08:00
},
setGame: (patch) => {
const { config } = get();
const next = { ...config, game: { ...config.game, ...patch } };
set({ config: next });
debouncedSave(next);
2026-06-28 03:37:07 +08:00
},
setJava: (patch) => {
const { config } = get();
const next = { ...config, java: { ...config.java, ...patch } };
set({ config: next });
debouncedSave(next);
2026-06-28 03:37:07 +08:00
},
setAdvanced: (patch) => {
const { config } = get();
const next = { ...config, advanced: { ...config.advanced, ...patch } };
set({ config: next });
debouncedSave(next);
2026-06-28 03:37:07 +08:00
},
setDownload: (patch) => {
const { config } = get();
const next = { ...config, download: { ...config.download, ...patch } };
set({ config: next });
debouncedSave(next);
2026-06-28 03:37:07 +08:00
},
setNetwork: (patch) => {
const { config } = get();
const next = { ...config, network: { ...config.network, ...patch } };
set({ config: next });
debouncedSave(next);
},
setInstances: (instances) => {
const { config } = get();
const next = { ...config, instances };
set({ config: next });
debouncedSave(next);
},
setOobe: (value) => {
const { config } = get();
const next = { ...config, oobe: value };
set({ config: next });
debouncedSave(next);
2026-06-28 03:37:07 +08:00
},
}));