'use client' import { useEffect, useRef, useState } from 'react' import { WowheadRefresh } from './WowheadRefresh' import { wowheadUrl, wowheadData } from '@/lib/wowhead' export type ActivityEntry = { id: number; name: string; icon: string; date: number } /** * Pestaña de Logros: filtros por ID y por nombre, tabla Logro | Fecha, paginación. * Mismo estilo que Misiones. Datos vía /api/armory/activity; 1ª página del servidor. */ export function ArmoryActivity({ guid, locale, initial, total: initialTotal, grandTotal, pageSize, labels, }: { guid: number locale: string initial: ActivityEntry[] total: number grandTotal: number pageSize: number labels: { filterId: string filterName: string empty: string logro: string date: string title: string countLine: string } }) { const [entries, setEntries] = useState(initial) const [total, setTotal] = useState(initialTotal) const [page, setPage] = useState(1) const [qid, setQid] = useState('') const [qname, setQname] = useState('') const [loading, setLoading] = useState(false) const pages = Math.max(1, Math.ceil(total / pageSize)) const load = async (p: number, id: string, name: string) => { setLoading(true) try { const res = await fetch( `/api/armory/activity?guid=${guid}&page=${p}&id=${encodeURIComponent(id)}&name=${encodeURIComponent(name)}&locale=${locale}`, ) const data = await res.json() setEntries(Array.isArray(data.entries) ? data.entries : []) setTotal(Number(data.total) || 0) } catch { setEntries([]) } finally { setLoading(false) } } const skip = useRef(true) useEffect(() => { if (skip.current) { skip.current = false return } const t = setTimeout(() => { setPage(1) load(1, qid, qname) }, 300) return () => clearTimeout(t) // eslint-disable-next-line react-hooks/exhaustive-deps }, [qid, qname]) const goto = (p: number) => { if (p < 1 || p > pages || p === page || loading) return setPage(p) load(p, qid, qname) } const fmt = (ts: number) => { const d = new Date(ts * 1000) const p = (n: number) => String(n).padStart(2, '0') return `${p(d.getDate())}/${p(d.getMonth() + 1)}/${d.getFullYear()} ${p(d.getHours())}:${p(d.getMinutes())}` } const win = 2 const from = Math.max(1, page - win) const to = Math.min(pages, page + win) const nums: number[] = [] for (let p = from; p <= to; p++) nums.push(p) const countLine = labels.countLine .replace('{total}', String(total)) .replace('{page}', String(page)) .replace('{pages}', String(pages)) return ( <>
setQid(e.target.value)} placeholder={labels.filterId} inputMode="numeric" /> setQname(e.target.value)} placeholder={labels.filterName} />

{countLine}

{labels.title} ({grandTotal})
{entries.length === 0 ? (

{labels.empty}

) : ( {entries.map((a) => ( ))}
{labels.logro} {labels.date}
{a.name} ({a.id}) {fmt(a.date)}
)} {pages > 1 && (
{from > 1 && ( <> {from > 2 && } )} {nums.map((p) => ( ))} {to < pages && ( <> {to < pages - 1 && } )}
)} ) }