All files / libs/payout-definition/plans/components/src/lib/plan-visibility/badge PlanVisibilityBadge.tsx

100% Statements 49/49
100% Branches 6/6
100% Functions 0/0
100% Lines 49/49

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 501x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 2x 2x 4x 4x 6x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 1x  
import { isNil } from 'lodash-es';
import { memo } from 'react';
import { FormattedMessage } from 'react-intl';
 
import { Badge, type BadgeProps } from '@allshares/studio-design-system';
import { canViewHiddenPlans } from '@amalia/kernel/auth/shared';
import { useAbilityContext } from '@amalia/kernel/auth/state';
import { type Plan } from '@amalia/payout-definition/plans/types';
 
export type PlanVisibilityBadgeProps = Pick<BadgeProps, 'ref' | 'size' | 'withShadow'> & {
  readonly isPlanHidden?: Plan['isHidden'] | null;
};
 
export const PlanVisibilityBadge = memo(function PlanVisibilityBadge({
  isPlanHidden,
  ref,
  ...badgeProps
}: PlanVisibilityBadgeProps) {
  const ability = useAbilityContext();
 
  // Non-admin user cannot see hidden plans, so there is no need to show the badge
  // since everything they'll see on the app are live plans.
  if (!canViewHiddenPlans(ability)) {
    return null;
  }
 
  // In case the plan visibility is not defined, we don't show the badge.
  if (isNil(isPlanHidden)) {
    return null;
  }
 
  return isPlanHidden ? (
    <Badge
      {...badgeProps}
      ref={ref}
      variant="warning"
    >
      <FormattedMessage defaultMessage="Hidden" />
    </Badge>
  ) : (
    <Badge
      {...badgeProps}
      ref={ref}
      variant="success"
    >
      <FormattedMessage defaultMessage="Live" />
    </Badge>
  );
});