You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

provider.tsx 1.1KB

123456789101112131415161718192021222324252627282930313233343536
  1. import {
  2. createContext,
  3. useEffect,
  4. useRef,
  5. } from 'react'
  6. import { useStore } from 'reactflow'
  7. import {
  8. createHooksStore,
  9. } from './store'
  10. import type { Shape } from './store'
  11. type HooksStore = ReturnType<typeof createHooksStore>
  12. export const HooksStoreContext = createContext<HooksStore | null | undefined>(null)
  13. type HooksStoreContextProviderProps = Partial<Shape> & {
  14. children: React.ReactNode
  15. }
  16. export const HooksStoreContextProvider = ({ children, ...restProps }: HooksStoreContextProviderProps) => {
  17. const storeRef = useRef<HooksStore | undefined>(undefined)
  18. const d3Selection = useStore(s => s.d3Selection)
  19. const d3Zoom = useStore(s => s.d3Zoom)
  20. useEffect(() => {
  21. if (storeRef.current && d3Selection && d3Zoom)
  22. storeRef.current.getState().refreshAll(restProps)
  23. // eslint-disable-next-line react-hooks/exhaustive-deps
  24. }, [d3Selection, d3Zoom])
  25. if (!storeRef.current)
  26. storeRef.current = createHooksStore(restProps)
  27. return (
  28. <HooksStoreContext.Provider value={storeRef.current}>
  29. {children}
  30. </HooksStoreContext.Provider>
  31. )
  32. }