Skip to content

Commit 8462727

Browse files
committed
Fix wobbly height and simplify bulk variable setting
1 parent 4f3d7ef commit 8462727

File tree

3 files changed

+28
-47
lines changed

3 files changed

+28
-47
lines changed

src/Panes/ResizePanel.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { useState } from 'preact/hooks'
2-
import { ReactNode, CSSProperties, useEffect } from 'react'
2+
import { ReactNode, useEffect } from 'react'
33
import './ResizePanel.css'
4+
import { atom, useAtom } from 'jotai'
5+
6+
export const plotHeightAtom = atom(300)
47
export default function ResizePanel(props: { children: ReactNode }) {
5-
const [height, setHeight] = useState(300)
8+
const [height, setHeight] = useAtom(plotHeightAtom)
69
const [startY, setStartY] = useState(0)
710
const [isDragging, setIsDragging] = useState(false)
811
const onMouseDown = (e: MouseEvent | TouchEvent) => {

src/Plot/Plot.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { getAllCompareRegTraits } from '../helpers/compareRegisterUtil'
1616
import { useAtomValue, useSetAtom } from 'jotai'
1717
import { userConfigState } from '../state/state'
1818
import { simulationState } from '../helpers/helpers'
19+
import { plotHeightAtom } from '../Panes/ResizePanel'
1920

2021
function usePrevious<T>(value: T) {
2122
const ref = useRef<T>()
@@ -27,9 +28,10 @@ function usePrevious<T>(value: T) {
2728

2829
export default function Plot({ style }: { style: Object }) {
2930
const containerRef = useRef<HTMLDivElement>(null)
30-
const [width, height_] = useSize(containerRef)
31-
const height_ouputCompare = height_ / 10
32-
const margin_ouputCompare = height_ / 30
31+
const [width] = useSize(containerRef)
32+
const height = useAtomValue(plotHeightAtom)
33+
const height_ouputCompare = height / 10
34+
const margin_ouputCompare = height / 30
3335
const { simulation, ocrMax, param, counterMax, values } =
3436
useAtomValue(simulationState)
3537

@@ -63,8 +65,7 @@ export default function Plot({ style }: { style: Object }) {
6365
}
6466
/* --- */
6567
const activeOCnXs = IOCR_states.filter(({ isActiveOutput }) => isActiveOutput)
66-
const height_timer =
67-
height_ - height_ouputCompare * (activeOCnXs.length + 0.5)
68+
const height_timer = height - height_ouputCompare * (activeOCnXs.length + 0.5)
6869
const xDomain = extent(simulation.t) as [number, number]
6970
const xScale = useMemo(
7071
() =>

src/state/state.tsx

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,27 @@ import { setHashFromObject, useHashChangedExternally } from './useHash'
44

55
import timers from '../data'
66
import { MicroControllers, PanelModes } from '../helpers/types'
7-
import { uniq, without } from 'lodash-es'
7+
import { memo } from 'react'
88

99
export const panelModeState = atom(PanelModes.Normal)
1010
const 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+
2628
export 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

4139
export 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-
7754
export const mcuTimers = atom((get) => {
7855
const micro = get(userConfigState('mcu')) as MicroControllers
7956
return timers[micro]

0 commit comments

Comments
 (0)