Skip to content

Commit

Permalink
Add Jotai to package.json and update Main page to use Jotai instead o…
Browse files Browse the repository at this point in the history
…f Valtio for state management
  • Loading branch information
Rel1cx committed Mar 27, 2023
1 parent 4f1e422 commit 9bf08c1
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 50 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
"@swan-io/chicane": "1.3.4",
"@tauri-apps/api": "1.2.0",
"@total-typescript/ts-reset": "0.4.2",
"jotai": "2.0.3",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-use": "17.4.0",
"ress": "5.0.2",
"ts-pattern": "4.2.2",
"valtio": "1.10.3"
"ts-pattern": "4.2.2"
},
"devDependencies": {
"@babel/core": "7.21.3",
Expand Down
34 changes: 14 additions & 20 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"package": {
"productName": "SensiMouse",
"version": "0.0.1"
"version": "0.0.2"
},
"tauri": {
"allowlist": {
Expand Down
6 changes: 5 additions & 1 deletion src/app.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { type MantineThemeOverride } from '@mantine/core'
import { MantineProvider } from '@mantine/core'
import { Provider } from 'jotai/react'
import { lazy, useMemo } from 'react'
import { match } from 'ts-pattern'

import { Router } from './router'
import { store } from './store'
import { styled } from './theme'

const Main = lazy(() => import('./pages/Main'))
Expand Down Expand Up @@ -40,7 +42,9 @@ export const App = () => {
return (
<MantineProvider withGlobalStyles withCSSVariables theme={theme}>
<AppShellScreen id="app-shell-screen">
<MainContent>{contentView}</MainContent>
<Provider store={store}>
<MainContent>{contentView}</MainContent>
</Provider>
</AppShellScreen>
</MantineProvider>
)
Expand Down
16 changes: 8 additions & 8 deletions src/pages/Main/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Input, Text } from '@mantine/core'
import { useSnapshot } from 'valtio'
import { useAtom } from 'jotai/react'

import { state, updateAccEnabled, updateSen } from '@/store'
import { accEnabledAtom, senAtom } from '@/store'

import * as SC from './styles'

Expand All @@ -14,7 +14,8 @@ const marks = [
]

const Main = () => {
const data = useSnapshot(state)
const [sen, setSen] = useAtom(senAtom)
const [accEnabled, setAccEnabled] = useAtom(accEnabledAtom)

return (
<SC.Container>
Expand All @@ -23,12 +24,11 @@ const Main = () => {
<Input.Wrapper label="Sensitivity">
<SC.xSlider
size="lg"
labelAlwaysOn
marks={marks}
min={0}
max={100}
value={data.sen}
onChange={updateSen}
value={sen}
onChange={setSen}
styles={theme => ({
markLabel: {
color: '#55585f'
Expand All @@ -41,9 +41,9 @@ const Main = () => {
size="md"
onLabel="ON"
offLabel="OFF"
checked={data.accEnabled}
checked={accEnabled}
onChange={event => {
updateAccEnabled(event.target.checked)
setAccEnabled(event.target.checked)
}}
/>
</Input.Wrapper>
Expand Down
3 changes: 2 additions & 1 deletion src/pages/Main/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export const xSlider = styled(Slider, {
})

export const xSwitch = styled(Switch, {
marginTop: '4px'
marginTop: '4px',
cursor: 'pointer'
})

export const xDivider = styled(Divider, {
Expand Down
30 changes: 13 additions & 17 deletions src/store.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
/* eslint-disable functional/immutable-data */
import { invoke } from '@tauri-apps/api/tauri'
import { proxy } from 'valtio'
import { atom, createStore } from 'jotai'

import { on } from './helper'

export const state = proxy({
sen: 0,
accEnabled: false
export const store = createStore()

export const senAtom = atom(0, (get, set, sen: number) => {
set(senAtom, sen)
invoke('set_mouse_cfg', { sen, accEnabled: get(accEnabledAtom) })
})

export const accEnabledAtom = atom(false, (get, set, accEnabled: boolean) => {
set(accEnabledAtom, accEnabled)
invoke('set_mouse_cfg', { sen: get(senAtom), accEnabled })
})

export const fetchState = async () => {
const [sen, accEnabled] = await invoke<[number, boolean]>('get_mouse_cfg')

state.sen = sen
state.accEnabled = accEnabled
}

export const updateSen = async (sen: number) => {
state.sen = sen
await invoke('set_mouse_cfg', { sen, accEnabled: state.accEnabled })
}

export const updateAccEnabled = async (accEnabled: boolean) => {
state.accEnabled = accEnabled
await invoke('set_mouse_cfg', { sen: state.sen, accEnabled })
store.set(senAtom, sen)
store.set(accEnabledAtom, accEnabled)
}

fetchState()
Expand Down

0 comments on commit 9bf08c1

Please sign in to comment.