All files / src/routes/_authed index.tsx

7.14% Statements 1/14
0% Branches 0/4
0% Functions 0/6
7.14% Lines 1/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                                  1x                                                                                                                                  
import { BottomNav } from '#/components/BottomNav'
import { NavigationNotice } from '#/components/NavigationNotice'
import { createFileRoute, useRouter } from '@tanstack/react-router'
import { listProperties } from '#/features/properties/functions'
import { listCurrentOwners } from '#/features/ownership/functions'
import type { CardOwner } from '#/features/properties/ownership-line'
import { PropertyList } from '#/features/properties/PropertyList'
import { CardRentChip } from '#/features/rent-payments/CardRentChip'
import { listRentThisMonth } from '#/features/rent-payments/functions'
import { RentSummary } from '#/features/rent-payments/RentSummary'
import { rentThisMonth } from '#/features/rent-payments/status'
import { listMyInvitations } from '#/features/sharing/functions'
import { InvitationCards } from '#/features/sharing/InvitationCards'
import { FlatStatusChip } from '#/features/tenancies/FlatStatusChip'
import { listTenancyStatuses } from '#/features/tenancies/functions'
import { m } from '#/paraglide/messages.js'
 
export const Route = createFileRoute('/_authed/')({
  loader: async () => {
    const [lists, invitations, shares, statuses, rent] = await Promise.all([
      listProperties(),
      listMyInvitations(),
      listCurrentOwners(),
      listTenancyStatuses(),
      listRentThisMonth(),
    ])
    const owners: Record<string, CardOwner[]> = {}
    for (const { propertyId, ...owner } of shares) {
      ;(owners[propertyId] ??= []).push(owner)
    }
    return { lists, invitations, owners, statuses, rent }
  },
  head: () => ({ meta: [{ title: m.properties_page_title() }] }),
  component: HomeRoute,
})
 
function HomeRoute() {
  const { lists, invitations, owners, statuses, rent } = Route.useLoaderData()
  const router = useRouter()
  // Computed in the browser from raw data and the server's today, as on the
  // flat details page.
  const thisMonth = rentThisMonth(rent.flats, rent.today)
  return (
    <>
      <NavigationNotice />
      <PropertyList
        properties={lists.properties}
        archived={lists.archived}
        owners={owners}
        statuses={Object.fromEntries(
          Object.entries(statuses).map(([id, status]) => [
            id,
            <FlatStatusChip key={id} status={status} />,
          ]),
        )}
        rentStatuses={Object.fromEntries(
          Object.entries(thisMonth.rows).map(([id, row]) => [
            id,
            row ? <CardRentChip key={id} row={row} /> : null,
          ]),
        )}
        summary={
          thisMonth.hasRent ? (
            <RentSummary
              month={rent.today.slice(0, 7)}
              received={thisMonth.received}
              expected={thisMonth.expected}
              late={thisMonth.late}
            />
          ) : null
        }
        bottomNav={<BottomNav current="flats" />}
        notices={
          <InvitationCards
            invitations={invitations}
            onAnswered={() => router.invalidate()}
          />
        }
      />
    </>
  )
}