update: persist store fullfiil + launchOptions validation

This commit is contained in:
Purp1e
2024-09-27 10:38:40 +08:00
parent 759cdbab98
commit 46c6dbfa61
7 changed files with 158 additions and 97 deletions

View File

@@ -2,40 +2,44 @@ import { create } from 'zustand'
import { tauriStore } from '@/utils/persist'
import { createJSONStorage, persist } from 'zustand/middleware'
interface AppState {
version: string
hasUpdate: boolean
hasInit: boolean
inited: boolean
notice: string
useMirror: boolean
setVersion: (version: string) => void
init: () => void
}
const { /* store, */storage } = tauriStore("app-test")
interface AppAction {
setVersion: (version: string) => void
setHasUpdate: (hasUpdate: boolean) => void
setInited: (hasInit: boolean) => void
setNotice: (notice: string) => void
setUseMirror: (useMirror: boolean) => void
}
const useAppStore = create<AppState>()(
const STORE_NAME = "app"
const { /* store, */ storage } = tauriStore(STORE_NAME)
const useAppStore = create<AppState & AppAction>()(
persist(
(set, get) => ({
(set/* , get */) => ({
version: "0.0.1",
hasUpdate: false,
hasInit: false,
inited: false,
notice: "Man! What can I say?",
useMirror: true,
setVersion: (version: string) => set(() => ({ version: version })),
init: () => {
console.log('init')
console.log(get().version)
// const version = await store.get("version")
// set(() => ({ version: version }))
},
// increasePopulation: () => set((state) => ({ bears: state.bears + 1 })),
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: 'app', // name of item in the storage (must be unique)
name: STORE_NAME, // name of item in the storage (must be unique)
storage: createJSONStorage(() => storage), // (optional) by default the 'localStorage' is used
},
))
)
)
export default useAppStore

View File

@@ -1,5 +1,7 @@
import { SteamUser } from '@/types/steam'
import { tauriStore } from '@/utils/persist'
import { create } from 'zustand'
import { persist, createJSONStorage } from 'zustand/middleware'
const mock_user: SteamUser = {
steamID64: "76561198052315353",
@@ -15,31 +17,44 @@ interface SteamState {
dir: string,
csDir: string,
users: SteamUser[],
currentUser: () => SteamUser,
isDirValid: boolean,
isCsDirValid: boolean,
init: () => void,
updateDir: (dir: string) => void,
updateCsDir: (dir: string) => void,
}
const useSteamStore = create<SteamState>()((set, get) => ({
// 路径
dir: "C:\\Program Files (x86)\\Steam",
csDir: "",
users: [mock_user] as SteamUser[],
currentUser: () => {
return get().users[0] || mock_user
},
// 路径是否可用
isDirValid: false,
isCsDirValid: false,
init: () => {
console.log('init')
},
updateDir: (dir: string) => set(() => ({ dir: dir })),
updateCsDir: (dir: string) => set(() => ({ csDir: dir })),
// increasePopulation: () => set((state) => ({ bears: state.bears + 1 })),
}))
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 } = 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

View File

@@ -1,20 +1,39 @@
import { tauriStore } from '@/utils/persist'
import { create } from 'zustand'
import { createJSONStorage, persist } from 'zustand/middleware'
export interface ToolState {
interface ToolState {
launchOptions: string[]
launchIndex: number
powerPlan: number
init: () => void
}
const useToolStore = create<ToolState>()((/* set */) => ({
launchOptions: ["-novid -nojoy -high -freq 120 -tickrate 128", "", ""],
launchIndex: 0,
powerPlan: 0,
init: () => {
console.log('init')
},
// increasePopulation: () => set((state) => ({ bears: state.bears + 1 })),
}))
interface ToolAction {
setLaunchOption: (option: string, index: number) => void
setLaunchOptions: (options: string[]) => void
setLaunchIndex: (index: number) => void
setPowerPlan: (index: number) => void
}
const STORE_NAME = "tool"
const { /* store, */ storage } = tauriStore(STORE_NAME)
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 default useToolStore