import React, { useMemo, useState } from 'react'; import { motion, AnimatePresence } from 'framer-motion'; import { useTranslation } from 'react-i18next'; import { ChevronRight, Layers, CheckCircle2, Circle, GitFork, Search, FolderPlus, PanelLeftClose, Map as MapIcon } from 'lucide-react'; import { groupOf, groupStats } from '../groups'; import { questXp } from '../quests'; const TREE_TYPES = new Set(['cluster', 'sketch', 'condition']); /** * Explorer-like tree of the board: groups (expandable) and the quests inside them. * Click a group to open it on the canvas, click a quest to jump to it. * Rows can be dragged onto a group (or the board root) to move them. */ const ProjectExplorer = ({ nodes, edges = [], boardTitle, currentGroupId, onOpenGroup, onFocusNode, onCreateGroup, onMoveToGroup, canEdit, onClose }) => { const { t } = useTranslation(); const [expanded, setExpanded] = useState(() => new Set()); const [query, setQuery] = useState(''); const [dropTarget, setDropTarget] = useState(null); const items = useMemo(() => nodes.filter((n) => TREE_TYPES.has(n.type)), [nodes]); const childrenOf = useMemo(() => { const map = new Map(); items.forEach((n) => { const g = groupOf(n); if (!map.has(g)) map.set(g, []); map.get(g).push(n); }); map.forEach((list) => list.sort((a, b) => { if ((a.type === 'cluster') !== (b.type === 'cluster')) return a.type === 'cluster' ? -1 : 1; return (a.position?.y || 0) - (b.position?.y || 0) || (a.position?.x || 0) - (b.position?.x || 0); })); return map; }, [items]); const q = query.trim().toLowerCase(); const matches = useMemo(() => { if (!q) return null; const byId = new Map(items.map((n) => [n.id, n])); const keep = new Set(); items.forEach((n) => { if ((n.data?.label || '').toLowerCase().includes(q)) { keep.add(n.id); let g = groupOf(n); while (g && !keep.has(g)) { keep.add(g); g = groupOf(byId.get(g)); } } }); return keep; }, [q, items]); const toggle = (id) => setExpanded((prev) => { const next = new Set(prev); if (next.has(id)) next.delete(id); else next.add(id); return next; }); const onDragStart = (e, node) => { if (!canEdit) return; e.dataTransfer.setData('text/tf-node', node.id); e.dataTransfer.effectAllowed = 'move'; }; const dropProps = (groupId) => (canEdit ? { onDragOver: (e) => { if (!e.dataTransfer.types.includes('text/tf-node')) return; e.preventDefault(); setDropTarget(groupId ?? 'root'); }, onDragLeave: () => setDropTarget(null), onDrop: (e) => { e.preventDefault(); setDropTarget(null); const id = e.dataTransfer.getData('text/tf-node'); if (id && id !== groupId) onMoveToGroup([id], groupId); }, } : {}); const subgoalEdges = useMemo( () => edges.filter((e) => e.sourceHandle && e.sourceHandle.startsWith('subgoals-source')), [edges] ); const renderRow = (n, depth, kidsOf) => { const isGroup = n.type === 'cluster'; const kids = isGroup ? [] : kidsOf(n.id); const canExpand = isGroup || kids.length > 0; const open = matches ? true : expanded.has(n.id); const isCurrent = isGroup && currentGroupId === n.id; const done = n.data?.status === 'Done'; const stats = isGroup ? groupStats(nodes, n.id) : null; const doneKids = kids.filter((k) => k.data?.status === 'Done').length; return (
{t('explorer.empty', 'The board is empty. Add quests on the canvas.')}
)} {canEdit && items.length > 0 && ({t('explorer.dnd_hint', 'Drag a row onto a group to move it there.')}
)}