import { useMemo } from 'react';
import type { ReactNode } from 'react';
import { ActionIcon, Group, Text } from '@mantine/core';
import {
Building2,
Eye,
MapPin,
Package,
Pencil,
Scale,
Warehouse as WarehouseIcon,
} from 'lucide-react';
import { DataTable, type ColumnDef } from '@edr/ui-common';
import { useQuery } from '@tanstack/react-query';
import { bookingTable } from '@/components/bookings/booking-ui.styles';
import { api } from '@/services/api';
import type { Warehouse } from '@/types/warehouse';
import { WarehouseStatusBadge, WarehouseTypeBadge } from './badges';
import { formatCapacity } from './options';
interface WarehouseTableProps {
warehouses: Warehouse[];
onView: (warehouse: Warehouse) => void;
onEdit: (warehouse: Warehouse) => void;
}
const HEADER = bookingTable.headerCell;
function CapacityCell({
current,
capacity,
icon,
}: {
current?: number | null;
capacity?: number | null;
icon: ReactNode;
}) {
const numericCurrent = Number(current) || 0;
const numericCapacity = Number(capacity) || 0;
const hasCapacity = numericCapacity > 0;
const ratio = hasCapacity ? Math.min(100, Math.max(0, (numericCurrent / numericCapacity) * 100)) : 0;
const isOverCapacity = hasCapacity && numericCurrent > numericCapacity;
return (
{icon}
{formatCapacity(numericCurrent, capacity)}
{hasCapacity ? (
) : null}
);
}
export function WarehouseTable({ warehouses, onView, onEdit }: WarehouseTableProps) {
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],
);
const columns: ColumnDef[] = [
{
id: 'code',
header: () => Warehouse,
cell: ({ row }) => (
{row.original.name}
),
},
{
id: 'facility',
header: () => Facility,
cell: ({ row }) => {
const name = row.original.stationId
? stationNameById.get(row.original.stationId)
: undefined;
return name ? (
{name}
) : (
-
);
},
},
{
id: 'type',
header: () => Type,
cell: ({ row }) => (
),
},
{
id: 'location',
header: () => Location,
cell: ({ row }) => (
{row.original.locationName ?? '-'}
),
},
{
id: 'weight',
header: () => Weight,
cell: ({ row }) => (
}
/>
),
},
{
id: 'containers',
header: () => Containers,
cell: ({ row }) => (
}
/>
),
},
{
id: 'status',
header: () => Status,
cell: ({ row }) => (
),
},
{
id: 'actions',
header: '',
cell: ({ row }) => (
e.stopPropagation()}>
onView(row.original)} title="View">
onEdit(row.original)} title="Edit">
),
},
];
return (
onView(warehouse)}
emptyMessage="No warehouses found."
containerClassName="border-0 bg-transparent shadow-none"
/>
);
}