import React, { useMemo, useState } from 'react'; import { motion, AnimatePresence } from 'framer-motion'; import { useTranslation } from 'react-i18next'; import { Search, CheckCircle2, Circle, ChevronRight, Layers, Map as MapIcon, Plus, Crosshair, Sparkles, Gift, GitFork, ArrowRight, ChevronsDownUp, ChevronsUpDown, X, } from 'lucide-react'; import { groupOf, getGroupPath } from '../groups'; import { questXp, hasReward } from '../quests'; const initials = (a) => { const name = (a.displayName || a.email || 'U').trim(); const parts = name.split(/\s+/); return (parts.length > 1 ? parts[0][0] + parts[1][0] : name.slice(0, 2)).toUpperCase(); }; const isSubgoalEdge = (e) => e.sourceHandle && e.sourceHandle.startsWith('subgoals-source'); const QuestRow = ({ node, depth, canEdit, onToggle, onOpen, onShow }) => { const { t } = useTranslation(); const d = node.data || {}; const done = d.status === 'Done'; const assignees = Array.isArray(d.assignees) ? d.assignees : []; return (
{depth > 0 && ↳} {d.isNextStep && !done && ( {t('quests.next_step', 'Next')} )}
{d.description &&

{d.description}

}
{(d.status === 'In Progress' || d.status === 'Review') && ( {d.status === 'Review' ? 'Review' : `${d.progress || 0}%`} )} {questXp(node)} XP {hasReward(node) && ( {d.reward} )} {(d.tags || []).map((tag) => ( {tag} ))} {assignees.length > 0 && ( a.displayName || a.email).join(', ')}> {assignees.slice(0, 3).map((a, i) => ( {initials(a)} ))} )}
); }; const DecisionRow = ({ node, depth, canEdit, onChoose }) => { const { t } = useTranslation(); const d = node.data || {}; const selected = d.selectedOptions || []; return ( {d.label || t('canvas.condition', 'Condition')}
{(d.options || []).map((opt) => { const on = selected.includes(opt.id); return ( ); })}
); }; const AddQuestInline = ({ onAdd }) => { const { t } = useTranslation(); const [open, setOpen] = useState(false); const [value, setValue] = useState(''); const submit = () => { const label = value.trim(); if (label) onAdd(label); setValue(''); }; return open ? (
setValue(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter') { e.preventDefault(); submit(); } if (e.key === 'Escape') { setOpen(false); setValue(''); } }} placeholder={t('tasks.new_quest_placeholder', 'New quest — Enter to add, Esc to close')} className="flex-1 bg-transparent outline-none text-sm font-bold text-primary" />
) : ( ); }; /** * The board as a to-do list: one section per group, quests nested under their * parent goal, quick completion, decisions you can answer in place. */ const TaskListView = ({ nodes, edges, permission, collaborators = [], updateNodeData, onOpenSettings, onShowOnMap, onOpenGroup, onAddQuest, }) => { const { t } = useTranslation(); const canEdit = permission !== 'Read'; const [query, setQuery] = useState(''); const [statusFilter, setStatusFilter] = useState('active'); // active | all | done const [tagFilter, setTagFilter] = useState('all'); const [personFilter, setPersonFilter] = useState('all'); const [collapsed, setCollapsed] = useState(() => new Set()); const tasks = useMemo(() => nodes.filter((n) => n.type === 'sketch' || n.type === 'condition'), [nodes]); const groups = useMemo(() => nodes.filter((n) => n.type === 'cluster'), [nodes]); const allTags = useMemo(() => [...new Set(tasks.flatMap((n) => n.data?.tags || []))].sort(), [tasks]); const q = query.trim().toLowerCase(); const matches = (n) => { if (n.type === 'condition') return statusFilter !== 'done' && (!q || (n.data?.label || '').toLowerCase().includes(q)) && tagFilter === 'all' && personFilter === 'all'; const d = n.data || {}; if (statusFilter === 'active' && d.status === 'Done') return false; if (statusFilter === 'done' && d.status !== 'Done') return false; if (tagFilter !== 'all' && !(d.tags || []).includes(tagFilter)) return false; if (personFilter !== 'all') { const people = (d.assignees || []).map((a) => a.sharedWithUserId || 'owner'); if (!people.includes(personFilter)) return false; } if (q && !`${d.label || ''} ${d.description || ''} ${d.reward || ''}`.toLowerCase().includes(q)) return false; return true; }; // Sections in explorer order: the board, then groups depth-first const sections = useMemo(() => { const result = [{ id: null, depth: 0 }]; const walk = (parentId, depth) => { groups .filter((g) => groupOf(g) === parentId) .sort((a, b) => (a.position?.y || 0) - (b.position?.y || 0)) .forEach((g) => { result.push({ id: g.id, depth: depth + 1, node: g }); walk(g.id, depth + 1); }); }; walk(null, 0); return result; }, [groups]); // Quests nested under the goal they are a subgoal of (within the same section) const rowsFor = (sectionId) => { const inSection = tasks.filter((n) => groupOf(n) === sectionId); const ids = new Set(inSection.map((n) => n.id)); const parentOf = new Map(); edges.filter(isSubgoalEdge).forEach((e) => { if (ids.has(e.source) && ids.has(e.target) && !parentOf.has(e.target)) parentOf.set(e.target, e.source); }); const byY = (a, b) => (a.position?.y || 0) - (b.position?.y || 0) || (a.position?.x || 0) - (b.position?.x || 0); const rows = []; const visited = new Set(); const visit = (n, depth) => { if (visited.has(n.id)) return; visited.add(n.id); const kids = inSection.filter((c) => parentOf.get(c.id) === n.id).sort(byY); const kidRows = []; const before = rows.length; kids.forEach((k) => visit(k, depth + 1)); kidRows.push(...rows.splice(before)); // A parent stays visible when one of its subgoals matches if (matches(n) || kidRows.length) rows.push({ node: n, depth }, ...kidRows); }; inSection.filter((n) => !parentOf.has(n.id)).sort(byY).forEach((n) => visit(n, 0)); return rows; }; const sectionStats = (sectionId) => { const quests = tasks.filter((n) => n.type === 'sketch' && (sectionId === null ? groupOf(n) === null : groupOf(n) === sectionId)); const done = quests.filter((n) => n.data?.status === 'Done'); return { total: quests.length, done: done.length, xp: done.reduce((s, n) => s + questXp(n), 0) }; }; const toggle = (node) => { if (!canEdit) return; if (node.data?.status !== 'Done' && node.data?.subgoals && node.data.subgoals.done < node.data.subgoals.total) { alert(t('canvas.subgoal_warning', 'Finish all subgoals first!')); return; } updateNodeData(node.id, { status: node.data?.status === 'Done' ? 'Todo' : 'Done' }); }; const choose = (node, optId) => { const selected = node.data?.selectedOptions || []; const next = node.data?.multiSelect ? (selected.includes(optId) ? selected.filter((x) => x !== optId) : [...selected, optId]) : (selected.includes(optId) ? [] : [optId]); updateNodeData(node.id, { selectedOptions: next }); }; const totals = sectionStatsAll(tasks); const sectionRows = sections.map((s) => ({ ...s, rows: rowsFor(s.id), stats: sectionStats(s.id) })); const visibleSections = sectionRows.filter((s) => s.rows.length > 0 || (!q && statusFilter !== 'done' && tagFilter === 'all' && personFilter === 'all')); const hasAnyRows = sectionRows.some((s) => s.rows.length > 0); const filtersActive = Boolean(q) || statusFilter === 'done' || tagFilter !== 'all' || personFilter !== 'all'; const allCollapsed = visibleSections.length > 0 && visibleSections.every((s) => collapsed.has(s.id ?? 'root')); const toggleSection = (key) => setCollapsed((prev) => { const next = new Set(prev); if (next.has(key)) next.delete(key); else next.add(key); return next; }); return (
{/* Toolbar */}
setQuery(e.target.value)} placeholder={t('tasks.search', 'Search quests, descriptions, rewards...')} className="w-full pl-9 pr-3 py-2.5 bg-surface-container-lowest border-2 border-primary rounded-xl text-sm font-bold text-primary outline-none shadow-[2px_2px_0px_0px_var(--color-primary)]" />
{[ ['active', t('tasks.filter_active', 'Active')], ['all', t('tasks.filter_all', 'All')], ['done', t('tasks.filter_done', 'Done')], ].map(([key, label]) => ( ))}
{allTags.length > 0 && ( )} {collaborators.length > 0 && ( )} {t('tasks.summary', { done: totals.done, total: totals.total, defaultValue: `${totals.done}/${totals.total} done` })} {totals.xp} XP
{!hasAnyRows && (filtersActive || !canEdit) ? (
{t('tasks.nothing', 'Nothing here')} {t('tasks.nothing_hint', 'Change the filters or add quests on the map.')}
) : ( visibleSections.map((section) => { const key = section.id ?? 'root'; const isCollapsed = collapsed.has(key); const path = section.id ? getGroupPath(nodes, section.id).map((g) => g.data?.label || t('groups.untitled', 'New group')).join(' › ') : null; const { total, done, xp } = section.stats; return (
{total > 0 && (
{done}/{total} {xp} XP
)} {section.id && ( )}
{!isCollapsed && ( {section.rows.map(({ node, depth }) => ( node.type === 'condition' ? : ))} {section.rows.length === 0 && (

{statusFilter === 'active' && total > 0 ? t('tasks.all_done_here', 'Everything here is done 🎉') : t('tasks.empty_section', 'No quests yet')}

)} {canEdit && statusFilter !== 'done' && onAddQuest(section.id, label)} />}
)}
); }) )}
); }; function sectionStatsAll(tasks) { const quests = tasks.filter((n) => n.type === 'sketch'); const done = quests.filter((n) => n.data?.status === 'Done'); return { total: quests.length, done: done.length, xp: done.reduce((s, n) => s + questXp(n), 0) }; } export default TaskListView;