[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] #[tauri::command]
pub fn check_file_exists(file: &str) -> bool { pub fn check_path(path: &str) -> bool {
std::path::Path::new(&file).exists() std::path::Path::new(&path).exists()
} }

View File

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