All files / libs/payout-definition/plans/views/hub/rule-designer/src/lib/compute use-token-in-error.ts

0% Statements 0/77
0% Branches 0/1
0% Functions 0/1
0% Lines 0/77

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78                                                                                                                                                           
import { useCallback } from 'react';

import { TokenType } from '@amalia/amalia-lang/tokens/types';
import { type EngineErrorContext } from '@amalia/core/types';

import { useDesignerDrawerContext } from '../drawer/PlanHubRuleDesignerDrawer.context';
import { type EditingDrawerToken } from '../drawer/PlanHubRuleDesignerDrawer.types';
import { useDesignerLibrary } from '../hooks/use-designer-library';
import { usePlanHubRuleDesignerContext } from '../PlanHubRuleDesigner.context';

import { type TokenInError } from './rule-designer-compute.types';

export const useTokenInError = () => {
  const { planId } = usePlanHubRuleDesignerContext();
  const { tokens } = useDesignerLibrary();
  const { openTokenInDrawer } = useDesignerDrawerContext();

  const getTokenInError = useCallback(
    (errorContext?: EngineErrorContext) => {
      const { tokenType, tokenMachineName, tokenId, tokenName } = errorContext ?? {};

      if (
        !tokenType ||
        // Cannot use includes here because we need typescript to infer the type of tokenType.
        (tokenType !== TokenType.FIELD &&
          tokenType !== TokenType.FILTER &&
          tokenType !== TokenType.VARIABLE &&
          tokenType !== TokenType.QUOTA &&
          tokenType !== TokenType.RULE)
      ) {
        return null;
      }

      const token =
        // First try to find it with its id.
        tokens[tokenType].find((t) => t.id === tokenId) ??
        // Or in the context of the plan
        tokens[tokenType].find(
          (t) => t.planId === planId && (t.machineName === tokenMachineName || t.name === tokenName),
        ) ??
        // Or in the global context
        tokens[tokenType].find(
          (t) => t.planId === null && (t.machineName === tokenMachineName || t.name === tokenName),
        );

      return token
        ? ({
            type: tokenType,
            id: token.id,
            machineName: token.machineName,
            name: token.name,
          } satisfies TokenInError)
        : null;
    },
    [planId, tokens],
  );

  const handleOpenTokenInError = useCallback(
    (tokenInError: TokenInError | null) => {
      if (!tokenInError || tokenInError.type === TokenType.RULE) {
        return;
      }

      openTokenInDrawer({
        isDirty: false,
        tokenType: tokenInError.type,
        id: tokenInError.id,
      } satisfies EditingDrawerToken);
    },
    [openTokenInDrawer],
  );

  return {
    getTokenInError,
    handleOpenTokenInError,
  };
};