'use client'; import { useLayerStore } from '@/store'; import { useContextMenuStore } from '@/store/context-menu-store'; import { Eye, EyeOff, Trash2, Copy, Layers, MoveUp, MoveDown } from 'lucide-react'; import { cn } from '@/lib/utils'; import { deleteLayerWithHistory, updateLayerWithHistory, duplicateLayerWithHistory, } from '@/lib/layer-operations'; export function LayersPanel() { const { layers, activeLayerId, setActiveLayer, reorderLayer } = useLayerStore(); const { showContextMenu } = useContextMenuStore(); // Sort layers by order (highest first) const sortedLayers = [...layers].sort((a, b) => b.order - a.order); const handleMoveLayer = (layerId: string, direction: 'up' | 'down') => { const layer = layers.find((l) => l.id === layerId); if (!layer) return; const layerIndex = sortedLayers.findIndex((l) => l.id === layerId); if (direction === 'up' && layerIndex > 0) { const targetLayer = sortedLayers[layerIndex - 1]; reorderLayer(layerId, targetLayer.order); } else if (direction === 'down' && layerIndex < sortedLayers.length - 1) { const targetLayer = sortedLayers[layerIndex + 1]; reorderLayer(layerId, targetLayer.order); } }; const handleContextMenu = (e: React.MouseEvent, layerId: string) => { e.preventDefault(); const layer = layers.find((l) => l.id === layerId); if (!layer) return; const layerIndex = sortedLayers.findIndex((l) => l.id === layerId); const canMoveUp = layerIndex > 0; const canMoveDown = layerIndex < sortedLayers.length - 1; const canDelete = layers.length > 1; showContextMenu(e.clientX, e.clientY, [ { label: 'Duplicate Layer', icon: , onClick: () => duplicateLayerWithHistory(layerId), }, { separator: true, label: '', onClick: () => {}, }, { label: 'Move Up', icon: , onClick: () => handleMoveLayer(layerId, 'up'), disabled: !canMoveUp, }, { label: 'Move Down', icon: , onClick: () => handleMoveLayer(layerId, 'down'), disabled: !canMoveDown, }, { separator: true, label: '', onClick: () => {}, }, { label: layer.visible ? 'Hide Layer' : 'Show Layer', icon: layer.visible ? : , onClick: () => updateLayerWithHistory(layerId, { visible: !layer.visible }, 'Toggle Visibility'), }, { separator: true, label: '', onClick: () => {}, }, { label: 'Delete Layer', icon: , onClick: () => { if (confirm('Delete this layer?')) { deleteLayerWithHistory(layerId); } }, disabled: !canDelete, danger: true, }, ]); }; return (

Layers

{sortedLayers.length === 0 ? (

No layers

) : ( sortedLayers.map((layer) => (
setActiveLayer(layer.id)} onContextMenu={(e) => handleContextMenu(e, layer.id)} >

{layer.name}

{layer.width} × {layer.height}

{Math.round(layer.opacity * 100)}%
)) )}
); }