Files
STARTLINER/src/stores.ts
2025-03-05 00:40:59 +01:00

194 lines
5.4 KiB
TypeScript

import { Ref, computed, ref } from 'vue';
import { defineStore } from 'pinia';
import { listen } from '@tauri-apps/api/event';
import { open } from '@tauri-apps/plugin-dialog';
import { invoke } from './invoke';
import { Game, Package, Profile, ProfileMeta } from './types';
import { changePrimaryColor, pkgKey } from './util';
type InstallStatus = {
pkg: string;
};
export const usePkgStore = defineStore('pkg', {
state: (): { pkg: { [key: string]: Package } } => {
return {
pkg: {},
};
},
getters: {
fromDepString: (state) => (str: string) => state.pkg[str] ?? null,
fromName: (state) => (namespace: string, name: string) =>
state.pkg[`${namespace}-${name}`] ?? null,
all: (state) => Object.values(state),
allLocal: (state) => Object.values(state.pkg).filter((p) => p.loc),
allRemote: (state) => Object.values(state.pkg).filter((p) => p.rmt),
},
actions: {
setupListeners() {
listen<InstallStatus>('install-start', async (ev) => {
const key = ev.payload.pkg;
await this.reload(key);
this.pkg[key].js.busy = true;
});
listen<InstallStatus>('install-end', async (ev) => {
const key = ev.payload.pkg;
await this.reload(key);
this.pkg[key].js.busy = false;
});
},
async reloadAll() {
await invoke('reload_all_packages');
const data = (await invoke('get_all_packages')) as {
[key: string]: Package;
};
for (const [k, v] of Object.entries(data)) {
this.reloadWith(k, v);
}
},
async reload(pkgOrKey: string | Package) {
const key =
typeof pkgOrKey === 'string' ? pkgOrKey : pkgKey(pkgOrKey);
const pkg: Package = await invoke('get_package', {
key,
});
this.reloadWith(key, pkg);
},
async reloadWith(key: string, pkg: Package) {
if (this.pkg[key] === undefined) {
this.pkg[key] = { js: { busy: false } } as Package;
} else {
this.pkg[key].loc = null;
this.pkg[key].rmt = null;
}
Object.assign(this.pkg[key], pkg);
},
async fetch() {
await invoke('fetch_listings');
await this.reloadAll();
},
},
});
export const usePrfStore = defineStore('prf', () => {
const current: Ref<Profile | null> = ref(null);
const list: Ref<ProfileMeta[]> = ref([]);
const isPkgEnabled = (pkg: Package | undefined) =>
computed(
() =>
pkg !== undefined &&
current.value !== null &&
current.value?.data.mods.includes(pkgKey(pkg))
);
const reload = async () => {
current.value = await invoke('get_current_profile');
if (current.value !== null) {
changePrimaryColor(current.value.game);
}
};
const save = async () => {
await invoke('save_current_profile');
};
const cfg = <T extends string | boolean | number>(key: string, dflt: T) =>
computed({
get() {
return (current.value?.data.cfg[key] as T | undefined) ?? dflt;
},
async set(value) {
if (value !== undefined) {
await invoke('set_cfg', { key, value: value });
await reload();
await save();
}
},
});
const prompt = async () => {
const exePath = await open({
multiple: false,
directory: false,
filters: [
{
name: 'mu3.exe or chusanApp.exe',
extensions: ['exe'],
},
],
});
if (exePath !== null) {
await create(exePath);
}
};
const create = async (exePath: string) => {
try {
await invoke('init_profile', { exePath });
await reload();
await reloadList();
} catch (e) {
current.value = null;
}
if (current.value !== null) {
const pkgs = usePkgStore();
pkgs.reloadAll();
}
};
const switchTo = async (game: Game, name: string) => {
await invoke('load_profile', { game, name });
await reload();
if (current.value !== null) {
const pkgs = usePkgStore();
pkgs.reloadAll();
}
};
const reloadList = async () => {
const raw = (await invoke('list_profiles')) as [Game, string][];
list.value = raw.map(([game, name]) => {
return {
game,
name,
};
});
};
const togglePkg = async (pkg: Package | undefined, enable: boolean) => {
if (pkg === undefined) {
return;
}
await invoke('toggle_package', { key: pkgKey(pkg), enable });
await reload();
await save();
};
listen<InstallStatus>('install-end', async () => {
await reload();
});
return {
current,
list,
isPkgEnabled,
reload,
save,
cfg,
prompt,
create,
switchTo,
reloadList,
togglePkg,
};
});