'use client';
import { ChevronDownIcon, ChevronRightIcon } from 'lucide-react';
import { useState } from 'react';
import { Button } from '@/components/ui/button';
import { Collapsible, CollapsibleContent } from '@/components/ui/collapsible';
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from '@/components/ui/table';
import { cn } from '@/lib/utils';
interface ShipmentRow {
id: string;
name: string;
service: string;
cost: number;
dispatch: string;
legs?: ShipmentRow[];
}
const shipments: ShipmentRow[] = [
{
id: 'HL-4102',
name: 'Nordic Retail Consolidation',
service: 'Ocean LCL',
cost: 38_400,
dispatch: '2025-02-03',
legs: [
{
id: 'HL-4102-A',
name: 'Oslo depot pickup',
service: 'Ground',
cost: 4_200,
dispatch: '2025-02-03',
},
{
id: 'HL-4102-B',
name: 'Rotterdam transshipment',
service: 'Ocean LCL',
cost: 27_900,
dispatch: '2025-02-08',
},
{
id: 'HL-4102-C',
name: 'Final mile to Bergen',
service: 'Ground',
cost: 6_300,
dispatch: '2025-02-19',
},
],
},
{
id: 'HL-4117',
name: 'Medical Devices Express',
service: 'Air Priority',
cost: 52_750,
dispatch: '2025-02-05',
legs: [
{
id: 'HL-4117-A',
name: 'Basel cold-chain collection',
service: 'Ground',
cost: 5_150,
dispatch: '2025-02-05',
},
{
id: 'HL-4117-B',
name: 'ZRH to ORD airfreight',
service: 'Air Priority',
cost: 41_600,
dispatch: '2025-02-06',
},
{
id: 'HL-4117-C',
name: 'Chicago customs clearance',
service: 'Brokerage',
cost: 6_000,
dispatch: '2025-02-07',
},
],
},
{
id: 'HL-4123',
name: 'Iberian Auto Parts Run',
service: 'Rail',
cost: 21_900,
dispatch: '2025-02-10',
legs: [
{
id: 'HL-4123-A',
name: 'Valencia to Lyon rail block',
service: 'Rail',
cost: 15_400,
dispatch: '2025-02-10',
},
{
id: 'HL-4123-B',
name: 'Lyon cross-dock and delivery',
service: 'Ground',
cost: 6_500,
dispatch: '2025-02-13',
},
],
},
{
id: 'HL-4131',
name: 'Dublin Warehouse Restock',
service: 'Ground',
cost: 9_650,
dispatch: '2025-02-12',
},
];
const GRID_COLS = 'grid grid-cols-[40px_110px_220px_110px_110px_110px]';
const HEADER_GRID_COLS =
'[&>tr]:grid [&>tr]:grid-cols-[40px_110px_220px_110px_110px_110px]';
interface AccordionRowProps {
row: ShipmentRow;
defaultOpen?: boolean;
}
function AccordionRow({ row, defaultOpen = false }: AccordionRowProps) {
const [isOpen, setIsOpen] = useState(defaultOpen);
const hasLegs = Boolean(row.legs && row.legs.length > 0);
return (
<>
<TableRow
className={cn(
GRID_COLS,
'bg-muted/50 hover:bg-muted/50',
isOpen && 'border-b-0'
)}
id={row.id}
>
<TableCell className="p-0">
<Button
aria-label={isOpen ? 'Collapse shipment' : 'Expand shipment'}
className={cn(
'h-full w-full rounded-none p-3 text-muted-foreground transition-colors',
hasLegs && 'hover:bg-transparent hover:text-foreground',
!hasLegs && 'cursor-default opacity-30'
)}
isDisabled={!hasLegs}
onPress={() => setIsOpen(!isOpen)}
size="icon"
variant="ghost"
>
{hasLegs ? (
isOpen ? (
<ChevronDownIcon className="size-4 transition-transform duration-200" />
) : (
<ChevronRightIcon className="size-4 transition-transform duration-200" />
)
) : (
<div className="size-4" />
)}
</Button>
</TableCell>
<TableCell className="p-3 font-medium font-mono text-muted-foreground text-sm">
{row.id}
</TableCell>
<TableCell className="truncate p-3 font-medium text-sm">
{row.name}
</TableCell>
<TableCell className="p-3 text-muted-foreground text-sm">
{row.service}
</TableCell>
<TableCell className="p-3 text-right font-mono font-semibold text-sm tabular-nums">
${row.cost.toLocaleString('en-US')}
</TableCell>
<TableCell className="p-3 text-muted-foreground text-sm">
{row.dispatch}
</TableCell>
</TableRow>
{hasLegs && (
<TableRow
className={cn(GRID_COLS, 'hover:bg-transparent')}
id={`${row.id}-legs`}
>
<TableCell className="col-span-6 p-0" colSpan={6}>
<Collapsible isExpanded={isOpen} onExpandedChange={setIsOpen}>
<CollapsibleContent>
<div className="w-full border-border border-b bg-muted/20">
<Table aria-label={`${row.name} legs`}>
<TableHeader
className={cn(
HEADER_GRID_COLS,
'[&>tr]:bg-muted/30 [&_tr:last-child]:border-b'
)}
>
<TableHead className="flex h-7 items-center border-border border-y px-3 py-1.5" />
<TableHead
className="flex h-7 items-center border-border border-y px-3 py-1.5 text-xs"
isRowHeader
>
Leg
</TableHead>
<TableHead className="flex h-7 items-center border-border border-y px-3 py-1.5 text-xs">
Description
</TableHead>
<TableHead className="flex h-7 items-center border-border border-y px-3 py-1.5 text-xs">
Service
</TableHead>
<TableHead className="flex h-7 items-center justify-end border-border border-y px-3 py-1.5 text-right text-xs">
Cost
</TableHead>
<TableHead className="flex h-7 items-center border-border border-y px-3 py-1.5 text-xs">
Dispatch
</TableHead>
</TableHeader>
<TableBody className="[&_tr:last-child]:border-b">
{row.legs?.map((leg) => (
<TableRow className={GRID_COLS} id={leg.id} key={leg.id}>
<TableCell className="px-3 py-2" />
<TableCell className="px-3 py-2 font-mono text-muted-foreground text-xs tabular-nums">
{leg.id}
</TableCell>
<TableCell className="truncate px-3 py-2 font-medium text-xs">
{leg.name}
</TableCell>
<TableCell className="px-3 py-2 text-muted-foreground text-xs">
{leg.service}
</TableCell>
<TableCell className="px-3 py-2 text-right font-mono font-semibold text-xs tabular-nums">
${leg.cost.toLocaleString('en-US')}
</TableCell>
<TableCell className="px-3 py-2 text-muted-foreground text-xs">
{leg.dispatch}
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</div>
</CollapsibleContent>
</Collapsible>
</TableCell>
</TableRow>
)}
</>
);
}
export default function Table01() {
return (
<div className="w-full max-w-5xl px-4">
<div className="mx-auto max-w-fit overflow-hidden rounded-lg border border-border bg-card shadow-sm">
<div className="overflow-x-auto">
<Table aria-label="Shipments">
<TableHeader className={cn(HEADER_GRID_COLS, '[&>tr]:bg-muted/50')}>
<TableHead className="p-3" />
<TableHead
className="p-3 font-semibold text-foreground text-sm"
isRowHeader
>
Ref
</TableHead>
<TableHead className="p-3 font-semibold text-foreground text-sm">
Shipment
</TableHead>
<TableHead className="p-3 font-semibold text-foreground text-sm">
Service
</TableHead>
<TableHead className="p-3 text-right font-semibold text-foreground text-sm">
Freight cost
</TableHead>
<TableHead className="p-3 font-semibold text-foreground text-sm">
Dispatch
</TableHead>
</TableHeader>
<TableBody>
{shipments.map((row, index) => (
<AccordionRow defaultOpen={index === 0} key={row.id} row={row} />
))}
</TableBody>
</Table>
</div>
</div>
</div>
);
}