Skip to content

The open alternative to Salesforce, designed for AI.

1.3 KB
Raw Blame
import { useCallback } from 'react';

import { useAvailableComponentInstanceIdOrThrow } from '@/ui/utilities/state/component-state/hooks/useAvailableComponentInstanceIdOrThrow';
import { globalComponentInstanceContextMap } from '@/ui/utilities/state/component-state/utils/globalComponentInstanceContextMap';
import { type ComponentFamilyState } from '@/ui/utilities/state/jotai/types/ComponentFamilyState';
import { doNotTranslate } from 'twenty-shared/i18n';

export const useAtomComponentFamilyStateCallbackState = <StateType, FamilyKey>(
  componentFamilyState: ComponentFamilyState<StateType, FamilyKey>,
  instanceIdFromProps?: string,
): ((
  familyKey: FamilyKey,
) => ReturnType<ComponentFamilyState<StateType, FamilyKey>['atomFamily']>) => {
  const componentInstanceContext = globalComponentInstanceContextMap.get(
    componentFamilyState.key,
  );

  if (!componentInstanceContext) {
    throw new Error(
      doNotTranslate(
        `Instance context for key "${componentFamilyState.key}" is not defined`,
      ),
    );
  }

  const instanceId = useAvailableComponentInstanceIdOrThrow(
    componentInstanceContext,
    instanceIdFromProps,
  );

  return useCallback(
    (familyKey: FamilyKey) =>
      componentFamilyState.atomFamily({ instanceId, familyKey }),
    [componentFamilyState, instanceId],
  );
};