From c533cf1d2382350fc8dbd452dad049ae43b7a314 Mon Sep 17 00:00:00 2001 From: Eva1ent Date: Tue, 11 Apr 2023 07:59:03 +0800 Subject: [PATCH] Fix: Save, Restore and Reapply Settings --- .eslintrc.json | 3 +- .prettierrc | 2 +- package.json | 7 ++- pnpm-lock.yaml | 90 ++++++++++++++++++++------------- src-tauri/tauri.conf.json | 2 +- src/{store.ts => atoms.ts} | 25 +-------- src/constants.ts | 2 +- src/helper.ts | 37 ++++++++------ src/i18n/index.ts | 4 +- src/lib/settings.ts | 7 --- src/main.ts | 45 +++++++++++++++-- src/pages/Main/index.tsx | 31 ++++++++++-- src/pages/Preferences/index.tsx | 20 ++++++-- src/settings.ts | 38 ++++++++++++++ src/types/index.ts | 3 ++ 15 files changed, 216 insertions(+), 100 deletions(-) rename src/{store.ts => atoms.ts} (54%) delete mode 100644 src/lib/settings.ts create mode 100644 src/settings.ts diff --git a/.eslintrc.json b/.eslintrc.json index 2954a55..38b8f3a 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -2,7 +2,8 @@ "root": true, "extends": ["with-tsconfig"], "rules": { - "new-cap": "off" + "new-cap": "off", + "no-sequences": "off" }, "overrides": [ { diff --git a/.prettierrc b/.prettierrc index d0b0e37..f445fd0 100644 --- a/.prettierrc +++ b/.prettierrc @@ -6,4 +6,4 @@ "singleQuote": true, "tabWidth": 4, "trailingComma": "none" -} \ No newline at end of file +} diff --git a/package.json b/package.json index 953c7ec..0c17576 100644 --- a/package.json +++ b/package.json @@ -33,15 +33,18 @@ "ress": "5.0.2", "tauri-plugin-autostart-api": "https://github.com/tauri-apps/tauri-plugin-autostart", "tauri-plugin-store-api": "github:tauri-apps/tauri-plugin-store", + "throttle-debounce": "5.0.0", "ts-pattern": "4.2.2", - "valtio": "1.10.4" + "valtio": "1.10.4", + "zod": "^3.21.4" }, "devDependencies": { "@babel/core": "7.21.4", "@tauri-apps/cli": "1.2.3", "@types/node": "18.15.11", - "@types/react": "18.0.33", + "@types/react": "18.0.34", "@types/react-dom": "18.0.11", + "@types/throttle-debounce": "5.0.0", "@vitejs/plugin-react": "3.1.0", "core-dts": "0.0.3", "eslint": "8.38.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3d7e91e..15d80aa 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8,7 +8,7 @@ overrides: dependencies: '@mantine/core': specifier: 6.0.6 - version: 6.0.6(@emotion/react@11.10.6)(@mantine/hooks@6.0.6)(@types/react@18.0.33)(react-dom@18.2.0)(react@18.2.0) + version: 6.0.6(@emotion/react@11.10.6)(@mantine/hooks@6.0.6)(@types/react@18.0.34)(react-dom@18.2.0)(react@18.2.0) '@mantine/hooks': specifier: 6.0.6 version: 6.0.6(react@18.2.0) @@ -51,12 +51,18 @@ dependencies: tauri-plugin-store-api: specifier: github:tauri-apps/tauri-plugin-store version: github.com/tauri-apps/tauri-plugin-store/f0da2d1ac8370bd4cc7c1c0617175f0d5b5833a0 + throttle-debounce: + specifier: 5.0.0 + version: 5.0.0 ts-pattern: specifier: 4.2.2 version: 4.2.2 valtio: specifier: 1.10.4 version: 1.10.4(react@18.2.0) + zod: + specifier: ^3.21.4 + version: 3.21.4 devDependencies: '@babel/core': @@ -69,11 +75,14 @@ devDependencies: specifier: 18.15.11 version: 18.15.11 '@types/react': - specifier: 18.0.33 - version: 18.0.33 + specifier: 18.0.34 + version: 18.0.34 '@types/react-dom': specifier: 18.0.11 version: 18.0.11 + '@types/throttle-debounce': + specifier: 5.0.0 + version: 5.0.0 '@vitejs/plugin-react': specifier: 3.1.0 version: 3.1.0(vite@4.2.1) @@ -466,7 +475,7 @@ packages: resolution: {integrity: sha512-G/YwXTkv7Den9mXDO7AhLWkE3q+I92B+VqAE+dYG4NGPaHZGvt3G8Q0p9vmE+sq7rTGphUbAvmQ9YpbfMQGGlA==} dev: false - /@emotion/react@11.10.6(@types/react@18.0.33)(react@18.2.0): + /@emotion/react@11.10.6(@types/react@18.0.34)(react@18.2.0): resolution: {integrity: sha512-6HT8jBmcSkfzO7mc+N1L9uwvOnlcGoix8Zn7srt+9ga0MjREo6lRpuVX0kzo6Jp6oTqDhREOFsygN6Ew4fEQbw==} peerDependencies: '@types/react': '*' @@ -482,7 +491,7 @@ packages: '@emotion/use-insertion-effect-with-fallbacks': 1.0.0(react@18.2.0) '@emotion/utils': 1.2.0 '@emotion/weak-memoize': 0.3.0 - '@types/react': 18.0.33 + '@types/react': 18.0.34 hoist-non-react-statics: 3.3.2 react: 18.2.0 dev: false @@ -869,7 +878,7 @@ packages: '@jridgewell/sourcemap-codec': 1.4.14 dev: true - /@mantine/core@6.0.6(@emotion/react@11.10.6)(@mantine/hooks@6.0.6)(@types/react@18.0.33)(react-dom@18.2.0)(react@18.2.0): + /@mantine/core@6.0.6(@emotion/react@11.10.6)(@mantine/hooks@6.0.6)(@types/react@18.0.34)(react-dom@18.2.0)(react@18.2.0): resolution: {integrity: sha512-0DtwlNcHEb6d8/qtoYCFHc/at+8w1sg3TUWh+cdFFiykZqpRrXBRoGGfbElN5RHtQqAVg4BXC0jDSHpz6CjPHw==} peerDependencies: '@mantine/hooks': 6.0.6 @@ -883,8 +892,8 @@ packages: '@radix-ui/react-scroll-area': 1.0.2(react-dom@18.2.0)(react@18.2.0) react: 18.2.0 react-dom: 18.2.0(react@18.2.0) - react-remove-scroll: 2.5.5(@types/react@18.0.33)(react@18.2.0) - react-textarea-autosize: 8.3.4(@types/react@18.0.33)(react@18.2.0) + react-remove-scroll: 2.5.5(@types/react@18.0.34)(react@18.2.0) + react-textarea-autosize: 8.3.4(@types/react@18.0.34)(react@18.2.0) transitivePeerDependencies: - '@emotion/react' - '@types/react' @@ -905,7 +914,7 @@ packages: react: '>=16.8.0' react-dom: '>=16.8.0' dependencies: - '@emotion/react': 11.10.6(@types/react@18.0.33)(react@18.2.0) + '@emotion/react': 11.10.6(@types/react@18.0.34)(react@18.2.0) clsx: 1.1.1 csstype: 3.0.9 react: 18.2.0 @@ -1290,11 +1299,11 @@ packages: /@types/react-dom@18.0.11: resolution: {integrity: sha512-O38bPbI2CWtgw/OoQoY+BRelw7uysmXbWvw3nLWO21H1HSh+GOlqPuXshJfjmpNlKiiSDG9cc1JZAaMmVdcTlw==} dependencies: - '@types/react': 18.0.33 + '@types/react': 18.0.34 dev: true - /@types/react@18.0.33: - resolution: {integrity: sha512-sHxzVxeanvQyQ1lr8NSHaj0kDzcNiGpILEVt69g9S31/7PfMvNCKLKcsHw4lYKjs3cGNJjXSP4mYzX43QlnjNA==} + /@types/react@18.0.34: + resolution: {integrity: sha512-NO1UO8941541CJl1BeOXi8a9dNKFK09Gnru5ZJqkm4Q3/WoQJtHvmwt0VX0SB9YCEwe7TfSSxDuaNmx6H2BAIQ==} dependencies: '@types/prop-types': 15.7.5 '@types/scheduler': 0.16.3 @@ -1307,6 +1316,10 @@ packages: resolution: {integrity: sha512-21cFJr9z3g5dW8B0CVI9g2O9beqaThGQ6ZFBqHfwhzLDKUxaqTIy3vnfah/UPkfOiF2pLq+tGz+W8RyCskuslw==} dev: true + /@types/throttle-debounce@5.0.0: + resolution: {integrity: sha512-Pb7k35iCGFcGPECoNE4DYp3Oyf2xcTd3FbFQxXUI9hEYKUl6YX+KLf7HrBmgVcD05nl50LIH6i+80js4iYmWbw==} + dev: true + /@types/which@2.0.2: resolution: {integrity: sha512-113D3mDkZDjo+EeUEHCFy0qniNc1ZpecGiAU7WSo7YDoSzolZIQKpYFHrPpjkB2nuyahcKfrmLXeQlh7gqJYdw==} dev: true @@ -4206,7 +4219,7 @@ packages: engines: {node: '>=0.10.0'} dev: true - /react-remove-scroll-bar@2.3.4(@types/react@18.0.33)(react@18.2.0): + /react-remove-scroll-bar@2.3.4(@types/react@18.0.34)(react@18.2.0): resolution: {integrity: sha512-63C4YQBUt0m6ALadE9XV56hV8BgJWDmmTPY758iIJjfQKt2nYwoUrPk0LXRXcB/yIj82T1/Ixfdpdk68LwIB0A==} engines: {node: '>=10'} peerDependencies: @@ -4216,13 +4229,13 @@ packages: '@types/react': optional: true dependencies: - '@types/react': 18.0.33 + '@types/react': 18.0.34 react: 18.2.0 - react-style-singleton: 2.2.1(@types/react@18.0.33)(react@18.2.0) + react-style-singleton: 2.2.1(@types/react@18.0.34)(react@18.2.0) tslib: 2.5.0 dev: false - /react-remove-scroll@2.5.5(@types/react@18.0.33)(react@18.2.0): + /react-remove-scroll@2.5.5(@types/react@18.0.34)(react@18.2.0): resolution: {integrity: sha512-ImKhrzJJsyXJfBZ4bzu8Bwpka14c/fQt0k+cyFp/PBhTfyDnU5hjOtM4AG/0AMyy8oKzOTR0lDgJIM7pYXI0kw==} engines: {node: '>=10'} peerDependencies: @@ -4232,16 +4245,16 @@ packages: '@types/react': optional: true dependencies: - '@types/react': 18.0.33 + '@types/react': 18.0.34 react: 18.2.0 - react-remove-scroll-bar: 2.3.4(@types/react@18.0.33)(react@18.2.0) - react-style-singleton: 2.2.1(@types/react@18.0.33)(react@18.2.0) + react-remove-scroll-bar: 2.3.4(@types/react@18.0.34)(react@18.2.0) + react-style-singleton: 2.2.1(@types/react@18.0.34)(react@18.2.0) tslib: 2.5.0 - use-callback-ref: 1.3.0(@types/react@18.0.33)(react@18.2.0) - use-sidecar: 1.1.2(@types/react@18.0.33)(react@18.2.0) + use-callback-ref: 1.3.0(@types/react@18.0.34)(react@18.2.0) + use-sidecar: 1.1.2(@types/react@18.0.34)(react@18.2.0) dev: false - /react-style-singleton@2.2.1(@types/react@18.0.33)(react@18.2.0): + /react-style-singleton@2.2.1(@types/react@18.0.34)(react@18.2.0): resolution: {integrity: sha512-ZWj0fHEMyWkHzKYUr2Bs/4zU6XLmq9HsgBURm7g5pAVfyn49DgUiNgY2d4lXRlYSiCif9YBGpQleewkcqddc7g==} engines: {node: '>=10'} peerDependencies: @@ -4251,14 +4264,14 @@ packages: '@types/react': optional: true dependencies: - '@types/react': 18.0.33 + '@types/react': 18.0.34 get-nonce: 1.0.1 invariant: 2.2.4 react: 18.2.0 tslib: 2.5.0 dev: false - /react-textarea-autosize@8.3.4(@types/react@18.0.33)(react@18.2.0): + /react-textarea-autosize@8.3.4(@types/react@18.0.34)(react@18.2.0): resolution: {integrity: sha512-CdtmP8Dc19xL8/R6sWvtknD/eCXkQr30dtvC4VmGInhRsfF8X/ihXCq6+9l9qbxmKRiq407/7z5fxE7cVWQNgQ==} engines: {node: '>=10'} peerDependencies: @@ -4267,7 +4280,7 @@ packages: '@babel/runtime': 7.21.0 react: 18.2.0 use-composed-ref: 1.3.0(react@18.2.0) - use-latest: 1.2.1(@types/react@18.0.33)(react@18.2.0) + use-latest: 1.2.1(@types/react@18.0.34)(react@18.2.0) transitivePeerDependencies: - '@types/react' dev: false @@ -4804,6 +4817,11 @@ packages: engines: {node: '>=10'} dev: false + /throttle-debounce@5.0.0: + resolution: {integrity: sha512-2iQTSgkkc1Zyk0MeVrt/3BvuOXYPl/R8Z0U2xxo9rjwNciaHDG3R+Lm6dh4EeUci49DanvBnuqI6jshoQQRGEg==} + engines: {node: '>=12.22'} + dev: false + /through@2.3.8: resolution: {integrity: sha512-w89qg7PI8wAdvX60bMDP+bFoD5Dvhm9oLheFp5O4a2QF0cSBGsBX4qZmadPMvVqlLJBBci+WqGGOAPvcDeNSVg==} dev: true @@ -4989,7 +5007,7 @@ packages: punycode: 2.3.0 dev: true - /use-callback-ref@1.3.0(@types/react@18.0.33)(react@18.2.0): + /use-callback-ref@1.3.0(@types/react@18.0.34)(react@18.2.0): resolution: {integrity: sha512-3FT9PRuRdbB9HfXhEq35u4oZkvpJ5kuYbpqhCfmiZyReuRgpnhDlbr2ZEnnuS0RrJAPn6l23xjFg9kpDM+Ms7w==} engines: {node: '>=10'} peerDependencies: @@ -4999,7 +5017,7 @@ packages: '@types/react': optional: true dependencies: - '@types/react': 18.0.33 + '@types/react': 18.0.34 react: 18.2.0 tslib: 2.5.0 dev: false @@ -5012,7 +5030,7 @@ packages: react: 18.2.0 dev: false - /use-isomorphic-layout-effect@1.1.2(@types/react@18.0.33)(react@18.2.0): + /use-isomorphic-layout-effect@1.1.2(@types/react@18.0.34)(react@18.2.0): resolution: {integrity: sha512-49L8yCO3iGT/ZF9QttjwLF/ZD9Iwto5LnH5LmEdk/6cFmXddqi2ulF0edxTwjj+7mqvpVVGQWvbXZdn32wRSHA==} peerDependencies: '@types/react': '*' @@ -5021,11 +5039,11 @@ packages: '@types/react': optional: true dependencies: - '@types/react': 18.0.33 + '@types/react': 18.0.34 react: 18.2.0 dev: false - /use-latest@1.2.1(@types/react@18.0.33)(react@18.2.0): + /use-latest@1.2.1(@types/react@18.0.34)(react@18.2.0): resolution: {integrity: sha512-xA+AVm/Wlg3e2P/JiItTziwS7FK92LWrDB0p+hgXloIMuVCeJJ8v6f0eeHyPZaJrM+usM1FkFfbNCrJGs8A/zw==} peerDependencies: '@types/react': '*' @@ -5034,12 +5052,12 @@ packages: '@types/react': optional: true dependencies: - '@types/react': 18.0.33 + '@types/react': 18.0.34 react: 18.2.0 - use-isomorphic-layout-effect: 1.1.2(@types/react@18.0.33)(react@18.2.0) + use-isomorphic-layout-effect: 1.1.2(@types/react@18.0.34)(react@18.2.0) dev: false - /use-sidecar@1.1.2(@types/react@18.0.33)(react@18.2.0): + /use-sidecar@1.1.2(@types/react@18.0.34)(react@18.2.0): resolution: {integrity: sha512-epTbsLuzZ7lPClpz2TyryBfztm7m+28DlEv2ZCQ3MDr5ssiwyOwGH/e5F9CkfWjJ1t4clvI58yF822/GUkjjhw==} engines: {node: '>=10'} peerDependencies: @@ -5049,7 +5067,7 @@ packages: '@types/react': optional: true dependencies: - '@types/react': 18.0.33 + '@types/react': 18.0.34 detect-node-es: 1.1.0 react: 18.2.0 tslib: 2.5.0 @@ -5243,6 +5261,10 @@ packages: engines: {node: '>=10'} dev: true + /zod@3.21.4: + resolution: {integrity: sha512-m46AKbrzKVzOzs/DZgVnG5H55N1sv1M8qZU3A8RIKbs3mrACDNeIOeilDymVb2HdmP8uwshOCF4uJ8uM9rCqJw==} + dev: false + /zx@7.2.1: resolution: {integrity: sha512-TgKwppaMLMNAXHhlhbBh7rMoOSx3/9qqnkv8frmhVlSomEuWkDijh/BCmYntkoS7ZQyemApAUyEi24jIrrS+hA==} engines: {node: '>= 16.0.0'} diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 3c062b7..7a823b2 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -8,7 +8,7 @@ }, "package": { "productName": "SensiMouse", - "version": "1.0.2" + "version": "1.0.3" }, "tauri": { "allowlist": { diff --git a/src/store.ts b/src/atoms.ts similarity index 54% rename from src/store.ts rename to src/atoms.ts index 22e7237..a4659e8 100644 --- a/src/store.ts +++ b/src/atoms.ts @@ -2,9 +2,7 @@ import { invoke } from '@tauri-apps/api/tauri' import { atom, getDefaultStore } from 'jotai' import * as autostart from 'tauri-plugin-autostart-api' -import { DEFAULT_ACC_ENABLED, DEFAULT_SEN } from './constants' -import { getMouseCfg, setMouseCfg } from './lib/cmd' -import { on } from './lib/dom' +import { setMouseCfg } from './lib/cmd' export const store = getDefaultStore() @@ -26,24 +24,3 @@ export const setAutoLaunchAtom = atom(null, async (_, set, enabled: boolean) => set(autoLaunchAtom, enabled) }) - -export const fetchState = async () => { - const ret = await getMouseCfg() - const [sen, accEnabled] = ret.match({ - Ok: v => v, - Error: () => [DEFAULT_SEN, DEFAULT_ACC_ENABLED] - }) - - const autoStart = await autostart.isEnabled() - - store.set(senAtom, sen) - store.set(accEnabledAtom, accEnabled) - store.set(autoLaunchAtom, autoStart) -} - -export const resetState = () => { - store.set(senAtom, DEFAULT_SEN) - store.set(accEnabledAtom, DEFAULT_ACC_ENABLED) -} - -on(window)('focus', fetchState) diff --git a/src/constants.ts b/src/constants.ts index ab1ae8c..28fb417 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,6 +1,6 @@ import { type Locales } from './i18n/i18n-types' -export const DEFAULT_LANG: Locales = 'en' +export const DEFAULT_LOCALE: Locales = 'en' export const DEFAULT_SEN = 90 diff --git a/src/helper.ts b/src/helper.ts index 32858dd..484b14a 100644 --- a/src/helper.ts +++ b/src/helper.ts @@ -1,19 +1,24 @@ -import { DEFAULT_LANG } from './constants' -import { i18nConfig, setLocale } from './i18n' -import { type Locales } from './i18n/i18n-types' -import { isLocale } from './i18n/i18n-util' -import { getSettings, settings } from './lib/settings' +import * as autostart from 'tauri-plugin-autostart-api' -export const initI18n = async () => { - const locale = await getSettings('locale') - setLocale(locale.getWithDefault(DEFAULT_LANG)) - return settings.onKeyChange('locale', (locale: Locales | null) => { - if (!locale || !isLocale(locale)) { - return - } - if (locale === i18nConfig.locale) { - return - } - setLocale(locale) +import { accEnabledAtom, autoLaunchAtom, senAtom, store } from './atoms' +import { DEFAULT_ACC_ENABLED, DEFAULT_SEN } from './constants' +import { getMouseCfg } from './lib/cmd' + +export const syncAppStateWithSystem = async () => { + const ret = await getMouseCfg() + const [sen, accEnabled] = ret.match({ + Ok: v => v, + Error: () => [DEFAULT_SEN, DEFAULT_ACC_ENABLED] }) + + const autoStart = await autostart.isEnabled() + + store.set(senAtom, sen) + store.set(accEnabledAtom, accEnabled) + store.set(autoLaunchAtom, autoStart) +} + +export const resetAppState = () => { + store.set(senAtom, DEFAULT_SEN) + store.set(accEnabledAtom, DEFAULT_ACC_ENABLED) } diff --git a/src/i18n/index.ts b/src/i18n/index.ts index ccaee4a..f5b1785 100644 --- a/src/i18n/index.ts +++ b/src/i18n/index.ts @@ -1,6 +1,6 @@ import { proxy, useSnapshot } from 'valtio' -import { DEFAULT_LANG } from '@/constants' +import { DEFAULT_LOCALE } from '@/constants' import L from './i18n-node' import { type Locales } from './i18n-types' @@ -8,7 +8,7 @@ import { type Locales } from './i18n-types' export const i18nConfig = proxy<{ locale: Locales }>({ - locale: DEFAULT_LANG + locale: DEFAULT_LOCALE }) export const setLocale = (locale: Locales) => { diff --git a/src/lib/settings.ts b/src/lib/settings.ts deleted file mode 100644 index b0c178c..0000000 --- a/src/lib/settings.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { Option } from '@swan-io/boxed' -import { Store } from 'tauri-plugin-store-api' - -export const settings = new Store('.settings.dat') - -// eslint-disable-next-line etc/no-misused-generics -export const getSettings = async (key: string) => Option.fromNullable(await settings.get(key)) diff --git a/src/main.ts b/src/main.ts index 955e9f1..b6340a4 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,13 +3,40 @@ import '@/styles/global.scss' import '@/styles/overrides.scss' import '@total-typescript/ts-reset' -import { initI18n } from './helper' +import * as autostart from 'tauri-plugin-autostart-api' + +import { i18nConfig, setLocale } from '@/i18n' +import { type Locales } from '@/i18n/i18n-types' +import { isLocale } from '@/i18n/i18n-util' + +import { accEnabledAtom, autoLaunchAtom, senAtom, store } from './atoms' +import { setMouseCfg } from './lib/cmd' import { renderApp } from './root' -import { fetchState } from './store' +import { loadSettings, resetSettings, type Settings, settings } from './settings' +import { defaultSettings } from './settings' const main = async () => { - await initI18n() - await fetchState() + const loadResult = await loadSettings() + + // eslint-disable-next-line no-console + const settings: Settings = await loadResult.tap(console.log).match({ + Ok: settings => { + setMouseCfg(settings.sen, settings.accEnabled) + return Promise.resolve(settings) + }, + Error: async () => { + await resetSettings() + return defaultSettings + } + }) + + const autoStart = await autostart.isEnabled() + + setLocale(settings.locale) + + store.set(senAtom, settings.sen) + store.set(accEnabledAtom, settings.accEnabled) + store.set(autoLaunchAtom, autoStart) renderApp().match({ Ok: _ => {}, @@ -19,4 +46,14 @@ const main = async () => { }) } +settings.onKeyChange('locale', (locale: Locales | null) => { + if (!locale || !isLocale(locale)) { + return + } + if (locale === i18nConfig.locale) { + return + } + setLocale(locale) +}) + main() diff --git a/src/pages/Main/index.tsx b/src/pages/Main/index.tsx index 2682b41..277b5d3 100644 --- a/src/pages/Main/index.tsx +++ b/src/pages/Main/index.tsx @@ -1,13 +1,16 @@ import { Input } from '@mantine/core' import { exit } from '@tauri-apps/api/process' import { useAtom } from 'jotai/react' +import { debounce } from 'throttle-debounce' +import { accEnabledAtom, senAtom } from '@/atoms' import { Button } from '@/components/Button' import { Divider } from '@/components/Divider' import { Header } from '@/components/Header' +import { DEFAULT_ACC_ENABLED, DEFAULT_SEN } from '@/constants' import { useTranslation } from '@/i18n' import { getWebviewWindow } from '@/lib/tauri' -import { accEnabledAtom, resetState, senAtom } from '@/store' +import { resetSettings, setSettingsByKey } from '@/settings' import * as SC from './styles' @@ -30,6 +33,10 @@ export const handleOpenPreferences = async () => { console.error('Failed to get preferences window') } +const saveSen = debounce(300, async (value: number) => { + await setSettingsByKey('sen', value) +}) + export default function Main() { const [sen, setSen] = useAtom(senAtom) const [accEnabled, setAccEnabled] = useAtom(accEnabledAtom) @@ -41,7 +48,17 @@ export default function Main() {
SensiMouse
- + { + setSen(value) + saveSen(value) + }} + /> - + diff --git a/src/pages/Preferences/index.tsx b/src/pages/Preferences/index.tsx index e978925..a0c3b0a 100644 --- a/src/pages/Preferences/index.tsx +++ b/src/pages/Preferences/index.tsx @@ -1,11 +1,11 @@ import { Checkbox, Flex, Select } from '@mantine/core' import { useAtom, useSetAtom } from 'jotai/react' +import { autoLaunchAtom, setAutoLaunchAtom } from '@/atoms' import { Header } from '@/components/Header' import { useLocale, useTranslation } from '@/i18n' import { isLocale } from '@/i18n/i18n-util' -import { settings } from '@/lib/settings' -import { autoLaunchAtom, setAutoLaunchAtom } from '@/store' +import { settings } from '@/settings' import { styled } from '@/theme' const themes = [ @@ -36,7 +36,17 @@ export default function Preferences() { return (
{T.THEME()}
- { + if (!value) { + return + } + settings.set('theme', value) + settings.save() + }} + />
{T.LANGUAGE()}