Files
cstb-next/src/store/steam.ts

105 lines
2.5 KiB
TypeScript

import type { SteamUser } from "@/types/steam"
import { store } from "@tauri-store/valtio"
import { DEFAULT_STORE_CONFIG } from "./config"
import { useSnapshot } from "valtio"
import { invoke } from "@tauri-apps/api/core"
const defaultValue = {
steamDir: "C:\\Program Files (x86)\\Steam",
cs2Dir: "",
steamDirValid: false,
cs2DirValid: false,
steamDirChecking: false,
cs2DirChecking: false,
users: [
{
steamID64: "76561198052315353",
steamID32: "STEAM_0:0:46157676",
accountName: "wrr",
personaName: "wrr",
recent: 0,
avatar: "",
},
] as SteamUser[],
}
export const steamStore = store(
"steam",
{ ...defaultValue },
DEFAULT_STORE_CONFIG,
)
export const useSteamStore = () => {
void steamStore.start
const state = useSnapshot(steamStore.state)
return {
state,
store: steamStore,
setDir,
setCsDir,
setUsers,
setSteamDirValid,
setCs2DirValid,
checkSteamDirValid,
checkCs2DirValid,
currentUser,
resetSteamStore,
}
}
const setDir = (dir: string) => {
steamStore.state.steamDir = dir
}
const setCsDir = (dir: string) => {
steamStore.state.cs2Dir = dir
}
const setUsers = (users: SteamUser[]) => {
steamStore.state.users = users
}
const setSteamDirValid = (valid: boolean) => {
steamStore.state.steamDirValid = valid
}
const setCs2DirValid = (valid: boolean) => {
steamStore.state.cs2DirValid = valid
}
const SetSteamDirChecking = (checking: boolean) => {
steamStore.state.steamDirChecking = checking
}
const SetCs2DirChecking = (checking: boolean) => {
steamStore.state.cs2DirChecking = checking
}
const checkSteamDirValid = async () => {
SetSteamDirChecking(true)
const pathExist = await invoke<boolean>("check_path", { path: steamStore.state.steamDir })
setSteamDirValid(pathExist)
setTimeout(() => {
SetSteamDirChecking(false)
}, 500)
}
const checkCs2DirValid = async () => {
SetCs2DirChecking(true)
const pathExist = await invoke<boolean>("check_path", { path: steamStore.state.cs2Dir })
setCs2DirValid(pathExist)
setTimeout(() => {
SetCs2DirChecking(false)
}, 500)
}
const currentUser = () => {
return steamStore.state.users[0] || defaultValue.users[0]
}
const resetSteamStore = () => {
setDir(defaultValue.steamDir)
setCsDir(defaultValue.cs2Dir)
setUsers(defaultValue.users)
setSteamDirValid(defaultValue.steamDirValid)
setCs2DirValid(defaultValue.cs2DirValid)
SetSteamDirChecking(defaultValue.steamDirChecking)
SetCs2DirChecking(defaultValue.cs2DirChecking)
}