FlowWeb / src /features /canvas /components /nodes /GroupNode.jsx
danylokhodus's picture
Claude Opus 5.5
feat: quest board — web auto-layout, groups, archive, XP and rewards
2740e39
Raw History Blame Contribute Delete
6.6 kB
import React from 'react';
import { Handle, Position } from '@xyflow/react';
import { motion } from 'framer-motion';
import { useTranslation } from 'react-i18next';
import { Layers, Settings, CheckCircle2, Circle, ChevronRight, ArrowRight, Sparkles } from 'lucide-react';
import { useTheme } from '../../../../context/ThemeContext';
const PALETTE = {
purple: { light: '#f3e8ff', dark: '#210638', border: '#a855f7' },
blue: { light: '#dbeafe', dark: '#111827', border: '#3b82f6' },
green: { light: '#dcfce7', dark: '#062f17', border: '#22c55e' },
yellow: { light: '#fef9c3', dark: '#2e2b14', border: '#eab308' },
pink: { light: '#fce7f3', dark: '#37061d', border: '#ec4899' },
orange: { light: '#ffedd5', dark: '#2e1208', border: '#f97316' },
white: { light: '#ffffff', dark: '#1c1917', border: '#78716c' },
};
// A collapsed group of quests. Double-click (or "Open") shows what is inside.
const GroupNode = ({ id, data, selected }) => {
const { t } = useTranslation();
const { isDarkMode } = useTheme();
const colors = PALETTE[data.color] || PALETTE.purple;
const stats = data.groupStats || { total: 0, done: 0, xpEarned: 0, xpTotal: 0, groups: 0, preview: [] };
const progress = stats.total ? stats.done / stats.total : 0;
const complete = stats.total > 0 && stats.done === stats.total;
const open = (e) => {
e.stopPropagation();
window.dispatchEvent(new CustomEvent('open-group-node', { detail: { nodeId: id } }));
};
const configure = (e) => {
e.stopPropagation();
window.dispatchEvent(new CustomEvent('configure-group-node', { detail: { nodeId: id } }));
};
const bg = isDarkMode ? colors.dark : colors.light;
return (
<div onDoubleClick={open} className="relative w-[280px] group">
{/* Stacked sheets: a group holds many cards */}
<div className="absolute inset-0 translate-x-2.5 translate-y-2.5 rounded-2xl border-2 opacity-40" style={{ borderColor: colors.border, backgroundColor: bg }} />
<div className="absolute inset-0 translate-x-1.5 translate-y-1.5 rounded-2xl border-2 opacity-70" style={{ borderColor: colors.border, backgroundColor: bg }} />
<div
className={`relative rounded-2xl border-[3px] px-4 pt-3 pb-3 flex flex-col gap-2.5 transition-shadow ${
selected ? 'ring-4 ring-emerald-400/30' : ''
} ${data.isNextStep && !complete ? 'ring-4 ring-success/30 shadow-[0_0_20px_rgba(34,197,94,0.25)]' : ''}`}
style={{ borderColor: colors.border, backgroundColor: bg }}
>
<Handle type="target" position={Position.Top} id="main-target" className="!bg-primary !w-3 !h-3 !border-2 !border-white" />
<Handle type="source" position={Position.Bottom} id="main-source" className="!bg-primary !w-3 !h-3 !border-2 !border-white" />
<div className="flex items-start gap-2">
<div className="w-8 h-8 shrink-0 rounded-lg flex items-center justify-center text-white" style={{ backgroundColor: colors.border }}>
<Layers size={17} />
</div>
<div className="flex-1 min-w-0">
<div className="text-[9px] font-extrabold uppercase tracking-widest opacity-60 text-primary">
{t('groups.badge', 'Group')}
</div>
<div className="font-display-lg text-xl leading-tight text-primary break-words">
{data.label || t('groups.untitled', 'New group')}
</div>
</div>
{data.isNextStep && !complete && <ArrowRight size={18} className="text-success animate-bounce-x shrink-0 mt-1" />}
<button
onClick={configure}
className="opacity-0 group-hover:opacity-100 transition-opacity p-1 text-primary/60 hover:text-primary hover:bg-primary/5 rounded-full cursor-pointer shrink-0"
title={t('groups.settings', 'Group settings')}
>
<Settings size={14} />
</button>
</div>
{data.description && (
<p className="text-sm font-accent-note text-on-surface-variant line-clamp-2 italic -mt-1">{data.description}</p>
)}
{/* Progress */}
<div className="flex flex-col gap-1">
<div className="flex items-center justify-between text-[10px] font-bold text-primary/70">
<span>{t('groups.quests_done', { done: stats.done, total: stats.total, defaultValue: `${stats.done}/${stats.total} quests` })}</span>
<span className="flex items-center gap-1 text-amber-600 dark:text-amber-400">
<Sparkles size={10} /> {stats.xpEarned}/{stats.xpTotal} XP
</span>
</div>
<div className="h-2.5 rounded-full border-2 overflow-hidden bg-white/50 dark:bg-black/20" style={{ borderColor: colors.border }}>
<motion.div
className="h-full rounded-full"
style={{ backgroundColor: complete ? '#22c55e' : colors.border }}
initial={false}
animate={{ width: `${Math.round(progress * 100)}%` }}
transition={{ type: 'spring', stiffness: 120, damping: 20 }}
/>
</div>
</div>
{/* Peek inside */}
{stats.preview.length > 0 && (
<ul className="flex flex-col gap-1">
{stats.preview.map((q) => (
<li key={q.id} className="flex items-center gap-1.5 text-[11px] font-bold text-primary/80 min-w-0">
{q.done ? <CheckCircle2 size={12} className="text-success shrink-0" /> : <Circle size={12} className="opacity-40 shrink-0" />}
<span className={`truncate ${q.done ? 'line-through opacity-50' : ''}`}>{q.label || t('canvas.new_goal', 'New Goal')}</span>
</li>
))}
{stats.total > stats.preview.length && (
<li className="text-[10px] font-bold text-primary/50 pl-4">
{t('groups.more', { count: stats.total - stats.preview.length, defaultValue: `+${stats.total - stats.preview.length} more` })}
</li>
)}
</ul>
)}
<button
onClick={open}
className="mt-0.5 flex items-center justify-center gap-1 py-1.5 rounded-xl border-2 text-xs font-extrabold uppercase tracking-wider text-primary hover:bg-white/60 dark:hover:bg-white/5 transition-colors cursor-pointer"
style={{ borderColor: colors.border }}
>
{stats.total === 0 && stats.groups === 0 ? t('groups.empty_open', 'Open and add quests') : t('groups.open', 'Open')}
<ChevronRight size={14} />
</button>
</div>
</div>
);
};
export default GroupNode;