Skip to content

The open alternative to Salesforce, designed for AI.

1.1 KB
Raw Blame
import { useComponentInstanceStateContext } from '@/ui/utilities/state/component-state/hooks/useComponentInstanceStateContext';
import { type ComponentInstanceStateContext } from '@/ui/utilities/state/component-state/types/ComponentInstanceStateContext';
import { isNonEmptyString } from '@sniptt/guards';
import { doNotTranslate } from 'twenty-shared/i18n';

// Callers make the id unique where they create it; it is never rewritten here, since some state is deliberately
// shared across surfaces and ids are looked up verbatim as DOM anchors
export const useAvailableComponentInstanceIdOrThrow = <
  T extends { instanceId: string },
>(
  Context: ComponentInstanceStateContext<T>,
  instanceIdFromProps?: string,
): string => {
  const instanceStateContext = useComponentInstanceStateContext(Context);

  const instanceIdFromContext = instanceStateContext?.instanceId;

  if (isNonEmptyString(instanceIdFromProps)) {
    return instanceIdFromProps;
  }

  if (isNonEmptyString(instanceIdFromContext)) {
    return instanceIdFromContext;
  }

  throw new Error(
    doNotTranslate(
      'Instance id is not provided and cannot be found in context.',
    ),
  );
};