import React, { useMemo } from 'react'; import { Pressable, ScrollView, StyleSheet, Text, View } from 'react-native'; import { Ionicons } from '@expo/vector-icons'; import { useLocalSearchParams, useRouter } from 'expo-router'; import { useQuery } from '@tanstack/react-query'; import { api, DocCategory, Document } from '@/api'; import { useT } from '@/i18n'; import { AppBar } from '@/components/AppBar'; import { Button, Card, IconButton, Loading, SectionTitle } from '@/components/ui'; import { DocumentRow } from '@/components/domain'; import { PropertyCover } from '@/components/PropertyCover'; import { categoryColor, colors, fontSize, radii, shadow, spacing, tint } from '@/theme'; export default function PropertyDetailScreen() { const { id } = useLocalSearchParams<{ id: string }>(); const propertyId = Number(id); const router = useRouter(); const t = useT(); const propertyQuery = useQuery({ queryKey: ['property', propertyId], queryFn: () => api.properties.get(propertyId), }); const docsQuery = useQuery({ queryKey: ['documents', propertyId], queryFn: () => api.documents.listByProperty(propertyId), }); const catsQuery = useQuery({ queryKey: ['categories'], queryFn: () => api.reference.categories() }); const docs: Document[] = docsQuery.data ?? []; const categories: DocCategory[] = catsQuery.data ?? []; const catName = (slug: string) => categories.find((c) => c.slug === slug)?.name ?? slug; const folders = useMemo(() => { const map = new Map(); for (const d of docs) { const entry = map.get(d.category) ?? { count: 0, expiring: 0 }; entry.count += 1; if (d.expiryStatus === 'expiring' || d.expiryStatus === 'expired') entry.expiring += 1; map.set(d.category, entry); } return Array.from(map.entries()).map(([slug, v]) => ({ slug, ...v })); }, [docs]); const expiringDocs = docs.filter((d) => d.expiryStatus === 'expiring' || d.expiryStatus === 'expired'); const recent = [...docs] .sort((a, b) => (b.uploadedAt > a.uploadedAt ? 1 : -1)) .slice(0, 3); const property = propertyQuery.data; const photoMissing = !property?.photoUrl; const addressLine = [property?.address, property?.zip, property?.city].filter(Boolean).join(', '); return ( router.push(`/property/edit/${propertyId}`)} /> } /> {propertyQuery.isLoading ? ( ) : ( {/* Tapping an empty cover is the shortest path to adding a photo. */} router.push(`/property/edit/${propertyId}`) : undefined} style={styles.coverWrap} > {property?.name} {addressLine ? ( {addressLine} ) : ( // Missing data is an invitation to fill it in, not a dash. router.push(`/property/edit/${propertyId}`)} > {t('property.addAddress')} )}