All files / libs/data-correction/overwrites/components/src/lib/overwrite-details OverwriteDetails.tsx

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

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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183                                                                                                                                                                                                                                                                                                                                                                             
import { css } from '@emotion/react';
import { IconPencil, IconTrash } from '@tabler/icons-react';
import { isNil } from 'lodash-es';
import { memo } from 'react';
import { FormattedMessage, useIntl } from 'react-intl';

import { Button, Group, IconButton, Stack, Typography } from '@allshares/studio-design-system';
import { type Overwrite } from '@amalia/core/types';
import { type FormatsEnum } from '@amalia/data-capture/fields/types';
import { OverwriteScopeEnum } from '@amalia/data-correction/overwrites/types';
import { dayjs } from '@amalia/ext/dayjs';
import { type CurrencySymbolsEnum } from '@amalia/ext/iso-4217';
import { FormattedPrettyValue } from '@amalia/kernel/intl/components';
import { isCurrencyValue } from '@amalia/kernel/monetary/types';
import { type ComputedOverwrite } from '@amalia/payout-calculation/types';
import { formatUserFullName } from '@amalia/tenants/users/types';

export type OnEditOverwrite = (overwrite: ComputedOverwrite | Overwrite) => void;
export type OnClearOverwrite = (overwrite: ComputedOverwrite | Overwrite) => void;

interface OverwriteDetailsProps {
  readonly overwrite: ComputedOverwrite | Overwrite;
  readonly hideOldValue?: boolean;
  readonly format: FormatsEnum;
  readonly currency?: CurrencySymbolsEnum;
  readonly className?: string;
  readonly onEditOverwrite?: OnEditOverwrite;
  readonly onClearOverwrite?: OnClearOverwrite;
}

/**
 * Kill me already.
 *
 * @param overwriteValue
 */
const getOverwriteValue = (overwriteValue: (ComputedOverwrite | Overwrite)['sourceValue']) => {
  if (isNil(overwriteValue)) {
    return null;
  }

  // Handles overwrite values that look like this: `{ amount: { value: 100, symbol: 'USD' } }`
  if (typeof overwriteValue === 'object' && !isCurrencyValue(overwriteValue)) {
    return Object.values(overwriteValue)[0];
  }

  return overwriteValue;
};

export const OverwriteDetails = memo(function OverwriteDetails({
  overwrite,
  format,
  currency,
  className,
  onClearOverwrite,
  onEditOverwrite,
  hideOldValue,
}: OverwriteDetailsProps) {
  const author =
    overwrite.creator && typeof overwrite.creator === 'object'
      ? formatUserFullName(overwrite.creator)
      : overwrite.creator;

  const { formatMessage } = useIntl();

  const trashLabel =
    overwrite.scope === OverwriteScopeEnum.FORECAST
      ? formatMessage({ defaultMessage: 'Clear simulation' })
      : formatMessage({ defaultMessage: 'Clear overwrite' });

  return (
    <Stack
      className={className}
      gap={8}
    >
      <div
        css={css`
          display: grid;
          grid-template-columns: auto 1fr;
          gap: 8px 16px;
          align-items: center;
        `}
      >
        {!hideOldValue &&
          (overwrite.scope === OverwriteScopeEnum.FORECAST ? (
            <Typography variant="bodySmallRegular">
              <FormattedMessage defaultMessage="Actual value" />
            </Typography>
          ) : (
            <Typography variant="bodySmallRegular">
              <FormattedMessage defaultMessage="Old value" />
            </Typography>
          ))}
        {!hideOldValue && (
          <Typography
            data-testid="overwrite-original-value"
            variant="bodyBaseRegular"
          >
            <FormattedPrettyValue
              currencySymbol={currency}
              format={format}
              value={getOverwriteValue(overwrite.sourceValue)}
            />
          </Typography>
        )}
        <Typography variant="bodySmallRegular">
          {overwrite.scope === OverwriteScopeEnum.FORECAST ? (
            <FormattedMessage defaultMessage="Simulated value" />
          ) : (
            <FormattedMessage defaultMessage="New value" />
          )}
        </Typography>
        <Typography
          data-testid="overwrite-new-value"
          variant="bodyBaseMedium"
          css={(theme) => css`
            color: ${overwrite.scope === OverwriteScopeEnum.FORECAST
              ? theme.ds.hues.purple[400]
              : theme.ds.colors.primary[400]};
          `}
        >
          <FormattedPrettyValue
            currencySymbol={currency}
            format={format}
            value={getOverwriteValue(overwrite.overwriteValue)}
          />
        </Typography>
      </div>

      <Group align="baseline">
        <Typography
          variant="bodySmallRegular"
          css={css`
            flex: 1;
            font-style: italic;
          `}
        >
          {overwrite.scope === OverwriteScopeEnum.FORECAST ? (
            <FormattedMessage
              defaultMessage="Simulated by {author} on {date}"
              values={{
                author,
                date: dayjs(overwrite.createdAt).format('L'),
              }}
            />
          ) : (
            <FormattedMessage
              defaultMessage="Overwritten by {author} on {date}"
              values={{
                author,
                date: dayjs(overwrite.createdAt).format('L'),
              }}
            />
          )}
        </Typography>

        {!!onEditOverwrite && !!onClearOverwrite && (
          <Group
            align="center"
            gap={4}
          >
            <Button
              icon={<IconPencil />}
              size="small"
              variant="light-text"
              onClick={() => onEditOverwrite(overwrite)}
            >
              <FormattedMessage defaultMessage="Edit" />
            </Button>
            <IconButton
              icon={<IconTrash />}
              label={trashLabel}
              outline="shadow"
              size="small"
              variant="danger"
              onClick={() => onClearOverwrite(overwrite)}
            />
          </Group>
        )}
      </Group>
    </Stack>
  );
});