Files
o2o-weather/admin/src/pages/UsersPage.tsx
T

121 lines
6.6 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { useSearchParams } from 'react-router'
import { useTargets } from '../api/targets'
import type { Target } from '../api/targets'
type Provider = 1 | 2 | 3
type User = { nickname: string; provider: Provider; points: number; level: number; level_name: string; suspended: boolean; created_at: string; last_active?: string }
type Res = { total: number; counts: Record<string, number>; items: User[] }
const PROVIDER: Record<Provider, { label: string; tone: string }> = {
1: { label: '웹 일반', tone: 'bg-rail text-ink-soft' },
2: { label: '웹 구글', tone: 'bg-warn-soft text-warn' },
3: { label: '토스', tone: 'bg-rust-soft text-rust' },
}
const DAY = ['일', '월', '화', '수', '목', '금', '토']
function day(iso: string) {
const d = new Date(iso)
return `${d.getFullYear() % 100}.${d.getMonth() + 1}.${d.getDate()} ${DAY[d.getDay()]}`
}
/** 가입자 목록: 올릴 곳(로컬·킹서버)마다 가입 경로·레벨·모은 각을 본다 */
export function UsersPage() {
const { data: targets = [] } = useTargets()
const [params, setParams] = useSearchParams()
const target = targets.find((t) => t.id === params.get('target')) ?? targets[0]
const provider = (Number(params.get('provider')) || 0) as 0 | Provider
const set = (next: Record<string, string>) => setParams((p) => {
for (const [k, v] of Object.entries(next)) {
if (v) p.set(k, v)
else p.delete(k)
}
return p
}, { replace: true })
return (
<div className="mx-auto flex max-w-[1440px] flex-col gap-5 px-4 py-5 sm:px-6">
<header className="flex flex-wrap items-center justify-between gap-x-6 gap-y-3">
<div className="flex min-w-0 flex-col gap-0.5">
<h1 className="text-[20px] font-bold tracking-[-0.02em]">가입자</h1>
<p className="text-[13px] text-muted">웹(일반·구글)과 토스로 가입한 사람. 마지막 활동은 각을 마지막으로 받은 날이에요.</p>
</div>
{targets.length > 1 && (
<div role="tablist" aria-label="서버" className="flex gap-1 rounded-xl border border-line bg-panel p-1">
{targets.map((t) => (
<button key={t.id} role="tab" aria-selected={t.id === target?.id} onClick={() => set({ target: t.id })} className={`rounded-lg px-4 py-2 text-[14px] font-bold transition-colors duration-150 ${t.id === target?.id ? 'bg-ink text-white' : 'text-ink-soft hover:bg-rail'}`}>
{t.label}
</button>
))}
</div>
)}
</header>
{target ? <UserList target={target} provider={provider} onProvider={(p) => set({ provider: p ? String(p) : '' })} /> : <p className="rounded-xl border border-bad/30 bg-bad-soft px-5 py-4 text-[14px] text-bad">서버가 없어요. 관리자 서버에 ADMIN_API_KEY(로컬)·KING_ADMIN_API_KEY(킹서버)를 주고 다시 켜 주세요.</p>}
</div>
)
}
function UserList({ target, provider, onProvider }: { target: Target; provider: 0 | Provider; onProvider: (p: 0 | Provider) => void }) {
const { data, error, isLoading } = useQuery({
queryKey: ['users', target.id],
queryFn: async () => {
const r = await fetch(`/api/users?target=${target.id}`)
const body = await r.json()
if (!r.ok || body.result?.success === false) throw new Error(body.error ?? body.result?.desc ?? `HTTP ${r.status}`)
return body as Res
},
})
const items = data?.items ?? []
const shown = provider ? items.filter((u) => u.provider === provider) : items
return (
<section className="flex flex-col gap-4">
<div role="radiogroup" aria-label="가입 경로" className="flex flex-wrap gap-2">
{([0, 1, 2, 3] as const).map((p) => (
<button key={p} role="radio" aria-checked={provider === p} onClick={() => onProvider(p)} className={`rounded-full border px-3 py-1 text-[12px] font-semibold transition-colors duration-150 ${provider === p ? 'border-ink bg-ink text-white' : 'border-line bg-panel text-ink hover:border-ink-soft'}`}>
{p === 0 ? '전체' : PROVIDER[p].label} <span className={provider === p ? 'text-white/70' : 'text-muted'}>{p === 0 ? (data?.total ?? 0) : (data?.counts[p] ?? 0)}</span>
</button>
))}
</div>
{error && (
<div role="alert" className="rounded-xl border border-bad/30 bg-bad-soft px-5 py-4 text-[14px] text-bad">
{target.label} 가입자를 받지 못했어요: {error instanceof Error ? error.message : String(error)}
</div>
)}
{isLoading && <div className="h-64 animate-pulse rounded-2xl bg-rail" aria-busy="true" />}
{data && shown.length === 0 && <p className="rounded-2xl border border-dashed border-line bg-panel px-5 py-10 text-center text-[14px] text-muted">가입자가 없어요.</p>}
{shown.length > 0 && (
<div className="overflow-x-auto rounded-2xl border border-line bg-panel">
<table className="w-full min-w-[720px] border-collapse text-[14px]">
<thead>
<tr className="border-b border-line text-left text-[12px] text-muted">
<th className="px-4 py-2.5 font-semibold">닉네임</th>
<th className="px-4 py-2.5 font-semibold">가입 경로</th>
<th className="px-4 py-2.5 font-semibold">레벨</th>
<th className="px-4 py-2.5 text-right font-semibold">각</th>
<th className="px-4 py-2.5 font-semibold">가입일</th>
<th className="px-4 py-2.5 font-semibold">마지막 활동</th>
</tr>
</thead>
<tbody>
{shown.map((u, i) => (
<tr key={`${u.created_at}-${i}`} className="border-b border-line/60 last:border-0">
<td className="px-4 py-2.5 font-semibold">
{u.nickname}
{u.suspended && <span className="ml-2 rounded-md bg-bad-soft px-1.5 py-0.5 text-[11px] font-bold text-bad">정지</span>}
</td>
<td className="px-4 py-2.5"><span className={`rounded-md px-1.5 py-0.5 text-[12px] font-bold ${PROVIDER[u.provider].tone}`}>{PROVIDER[u.provider].label}</span></td>
<td className="px-4 py-2.5">Lv.{u.level} <span className="text-ink-soft">{u.level_name}</span></td>
<td className="px-4 py-2.5 text-right tabular-nums">{u.points.toLocaleString()}</td>
<td className="px-4 py-2.5 tabular-nums text-ink-soft">{day(u.created_at)}</td>
<td className="px-4 py-2.5 tabular-nums text-ink-soft">{u.last_active ? day(u.last_active) : '—'}</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</section>
)
}