Spaces:
Running
Running
File size: 10,579 Bytes
2740e39 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | 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 (
<div key={n.id}>
<motion.div
layout="position"
draggable={canEdit}
onDragStart={(e) => 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 ? (
<button
onClick={(e) => { e.stopPropagation(); toggle(n.id); }}
className="p-0.5 rounded hover:bg-primary/10 cursor-pointer shrink-0"
>
<motion.span animate={{ rotate: open ? 90 : 0 }} className="block"><ChevronRight size={13} /></motion.span>
</button>
) : <span className="w-[17px] shrink-0" />}
{isGroup ? <Layers size={14} className={`${isCurrent ? 'text-white' : 'text-purple-500'} shrink-0`} />
: n.type === 'condition' ? <GitFork size={13} className="text-primary/60 shrink-0" />
: done ? <CheckCircle2 size={14} className="text-success shrink-0" /> : <Circle size={14} className="text-primary/30 shrink-0" />}
<span className={`flex-1 truncate font-bold ${done ? 'line-through opacity-50' : ''}`}>
{n.data?.label || (isGroup ? t('groups.untitled', 'New group') : n.type === 'condition' ? t('canvas.condition', 'Condition') : t('canvas.new_goal', 'New Goal'))}
</span>
{isGroup && stats && (
<span className={`text-[10px] font-extrabold shrink-0 ${isCurrent ? 'text-white/80' : 'text-primary/50'}`}>{stats.done}/{stats.total}</span>
)}
{!isGroup && kids.length > 0 && (
<span className="text-[10px] font-extrabold text-primary/40 shrink-0">{doneKids}/{kids.length}</span>
)}
{n.type === 'sketch' && !done && kids.length === 0 && (
<span className="text-[9px] font-extrabold text-amber-600 opacity-0 group-hover:opacity-100 shrink-0">+{questXp(n)}</span>
)}
</motion.div>
{isGroup && stats && stats.total > 0 && (
<div className="h-0.5 rounded-full bg-primary/5 mb-0.5" style={{ marginLeft: 26 + depth * 14, marginRight: 8 }}>
<div className="h-full rounded-full bg-purple-400 transition-all" style={{ width: `${(stats.done / stats.total) * 100}%` }} />
</div>
)}
<AnimatePresence initial={false}>
{canExpand && open && (
<motion.div
initial={{ height: 0, opacity: 0 }}
animate={{ height: 'auto', opacity: 1 }}
exit={{ height: 0, opacity: 0 }}
transition={{ duration: 0.2 }}
className="overflow-hidden"
>
{isGroup ? renderLevel(n.id, depth + 1) : kids.map((k) => renderRow(k, depth + 1, kidsOf))}
{isGroup && (childrenOf.get(n.id) || []).length === 0 && (
<div className="text-[11px] italic text-on-surface-variant/50 py-1" style={{ paddingLeft: 26 + (depth + 1) * 14 }}>
{t('explorer.empty_group', 'Empty')}
</div>
)}
</motion.div>
)}
</AnimatePresence>
</div>
);
};
// 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 (
<motion.aside
initial={{ width: 0, opacity: 0 }}
animate={{ width: 270, opacity: 1 }}
exit={{ width: 0, opacity: 0 }}
transition={{ type: 'spring', stiffness: 300, damping: 32 }}
className="h-full shrink-0 overflow-hidden border-r-4 border-primary bg-surface-container-lowest flex flex-col z-30"
>
<div className="w-[270px] flex flex-col h-full">
<div className="px-3 pt-3 pb-2 flex items-center gap-2 border-b-2 border-primary/10">
<span className="text-[11px] font-extrabold uppercase tracking-widest text-on-surface-variant/70 flex-1">{t('explorer.title', 'Structure')}</span>
{canEdit && (
<button onClick={onCreateGroup} title={t('explorer.new_group', 'New group')} className="p-1.5 rounded-lg hover:bg-purple-500/10 text-purple-600 cursor-pointer">
<FolderPlus size={15} />
</button>
)}
<button onClick={onClose} title={t('explorer.hide', 'Hide panel')} className="p-1.5 rounded-lg hover:bg-primary/5 text-primary cursor-pointer">
<PanelLeftClose size={15} />
</button>
</div>
<div className="px-3 py-2">
<div className="relative">
<Search size={13} className="absolute left-2.5 top-1/2 -translate-y-1/2 text-on-surface-variant" />
<input
value={query}
onChange={(e) => 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"
/>
</div>
</div>
<div className="flex-1 overflow-y-auto px-1.5 pb-4 custom-scrollbar">
<div
{...dropProps(null)}
onClick={() => 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' : ''}`}
>
<MapIcon size={14} className="shrink-0" />
<span className="truncate flex-1">{boardTitle || t('explorer.board', 'Board')}</span>
</div>
<div className="mt-1">{renderLevel(null, 0)}</div>
{items.length === 0 && (
<p className="text-xs text-on-surface-variant/60 px-3 py-4">{t('explorer.empty', 'The board is empty. Add quests on the canvas.')}</p>
)}
{canEdit && items.length > 0 && (
<p className="text-[10px] text-on-surface-variant/50 px-3 pt-4 leading-relaxed">{t('explorer.dnd_hint', 'Drag a row onto a group to move it there.')}</p>
)}
</div>
</div>
</motion.aside>
);
};
export default ProjectExplorer;
|