import { useMemo } from 'react'; import { ActionIcon, Box, Card, Divider, Group, Progress, SimpleGrid, Stack, Text, Tooltip } from '@mantine/core'; import { Building2, Eye, MapPin, Package, Pencil, Weight } from 'lucide-react'; import { useQuery } from '@tanstack/react-query'; import { api } from '@/services/api'; import type { Warehouse } from '@/types/warehouse'; import { WarehouseStatusBadge, WarehouseTypeBadge } from './badges'; import { formatCapacity } from './options'; interface WarehouseCardViewProps { warehouses: Warehouse[]; onView: (warehouse: Warehouse) => void; onEdit: (warehouse: Warehouse) => void; } export function WarehouseCardView({ warehouses, onView, onEdit }: WarehouseCardViewProps) { const { data: stations } = useQuery( api.stations.list.queryOptions({ staleTime: 5 * 60 * 1000 }), ); const stationNameById = useMemo( () => new Map((stations ?? []).map((s) => [s.id, s.name])), [stations], ); if (warehouses.length === 0) { return ( No warehouses found. ); } return ( {warehouses.map((warehouse) => ( {warehouse.name} {warehouse.code} {warehouse.stationId && stationNameById.get(warehouse.stationId) && ( {stationNameById.get(warehouse.stationId)} )} {warehouse.locationName && ( {warehouse.locationName} )} onView(warehouse)} aria-label="View warehouse"> onEdit(warehouse)} aria-label="Edit warehouse"> ))} ); } const capacityPercent = (current?: number | null, capacity?: number | null) => { if (!capacity || capacity <= 0) return 0; return Math.min(100, Math.max(0, ((current ?? 0) / capacity) * 100)); }; function CapacityRow({ icon: Icon, label, current, capacity, }: { icon: typeof Weight; label: string; current?: number | null; capacity?: number | null; }) { const percent = capacityPercent(current, capacity); const color = percent >= 90 ? 'red' : percent >= 70 ? 'orange' : 'green'; return ( {label} {formatCapacity(Number(current) || 0, capacity)} ); }