[feat] better store operation

This commit is contained in:
2025-03-17 11:48:30 +08:00
parent 47dc77732f
commit db3cf9aef2
15 changed files with 169 additions and 127 deletions

View File

@@ -1,6 +1,8 @@
import { store } from "tauri-plugin-valtio"
import { useSnapshot } from "valtio"
import { DEFAULT_STORE_CONFIG } from "."
const defaultValue = {
launchOptions: [
"-novid -high -freq 144 -fullscreen",
@@ -17,7 +19,22 @@ export const toolStore = store(
DEFAULT_STORE_CONFIG,
)
export const setLaunchOption = (option: string, index: number) => {
export const useToolStore = () => {
void toolStore.start
const state = useSnapshot(toolStore.state)
return {
state,
setLaunchOption,
setLaunchOptions,
setLaunchIndex,
setPowerPlan,
addLaunchOption,
resetToolStore,
}
}
const setLaunchOption = (option: string, index: number) => {
toolStore.state.launchOptions = [
...toolStore.state.launchOptions.slice(0, index),
option,
@@ -25,19 +42,19 @@ export const setLaunchOption = (option: string, index: number) => {
]
}
export const setLaunchOptions = (options: string[]) => {
const setLaunchOptions = (options: string[]) => {
toolStore.state.launchOptions = options
}
export const setLaunchIndex = (index: number) => {
const setLaunchIndex = (index: number) => {
toolStore.state.launchIndex = index
}
export const setPowerPlan = (plan: number) => {
const setPowerPlan = (plan: number) => {
toolStore.state.powerPlan = plan
}
export const addLaunchOption = (option: string) => {
const addLaunchOption = (option: string) => {
// 限制最高10个
if (toolStore.state.launchOptions.length >= 10) {
return
@@ -45,7 +62,7 @@ export const addLaunchOption = (option: string) => {
toolStore.state.launchOptions = [...toolStore.state.launchOptions, option]
}
export const resetToolStore = () => {
const resetToolStore = () => {
setLaunchOptions(defaultValue.launchOptions)
setLaunchIndex(defaultValue.launchIndex)
setPowerPlan(defaultValue.powerPlan)