[feat] better store operation

This commit is contained in:
2025-03-17 11:48:30 +08:00
parent 47dc77732f
commit db3cf9aef2
15 changed files with 169 additions and 127 deletions

View File

@@ -1,6 +1,7 @@
import type { SteamUser } from "@/types/steam"
import { store } from "tauri-plugin-valtio"
import { DEFAULT_STORE_CONFIG } from "."
import { useSnapshot } from "valtio"
const defaultValue = {
dir: "C:\\Program Files (x86)\\Steam",
@@ -18,33 +19,50 @@ const defaultValue = {
isDirValid: false,
isCsDirValid: false,
}
export const steamStore = store(
"steam",
{ ...defaultValue },
DEFAULT_STORE_CONFIG,
)
export const setDir = (dir: string) => {
export const useSteamStore = () => {
void steamStore.start
const state = useSnapshot(steamStore.state)
return {
state,
setDir,
setCsDir,
setUsers,
setIsDirValid,
setIsCsDirValid,
currentUser,
resetSteamStore,
}
}
const setDir = (dir: string) => {
steamStore.state.dir = dir
}
export const setCsDir = (dir: string) => {
const setCsDir = (dir: string) => {
steamStore.state.csDir = dir
}
export const setUsers = (users: SteamUser[]) => {
const setUsers = (users: SteamUser[]) => {
steamStore.state.users = users
}
export const setIsDirValid = (valid: boolean) => {
const setIsDirValid = (valid: boolean) => {
steamStore.state.isDirValid = valid
}
export const setIsCsDirValid = (valid: boolean) => {
const setIsCsDirValid = (valid: boolean) => {
steamStore.state.isCsDirValid = valid
}
export const currentUser = () => {
const currentUser = () => {
return steamStore.state.users[0] || defaultValue.users[0]
}
export const resetSteamStore = () => {
const resetSteamStore = () => {
setDir(defaultValue.dir)
setCsDir(defaultValue.csDir)
setUsers(defaultValue.users)