All files / libs/reporting/dashboards/components/src/lib/home-legacy/benchmark-by-rule-widget/benchmark-by-rule BenchmarkByRuleRow.tsx

25% Statements 3/12
0% Branches 0/12
0% Functions 0/5
30% Lines 3/10

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                    15x 15x                   15x                                                                                                                                                                                                                                                        
import { css, useTheme } from '@emotion/react';
import { mapValues } from 'lodash-es';
import { memo, useMemo } from 'react';
import { Bar, BarChart, ResponsiveContainer, XAxis, YAxis } from 'recharts';
 
import { Group, Skeleton, Stack, Typography, useColorSchemeContext } from '@allshares/studio-design-system';
import { FormattedAmount } from '@amalia/kernel/intl/components';
 
import { getUserCurrency, sumTotalForUser, type DataBenchmarkByRule } from '../../dashboard.utils';
 
const LABEL_PLACEHOLDER = 'Placeholder';
const AMOUNT_PLACEHOLDER = '100.00 €';
 
type BenchmarkByRuleRowProps = {
  readonly idx: number;
  readonly max: number;
  readonly row: DataBenchmarkByRule;
  readonly colors: string[];
  readonly loading?: boolean;
};
 
export const BenchmarkByRuleRow = memo(function BenchmarkByRuleRow({
  idx,
  max,
  row,
  colors,
  loading,
}: BenchmarkByRuleRowProps) {
  const theme = useTheme();
  const { colorScheme } = useColorSchemeContext();
  const amount = useMemo(() => sumTotalForUser(row), [row]);
  const currencySymbol = useMemo(() => getUserCurrency(row), [row]);
 
  return (
    <Stack
      key={`rank=${idx + 1}`}
      css={css`
        height: 50px;
        margin-bottom: 4px;
        width: 100%;
      `}
    >
      <Group
        css={css`
          padding-left: 4px;
          width: 100%;
        `}
      >
        <Group
          css={css`
            width: 100%;
          `}
        >
          <Group
            css={css`
              position: relative;
              width: 100%;
            `}
          >
            <Typography variant="bodyBaseRegular">
              {loading ? <Skeleton>{LABEL_PLACEHOLDER}</Skeleton> : row.label}
            </Typography>
          </Group>
          <Typography
            variant="bodyBaseRegular"
            style={{
              textAlign: 'right',
              width: 120,
              paddingRight: 6,
            }}
          >
            {loading ? (
              <Skeleton>{AMOUNT_PLACEHOLDER}</Skeleton>
            ) : (
              <FormattedAmount
                currencySymbol={currencySymbol}
                value={amount}
              />
            )}
          </Typography>
        </Group>
      </Group>
      {loading ? (
        <Skeleton
          shape="round"
          css={css`
            width: calc(${100 - idx * 6}%);
            height: 9px;
            margin: 8px 4px;
          `}
        />
      ) : (
        <ResponsiveContainer
          height="100%"
          width="100%"
        >
          <BarChart
            height={300}
            layout="vertical"
            maxBarSize={max}
            width={500}
            data={[
              {
                ...mapValues(row.stats, (s) => s.value),
                name: row.label,
              },
            ]}
          >
            <XAxis
              hide
              domain={[0, max]}
              shapeRendering="crispEdges"
              type="number"
            />
            <YAxis
              hide
              dataKey="name"
              scale="band"
              shapeRendering="crispEdges"
              type="category"
            />
 
            {Object.keys(row.stats).map((bar, index) => (
              <Bar
                key={bar}
                {...(index === 0
                  ? {
                      background: {
                        fill: colorScheme === 'dark' ? theme.ds.colors.dark[500] : theme.ds.colors.gray[100],
                      },
                    }
                  : {})}
                barSize={9}
                dataKey={bar}
                fill={colors[index % colors.length]}
                radius={Object.keys(row.stats).length === index + 1 ? [0, 10, 10, 0] : [0, 0, 0, 0]}
                stackId="1"
              />
            ))}
          </BarChart>
        </ResponsiveContainer>
      )}
    </Stack>
  );
});