farmcontrol-ui/src/components/Dashboard/context/DashboardObjectToolsContext.jsx
Tom Butcher 42d6ac6422 Add Dashboard Object Tools Context and Chevron Icons
- Introduced DashboardObjectToolsContext and DashboardObjectToolsProvider for managing object tools state within the dashboard.
- Added ChevronLeftIcon and ChevronRightIcon components for navigation buttons, enhancing user interface consistency.
- Updated ObjectTableNavigationButtons to utilize new chevron icons and improved layout for better user experience.
- Refactored KeyboardShortcut and Tooltip components for cleaner code and improved functionality.
2026-08-21 21:11:15 +01:00

79 lines
2.0 KiB
JavaScript

import {
createContext,
useCallback,
useContext,
useEffect,
useRef,
useState
} from 'react'
import PropTypes from 'prop-types'
const DashboardObjectToolsContext = createContext()
export const DashboardObjectToolsProvider = ({ children }) => {
const [currentObjectTools, setCurrentObjectToolsState] = useState(null)
const ownerRef = useRef(null)
const setCurrentObjectTools = useCallback((tools, ownerId) => {
ownerRef.current = ownerId
setCurrentObjectToolsState(tools)
}, [])
const clearCurrentObjectTools = useCallback((ownerId) => {
if (ownerRef.current !== ownerId) {
return
}
ownerRef.current = null
setCurrentObjectToolsState(null)
}, [])
return (
<DashboardObjectToolsContext.Provider
value={{
currentObjectTools,
setCurrentObjectTools,
clearCurrentObjectTools
}}
>
{children}
</DashboardObjectToolsContext.Provider>
)
}
DashboardObjectToolsProvider.propTypes = {
children: PropTypes.node.isRequired
}
// eslint-disable-next-line react-refresh/only-export-components
export const useDashboardObjectToolsContext = () => {
const context = useContext(DashboardObjectToolsContext)
if (!context) {
throw new Error(
'useDashboardObjectToolsContext must be used within a DashboardObjectToolsProvider'
)
}
return context
}
// eslint-disable-next-line react-refresh/only-export-components
export const useDashboardObjectTools = (tools) => {
const { setCurrentObjectTools, clearCurrentObjectTools } =
useDashboardObjectToolsContext()
const ownerIdRef = useRef(null)
if (ownerIdRef.current == null) {
ownerIdRef.current = Symbol('dashboardObjectTools')
}
useEffect(() => {
setCurrentObjectTools(tools, ownerIdRef.current)
}, [tools, setCurrentObjectTools])
useEffect(() => {
const ownerId = ownerIdRef.current
return () => clearCurrentObjectTools(ownerId)
}, [clearCurrentObjectTools])
}
export { DashboardObjectToolsContext }