All files / src/features/transactions TransactionsPage.tsx

100% Statements 36/36
100% Branches 26/26
100% Functions 10/10
100% Lines 36/36

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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369                                                        4x               21x                       6x 6x 15x 15x 15x 10x           10x   15x   6x       25x 25x                                     4x 4x 4x   4x   4x 4x 4x   4x     10x                                       12x 12x 12x     12x                                                                                                                 5x 5x 5x 5x                                                                       9x 9x           1x                                                                                                                                       8x 8x                                                 12x                                                                                            
// A flat's Entries page (Design canvas: Kirjaukset): one year's entries by
// month, newest first, with the year's income and expenses (openspec:
// transactions, "The entries page"). Owners get New entry.
import Box from '@mui/material/Box'
import Button from '@mui/material/Button'
import IconButton from '@mui/material/IconButton'
import Link from '@mui/material/Link'
import Stack from '@mui/material/Stack'
import Typography from '@mui/material/Typography'
import { createLink } from '@tanstack/react-router'
import {
  formatDayMonth,
  formatMoney,
  formatMoneyChange,
  formatMonth,
} from '#/lib/format'
import { m } from '#/paraglide/messages.js'
import { colors, radii } from '#/theme/tokens'
import { categoryLabel } from './labels'
import type { TransactionRow } from './transaction.repository.server'
import type { TransactionsView } from './transactions.server'
 
interface Props {
  view: TransactionsView
  onBack: () => void
  onYear: (year: number) => void
}
 
const card = {
  bgcolor: 'background.paper',
  border: `1px solid ${colors.cardBorder}`,
  borderRadius: `${String(radii.card)}px`,
} as const
 
/** The amount with its sign: income +, expense −. */
export function signedAmount(entry: Pick<TransactionRow, 'type' | 'amount'>) {
  return formatMoneyChange(
    entry.type === 'income' ? entry.amount : -entry.amount,
  )
}
 
/** The entries grouped by month, keeping their order (newest first). */
export function byMonth(entries: readonly TransactionRow[]) {
  const months: {
    key: string
    year: number
    month: number
    entries: TransactionRow[]
  }[] = []
  for (const entry of entries) {
    const key = entry.date.slice(0, 7)
    let group = months.at(-1)
    if (group?.key !== key) {
      group = {
        key,
        year: Number(key.slice(0, 4)),
        month: Number(key.slice(5, 7)),
        entries: [],
      }
      months.push(group)
    }
    group.entries.push(entry)
  }
  return months
}
 
function Icon(props: { path: string; size?: number; label?: string }) {
  const size = props.size ?? 24
  return (
    <svg
      width={size}
      height={size}
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      strokeWidth="2.4"
      strokeLinecap="round"
      strokeLinejoin="round"
      {...(props.label
        ? { role: 'img', 'aria-label': props.label }
        : { 'aria-hidden': true })}
    >
      <path d={props.path} />
    </svg>
  )
}
 
const TextLink = createLink(Link)
const RowLink = createLink(Link)
const ButtonLink = createLink(Button)
 
const PLUS = 'M12 5 V19 M5 12 H19'
 
const BACK = 'M15 5 L8 12 L15 19'
const UP = 'M7 17 L17 7 M9 7 H17 V15'
const DOWN = 'M17 7 L7 17 M7 9 V17 H15'
const CLIP =
  'M20 11.5 L12.5 19 a5 5 0 0 1 -7 -7 L13 4.5 a3.5 3.5 0 0 1 5 5 L10.5 17 a2 2 0 0 1 -3 -3 L14.5 7'
 
function Total(props: { label: string; amount: string }) {
  return (
    <Stack sx={{ ...card, borderRadius: '18px', px: 1.75, py: 1.5, gap: 0.25 }}>
      <Typography
        variant="body2"
        sx={{ fontWeight: 800, color: 'text.secondary' }}
      >
        {props.label}
      </Typography>
      <Typography sx={{ fontSize: 22, fontWeight: 900 }}>
        {props.amount}
      </Typography>
    </Stack>
  )
}
 
export function EntryRow(props: {
  entry: TransactionRow
  propertyId: string
  first: boolean
}) {
  const { entry } = props
  const income = entry.type === 'income'
  const details = [formatDayMonth(entry.date), entry.note]
    .filter(Boolean)
    .join(' · ')
  return (
    <Box
      component="li"
      sx={{ borderTop: props.first ? 'none' : `1px solid ${colors.divider}` }}
    >
      <RowLink
        to="/properties/$propertyId/transactions/$transactionId"
        params={{ propertyId: props.propertyId, transactionId: entry.id }}
        underline="none"
        sx={{
          display: 'flex',
          alignItems: 'center',
          gap: 1.5,
          minHeight: 64,
          px: 1.75,
          py: 1,
          color: 'text.primary',
        }}
      >
        <Box
          sx={{
            width: 38,
            height: 38,
            borderRadius: '19px',
            flexShrink: 0,
            display: 'flex',
            alignItems: 'center',
            justifyContent: 'center',
            bgcolor: income ? colors.lavenderTint : colors.yellowTint,
          }}
        >
          <Icon path={income ? DOWN : UP} size={18} />
        </Box>
        <Stack sx={{ flex: 1, minWidth: 0 }}>
          <Typography sx={{ fontSize: 15, fontWeight: 900 }}>
            {categoryLabel[entry.category]()}
          </Typography>
          <Typography variant="body2" color="text.secondary" noWrap>
            {details}
          </Typography>
        </Stack>
        {entry.hasReceipt ? (
          <Box sx={{ color: 'text.secondary', display: 'flex' }}>
            <Icon path={CLIP} size={18} label={m.transactions_has_receipt()} />
          </Box>
        ) : null}
        <Typography
          sx={{ fontSize: 16, fontWeight: 900, whiteSpace: 'nowrap' }}
        >
          {signedAmount(entry)}
        </Typography>
      </RowLink>
    </Box>
  )
}
 
export function TransactionsPage(props: Props) {
  const { view } = props
  const isOwner = view.role === 'owner'
  const months = byMonth(view.entries)
  return (
    <Stack sx={{ minHeight: '100dvh' }}>
      <Box
        component="header"
        sx={{
          display: 'grid',
          gridTemplateColumns: '44px 1fr 44px',
          alignItems: 'center',
          px: 1.5,
          pt: 2,
          pb: 0.5,
        }}
      >
        <IconButton aria-label={m.amounts_back()} onClick={props.onBack}>
          <Icon path={BACK} />
        </IconButton>
        <Stack sx={{ alignItems: 'center', minWidth: 0 }}>
          <Typography variant="h3" component="h1" sx={{ fontSize: 18 }}>
            {m.transactions_title()}
          </Typography>
          <Typography variant="body2" color="text.secondary" noWrap>
            {view.property.streetAddress}
          </Typography>
        </Stack>
      </Box>
      <Stack
        component="main"
        sx={{ flex: 1, gap: 1.5, px: 2.5, pt: 1.25, pb: 3 }}
      >
        <Stack
          role="group"
          aria-label={m.transactions_year()}
          direction="row"
          sx={{ gap: 1, flexWrap: 'wrap' }}
        >
          {view.years.map((year) => {
            const pressed = year === view.year
            return (
              <Button
                key={year}
                variant={pressed ? 'contained' : 'outlined'}
                aria-pressed={pressed}
                onClick={() => {
                  props.onYear(year)
                }}
                sx={{ borderRadius: 999, minHeight: 44, px: 2.25 }}
              >
                {String(year)}
              </Button>
            )
          })}
        </Stack>
        <Box
          component="section"
          aria-label={m.transactions_totals()}
          sx={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 1.25 }}
        >
          <Total
            label={m.transactions_income()}
            amount={formatMoneyChange(view.totals.income)}
          />
          <Total
            label={m.transactions_expenses()}
            amount={
              view.totals.expense === 0
                ? formatMoney(0)
                : formatMoneyChange(-view.totals.expense)
            }
          />
        </Box>
        <Box
          sx={{
            bgcolor: colors.lavenderTint,
            borderRadius: `${String(radii.tile)}px`,
            px: 1.75,
            py: 1.5,
            fontSize: 13,
            fontWeight: 700,
            lineHeight: 1.4,
          }}
        >
          {m.transactions_computed_note()}{' '}
          <TextLink
            to="/properties/$propertyId/amounts"
            params={{ propertyId: view.property.id }}
            sx={{ fontWeight: 900 }}
          >
            {m.amounts_title()}
          </TextLink>
        </Box>
        {months.length === 0 ? (
          <Stack
            component="section"
            sx={{
              ...card,
              px: 2.5,
              py: 3,
              gap: 1.25,
              alignItems: 'center',
              textAlign: 'center',
            }}
          >
            <Typography variant="h3" component="h2" sx={{ fontSize: 17 }}>
              {m.transactions_empty_title({ year: String(view.year) })}
            </Typography>
            <Typography variant="body2" color="text.secondary">
              {m.transactions_empty_text()}
            </Typography>
          </Stack>
        ) : (
          months.map((group) => {
            const heading = formatMonth(group.year, group.month)
            return (
              <Stack
                key={group.key}
                component="section"
                aria-label={heading}
                sx={{ gap: 0.75 }}
              >
                <Typography
                  variant="h3"
                  component="h2"
                  sx={{ fontSize: 15, mt: 0.5 }}
                >
                  {heading}
                </Typography>
                <Box
                  component="ul"
                  sx={{
                    ...card,
                    m: 0,
                    p: 0,
                    listStyle: 'none',
                    overflow: 'hidden',
                  }}
                >
                  {group.entries.map((entry, i) => (
                    <EntryRow
                      key={entry.id}
                      entry={entry}
                      propertyId={view.property.id}
                      first={i === 0}
                    />
                  ))}
                </Box>
              </Stack>
            )
          })
        )}
        {isOwner ? null : (
          <Typography variant="body2" color="text.secondary">
            {m.transactions_member_note()}
          </Typography>
        )}
      </Stack>
      {isOwner ? (
        <Box
          component="footer"
          sx={{
            position: 'sticky',
            bottom: 0,
            px: 2.5,
            pt: 1.5,
            pb: 3.5,
            bgcolor: 'background.default',
            borderTop: `1px solid ${colors.cardBorder}`,
          }}
        >
          <ButtonLink
            to="/transactions/new"
            search={{ propertyId: view.property.id }}
            variant="contained"
            fullWidth
            startIcon={<Icon path={PLUS} size={20} />}
            sx={{ minHeight: 56, borderRadius: 999, fontSize: 17 }}
          >
            {m.transactions_new()}
          </ButtonLink>
        </Box>
      ) : null}
    </Stack>
  )
}