UNPKG

@atlaskit/editor-plugin-find-replace

Version:

find replace plugin for @atlaskit/editor-core

25 lines 1.09 kB
import { useState, useEffect, useRef } from 'react'; import isEqual from 'lodash/isEqual'; // custom non-debounced hook for the find-replace plugin shared state export function useFindReplacePluginStateSelector(api, key) { const [selectedState, setSelectedState] = useState(() => { const currentState = api === null || api === void 0 ? void 0 : api.findReplace.sharedState.currentState(); return currentState === null || currentState === void 0 ? void 0 : currentState[key]; }); const previousStateRef = useRef(selectedState); useEffect(() => { const unsub = api === null || api === void 0 ? void 0 : api.findReplace.sharedState.onChange(({ nextSharedState }) => { const newState = nextSharedState === null || nextSharedState === void 0 ? void 0 : nextSharedState[key]; if (!isEqual(previousStateRef.current, newState)) { previousStateRef.current = newState; setSelectedState(newState); } }); return () => { unsub === null || unsub === void 0 ? void 0 : unsub(); }; }, [api, key]); return selectedState; }