import { useMemo } from 'react'; import { cn } from '@utils/cn'; import type { WarehouseLocation, LocationType } from '../types'; export interface WarehouseLayoutProps { locations: WarehouseLocation[]; onLocationClick?: (location: WarehouseLocation) => void; selectedLocationId?: string; className?: string; } const locationTypeColors: Record = { zone: 'bg-indigo-100 border-indigo-300 dark:bg-indigo-900/30 dark:border-indigo-700', aisle: 'bg-cyan-100 border-cyan-300 dark:bg-cyan-900/30 dark:border-cyan-700', rack: 'bg-teal-100 border-teal-300 dark:bg-teal-900/30 dark:border-teal-700', shelf: 'bg-green-100 border-green-300 dark:bg-green-900/30 dark:border-green-700', bin: 'bg-gray-100 border-gray-300 dark:bg-gray-700/50 dark:border-gray-600', }; interface LayoutNode { location: WarehouseLocation; children: LayoutNode[]; } function buildLayoutTree(locations: WarehouseLocation[]): LayoutNode[] { const map = new Map(); const roots: LayoutNode[] = []; // Sort by location type priority const typePriority: Record = { zone: 0, aisle: 1, rack: 2, shelf: 3, bin: 4, }; const sorted = [...locations].sort((a, b) => { return typePriority[a.locationType] - typePriority[b.locationType]; }); sorted.forEach((loc) => { map.set(loc.id, { location: loc, children: [] }); }); sorted.forEach((loc) => { const node = map.get(loc.id)!; if (loc.parentId && map.has(loc.parentId)) { const parent = map.get(loc.parentId)!; parent.children.push(node); } else { roots.push(node); } }); return roots; } function LayoutCell({ node, onClick, isSelected, depth = 0, }: { node: LayoutNode; onClick?: (location: WarehouseLocation) => void; isSelected: boolean; depth?: number; }) { const { location, children } = node; const hasChildren = children.length > 0; return (
{ e.stopPropagation(); onClick?.(location); }} >
{location.code} {!location.isActive && ( Inactivo )}

{location.name}

{hasChildren && (
= 3 && 'grid-cols-3' )} > {children.map((child) => ( ))}
)}
); } export function WarehouseLayout({ locations, onLocationClick, selectedLocationId, className, }: WarehouseLayoutProps) { const tree = useMemo(() => buildLayoutTree(locations), [locations]); if (locations.length === 0) { return (

No hay layout configurado

Agregue ubicaciones para visualizar el layout

); } return (
{/* Legend */}
Leyenda: {Object.entries(locationTypeColors).map(([type, color]) => (
{type === 'zone' ? 'Zona' : type === 'aisle' ? 'Pasillo' : type === 'rack' ? 'Rack' : type === 'shelf' ? 'Estante' : 'Bin'}
))}
{/* Layout Grid */}
= 3 && 'grid-cols-3' )} > {tree.map((node) => ( ))}
); }