All files / src/features/rent-payments RentSummary.tsx

100% Statements 3/3
100% Branches 2/2
100% Functions 1/1
100% Lines 3/3

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                                                  2x 2x 2x                                                                                                                                              
// This month's rent across the user's flats, at the top of the flats list
// (Design canvas: Asunnot ยท vuokran tila; openspec: rent-payments, "The rent
// summary on the flats list"): received out of expected, whole rents, and
// how many flats are late.
import Box from '@mui/material/Box'
import Stack from '@mui/material/Stack'
import Typography from '@mui/material/Typography'
import { useId } from 'react'
import { formatMoney } from '#/lib/format'
import { m } from '#/paraglide/messages.js'
import { colors, radii } from '#/theme/tokens'
import { rentSummaryTitle } from './labels'
 
interface Props {
  /** `YYYY-MM`. */
  month: string
  /** Cents. */
  received: number
  /** Cents. */
  expected: number
  /** Flats whose rent is late. */
  late: number
}
 
export function RentSummary(props: Props) {
  const titleId = useId()
  const share = Math.min(1, props.received / Math.max(1, props.expected))
  return (
    <Stack
      component="section"
      aria-labelledby={titleId}
      sx={{
        bgcolor: 'primary.main',
        color: 'primary.contrastText',
        borderRadius: `${String(radii.hero)}px`,
        px: 2.25,
        py: 2,
        gap: 1,
      }}
    >
      <Typography
        id={titleId}
        component="h2"
        sx={{ fontSize: 13, fontWeight: 800, color: colors.onPlumMuted }}
      >
        {rentSummaryTitle(props.month)}
      </Typography>
      <Typography
        sx={{
          display: 'flex',
          flexWrap: 'wrap',
          alignItems: 'baseline',
          columnGap: 1,
        }}
      >
        <Box
          component="span"
          sx={{ fontSize: 28, fontWeight: 900, lineHeight: 1.1 }}
        >
          {formatMoney(props.received)}
        </Box>{' '}
        <Box
          component="span"
          sx={{ fontSize: 15, fontWeight: 800, color: colors.onPlumMuted }}
        >
          {m.rent_summary_of_expected({
            expected: formatMoney(props.expected),
          })}
        </Box>
      </Typography>
      <Box
        aria-hidden="true"
        sx={{
          height: 8,
          borderRadius: 4,
          bgcolor: colors.onPlumTint,
          overflow: 'hidden',
        }}
      >
        <Box
          sx={{
            width: `${String(Math.round(share * 100))}%`,
            height: 8,
            borderRadius: 4,
            bgcolor: 'accent.main',
          }}
        />
      </Box>
      {props.late > 0 ? (
        <Typography
          sx={{ fontSize: 13, fontWeight: 700, color: colors.onPlumMuted }}
        >
          {m.rent_summary_late({ count: props.late })}
        </Typography>
      ) : null}
    </Stack>
  )
}