All files / libs/reporting/dashboards/components/src/lib/home-legacy/leaderboard-widget/leaderboard-graph LeaderBoardRow.tsx

100% Statements 10/10
60% Branches 9/15
100% Functions 5/5
100% Lines 8/8

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                                            15x               83x 83x 83x 83x   83x                                                                                                                                                                               83x                                       83x                                            
import { css, useTheme } from '@emotion/react';
import { IconAward } from '@tabler/icons-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 { UserPrettyFormat } from '@amalia/data-capture/fields/components';
import { CurrencySymbolsEnum } from '@amalia/ext/iso-4217';
import { FormattedAmount } from '@amalia/kernel/intl/components';
 
import { getUserCurrency, sumTotalForUser, type DataLeaderboard } from '../../dashboard.utils';
 
type LeaderBoardRowProps = {
  readonly idx: number;
  readonly max: number;
  readonly row: DataLeaderboard;
  readonly colors: string[];
  readonly loading?: boolean;
  readonly rowsCount?: number;
};
 
export const LeaderBoardRow = memo(function LeaderBoardRow({
  idx,
  max,
  row,
  colors,
  loading,
  rowsCount = 0,
}: LeaderBoardRowProps) {
  const theme = useTheme();
  const { colorScheme } = useColorSchemeContext();
  const amount = useMemo(() => sumTotalForUser(row), [row]);
  const currencySymbol = useMemo(() => getUserCurrency(row), [row]);
 
  return (
    <Stack
      key={`rank=${idx + 1}`}
      gap={4}
      id={`leaderboard-rank-${idx + 1}`}
      css={css`
        height: 50px;
        width: 100%;
      `}
    >
      <Group
        css={css`
          padding-left: 4px;
          width: 100%;
        `}
      >
        <Group
          align="center"
          css={css`
            width: 100%;
          `}
        >
          <Group
            align="center"
            gap={8}
            css={css`
              position: relative;
              width: 100%;
            `}
          >
            <UserPrettyFormat
              firstName={row.user.firstName}
              isLoading={loading}
              lastName={row.user.lastName}
              pictureURL={row.user.pictureURL}
            />
 
            {idx === 0 && amount > 0 && rowsCount > 1 && (
              <IconAward
                color={theme.ds.colors.primary[400]}
                size={16}
              />
            )}
          </Group>
 
          <Typography
            style={{ justifyContent: 'flex-end' }}
            variant="bodyBaseMedium"
          >
            {loading ? (
              <Skeleton>
                {/* Placeholder for the skeleton. */}
                <FormattedAmount
                  currencySymbol={CurrencySymbolsEnum.EUR}
                  value={10_000}
                />
              </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.user.lastName,
              },
            ]}
          >
            <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[700] : theme.ds.colors.gray[50],
                      },
                    }
                  : {})}
                barSize={9}
                dataKey={bar}
                fill={colors[index % colors.length]}
                radius={Object.keys(row.stats).length === index + 1 ? 10 : 0}
                stackId="1"
              />
            ))}
          </BarChart>
        </ResponsiveContainer>
      )}
    </Stack>
  );
});