All files / src/features/properties PropertyList.tsx

100% Statements 12/12
100% Branches 23/23
100% Functions 10/10
100% Lines 11/11

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                                                                            10x                                                               9x 9x                                                                                                                                                                         3x 3x               1x                                                         1x                       2x                                                                 8x                                                                                 8x                                                 8x                                  
// The flats list, the home page (Design canvas: "Asunnot"): the header, the
// title with the count, and a card per flat. Owners, the tenancy and rent
// statuses and this month's rent summary come from their own features
// through the slots.
import Box from '@mui/material/Box'
import Button from '@mui/material/Button'
import IconButton from '@mui/material/IconButton'
import Stack from '@mui/material/Stack'
import Typography from '@mui/material/Typography'
import { Link } from '@tanstack/react-router'
import { useState, type ReactNode } from 'react'
import { Logo, LogoMark } from '#/components/Logo'
import { formatArea, formatDate } from '#/lib/format'
import { m } from '#/paraglide/messages.js'
import { colors, radii } from '#/theme/tokens'
import type { ArchivedProperty, Property } from './property'
import { ownershipLine, type CardOwner } from './ownership-line'
import { roomCountLabel } from './room-count'
 
interface Props {
  properties: readonly Property[]
  /** The current owners by flat id, for each card's ownership line. */
  owners?: Readonly<Record<string, readonly CardOwner[]>>
  /** Each card's tenancy status by flat id, e.g. a chip. */
  statuses?: Readonly<Record<string, ReactNode>>
  /** Each card's rent status by flat id, next to the tenancy status. */
  rentStatuses?: Readonly<Partial<Record<string, ReactNode>>>
  /** This month's rent summary, above the notices and the list. */
  summary?: ReactNode
  /** The bottom navigation bar. */
  bottomNav?: ReactNode
  /** Shown above the list or the empty state, e.g. flat invitations. */
  notices?: ReactNode
  /** The flats the user has archived, in a section after the list. */
  archived?: readonly ArchivedProperty[]
}
 
function PlusIcon() {
  return (
    <svg
      width="22"
      height="22"
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      strokeWidth="2.6"
      strokeLinecap="round"
      aria-hidden="true"
    >
      <path d="M12 5 V19 M5 12 H19" />
    </svg>
  )
}
 
export function PropertyCard({
  property,
  owners = [],
  status,
  rentStatus,
  archivedOn,
}: {
  property: Property
  /** For an archived flat's card, in place of the statuses. */
  archivedOn?: string
  owners?: readonly CardOwner[] | undefined
  /** The tenancy status, e.g. a chip. */
  status?: ReactNode
  /** This month's rent status, e.g. a chip. */
  rentStatus?: ReactNode
}) {
  const ownership = ownershipLine(owners)
  return (
    <Link
      to="/properties/$propertyId"
      params={{ propertyId: property.id }}
      style={{ display: 'block', color: 'inherit', textDecoration: 'none' }}
    >
      <Box
        sx={{
          display: 'flex',
          alignItems: 'center',
          gap: 1.75,
          p: 1.75,
          bgcolor: 'background.paper',
          border: `1px solid ${colors.cardBorder}`,
          borderRadius: `${String(radii.card)}px`,
        }}
      >
        <Box
          sx={{
            width: 56,
            height: 56,
            flexShrink: 0,
            display: 'flex',
            alignItems: 'center',
            justifyContent: 'center',
            borderRadius: '18px',
            bgcolor: colors.yellowTint,
          }}
        >
          <LogoMark size={34} />
        </Box>
        <Stack sx={{ minWidth: 0 }}>
          <Typography variant="subtitle1" component="span">
            {property.streetAddress}
          </Typography>
          <Typography variant="body2" component="span" color="text.secondary">
            {m.property_card_details({
              city: property.city,
              rooms: roomCountLabel(property.roomCount),
              area: formatArea(property.floorArea),
            })}
          </Typography>
          {ownership === '' ? null : (
            <Typography
              variant="caption"
              component="span"
              color="text.secondary"
              sx={{ fontWeight: 800 }}
            >
              {ownership}
            </Typography>
          )}
          {archivedOn === undefined ? null : (
            <Typography
              variant="caption"
              component="span"
              color="text.secondary"
              sx={{ fontWeight: 800 }}
            >
              {m.property_archived_on({ date: formatDate(archivedOn) })}
            </Typography>
          )}
          {status || rentStatus ? (
            <Stack
              direction="row"
              sx={{
                alignItems: 'center',
                columnGap: 1,
                rowGap: 0.5,
                flexWrap: 'wrap',
                mt: 0.5,
              }}
            >
              {status}
              {rentStatus}
            </Stack>
          ) : null}
        </Stack>
      </Box>
    </Link>
  )
}
 
/** The archived flats, closed at first (spec: "Archived flats on the flats list"). */
function ArchivedSection(props: { flats: readonly ArchivedProperty[] }) {
  const [open, setOpen] = useState(false)
  return (
    <Stack component="section" sx={{ gap: 1.25, mt: 1 }}>
      <Button
        variant="text"
        color="inherit"
        aria-expanded={open}
        aria-controls="archived-flats"
        onClick={() => {
          setOpen((was) => !was)
        }}
        sx={{ alignSelf: 'flex-start', color: 'text.secondary' }}
        endIcon={
          <svg
            width="18"
            height="18"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            strokeWidth="2.4"
            strokeLinecap="round"
            strokeLinejoin="round"
            aria-hidden="true"
            style={{ transform: open ? 'rotate(180deg)' : undefined }}
          >
            <path d="M6 9 L12 15 L18 9" />
          </svg>
        }
      >
        {m.properties_archived({ count: props.flats.length })}
      </Button>
      {open ? (
        <Stack
          id="archived-flats"
          component="ul"
          sx={{ gap: 1.25, m: 0, p: 0, listStyle: 'none' }}
        >
          {props.flats.map((flat) => (
            <li key={flat.id}>
              <PropertyCard property={flat} archivedOn={flat.archivedOn} />
            </li>
          ))}
        </Stack>
      ) : null}
    </Stack>
  )
}
 
/** No flats yet: what Kotivalo is for, and the way to the first flat. */
function EmptyState() {
  return (
    <Stack
      sx={{
        alignItems: 'center',
        gap: 1,
        px: 3,
        py: 4,
        textAlign: 'center',
        border: `1.5px dashed ${colors.dashed}`,
        borderRadius: `${String(radii.card)}px`,
      }}
    >
      <LogoMark size={72} roof={colors.plum} />
      <Typography variant="h3" component="h2" sx={{ mt: 1 }}>
        {m.properties_empty_title()}
      </Typography>
      <Typography variant="body2" color="text.secondary">
        {m.properties_empty_text()}
      </Typography>
      <Button
        component={Link}
        to="/properties/new"
        color="accent"
        startIcon={<PlusIcon />}
        sx={{ mt: 1.5 }}
      >
        {m.property_add()}
      </Button>
    </Stack>
  )
}
 
function Header(): ReactNode {
  return (
    <Stack
      component="header"
      direction="row"
      sx={{
        alignItems: 'center',
        justifyContent: 'space-between',
        pl: 2.5,
        pr: 1.5,
        pt: 2,
        pb: 0.5,
      }}
    >
      <Logo size={28} />
      <Stack direction="row" sx={{ alignItems: 'center', gap: 1 }}>
        <IconButton
          component={Link}
          to="/properties/new"
          aria-label={m.property_add()}
          sx={{
            border: `1.5px solid ${colors.inputBorder}`,
            bgcolor: 'background.paper',
          }}
        >
          <PlusIcon />
        </IconButton>
      </Stack>
    </Stack>
  )
}
 
export function PropertyList({
  properties,
  owners = {},
  statuses = {},
  rentStatuses = {},
  summary,
  bottomNav,
  notices,
  archived = [],
}: Props) {
  return (
    <Stack sx={{ minHeight: '100dvh' }}>
      <Header />
      <Stack
        component="main"
        sx={{ flex: 1, gap: 1.75, px: 2.5, pt: 1, pb: 4 }}
      >
        <Stack>
          <Typography variant="h1">{m.properties_title()}</Typography>
          {properties.length === 0 ? null : (
            <Typography variant="body2" color="text.secondary">
              {m.properties_count({ count: properties.length })}
            </Typography>
          )}
        </Stack>
        {summary}
        {notices}
        {properties.length === 0 ? (
          <EmptyState />
        ) : (
          <Stack
            component="ul"
            sx={{ gap: 1.25, m: 0, p: 0, listStyle: 'none' }}
          >
            {properties.map((property) => (
              <li key={property.id}>
                <PropertyCard
                  property={property}
                  owners={owners[property.id]}
                  status={statuses[property.id]}
                  rentStatus={rentStatuses[property.id]}
                />
              </li>
            ))}
          </Stack>
        )}
        {archived.length === 0 ? null : <ArchivedSection flats={archived} />}
      </Stack>
      {bottomNav}
    </Stack>
  )
}