All files / libs/payout-definition/plans/views/hub/rule-designer/src/lib/compute/breadcrumbs RuleDesignerPreviewStatement.tsx

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

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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126                                                                                                                                                                                                                                                           
import { css } from '@emotion/react';
import { Fragment, memo, useCallback } from 'react';
import { FormattedMessage } from 'react-intl';

import { Button, Group, Typography } from '@allshares/studio-design-system';
import { type Statement } from '@amalia/core/types';
import { useBoolState } from '@amalia/ext/react/hooks';
import { StatementDrawer } from '@amalia/payout-calculation/statements/components';
import { type Period } from '@amalia/payout-definition/periods/types';
import { type Plan, type PlanAssignmentWithUser } from '@amalia/payout-definition/plans/types';

import { useRuleDesignerComputeContext } from '../RuleDesignerCompute.context';

interface RuleDesignerPreviewStatementProps {
  readonly assignmentsWithStatements?: {
    planAssignment: PlanAssignmentWithUser;
    userStatement?: Pick<Statement, 'id'>;
  }[];
  readonly plan: Plan;
  readonly period: Period;
  readonly isForecastedView: boolean;
}

export const RuleDesignerPreviewStatement = memo(function RuleDesignerPreviewStatement({
  assignmentsWithStatements,
  plan,
  period,
  isForecastedView,
}: RuleDesignerPreviewStatementProps) {
  const { userId, setUserId } = useRuleDesignerComputeContext();
  const { isOpen, setOpenTrue, setOpenFalse } = useBoolState(false, 'open');

  const activeUserIndex = (assignmentsWithStatements ?? []).findIndex(
    ({ planAssignment }) => planAssignment.userId === userId,
  );

  const activeUser = assignmentsWithStatements?.[activeUserIndex];

  const handleGoToPreviousUser = useCallback(() => {
    const targetUserId = assignmentsWithStatements?.[activeUserIndex - 1]?.planAssignment.userId;
    if (targetUserId) {
      setUserId(targetUserId);
    }
  }, [assignmentsWithStatements, activeUserIndex, setUserId]);

  const handleGoToNextUser = useCallback(() => {
    const targetUserId = assignmentsWithStatements?.[activeUserIndex + 1]?.planAssignment.userId;
    if (targetUserId) {
      setUserId(targetUserId);
    }
  }, [assignmentsWithStatements, activeUserIndex, setUserId]);

  if (!activeUser) {
    return null;
  }

  return (
    <Fragment>
      <Button
        disabled={activeUserIndex < 0}
        variant="light"
        onClick={setOpenTrue}
      >
        <FormattedMessage defaultMessage="Preview statement" />
      </Button>

      <StatementDrawer
        isOpen={isOpen}
        navigation={
          <Group
            align="center"
            gap={8}
          >
            <StatementDrawer.Navigation>
              <StatementDrawer.Navigation.Previous
                disabled={activeUserIndex === 0}
                onClick={handleGoToPreviousUser}
              />

              <StatementDrawer.Navigation.Next
                disabled={activeUserIndex >= assignmentsWithStatements.length - 1}
                onClick={handleGoToNextUser}
              />
            </StatementDrawer.Navigation>

            <Typography
              variant="bodyXsmallMedium"
              css={(theme) => css`
                color: ${theme.ds.colors.gray[500]};

                [data-color-scheme='dark'] & {
                  color: ${theme.ds.colors.gray[600]};
                }
              `}
            >
              <FormattedMessage
                defaultMessage="{currentIndex, number} of {totalCount, plural, one {# statement} other {# statements}}"
                values={{
                  currentIndex: activeUserIndex + 1,
                  totalCount: assignmentsWithStatements.length,
                }}
              />
            </Typography>
          </Group>
        }
        onClose={setOpenFalse}
      >
        {activeUser.userStatement ? (
          <StatementDrawer.Statement
            isForecast={isForecastedView}
            showWorkflows={false}
            statementId={activeUser.userStatement.id}
          />
        ) : (
          <StatementDrawer.NoStatement
            isForecast={isForecastedView}
            periodId={period.id}
            plan={plan}
            user={activeUser.planAssignment.user}
          />
        )}
      </StatementDrawer>
    </Fragment>
  );
});