'use client'; import { useState, useTransition } from 'react'; import { DragDropContext, Droppable, Draggable, DropResult, } from '@hello-pangea/dnd'; import type { SwotItem, Action, ActionLink, SwotCategory } from '@prisma/client'; import { SwotQuadrant } from './SwotQuadrant'; import { SwotCard } from './SwotCard'; import { ActionPanel } from './ActionPanel'; import { moveSwotItem } from '@/actions/swot'; type ActionWithLinks = Action & { links: (ActionLink & { swotItem: SwotItem })[]; }; interface SwotBoardProps { sessionId: string; items: SwotItem[]; actions: ActionWithLinks[]; } const QUADRANTS: { category: SwotCategory; title: string; icon: string }[] = [ { category: 'STRENGTH', title: 'Forces', icon: '💪' }, { category: 'WEAKNESS', title: 'Faiblesses', icon: '⚠️' }, { category: 'OPPORTUNITY', title: 'Opportunités', icon: '🚀' }, { category: 'THREAT', title: 'Menaces', icon: '🛡️' }, ]; export function SwotBoard({ sessionId, items, actions }: SwotBoardProps) { const [isPending, startTransition] = useTransition(); const [linkMode, setLinkMode] = useState(false); const [selectedItems, setSelectedItems] = useState([]); const [highlightedItems, setHighlightedItems] = useState([]); const itemsByCategory = QUADRANTS.reduce( (acc, q) => { acc[q.category] = items .filter((item) => item.category === q.category) .sort((a, b) => a.order - b.order); return acc; }, {} as Record ); function handleDragEnd(result: DropResult) { if (!result.destination) return; const { source, destination, draggableId } = result; const sourceCategory = source.droppableId as SwotCategory; const destCategory = destination.droppableId as SwotCategory; // If same position, do nothing if (sourceCategory === destCategory && source.index === destination.index) { return; } startTransition(async () => { await moveSwotItem(draggableId, sessionId, destCategory, destination.index); }); } function toggleItemSelection(itemId: string) { if (!linkMode) return; setSelectedItems((prev) => prev.includes(itemId) ? prev.filter((id) => id !== itemId) : [...prev, itemId] ); } function handleActionHover(linkedItemIds: string[]) { setHighlightedItems(linkedItemIds); } function handleActionLeave() { setHighlightedItems([]); } function exitLinkMode() { setLinkMode(false); setSelectedItems([]); } return (
{/* Link Mode Banner */} {linkMode && (
🔗

Mode Liaison

Sélectionnez les items à lier ({selectedItems.length} sélectionné {selectedItems.length > 1 ? 's' : ''})

)} {/* SWOT Matrix */}
{QUADRANTS.map((quadrant) => ( {(provided, snapshot) => ( {itemsByCategory[quadrant.category].map((item, index) => ( {(dragProvided, dragSnapshot) => ( toggleItemSelection(item.id)} ref={dragProvided.innerRef} {...dragProvided.draggableProps} {...dragProvided.dragHandleProps} /> )} ))} {provided.placeholder} )} ))}
{/* Actions Panel */} setLinkMode(true)} onExitLinkMode={exitLinkMode} onClearSelection={() => setSelectedItems([])} onActionHover={handleActionHover} onActionLeave={handleActionLeave} />
); }