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 (
onDragStart(e, n)} {...(isGroup ? dropProps(n.id) : {})} onClick={() => (isGroup ? onOpenGroup(n.id) : onFocusNode(n.id))} className={`group flex items-center gap-1.5 pr-2 py-1 rounded-lg cursor-pointer select-none text-sm transition-colors ${ isCurrent ? 'bg-purple-500 text-white' : 'hover:bg-secondary/10 text-primary' } ${dropTarget === n.id ? 'ring-2 ring-purple-400 bg-purple-500/10' : ''}`} style={{ paddingLeft: 6 + depth * 14 }} > {canExpand ? ( ) : } {isGroup ? : n.type === 'condition' ? : done ? : } {n.data?.label || (isGroup ? t('groups.untitled', 'New group') : n.type === 'condition' ? t('canvas.condition', 'Condition') : t('canvas.new_goal', 'New Goal'))} {isGroup && stats && ( {stats.done}/{stats.total} )} {!isGroup && kids.length > 0 && ( {doneKids}/{kids.length} )} {n.type === 'sketch' && !done && kids.length === 0 && ( +{questXp(n)} )} {isGroup && stats && stats.total > 0 && (
)} {canExpand && open && ( {isGroup ? renderLevel(n.id, depth + 1) : kids.map((k) => renderRow(k, depth + 1, kidsOf))} {isGroup && (childrenOf.get(n.id) || []).length === 0 && (
{t('explorer.empty_group', 'Empty')}
)}
)}
); }; // One level of the tree: groups first, then quests nested under the goal they are a subgoal of const renderLevel = (groupId, depth) => { const all = (childrenOf.get(groupId) || []).filter((n) => !matches || matches.has(n.id)); const ids = new Set(all.map((n) => n.id)); const parentOf = new Map(); subgoalEdges.forEach((e) => { if (ids.has(e.source) && ids.has(e.target) && !parentOf.has(e.target) && e.source !== e.target) parentOf.set(e.target, e.source); }); const kidsOf = (id) => all.filter((n) => parentOf.get(n.id) === id); return all.filter((n) => !parentOf.has(n.id)).map((n) => renderRow(n, depth, kidsOf)); }; return (
{t('explorer.title', 'Structure')} {canEdit && ( )}
setQuery(e.target.value)} placeholder={t('explorer.search', 'Find a quest...')} className="w-full pl-8 pr-2 py-1.5 bg-surface-container-low border-2 border-transparent focus:border-primary rounded-lg text-xs outline-none" />
onOpenGroup(null)} className={`flex items-center gap-2 px-2 py-1.5 rounded-lg cursor-pointer font-bold text-sm ${ currentGroupId === null ? 'bg-primary text-on-primary' : 'text-primary hover:bg-secondary/10' } ${dropTarget === 'root' ? 'ring-2 ring-purple-400' : ''}`} > {boardTitle || t('explorer.board', 'Board')}
{renderLevel(null, 0)}
{items.length === 0 && (

{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.')}

)}
); }; export default ProjectExplorer;