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

89 lines
2.2 KiB
TypeScript
Raw Normal View History

2024-11-11 10:04:00 +08:00
import type { SteamUser } from "@/types/steam"
import { store } from "@tauri-store/valtio"
import { DEFAULT_STORE_CONFIG } from "./config"
2025-03-17 11:48:30 +08:00
import { useSnapshot } from "valtio"
import { useEffect } from "react"
import { addToast } from "@heroui/react"
import { invoke } from "@tauri-apps/api/core"
import { dir } from "console"
const defaultValue = {
steamDir: "C:\\Program Files (x86)\\Steam",
cs2Dir: "",
2025-03-12 22:20:06 +08:00
users: [
{
steamID64: "76561198052315353",
steamID32: "STEAM_0:0:46157676",
accountName: "wrr",
personaName: "wrr",
recent: 0,
avatar: "",
},
] as SteamUser[],
steamDirValid: false,
cs2DirValid: false,
}
2025-03-17 11:48:30 +08:00
2025-03-12 22:20:06 +08:00
export const steamStore = store(
"steam",
{ ...defaultValue },
DEFAULT_STORE_CONFIG,
)
2025-03-17 11:48:30 +08:00
export const useSteamStore = () => {
void steamStore.start
const state = useSnapshot(steamStore.state)
return {
state,
setDir,
setCsDir,
setUsers,
setSteamDirValid,
setCs2DirValid,
checkSteamDirValid,
checkCs2DirValid,
2025-03-17 11:48:30 +08:00
currentUser,
resetSteamStore,
}
}
const setDir = (dir: string) => {
steamStore.state.steamDir = dir
2025-03-12 22:20:06 +08:00
}
2025-03-17 11:48:30 +08:00
const setCsDir = (dir: string) => {
steamStore.state.cs2Dir = dir
2025-03-12 22:20:06 +08:00
}
2025-03-17 11:48:30 +08:00
const setUsers = (users: SteamUser[]) => {
2025-03-12 22:20:06 +08:00
steamStore.state.users = users
}
const setSteamDirValid = (valid: boolean) => {
steamStore.state.steamDirValid = valid
2025-03-12 22:20:06 +08:00
}
const setCs2DirValid = (valid: boolean) => {
steamStore.state.cs2DirValid = valid
}
const checkSteamDirValid = async () => {
const pathExist = await invoke<boolean>("check_path", { path: steamStore.state.steamDir })
console.log("steamDir", steamStore.state.steamDir, "pathExist", pathExist)
setSteamDirValid(pathExist)
}
const checkCs2DirValid = async () => {
const pathExist = await invoke<boolean>("check_path", { path: steamStore.state.cs2Dir })
console.log("cs2Dir", steamStore.state.cs2Dir, "pathExist", pathExist)
setCs2DirValid(pathExist)
2025-03-12 22:20:06 +08:00
}
2025-03-17 11:48:30 +08:00
const currentUser = () => {
return steamStore.state.users[0] || defaultValue.users[0]
}
2025-03-17 11:48:30 +08:00
const resetSteamStore = () => {
setDir(defaultValue.steamDir)
setCsDir(defaultValue.cs2Dir)
setUsers(defaultValue.users)
setSteamDirValid(defaultValue.steamDirValid)
setCs2DirValid(defaultValue.cs2DirValid)
}