All files / src/features/documents DocumentsPage.tsx

100% Statements 23/23
100% Branches 22/22
100% Functions 9/9
100% Lines 23/23

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                                                  4x           4x 4x   4x 4x 4x     55x 55x                                             18x 18x                                                                                                     7x 7x 7x 7x 7x 2x   7x                                                                                                               12x 18x 18x           2x                   11x                                               16x                                                                                                            
// A flat's documents page (Design canvas: Dokumentit): the documents grouped
// by kind, with filter buttons; owners add and see their storage use
// (openspec: documents, "The documents page").
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 { useState } from 'react'
import { formatDate, formatFileSize } from '#/lib/format'
import { m } from '#/paraglide/messages.js'
import type { DocumentKind } from '#/server/db/schema'
import { STORAGE_QUOTA } from '#/server/files/quota'
import { colors, radii } from '#/theme/tokens'
import type { DocumentRow } from './document.repository.server'
import type { DocumentsView } from './documents.server'
import { groupByKind, kindHeading, kindLabel } from './kinds'
 
interface Props {
  view: DocumentsView
  onBack: () => void
}
 
const card = {
  bgcolor: 'background.paper',
  border: `1px solid ${colors.cardBorder}`,
  borderRadius: `${String(radii.card)}px`,
} as const
 
const RowLink = createLink(Link)
const ButtonLink = createLink(Button)
 
const BACK = 'M15 5 L8 12 L15 19'
const PLUS = 'M12 5 V19 M5 12 H19'
const FILE = 'M7 3 H14 L18 7 V21 H7 Z M14 3 V7 H18'
 
export function Icon(props: { path: string; size?: number }) {
  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"
      aria-hidden="true"
    >
      <path d={props.path} />
    </svg>
  )
}
 
/** One document in a list: name, then kind, date and size. */
export function DocumentRowItem(props: {
  document: DocumentRow
  propertyId: string
  first: boolean
}) {
  const { document } = props
  return (
    <Box
      component="li"
      sx={{ borderTop: props.first ? 'none' : `1px solid ${colors.divider}` }}
    >
      <RowLink
        to="/properties/$propertyId/documents/$documentId"
        params={{ propertyId: props.propertyId, documentId: document.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: colors.lavenderTint,
          }}
        >
          <Icon path={FILE} size={18} />
        </Box>
        <Stack sx={{ flex: 1, minWidth: 0 }}>
          <Typography noWrap sx={{ fontSize: 15, fontWeight: 900 }}>
            {document.name}
          </Typography>
          <Typography variant="body2" color="text.secondary" noWrap>
            {[
              kindLabel[document.kind](),
              formatDate(document.date),
              formatFileSize(document.size),
            ].join(' ยท ')}
          </Typography>
        </Stack>
      </RowLink>
    </Box>
  )
}
 
export function DocumentsPage(props: Props) {
  const { view } = props
  const isOwner = view.role === 'owner'
  const [filter, setFilter] = useState<DocumentKind>()
  const groups = groupByKind(view.documents)
  const shown = filter
    ? groups.filter((group) => group.kind === filter)
    : groups
  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.documents_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 }}
      >
        {groups.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.documents_empty_title()}
            </Typography>
            <Typography variant="body2" color="text.secondary">
              {m.documents_empty_text()}
            </Typography>
          </Stack>
        ) : (
          <>
            <Stack
              role="group"
              aria-label={m.documents_filter()}
              direction="row"
              sx={{ gap: 1, flexWrap: 'wrap' }}
            >
              {[undefined, ...groups.map((group) => group.kind)].map((kind) => {
                const pressed = kind === filter
                return (
                  <Button
                    key={kind ?? 'all'}
                    variant={pressed ? 'contained' : 'outlined'}
                    aria-pressed={pressed}
                    onClick={() => {
                      setFilter(kind)
                    }}
                    sx={{ borderRadius: 999, minHeight: 44, px: 2.25 }}
                  >
                    {kind ? kindHeading[kind]() : m.documents_all()}
                  </Button>
                )
              })}
            </Stack>
            {shown.map((group) => (
              <Stack
                key={group.kind}
                component="section"
                aria-label={kindHeading[group.kind]()}
                sx={{ gap: 0.75 }}
              >
                <Typography
                  variant="h3"
                  component="h2"
                  sx={{ fontSize: 15, mt: 0.5 }}
                >
                  {kindHeading[group.kind]()}
                </Typography>
                <Box
                  component="ul"
                  sx={{
                    ...card,
                    m: 0,
                    p: 0,
                    listStyle: 'none',
                    overflow: 'hidden',
                  }}
                >
                  {group.documents.map((document, i) => (
                    <DocumentRowItem
                      key={document.id}
                      document={document}
                      propertyId={view.property.id}
                      first={i === 0}
                    />
                  ))}
                </Box>
              </Stack>
            ))}
          </>
        )}
        {isOwner && view.usage ? (
          <Typography variant="body2" color="text.secondary">
            {m.documents_usage({
              used: formatFileSize(view.usage.bytes),
              limit: formatFileSize(STORAGE_QUOTA.bytes),
            })}
          </Typography>
        ) : null}
        {isOwner ? null : (
          <Typography variant="body2" color="text.secondary">
            {m.documents_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="/properties/$propertyId/documents/new"
            params={{ propertyId: view.property.id }}
            variant="contained"
            fullWidth
            startIcon={<Icon path={PLUS} size={20} />}
            sx={{ minHeight: 56, borderRadius: 999, fontSize: 17 }}
          >
            {m.documents_new()}
          </ButtonLink>
        </Box>
      ) : null}
    </Stack>
  )
}