utopia-ui/src/Components/Map/hooks/useLayers.tsx
Ulf Gebhardt 12fd624780
refactor(source): define more types (#149)
* define more types

* lint fixes

* update snapshot and reduce coverage

* revert role change, since it incompatible with directus
2025-02-22 16:09:38 +00:00

61 lines
1.7 KiB
TypeScript

import { useCallback, useReducer, createContext, useContext } from 'react'
import type { LayerProps } from '#types/LayerProps'
interface ActionType {
type: 'ADD LAYER'
layer: LayerProps
}
type UseItemManagerResult = ReturnType<typeof useLayerManager>
const LayerContext = createContext<UseItemManagerResult>({
layers: [],
// eslint-disable-next-line @typescript-eslint/no-empty-function
addLayer: () => {},
})
function useLayerManager(initialLayers: LayerProps[]): {
layers: LayerProps[]
addLayer: (layer: LayerProps) => void
} {
const [layers, dispatch] = useReducer((state: LayerProps[], action: ActionType) => {
switch (action.type) {
case 'ADD LAYER':
// eslint-disable-next-line no-case-declarations
const exist = state.find((layer) => layer.name === action.layer.name)
if (!exist) {
return [...state, action.layer]
} else return state
default:
throw new Error()
}
}, initialLayers)
const addLayer = useCallback((layer: LayerProps) => {
dispatch({
type: 'ADD LAYER',
layer,
})
}, [])
return { layers, addLayer }
}
export const LayersProvider: React.FunctionComponent<{
initialLayers: LayerProps[]
children?: React.ReactNode
}> = ({ initialLayers, children }: { initialLayers: LayerProps[]; children?: React.ReactNode }) => (
<LayerContext.Provider value={useLayerManager(initialLayers)}>{children}</LayerContext.Provider>
)
export const useLayers = (): LayerProps[] => {
const { layers } = useContext(LayerContext)
return layers
}
export const useAddLayer = (): UseItemManagerResult['addLayer'] => {
const { addLayer } = useContext(LayerContext)
return addLayer
}