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 }) => ( Done ); /** * 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 ( {open && ( <> {t('archive.title', 'Archive')} {[ { 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) => ( {s.label} {s.value} ))} {['quests', 'rewards'].map((key) => ( 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 && } {key === 'quests' ? t('quests.quests', 'Quests') : t('quests.rewards', 'Rewards')} ))} 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" /> {filtered.length === 0 ? ( {q ? t('archive.nothing_found', 'Nothing found') : t('archive.empty', 'No completed quests yet')} {!q && {t('archive.empty_hint', 'Finish a quest on the board — it will land here with its XP and reward.')}} ) : tab === 'rewards' ? ( rewards.length === 0 ? ( {t('archive.no_rewards', 'No rewards collected yet. Add a reward to a quest in its settings.')} ) : ( {rewards.map((n, i) => ( {n.data.reward} {n.data.label} ))} ) ) : ( {days.map((key) => ( {dayLabel(key, t, i18n.language)} +{byDay.get(key).reduce((s, n) => s + questXp(n), 0)} XP {byDay.get(key).map((n) => { const delay = Math.min(index++, 12) * 0.045; const path = groupPathOf(n); return ( {n.data.label || t('canvas.new_goal', 'New Goal')} {path && ( {path} )} +{questXp(n)} XP {hasReward(n) && ( {n.data.reward} )} {n.data.completedAt && ( {new Date(n.data.completedAt).toLocaleTimeString(i18n.language, { hour: '2-digit', minute: '2-digit' })} )} 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"> {t('archive.show', 'Show on board')} {canEdit && ( 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"> {t('archive.restore', 'Back to work')} )} ); })} ))} )} > )} ); }; export default ArchiveDrawer;
{t('archive.empty_hint', 'Finish a quest on the board — it will land here with its XP and reward.')}
{t('archive.no_rewards', 'No rewards collected yet. Add a reward to a quest in its settings.')}