diff --git a/bun.lockb b/bun.lockb index dd2e892..61fa940 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 618a2de..6d6ad7f 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "@tauri-apps/plugin-store": "^2.2.0", "@tauri-store/valtio": "^2.0.0", "@types/throttle-debounce": "^5.0.2", + "@uidotdev/usehooks": "^2.4.1", "framer-motion": "^12.5.0", "jotai": "^2.12.2", "next": "15.2.2", diff --git a/src/app/(main)/preference/path/page.tsx b/src/app/(main)/preference/path/page.tsx index 299a1dc..1521c07 100644 --- a/src/app/(main)/preference/path/page.tsx +++ b/src/app/(main)/preference/path/page.tsx @@ -6,10 +6,10 @@ export default function Page() { return (
-

Steam路径:{steam.state.dir}

-

游戏路径:{steam.state.csDir}

-

Steam路径有效:{steam.state.isDirValid ? "是" : "否"}

-

游戏路径有效:{steam.state.isCsDirValid ? "是" : "否"}

+

Steam路径:{steam.state.steamDir}

+

游戏路径:{steam.state.cs2Dir}

+

Steam路径有效:{steam.state.steamDirValid ? "是" : "否"}

+

游戏路径有效:{steam.state.cs2DirValid ? "是" : "否"}

Steam账号:{steam.currentUser().accountName}

) diff --git a/src/app/layout.tsx b/src/app/layout.tsx index 9099899..74853f9 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -1,14 +1,27 @@ "use client" -import { steamStore } from "@/store/steam" +import { steamStore, useSteamStore } from "@/store/steam" import { useEffect } from "react" import "./globals.css" import Providers from "./providers" import { init } from "@/store" +import { useDebounce } from "@uidotdev/usehooks" + export default function RootLayout({ children }: { children: React.ReactNode }) { useEffect(() => { void init() }) + // 检测steam路径和游戏路径是否有效 + const steam = useSteamStore() + const debounceSteamDir = useDebounce(steam.state.steamDir, 500) + const debounceCs2Dir = useDebounce(steam.state.cs2Dir, 500) + useEffect(() => { + steam.checkSteamDirValid() + }, [debounceSteamDir]) + useEffect(() => { + steam.checkCs2DirValid() + }, [debounceCs2Dir]) + return ( diff --git a/src/app/prepare/page.tsx b/src/app/prepare/page.tsx index 85844ca..c9b6b1c 100644 --- a/src/app/prepare/page.tsx +++ b/src/app/prepare/page.tsx @@ -4,13 +4,13 @@ import { useSteamStore } from "@/store/steam" import { useEffect, useState } from "react" export default function Page() { const steam = useSteamStore() - const [steamDir, setSteamDir] = useState(steam.state.dir) - const [cs2Dir, setCs2Dir] = useState(steam.state.csDir) + const [steamDir, setSteamDir] = useState(steam.state.steamDir) + const [cs2Dir, setCs2Dir] = useState(steam.state.cs2Dir) useEffect(() => { - setSteamDir(steam.state.dir) - setCs2Dir(steam.state.csDir) - }, [steam.state.dir, steam.state.csDir]) + setSteamDir(steam.state.steamDir) + setCs2Dir(steam.state.cs2Dir) + }, [steam.state.steamDir, steam.state.cs2Dir]) return (
{ type="button" onClick={() => invoke("launch_game", { - steamPath: `${steam.state.dir}/steam.exe`, + steamPath: `${steam.state.steamDir}/steam.exe`, launchOption: tool.state.launchOptions[tool.state.launchIndex] || "", server: "perfectworld", }) @@ -35,7 +35,7 @@ const FastLaunch = () => { type="button" onClick={() => invoke("launch_game", { - steamPath: `${steam.state.dir}/steam.exe`, + steamPath: `${steam.state.steamDir}/steam.exe`, launchOption: tool.state.launchOptions[tool.state.launchIndex] || "", server: "worldwide", }) diff --git a/src/components/cstb/Prepare.tsx b/src/components/cstb/Prepare.tsx index b9c0a48..2bdbca6 100644 --- a/src/components/cstb/Prepare.tsx +++ b/src/components/cstb/Prepare.tsx @@ -1,4 +1,4 @@ -import { addToast, Button, Spinner } from "@heroui/react" +import { addToast, Button, Chip, Spinner } from "@heroui/react" import { useRouter } from "next/navigation" import { useEffect, useState } from "react" import { steamStore, useSteamStore } from "@/store/steam" @@ -31,43 +31,33 @@ export function Prepare() { const router = useRouter() const [loading, setLoading] = useState(true) const [checking, setChecking] = useState(false) - const [, setSteamDir] = useState(steam.state.dir) - const [, setCs2Dir] = useState(steam.state.csDir) + const [, setSteamDir] = useState(steam.state.steamDir) + const [, setCs2Dir] = useState(steam.state.cs2Dir) const [inited, setInited] = useState(false) - const [links, setLinks] = useState([]) useEffect(() => { const initValues = async () => { - await steamStore.start() - const initialSteamDir = steam.state.dir - const initialCs2Dir = steam.state.csDir + const initialSteamDir = steam.state.steamDir + const initialCs2Dir = steam.state.cs2Dir setSteamDir(initialSteamDir) setCs2Dir(initialCs2Dir) + + const allCheckPassed = steam.state.steamDirValid && steam.state.cs2DirValid + setInited(steam.state.steamDirValid && steam.state.cs2DirValid) + // 逻辑有点问题,第一次启动时检查成功直接跳转,第一次检查失败不跳转,用户手动点击 + if (allCheckPassed) { + router.push("/home") + return + } + + setTimeout(() => { + setLoading(false) + }, 300) } void initValues() }) - useEffect(() => { - void onOpenUrl((urls) => { - console.log("deep link:", urls) - setLinks(urls) - }) - const checkPaths = async () => { - setChecking(true) - const exist: boolean = - (await check_path(path.resolve(steam.state.dir, "steam.exe"))) && - (await check_path(path.resolve(steam.state.csDir, "cs2.exe"))) - - setTimeout(() => { - setInited(exist) - setLoading(false) - setChecking(false) - }, 500) - } - void checkPaths() - }, [steam.state.dir, steam.state.csDir, router]) - const handleSelectSteamDir = async () => { const selected = await open({ title: "选择 Steam.exe 文件", @@ -135,7 +125,7 @@ export function Prepare() {
{ setSteamDir(e.target.value) steam.setDir(e.target.value) @@ -149,7 +139,7 @@ export function Prepare() {
{ setCs2Dir(e.target.value) steam.setCsDir(e.target.value) @@ -160,7 +150,12 @@ export function Prepare() {
- {links.length > 0 &&

{links}

} + + +
+ {steam.state.steamDirValid ? "Steam √" : "Steam ×"} + {steam.state.cs2DirValid ? "CS2 √" : "CS2 ×"} +
) } + +function TestDeepLink() { + const [links, setLinks] = useState([]) + const router = useRouter() + + useEffect(() => { + void onOpenUrl((urls) => { + console.log("deep link:", urls) + setLinks(urls) + }) + }, [router]) + return <>{links.length > 0 &&

{links}

} +} diff --git a/src/store/index.ts b/src/store/index.ts index 9666764..bd09a09 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -10,6 +10,5 @@ export async function init() { await toolStore.start() await steamStore.start() const appConfigDirPath = await appConfigDir() - console.log(appConfigDirPath) await setStoreCollectionPath(path.resolve(appConfigDirPath, "cstb")) } diff --git a/src/store/steam.ts b/src/store/steam.ts index 88b0db3..20fae8c 100644 --- a/src/store/steam.ts +++ b/src/store/steam.ts @@ -2,10 +2,14 @@ import type { SteamUser } from "@/types/steam" import { store } from "@tauri-store/valtio" import { DEFAULT_STORE_CONFIG } from "./config" 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 = { - dir: "C:\\Program Files (x86)\\Steam", - csDir: "", + steamDir: "C:\\Program Files (x86)\\Steam", + cs2Dir: "", users: [ { steamID64: "76561198052315353", @@ -16,8 +20,8 @@ const defaultValue = { avatar: "", }, ] as SteamUser[], - isDirValid: false, - isCsDirValid: false, + steamDirValid: false, + cs2DirValid: false, } export const steamStore = store( @@ -35,27 +39,41 @@ export const useSteamStore = () => { setDir, setCsDir, setUsers, - setIsDirValid, - setIsCsDirValid, + setSteamDirValid, + setCs2DirValid, + checkSteamDirValid, + checkCs2DirValid, currentUser, resetSteamStore, } } const setDir = (dir: string) => { - steamStore.state.dir = dir + steamStore.state.steamDir = dir } const setCsDir = (dir: string) => { - steamStore.state.csDir = dir + steamStore.state.cs2Dir = dir } const setUsers = (users: SteamUser[]) => { steamStore.state.users = users } -const setIsDirValid = (valid: boolean) => { - steamStore.state.isDirValid = valid +const setSteamDirValid = (valid: boolean) => { + steamStore.state.steamDirValid = valid } -const setIsCsDirValid = (valid: boolean) => { - steamStore.state.isCsDirValid = valid +const setCs2DirValid = (valid: boolean) => { + steamStore.state.cs2DirValid = valid +} + +const checkSteamDirValid = async () => { + const pathExist = await invoke("check_path", { path: steamStore.state.steamDir }) + console.log("steamDir", steamStore.state.steamDir, "pathExist", pathExist) + setSteamDirValid(pathExist) +} + +const checkCs2DirValid = async () => { + const pathExist = await invoke("check_path", { path: steamStore.state.cs2Dir }) + console.log("cs2Dir", steamStore.state.cs2Dir, "pathExist", pathExist) + setCs2DirValid(pathExist) } const currentUser = () => { @@ -63,9 +81,9 @@ const currentUser = () => { } const resetSteamStore = () => { - setDir(defaultValue.dir) - setCsDir(defaultValue.csDir) + setDir(defaultValue.steamDir) + setCsDir(defaultValue.cs2Dir) setUsers(defaultValue.users) - setIsDirValid(defaultValue.isDirValid) - setIsCsDirValid(defaultValue.isCsDirValid) -} + setSteamDirValid(defaultValue.steamDirValid) + setCs2DirValid(defaultValue.cs2DirValid) +} \ No newline at end of file