import { useState, type ReactNode } from 'react' import { useQuery, useQueryClient } from '@tanstack/react-query' import { Loader2, Lock, Plus, Trash2 } from 'lucide-react' import { ApiError, getAdminKey, request, setAdminKey } from '@/lib/api' import { timeAgo } from '@/lib/format' import type { SettingsResponse, StatusResponse, TgChat, TgDialog } from '@/lib/types' /* ------------------------------------------------------------- helpers -- */ function Section({ title, hint, children }: { title: string; hint?: ReactNode; children: ReactNode }) { return (

{title}

{hint &&
{hint}
}
{children}
) } function Btn({ children, onClick, busy, kind = 'primary', disabled, }: { children: ReactNode onClick: () => void busy?: boolean kind?: 'primary' | 'ghost' | 'danger' disabled?: boolean }) { const style = kind === 'primary' ? 'bg-brand-100 text-white hover:opacity-90' : kind === 'danger' ? 'border border-red-500/40 text-red-400 hover:bg-red-500/10' : 'border border-[var(--border-strong)] text-fg-base hover:bg-[var(--bg-subtle-hover)]' return ( ) } const inputCls = 'w-full rounded-lg border border-[var(--border-strong)] bg-background px-3 py-2 text-sm outline-none focus:border-brand-60' function Dot({ tone }: { tone: 'ok' | 'warn' | 'bad' | 'off' }) { const color = { ok: 'bg-emerald-400', warn: 'bg-amber-400', bad: 'bg-red-400', off: 'bg-zinc-500' }[tone] return } function Msg({ text, error }: { text: string | null; error?: boolean }) { if (!text) return null return

{text}

} /** Run an action with busy + message state. */ function useAction() { const [busy, setBusy] = useState(null) const [msg, setMsg] = useState<{ text: string; error?: boolean; at: string } | null>(null) async function run(name: string, fn: () => Promise) { setBusy(name) setMsg(null) try { const text = await fn() if (text) setMsg({ text, at: name }) } catch (err) { setMsg({ text: err instanceof Error ? err.message : String(err), error: true, at: name }) } finally { setBusy(null) } } const msgFor = (...names: string[]) => (msg && names.includes(msg.at) ? : null) return { busy, run, msgFor } } /* --------------------------------------------------------------- panel -- */ export default function SettingsPanel({ status }: { status?: StatusResponse }) { const qc = useQueryClient() const settings = useQuery({ queryKey: ['settings'], queryFn: () => request('settings'), refetchInterval: 15000, retry: (count, err) => !(err instanceof ApiError && err.needsKey) && count < 2, }) const refresh = () => { qc.invalidateQueries({ queryKey: ['settings'] }) qc.invalidateQueries({ queryKey: ['status'] }) } if (settings.error?.needsKey) return if (settings.isLoading || !settings.data) return (
{settings.error ? settings.error.message : '读取设置…'}
) const s = settings.data return (
) } function KeyGate({ onDone }: { onDone: () => void }) { const [key, setKey] = useState(getAdminKey()) const { busy, run, msgFor } = useAction() return (
setKey(e.target.value)} placeholder="管理密码" /> run('key', async () => { setAdminKey(key.trim()) await request('auth/check', { body: {} }) onDone() }) } > 确认
{msgFor('key')}
) } /* -------------------------------------------------------------- add box -- */ function AddBox({ onDone }: { onDone: () => void }) { const qc = useQueryClient() const [input, setInput] = useState('') const { busy, run, msgFor } = useAction() return (
setInput(e.target.value)} placeholder="https://x.com/…/status/… 或 赠链码" /> run('add', async () => { const r = await request<{ message?: string; codes?: { code: string; usable: boolean; seats: number | null; status: string | null }[]; pushed?: unknown[] }>('add', { body: { input }, }) setInput('') qc.invalidateQueries({ queryKey: ['feed'] }) onDone() if (r.message) return r.message const list = (r.codes || []) .map((c) => `${c.code}:${c.usable ? `有名额(${c.seats} 份)` : c.status === 'INVALID' ? '无效' : '已不可用'}`) .join(';') return `已添加 ${list}${r.pushed?.length ? ',已推送' : ''}` }) } > 添加
{msgFor('add')}
) } /* -------------------------------------------------------------------- X -- */ function XSection({ s, onDone }: { s: SettingsResponse; onDone: () => void }) { const [cookies, setCookies] = useState('') const { busy, run, msgFor } = useAction() const x = s.x const tone = !x.connected ? 'off' : x.expired ? 'bad' : x.pausedUntil ? 'warn' : 'ok' const label = !x.connected ? '未登录:只能读少数账号主页,很多赠链会漏掉' : x.expired ? `登录已失效:${x.error || '请重新粘贴 Cookie'}` : x.pausedUntil ? `X 限流中,${timeAgo(x.pausedUntil * 1000).replace('前', '后')}自动恢复` : `已登录${x.username ? ` @${x.username}` : ''},每分钟搜索一次` return (
登录后才能实时搜索全站的赠链。在电脑浏览器登录 x.com → 按 F12 → Application / 存储 → Cookies → x.com,把 auth_token和ct0 两项复制进来(格式:auth_token=…; ct0=…)。 } >
{label}