Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

1.4 KB
Raw Blame
import type { ContainerElement, ElementId } from 'features/nodes/types/workflow';
import type { PropsWithChildren } from 'react';
import { createContext, memo, useContext, useMemo } from 'react';
import { assert } from 'tsafe';

type ContainerContextValue = {
  id: ElementId;
  layout: ContainerElement['data']['layout'];
};

const ContainerContext = createContext<ContainerContextValue | null>(null);

export const ContainerContextProvider = memo(({ id, layout, children }: PropsWithChildren<ContainerContextValue>) => {
  const ctxValue = useMemo(() => ({ id, layout }), [id, layout]);
  return <ContainerContext.Provider value={ctxValue}>{children}</ContainerContext.Provider>;
});
ContainerContextProvider.displayName = 'ContainerContextProvider';

export const useContainerContext = () => {
  const container = useContext(ContainerContext);
  assert(
    container !== null,
    'useContainerContext must be used inside a ContainerContextProvider and cannot be used by the root container'
  );
  return container;
};

const DepthContext = createContext<number>(0);

export const DepthContextProvider = memo(({ depth, children }: PropsWithChildren<{ depth: number }>) => {
  return <DepthContext.Provider value={depth}>{children}</DepthContext.Provider>;
});
DepthContextProvider.displayName = 'DepthContextProvider';

export const useDepthContext = () => {
  const depth = useContext(DepthContext);
  return depth;
};