Spaces:
Running

FlowWeb / src /features /canvas /components /ArchiveDrawer.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
13.8 kB
import React, { useMemo, useState } from 'react';
import { motion, AnimatePresence } from 'framer-motion';
import { useTranslation } from 'react-i18next';
import { X, Archive, Search, Trophy, Sparkles, Gift, RotateCcw, Crosshair, Layers } from 'lucide-react';
import { isDoneQuest, questXp, hasReward } from '../quests';
const dayKey = (iso) => (iso ? new Date(iso).toDateString() : 'earlier');
const dayLabel = (key, t, locale) => {
if (key === 'earlier') return t('archive.earlier', 'Earlier');
const date = new Date(key);
const today = new Date();
const yesterday = new Date();
yesterday.setDate(today.getDate() - 1);
if (date.toDateString() === today.toDateString()) return t('archive.today', 'Today');
if (date.toDateString() === yesterday.toDateString()) return t('archive.yesterday', 'Yesterday');
return date.toLocaleDateString(locale, { day: 'numeric', month: 'long', year: date.getFullYear() === today.getFullYear() ? undefined : 'numeric' });
};
const Stamp = ({ delay }) => (
<motion.div
initial={{ scale: 2.2, rotate: -25, opacity: 0 }}
animate={{ scale: 1, rotate: -12, opacity: 1 }}
transition={{ type: 'spring', stiffness: 380, damping: 14, delay }}
className="absolute -top-2 -right-1 px-1.5 py-0.5 border-2 border-success text-success rounded-md text-[9px] font-extrabold uppercase tracking-widest bg-surface-container-lowest"
>
Done
</motion.div>
);
/**
* Archive of completed quests ("trophy room"): grouped by the day they were finished,
* with the rewards collected. Quests can be restored or shown on the board.
*/
const ArchiveDrawer = ({ open, onClose, nodes, groupPathOf, onRestore, onShow, canEdit }) => {
const { t, i18n } = useTranslation();
const [tab, setTab] = useState('quests');
const [query, setQuery] = useState('');
const done = useMemo(
() => nodes
.filter(isDoneQuest)
.sort((a, b) => (b.data.completedAt || '').localeCompare(a.data.completedAt || '')),
[nodes]
);
const q = query.trim().toLowerCase();
const filtered = done.filter((n) =>
!q || (n.data.label || '').toLowerCase().includes(q) || (n.data.reward || '').toLowerCase().includes(q)
);
const rewards = filtered.filter(hasReward);
const xp = done.reduce((s, n) => s + questXp(n), 0);
const days = [];
const byDay = new Map();
filtered.forEach((n) => {
const key = dayKey(n.data.completedAt);
if (!byDay.has(key)) {
byDay.set(key, []);
days.push(key);
}
byDay.get(key).push(n);
});
let index = 0;
return (
<AnimatePresence>
{open && (
<>
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
onClick={onClose}
className="absolute inset-0 z-40 bg-primary/15 backdrop-blur-[2px]"
/>
<motion.aside
initial={{ x: '105%' }}
animate={{ x: 0 }}
exit={{ x: '105%' }}
transition={{ type: 'spring', stiffness: 260, damping: 30 }}
className="absolute top-0 right-0 bottom-0 z-50 w-full max-w-[430px] bg-surface-container-lowest border-l-4 border-primary flex flex-col shadow-[-8px_0_0_0_rgba(0,0,0,0.04)]"
>
<div className="p-5 pb-4 border-b-2 border-primary/10 flex flex-col gap-4">
<div className="flex items-center justify-between">
<h3 className="text-3xl font-display-lg text-primary flex items-center gap-2">
<motion.span initial={{ rotate: -30, scale: 0 }} animate={{ rotate: 0, scale: 1 }} transition={{ type: 'spring', stiffness: 300, damping: 12, delay: 0.15 }}>
<Archive size={26} />
</motion.span>
{t('archive.title', 'Archive')}
</h3>
<button onClick={onClose} className="p-1.5 rounded-xl border-2 border-primary hover:bg-red-500/10 cursor-pointer text-primary">
<X size={18} />
</button>
</div>
<div className="grid grid-cols-3 gap-2">
{[
{ icon: Trophy, label: t('archive.quests_done', 'Quests done'), value: done.length, color: 'text-primary' },
{ icon: Sparkles, label: 'XP', value: xp, color: 'text-amber-600' },
{ icon: Gift, label: t('quests.rewards', 'Rewards'), value: done.filter(hasReward).length, color: 'text-pink-600' },
].map((s, i) => (
<motion.div
key={s.label}
initial={{ opacity: 0, y: 12 }}
animate={{ opacity: 1, y: 0 }}
transition={{ delay: 0.1 + i * 0.06 }}
className="rounded-xl border-2 border-primary/10 bg-secondary/5 px-2.5 py-2"
>
<div className="flex items-center gap-1 text-[10px] font-bold uppercase tracking-wider text-on-surface-variant/70"><s.icon size={11} /> {s.label}</div>
<div className={`font-display-lg text-3xl leading-none mt-0.5 ${s.color}`}>{s.value}</div>
</motion.div>
))}
</div>
<div className="flex gap-2 items-center">
<div className="flex bg-secondary/5 border-2 border-primary rounded-xl p-1 shrink-0">
{['quests', 'rewards'].map((key) => (
<button
key={key}
onClick={() => setTab(key)}
className={`relative px-3 py-1 rounded-lg text-[11px] font-extrabold uppercase tracking-wider cursor-pointer ${tab === key ? 'text-on-primary' : 'text-primary'}`}
>
{tab === key && <motion.span layoutId="archive-tab" className="absolute inset-0 bg-primary rounded-lg" transition={{ type: 'spring', stiffness: 400, damping: 32 }} />}
<span className="relative">{key === 'quests' ? t('quests.quests', 'Quests') : t('quests.rewards', 'Rewards')}</span>
</button>
))}
</div>
<div className="relative flex-1">
<Search size={14} 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('archive.search', 'Search...')}
className="w-full pl-8 pr-2 py-1.5 bg-surface-container-low border-2 border-transparent focus:border-primary rounded-xl text-sm outline-none"
/>
</div>
</div>
</div>
<div className="flex-1 overflow-y-auto p-5 custom-scrollbar">
{filtered.length === 0 ? (
<motion.div initial={{ opacity: 0, scale: 0.9 }} animate={{ opacity: 1, scale: 1 }} className="flex flex-col items-center text-center gap-3 py-16">
<motion.div animate={{ y: [0, -8, 0] }} transition={{ duration: 2.4, repeat: Infinity, ease: 'easeInOut' }}>
<Trophy size={56} className="text-primary/20" />
</motion.div>
<span className="font-accent-note text-2xl text-secondary">
{q ? t('archive.nothing_found', 'Nothing found') : t('archive.empty', 'No completed quests yet')}
</span>
{!q && <p className="text-sm text-on-surface-variant max-w-[260px]">{t('archive.empty_hint', 'Finish a quest on the board — it will land here with its XP and reward.')}</p>}
</motion.div>
) : tab === 'rewards' ? (
rewards.length === 0 ? (
<p className="text-center text-sm text-on-surface-variant py-12">{t('archive.no_rewards', 'No rewards collected yet. Add a reward to a quest in its settings.')}</p>
) : (
<div className="grid grid-cols-2 gap-3">
<AnimatePresence>
{rewards.map((n, i) => (
<motion.div
key={n.id}
layout
initial={{ rotateY: 90, opacity: 0 }}
animate={{ rotateY: 0, opacity: 1 }}
exit={{ scale: 0.8, opacity: 0 }}
transition={{ type: 'spring', stiffness: 220, damping: 20, delay: i * 0.05 }}
className="relative rounded-2xl border-2 border-dashed border-pink-400 bg-pink-50 dark:bg-pink-950/20 p-3 flex flex-col gap-1.5"
>
<Gift size={22} className="text-pink-500" />
<span className="font-bold text-primary text-sm leading-tight break-words">{n.data.reward}</span>
<span className="text-[10px] text-on-surface-variant truncate">{n.data.label}</span>
</motion.div>
))}
</AnimatePresence>
</div>
)
) : (
<div className="flex flex-col gap-6">
{days.map((key) => (
<div key={key} className="flex flex-col gap-2.5">
<motion.div initial={{ opacity: 0, x: -10 }} animate={{ opacity: 1, x: 0 }} className="flex items-center gap-2">
<span className="text-[11px] font-extrabold uppercase tracking-widest text-on-surface-variant/70">{dayLabel(key, t, i18n.language)}</span>
<span className="flex-1 h-px bg-primary/10" />
<span className="text-[10px] font-bold text-amber-600">+{byDay.get(key).reduce((s, n) => s + questXp(n), 0)} XP</span>
</motion.div>
<AnimatePresence>
{byDay.get(key).map((n) => {
const delay = Math.min(index++, 12) * 0.045;
const path = groupPathOf(n);
return (
<motion.div
key={n.id}
layout
initial={{ opacity: 0, x: 40 }}
animate={{ opacity: 1, x: 0 }}
exit={{ opacity: 0, x: 80, scale: 0.9, transition: { duration: 0.25 } }}
transition={{ type: 'spring', stiffness: 260, damping: 24, delay }}
className="group relative rounded-2xl border-2 border-primary/15 bg-surface-container-low px-3.5 py-3 flex flex-col gap-1.5 hover:border-primary/40 transition-colors"
>
<Stamp delay={delay + 0.2} />
<span className="font-bold text-primary pr-12 leading-tight break-words">{n.data.label || t('canvas.new_goal', 'New Goal')}</span>
{path && (
<span className="flex items-center gap-1 text-[10px] font-bold text-purple-600 dark:text-purple-300">
<Layers size={10} /> {path}
</span>
)}
<div className="flex items-center gap-1.5 flex-wrap">
<span className="flex items-center gap-0.5 text-[10px] font-extrabold px-1.5 py-0.5 rounded-md bg-amber-400 text-amber-950">
<Sparkles size={9} /> +{questXp(n)} XP
</span>
{hasReward(n) && (
<span className="flex items-center gap-1 text-[10px] font-extrabold px-1.5 py-0.5 rounded-md bg-pink-500 text-white max-w-[200px]">
<Gift size={10} className="shrink-0" /> <span className="truncate">{n.data.reward}</span>
</span>
)}
{n.data.completedAt && (
<span className="text-[10px] text-on-surface-variant/70 ml-auto">
{new Date(n.data.completedAt).toLocaleTimeString(i18n.language, { hour: '2-digit', minute: '2-digit' })}
</span>
)}
</div>
<div className="flex gap-2 pt-1 opacity-70 group-hover:opacity-100 transition-opacity">
<button onClick={() => onShow(n.id)} className="flex items-center gap-1 text-[11px] font-bold text-primary px-2 py-1 rounded-lg border border-primary/20 hover:bg-primary/5 cursor-pointer">
<Crosshair size={12} /> {t('archive.show', 'Show on board')}
</button>
{canEdit && (
<button onClick={() => onRestore(n.id)} className="flex items-center gap-1 text-[11px] font-bold text-primary px-2 py-1 rounded-lg border border-primary/20 hover:bg-primary/5 cursor-pointer">
<RotateCcw size={12} /> {t('archive.restore', 'Back to work')}
</button>
)}
</div>
</motion.div>
);
})}
</AnimatePresence>
</div>
))}
</div>
)}
</div>
</motion.aside>
</>
)}
</AnimatePresence>
);
};
export default ArchiveDrawer;