All files / src/features/transactions TransactionDetails.tsx

100% Statements 14/14
96.87% Branches 31/32
100% Functions 5/5
100% Lines 14/14

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                                                                  2x             35x                                                 8x                                   9x 9x 9x 9x 9x         9x 9x 9x 9x                                                                                                                                                                                                                                                                                                                                                                           1x                                                                                                             1x              
// An entry's details (Design canvas: "Menon tiedot", "Tulon tiedot · ei
// kuittia", "Kirjauksen tiedot · jäsen"): the signed amount, category,
// deductibility, flat, date, the year summary row, who logged it and the
// receipt (openspec: transactions, "Entry details").
import Box from '@mui/material/Box'
import Button from '@mui/material/Button'
import ButtonBase from '@mui/material/ButtonBase'
import IconButton from '@mui/material/IconButton'
import Stack from '@mui/material/Stack'
import Typography from '@mui/material/Typography'
import { useState, type ReactNode } from 'react'
import { ImageViewer } from '#/components/ImageViewer'
import type { Property } from '#/features/properties/property'
import { formatDate, formatDay, formatFileSize } from '#/lib/format'
import { m } from '#/paraglide/messages.js'
import type { PropertyRole } from '#/server/db/schema'
import { colors, radii } from '#/theme/tokens'
import { CATEGORIES } from './categories'
import { categoryLabel, receiptUrl, summaryRowLabel } from './labels'
import type { TransactionDetails as Entry } from './transaction.repository.server'
import { signedAmount } from './TransactionsPage'
 
interface Props {
  entry: Entry
  property: Property
  role: PropertyRole
  /** Whether the signed-in user logged it, shown as "You". */
  loggedByYou: boolean
  onBack: () => void
  /** The edit button, for owners. */
  editAction?: ReactNode
}
 
const card = {
  bgcolor: 'background.paper',
  border: `1px solid ${colors.cardBorder}`,
  borderRadius: `${String(radii.card)}px`,
} as const
 
function Row(props: { label: string; value: string; first?: boolean }) {
  return (
    <Stack
      direction="row"
      sx={{
        justifyContent: 'space-between',
        alignItems: 'center',
        gap: 2,
        minHeight: 46,
        borderTop: props.first ? 'none' : `1px solid ${colors.divider}`,
      }}
    >
      <Typography
        variant="body2"
        sx={{ fontWeight: 700, color: 'text.secondary' }}
      >
        {props.label}
      </Typography>
      <Typography variant="body2" sx={{ fontWeight: 800, textAlign: 'right' }}>
        {props.value}
      </Typography>
    </Stack>
  )
}
 
function DownloadIcon() {
  return (
    <svg
      width="18"
      height="18"
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      strokeWidth="2.4"
      strokeLinecap="round"
      strokeLinejoin="round"
      aria-hidden="true"
    >
      <path d="M12 4 V15 M7 10 L12 15 L17 10 M5 20 H19" />
    </svg>
  )
}
 
export function TransactionDetails(props: Props) {
  const { entry } = props
  const [viewing, setViewing] = useState(false)
  const income = entry.type === 'income'
  const { row } = CATEGORIES[entry.category]
  const treatment = income
    ? m.transactions_taxable()
    : row
      ? m.transactions_deductible()
      : m.transactions_not_deducted()
  const receipt = entry.receipt
  const isPdf = receipt?.contentType === 'application/pdf'
  const author = props.loggedByYou ? m.sharing_you() : entry.createdBy.name
  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.transactions_back()} onClick={props.onBack}>
          <svg
            width="24"
            height="24"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            strokeWidth="2.4"
            strokeLinecap="round"
            strokeLinejoin="round"
            aria-hidden="true"
          >
            <path d="M15 5 L8 12 L15 19" />
          </svg>
        </IconButton>
        <Typography
          variant="h3"
          component="h1"
          sx={{ fontSize: 18, textAlign: 'center' }}
        >
          {income
            ? m.transactions_income_title()
            : m.transactions_expense_title()}
        </Typography>
      </Box>
      <Stack
        component="main"
        sx={{ flex: 1, gap: 1.75, px: 2.5, pt: 0.5, pb: 3 }}
      >
        <Stack
          sx={{
            alignItems: 'center',
            gap: 0.75,
            textAlign: 'center',
            pt: 0.75,
          }}
        >
          <Typography
            sx={{
              fontSize: 44,
              fontWeight: 900,
              letterSpacing: '-0.03em',
              lineHeight: 1.05,
            }}
          >
            {signedAmount(entry)}
          </Typography>
          {entry.note ? (
            <Typography sx={{ fontSize: 16, fontWeight: 800 }}>
              {entry.note}
            </Typography>
          ) : null}
          <Stack
            direction="row"
            sx={{ gap: 0.75, flexWrap: 'wrap', justifyContent: 'center' }}
          >
            <Box
              sx={{
                bgcolor: colors.plum,
                color: colors.cream,
                borderRadius: 999,
                px: 1.5,
                py: 0.5,
                fontSize: 12,
                fontWeight: 800,
              }}
            >
              {categoryLabel[entry.category]()}
            </Box>
            <Box
              sx={{
                bgcolor: colors.yellowTint,
                borderRadius: 999,
                px: 1.5,
                py: 0.5,
                fontSize: 12,
                fontWeight: 800,
              }}
            >
              {treatment}
            </Box>
          </Stack>
        </Stack>
        <Box sx={{ ...card, px: 2 }}>
          <Row
            first
            label={m.transactions_flat()}
            value={props.property.streetAddress}
          />
          <Row label={m.transactions_date()} value={formatDate(entry.date)} />
          {row && (
            <Row
              label={m.transactions_summary_row()}
              value={m.transactions_summary_row_value({
                year: entry.date.slice(0, 4),
                row: summaryRowLabel[row](),
              })}
            />
          )}
          <Row
            label={m.transactions_logged_by()}
            value={m.transactions_logged_by_value({
              name: author,
              date: formatDay(entry.createdAt),
            })}
          />
        </Box>
        <Stack
          component="section"
          aria-labelledby="receipt-heading"
          sx={{ gap: 1 }}
        >
          <Stack
            direction="row"
            sx={{
              justifyContent: 'space-between',
              alignItems: 'center',
              gap: 1,
            }}
          >
            <Typography
              id="receipt-heading"
              variant="h3"
              component="h2"
              sx={{ fontSize: 15 }}
            >
              {m.transactions_receipt()}
            </Typography>
            {receipt ? (
              <Typography
                variant="body2"
                color="text.secondary"
                noWrap
                sx={{ minWidth: 0 }}
              >
                {m.transactions_receipt_meta({
                  name: receipt.filename,
                  size: formatFileSize(receipt.size),
                })}
              </Typography>
            ) : null}
          </Stack>
          {!receipt ? (
            <Box
              sx={{
                ...card,
                borderRadius: `${String(radii.tile)}px`,
                px: 2,
                py: 1.75,
              }}
            >
              <Typography variant="body2" color="text.secondary">
                {m.transactions_no_receipt()}
              </Typography>
            </Box>
          ) : isPdf ? (
            <Button
              variant="outlined"
              href={receiptUrl(receipt.id)}
              target="_blank"
              rel="noopener"
              sx={{ minHeight: 64, borderRadius: `${String(radii.tile)}px` }}
            >
              {m.transactions_open_pdf()}
            </Button>
          ) : (
            <ButtonBase
              aria-label={m.transactions_open_receipt()}
              onClick={() => {
                setViewing(true)
              }}
              sx={{
                height: 214,
                borderRadius: `${String(radii.card)}px`,
                bgcolor: colors.sand,
                overflow: 'hidden',
              }}
            >
              <Box
                component="img"
                src={receiptUrl(receipt.id)}
                alt=""
                sx={{
                  maxHeight: '100%',
                  maxWidth: '100%',
                  objectFit: 'contain',
                }}
              />
            </ButtonBase>
          )}
        </Stack>
        <Box
          sx={{
            mt: 'auto',
            display: 'grid',
            gridTemplateColumns:
              receipt && props.editAction ? '1fr 1fr' : '1fr',
            gap: 1.25,
          }}
        >
          {receipt ? (
            <Button
              variant="outlined"
              href={receiptUrl(receipt.id, true)}
              startIcon={<DownloadIcon />}
              sx={{ minHeight: 52, borderRadius: 999 }}
            >
              {m.transactions_download()}
            </Button>
          ) : null}
          {props.role === 'owner' ? props.editAction : null}
        </Box>
      </Stack>
      {receipt && !isPdf ? (
        <ImageViewer
          src={receiptUrl(receipt.id)}
          downloadHref={receiptUrl(receipt.id, true)}
          filename={receipt.filename}
          title={m.transactions_receipt()}
          alt={m.transactions_receipt_alt({ name: receipt.filename })}
          closeLabel={m.transactions_close()}
          downloadLabel={m.transactions_download()}
          open={viewing}
          onClose={() => {
            setViewing(false)
          }}
        />
      ) : null}
    </Stack>
  )
}