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.0KB

1234567891011121314151617181920212223242526272829303132333435
  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. }, [d3Selection, d3Zoom])
  24. if (!storeRef.current)
  25. storeRef.current = createHooksStore(restProps)
  26. return (
  27. <HooksStoreContext.Provider value={storeRef.current}>
  28. {children}
  29. </HooksStoreContext.Provider>
  30. )
  31. }