Spaces:
Running
Running
Download src/features/canvas/components/ProjectExplorer.jsx from DanilDrotik/FlowWeb: direct link, hf CLI and curl.
- Browser
- Download file 10.6 kB
-
https://huggingface.co/spaces/DanilDrotik/FlowWeb/resolve/main/src/features/canvas/components/ProjectExplorer.jsx
- Command line
-
hf download hf://spaces/DanilDrotik/FlowWeb/src/features/canvas/components/ProjectExplorer.jsx
-
curl -L -o ProjectExplorer.jsx https://huggingface.co/spaces/DanilDrotik/FlowWeb/resolve/main/src/features/canvas/components/ProjectExplorer.jsx
10.6 kB
| 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; | |