'use client' import { useEffect, useRef, useState } from 'react' import { WowheadRefresh } from './WowheadRefresh' import { wowheadUrl, wowheadData } from '@/lib/wowhead' export type QuestStatus = 'rewarded' | 'complete' | 'inprogress' | 'failed' export type QuestEntry = { id: number; name: string; status: QuestStatus } // Color + icono por estado (mismo criterio para nombre, texto de estado e icono del filtro). const STATUS: Record = { all: { color: '#d4cdbb', icon: '' }, inprogress: { color: '#d79602', icon: 'fas fa-scroll' }, complete: { color: '#1eff00', icon: 'fas fa-check' }, failed: { color: '#ff5555', icon: 'fas fa-times' }, rewarded: { color: '#a335ee', icon: 'fas fa-gift' }, } export function ArmoryQuests({ guid, locale, initial, total: initialTotal, grandTotal, pageSize, statuses, badgeLabels, labels, }: { guid: number locale: string initial: QuestEntry[] total: number grandTotal: number pageSize: number statuses: { key: string; label: string; count: number }[] badgeLabels: Record labels: { filterId: string; filterName: string; empty: string; quest: string; statusCol: string; title: string; pageOf: 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 [status, setStatus] = useState('all') const [open, setOpen] = useState(false) const [loading, setLoading] = useState(false) const pages = Math.max(1, Math.ceil(total / pageSize)) const load = async (p: number, id: string, name: string, st: string) => { setLoading(true) try { const res = await fetch( `/api/armory/quests?guid=${guid}&page=${p}&id=${encodeURIComponent(id)}&name=${encodeURIComponent(name)}&status=${st}&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, status) }, 300) return () => clearTimeout(t) // eslint-disable-next-line react-hooks/exhaustive-deps }, [qid, qname, status]) const goto = (p: number) => { if (p < 1 || p > pages || p === page || loading) return setPage(p) load(p, qid, qname, status) } const pick = (key: string) => { setOpen(false) setStatus(key) } 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 current = statuses.find((s) => s.key === status) ?? statuses[0] const optLabel = (s: { key: string; label: string; count: number }) => s.key === 'all' ? `${s.label}` : `${s.label} (${s.count})` return ( <>
{labels.title} ({grandTotal})
setQid(e.target.value)} placeholder={labels.filterId} inputMode="numeric" /> setQname(e.target.value)} placeholder={labels.filterName} />
{open && (
{statuses.map((s) => ( ))}
)}
{entries.length === 0 ? (

{labels.empty}

) : ( {entries.map((q) => { const color = STATUS[q.status]?.color ?? '#d4cdbb' return ( ) })}
{labels.quest} {labels.statusCol}
{q.name} ({q.id}) {badgeLabels[q.status] ?? q.status}
)} {pages > 1 && (
{from > 1 && ( <> {from > 2 && } )} {nums.map((p) => ( ))} {to < pages && ( <> {to < pages - 1 && } )}
)} ) }