[feat] use heroui + setup persistore + setup toast

todo: fix launchoption problem
This commit is contained in:
Purp1e
2025-03-12 11:22:32 +08:00
parent 2ef6d2c298
commit 9103150562
27 changed files with 745 additions and 264 deletions

View File

@@ -1,80 +1,33 @@
import { Store } from "@tauri-apps/plugin-store"
// import { create } from "zustand"
// import { createJSONStorage, persist } from "zustand/middleware"
import { store } from 'tauri-plugin-valtio';
import { DEFAULT_STORE_CONFIG } from '.';
// interface AppState {
// version: string
// hasUpdate: boolean
// inited: boolean
// notice: string
// useMirror: boolean
// }
// Usage:
// import {appStore} from "@/store/app"
// import { useSnapshot } from "valtio"
// const app = useSnapshot(appStore.state)
// { app.version }
// () => appStore.setVersion("0.0.1")
// interface AppAction {
// setVersion: (version: string) => void
// setHasUpdate: (hasUpdate: boolean) => void
// setInited: (hasInit: boolean) => void
// setNotice: (notice: string) => void
// setUseMirror: (useMirror: boolean) => void
// }
const STORE_NAME = "app"
// 节流设置
export const THROTTLE_TIME = 300
export const THROTTLE_TIME_STORE = 2000
export const THROTTLE_LEADING = true
export const THROTTLE_TRAILING = true
// const { /* store, */ storage } = await tauriStore(STORE_NAME)
// const useAppStore = create<AppState & AppAction>()(
// persist(
// (set /* , get */) => ({
// version: "0.0.1",
// hasUpdate: false,
// inited: false,
// notice: "Man! What can I say?",
// useMirror: true,
// setVersion: (version: string) => set(() => ({ version: version })),
// setHasUpdate: (hasUpdate: boolean) =>
// set(() => ({ hasUpdate: hasUpdate })),
// setInited: (inited: boolean) => set(() => ({ inited: inited })),
// setNotice: (notice: string) => set(() => ({ notice: notice })),
// setUseMirror: (useMirror: boolean) =>
// set(() => ({ useMirror: useMirror })),
// }),
// {
// name: STORE_NAME, // name of item in the storage (must be unique)
// storage: createJSONStorage(() => storage), // (optional) by default the 'localStorage' is used
// },
// ),
// )
const useAppStore = async (): Promise<any> => {
let store = await Store.get(`${STORE_NAME || "store"}.settings.json`);
if (!store) {
store = await Store.load(`${STORE_NAME || "store"}.settings.json`, {
autoSave: THROTTLE_TIME_STORE,
});
}
// 生成随机字符串每1s打印
const randomString = Math.random().toString(36).substring(2, 15)
setInterval(() => {
console.log("app store", randomString)
}, 1000)
return {
version: async () => (await store.get<string>("version")) || "0.0.1",
hasUpdate: async () => (await store.get<boolean>("hasUpdate")) || false,
inited: async () => (await store.get<boolean>("inited")) || false,
notice: async () => (await store.get<string>("notice")) || "",
useMirror: async () => (await store.get<boolean>("useMirror")) || true,
setVersion: async (version: string) => await store.set("version", version),
setHasUpdate: async (hasUpdate: boolean) => await store.set("hasUpdate", hasUpdate),
setInited: async (inited: boolean) => await store.set("inited", inited),
setNotice: async (notice: string) => await store.set("notice", notice),
setUseMirror: async (useMirror: boolean) => await store.set("useMirror", useMirror),
}
const defaultValue = {
version: "0.0.1",
hasUpdate: false,
inited: false,
notice: "",
useMirror: true
}
export default useAppStore
export const appStore = store('app', { ...defaultValue }, DEFAULT_STORE_CONFIG);
export const setVersion = (version: string) => { appStore.state.version = version }
export const setHasUpdate = (hasUpdate: boolean) => { appStore.state.hasUpdate = hasUpdate }
export const setInited = (inited: boolean) => { appStore.state.inited = inited }
export const setNotice = (notice: string) => { appStore.state.notice = notice }
export const setUseMirror = (useMirror: boolean) => { appStore.state.useMirror = useMirror }
export const resetAppStore = () => {
setVersion(defaultValue.version)
setHasUpdate(defaultValue.hasUpdate)
setInited(defaultValue.inited)
setNotice(defaultValue.notice)
setUseMirror(defaultValue.useMirror)
}

6
src/store/index.ts Normal file
View File

@@ -0,0 +1,6 @@
export const DEFAULT_STORE_CONFIG = {
saveOnChange: true,
saveOnExit: true,
saveStrategy: 'debounce' as const,
saveInterval: 3000,
};

View File

@@ -1,60 +1,37 @@
import type { SteamUser } from "@/types/steam"
import { tauriStore } from "@/utils/old"
import { create } from "zustand"
import { createJSONStorage, persist } from "zustand/middleware"
import { store } from 'tauri-plugin-valtio';
import { DEFAULT_STORE_CONFIG } from '.';
const mock_user: SteamUser = {
steamID64: "76561198052315353",
steamID32: "STEAM_0:0:46157676",
accountName: "wrr",
personaName: "wrr",
recent: 0,
avatar: "",
// "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/0d/0d1d4b9e1a9e8b2b3d4d0a7e1b3d6b1e3f8f4f7.jpg",
const defaultValue = {
dir: "C:\\Program Files (x86)\\Steam",
csDir: "",
users: [{
steamID64: "76561198052315353",
steamID32: "STEAM_0:0:46157676",
accountName: "wrr",
personaName: "wrr",
recent: 0,
avatar: ""
}] as SteamUser[],
isDirValid: false,
isCsDirValid: false
}
export const steamStore = store('steam', {...defaultValue}, DEFAULT_STORE_CONFIG);
export const setDir = (dir: string) => { steamStore.state.dir = dir }
export const setCsDir = (dir: string) => { steamStore.state.csDir = dir }
export const setUsers = (users: SteamUser[]) => { steamStore.state.users = users }
export const setIsDirValid = (valid: boolean) => { steamStore.state.isDirValid = valid }
export const setIsCsDirValid = (valid: boolean) => { steamStore.state.isCsDirValid = valid }
export const currentUser = () => {
return steamStore.state.users[0] || defaultValue.users[0]
}
interface SteamState {
dir: string
csDir: string
users: SteamUser[]
isDirValid: boolean
isCsDirValid: boolean
}
interface SteamAction {
setDir: (dir: string) => void
setCsDir: (dir: string) => void
setUsers: (users: SteamUser[]) => void
setIsDirValid: (valid: boolean) => void
setIsCsDirValid: (valid: boolean) => void
currentUser: () => SteamUser
}
const STORE_NAME = "steam"
const { /* store, */ storage } = await tauriStore(STORE_NAME)
const useSteamStore = create<SteamState & SteamAction>()(
persist(
(set, get) => ({
dir: "C:\\Program Files (x86)\\Steam",
csDir: "",
users: [mock_user] as SteamUser[],
isDirValid: false,
isCsDirValid: false,
setDir: (dir: string) => set(() => ({ dir: dir })),
setCsDir: (dir: string) => set(() => ({ csDir: dir })),
setUsers: (users: SteamUser[]) => set(() => ({ users: users })),
setIsDirValid: (valid: boolean) => set(() => ({ isDirValid: valid })),
setIsCsDirValid: (valid: boolean) => set(() => ({ isCsDirValid: valid })),
currentUser: () => {
return get().users[0] || mock_user
},
}),
{
name: STORE_NAME, // name of item in the storage (must be unique)
storage: createJSONStorage(() => storage), // (optional) by default the 'localStorage' is used
},
),
)
export default useSteamStore
export const resetSteamStore = () => {
setDir(defaultValue.dir)
setCsDir(defaultValue.csDir)
setUsers(defaultValue.users)
setIsDirValid(defaultValue.isDirValid)
setIsCsDirValid(defaultValue.isCsDirValid)
}

View File

@@ -1,51 +1,36 @@
import { tauriStore } from "@/utils/old"
import { create } from "zustand"
import { createJSONStorage, persist } from "zustand/middleware"
import { store } from 'tauri-plugin-valtio';
import { DEFAULT_STORE_CONFIG } from '.';
interface ToolState {
launchOptions: string[]
launchIndex: number
powerPlan: number
const defaultValue = {
launchOptions: [] as string[],
launchIndex: 0,
powerPlan: 0
}
interface ToolAction {
setLaunchOption: (option: string, index: number) => void
setLaunchOptions: (options: string[]) => void
setLaunchIndex: (index: number) => void
setPowerPlan: (index: number) => void
export const toolStore = store('tool', { ...defaultValue }, DEFAULT_STORE_CONFIG);
export const setLaunchOption = (option: string, index: number) => {
toolStore.state.launchOptions = [
...toolStore.state.launchOptions.slice(0, index),
option,
...toolStore.state.launchOptions.slice(index + 1)
]
}
const STORE_NAME = "tool"
const { /* store, */ storage } = await tauriStore(STORE_NAME)
export const setLaunchOptions = (options: string[]) => {
toolStore.state.launchOptions = options
}
const useToolStore = create<ToolState & ToolAction>()(
persist(
(set /* , get */) => ({
launchOptions: [
"-high -refresh 120 -novid -nojoy -tickrate 128 +cl_cmdrate 128 +cl_updaterate 128 +exec auto.cfg +test",
"",
"",
],
launchIndex: 0,
powerPlan: 0,
setLaunchOption: (option: string, index: number) =>
set((state) => ({
launchOptions: [
...state.launchOptions.slice(0, index),
option,
...state.launchOptions.slice(index + 1),
],
})),
setLaunchOptions: (options: string[]) =>
set(() => ({ launchOptions: options })),
setLaunchIndex: (index: number) => set(() => ({ launchIndex: index })),
setPowerPlan: (index: number) => set(() => ({ powerPlan: index })),
}),
{
name: STORE_NAME, // name of item in the storage (must be unique)
storage: createJSONStorage(() => storage), // (optional) by default the 'localStorage' is used
},
),
)
export const setLaunchIndex = (index: number) => {
toolStore.state.launchIndex = index
}
export default useToolStore
export const setPowerPlan = (plan: number) => {
toolStore.state.powerPlan = plan
}
export const resetToolStore = () => {
toolStore.state.launchOptions = defaultValue.launchOptions
toolStore.state.launchIndex = defaultValue.launchIndex
toolStore.state.powerPlan = defaultValue.powerPlan
}