@@ -4,25 +4,27 @@ import { setHashFromObject, useHashChangedExternally } from './useHash'
44
55import timers from '../data'
66import { MicroControllers , PanelModes } from '../helpers/types'
7- import { uniq , without } from 'lodash-es '
7+ import { memo } from 'react '
88
99export const panelModeState = atom ( PanelModes . Normal )
1010const defaultState : Record < string , string > = {
1111 mcu : MicroControllers . ATMEGA328P ,
1212 timer : '0'
1313}
1414
15- export const RegisterHashWatcher = ( ) => {
15+ export const RegisterHashWatcher = memo ( ( ) => {
1616 const set = useSetAtom ( userConfigStateBulk )
1717 const params = useHashChangedExternally ( )
18- set ( params )
18+ set ( { ... defaultState , ... params } )
1919 return < > </ >
20- }
21- export const RegisterHashUpdater = ( ) => {
20+ } )
21+
22+ export const RegisterHashUpdater = memo ( ( ) => {
2223 const obj = useAtomValue ( userConfigStateBulk )
2324 setHashFromObject ( { ...defaultState , ...obj } )
2425 return < > </ >
25- }
26+ } )
27+
2628export const RegisterHashLink = ( ) => {
2729 return (
2830 < >
@@ -32,48 +34,23 @@ export const RegisterHashLink = () => {
3234 )
3335}
3436
35- const userConfigState_vars = atom < string [ ] > ( [ ] )
36-
37- const userConfigState_store = atomFamily ( ( variable : string ) =>
38- atom < string | undefined > ( defaultState [ variable ] )
39- )
37+ export const userConfigStateBulk = atom ( defaultState )
4038
4139export const userConfigState = atomFamily ( ( variable : string ) =>
4240 atom (
43- ( get ) => get ( userConfigState_store ( variable ) ) ,
44- ( get , set , value : string | undefined ) => {
45- set ( userConfigState_store ( variable ) , value )
46- const vars = get ( userConfigState_vars )
47- const exists = vars . includes ( variable )
48- if ( exists && value === undefined ) {
49- set ( userConfigState_vars , without ( vars , variable ) )
50- } else if ( ! exists && value != undefined ) {
51- set ( userConfigState_vars , [ ...vars , variable ] )
41+ ( get ) => get ( userConfigStateBulk ) [ variable ] ,
42+ ( get , set , value ?: string ) => {
43+ const was = get ( userConfigStateBulk )
44+ if ( value === undefined ) {
45+ const { [ variable ] : _removedValue , ...without } = was
46+ set ( userConfigStateBulk , without )
47+ } else {
48+ set ( userConfigStateBulk , { ...was , [ variable ] : value } )
5249 }
5350 }
5451 )
5552)
5653
57- export const userConfigStateBulk = atom (
58- ( get ) =>
59- Object . fromEntries (
60- get ( userConfigState_vars ) . map ( ( variable ) => [
61- variable ,
62- get ( userConfigState ( variable ) )
63- ] )
64- ) ,
65- ( get , set , value : Record < string , string | undefined > ) => {
66- const variables = uniq ( [
67- ...get ( userConfigState_vars ) ,
68- ...Object . keys ( value )
69- ] )
70- for ( const variable of variables ) {
71- if ( get ( userConfigState ( variable ) ) !== value [ variable ] )
72- set ( userConfigState ( variable ) , value [ variable ] )
73- }
74- }
75- )
76-
7754export const mcuTimers = atom ( ( get ) => {
7855 const micro = get ( userConfigState ( 'mcu' ) ) as MicroControllers
7956 return timers [ micro ]
0 commit comments