[feat] add check path logic to prepare

todo: more complete logic
This commit is contained in:
Purp1e
2025-03-18 02:16:15 +08:00
parent db3cf9aef2
commit 12e908f107
3 changed files with 28 additions and 17 deletions

View File

@@ -52,6 +52,6 @@ pub fn set_auto_login_user(user: &str) -> String {
}
#[tauri::command]
pub fn check_file_exists(file: &str) -> bool {
std::path::Path::new(&file).exists()
pub fn check_path(path: &str) -> bool {
std::path::Path::new(&path).exists()
}

View File

@@ -82,7 +82,7 @@ fn main() {
cmds::kill_steam,
cmds::get_steam_path,
cmds::set_auto_login_user,
cmds::check_file_exists,
cmds::check_path,
on_button_clicked
])
.run(ctx)

View File

@@ -1,8 +1,9 @@
import { Button, Spinner } from "@heroui/react"
import { addToast, Button, Spinner } from "@heroui/react"
import { useRouter } from "next/navigation"
import { useEffect, useState } from "react"
import { useSteamStore } from "@/store/steam"
import { open } from "@tauri-apps/plugin-dialog"
import { invoke } from "@tauri-apps/api/core"
export function Prepare() {
const steam = useSteamStore()
@@ -10,13 +11,17 @@ export function Prepare() {
const [loading, setLoading] = useState(true)
const [steamDir, setSteamDir] = useState(steam.state.dir)
const [cs2Dir, setCs2Dir] = useState(steam.state.csDir)
const [inited, setInited] = useState(false)
useEffect(() => {
const checkPaths = () => {
if (steam.state.dir && steam.state.csDir) {
// router.push("/home")
const checkPaths = async () => {
if (
(await invoke("check_path", { path: steam.state.dir })) &&
(await invoke("check_path", { path: steam.state.csDir }))
) {
setInited(true)
}
setLoading(false)
}
checkPaths()
@@ -25,10 +30,15 @@ export function Prepare() {
const handleSelectSteamDir = async () => {
const selected = await open({
title: "选择 Steam.exe 文件",
filters: [{ name: "Steam", extensions: ["exe"] }],
filters: [{ name: "steam", extensions: ["exe"] }],
})
if (selected) {
const dir = selected.replace(/\\[^\\]+$/, "")
const pathExist = await invoke("check_path", { path: dir })
if (!pathExist) {
addToast({ title: "路径不存在", color: "warning" })
return
}
setSteamDir(dir)
steam.setDir(dir)
}
@@ -37,9 +47,15 @@ export function Prepare() {
const handleSelectCs2Dir = async () => {
const selected = await open({
title: "选择 CS2.exe 文件",
filters: [{ name: "Cs2", extensions: ["exe"] }],
filters: [{ name: "cs2", extensions: ["exe"] }],
})
if (selected) {
const dir = selected.replace(/\\[^\\]+$/, "")
const pathExist = await invoke("check_path", { path: dir })
if (!pathExist) {
addToast({ title: "路径不存在", color: "warning" })
return
}
setCs2Dir(selected)
steam.setCsDir(selected)
}
@@ -86,12 +102,7 @@ export function Prepare() {
</div>
<section className="flex justify-center w-full gap-3 mt-6">
<Button
onPress={() => alert("获取")}
variant="ghost"
color="default"
size="sm"
>
<Button onPress={() => alert("获取")} variant="ghost" color="default" size="sm">
</Button>
<Button
@@ -99,7 +110,7 @@ export function Prepare() {
variant="solid"
color="primary"
size="sm"
isDisabled={!steamDir || !cs2Dir}
isDisabled={!inited}
>
</Button>