Refactor KeyboardShortcut component to simplify state management
All checks were successful
farmcontrol/farmcontrol-ui/pipeline/head This commit looks good

- Removed unused state for tracking pressed keys, streamlining the key handling logic.
- Eliminated the sync function for pressed keys, enhancing performance and reducing complexity.
- Maintained existing functionality while improving the overall structure of the component.
This commit is contained in:
Tom Butcher 2026-08-01 14:13:24 +01:00
parent 2d32c55456
commit dc4a38f391

View File

@ -1,4 +1,4 @@
import { useEffect, useRef, cloneElement, useMemo, useState } from 'react' import { useEffect, useRef, cloneElement, useMemo } from 'react'
import PropTypes from 'prop-types' import PropTypes from 'prop-types'
import { Popover, Typography } from 'antd' import { Popover, Typography } from 'antd'
@ -50,18 +50,12 @@ const KeyboardShortcut = ({
const childRef = useRef() const childRef = useRef()
const pressedKeysRef = useRef(new Set()) const pressedKeysRef = useRef(new Set())
const shortcutKeys = useMemo(() => parseShortcut(shortcut), [shortcut]) const shortcutKeys = useMemo(() => parseShortcut(shortcut), [shortcut])
const [pressedKeys, setPressedKeys] = useState([])
useEffect(() => { useEffect(() => {
const syncPressedKeys = () => {
setPressedKeys([...pressedKeysRef.current])
}
const handleKeyDown = (event) => { const handleKeyDown = (event) => {
const key = getPressedKey(event) const key = getPressedKey(event)
if (key) { if (key) {
pressedKeysRef.current.add(key) pressedKeysRef.current.add(key)
syncPressedKeys()
} }
if (log) { if (log) {
@ -83,7 +77,6 @@ const KeyboardShortcut = ({
const key = getPressedKey(event) const key = getPressedKey(event)
if (key) { if (key) {
pressedKeysRef.current.delete(key) pressedKeysRef.current.delete(key)
syncPressedKeys()
} }
} }