import { useEffect, useState, type ReactNode } from 'react' import { useQuery, useQueryClient } from '@tanstack/react-query' import { ExternalLink, Gift, Loader2, RefreshCw, Search, Send, Settings, X as XIcon } from 'lucide-react' import { request } from './lib/api' import { formatCount, timeAgo } from './lib/format' import type { FeedResponse, StatusResponse, UsableCode } from './lib/types' import PostCard from './components/PostCard' import { CopyButton, countdown, useTick } from './components/CodeBadge' import SettingsPanel from './components/SettingsPanel' type View = 'gifts' | 'links' | 'talk' | 'all' type Source = '' | 'x' | 'telegram' | 'manual' const VIEWS: { id: View; label: string }[] = [ { id: 'gifts', label: '赠链' }, { id: 'links', label: '赠链 + 盲盒链接' }, { id: 'talk', label: '讨论' }, { id: 'all', label: '全部' }, ] const SOURCES: { id: Source; label: string }[] = [ { id: '', label: '全部来源' }, { id: 'x', label: 'X' }, { id: 'telegram', label: 'Telegram' }, { id: 'manual', label: '手动' }, ] function useDebounced(value: T, ms = 300) { const [v, setV] = useState(value) useEffect(() => { const t = setTimeout(() => setV(value), ms) return () => clearTimeout(t) }, [value, ms]) return v } export default function App() { const qc = useQueryClient() const [view, setView] = useState('gifts') const [source, setSource] = useState('') const [q, setQ] = useState('') const query = useDebounced(q.trim()) const [showSettings, setShowSettings] = useState(false) const [scanning, setScanning] = useState(false) const [notice, setNotice] = useState<{ text: string; error?: boolean } | null>(null) const feed = useQuery({ queryKey: ['feed', view, source, query], queryFn: () => { const params = new URLSearchParams({ view, limit: '200' }) if (source) params.set('source', source) if (query) params.set('q', query) return request(`feed?${params}`) }, refetchInterval: 15000, placeholderData: (prev) => prev, }) const status = useQuery({ queryKey: ['status'], queryFn: () => request('status'), refetchInterval: 15000, }) async function scan() { setScanning(true) setNotice(null) try { const r = await request<{ newPosts?: number; newCodes?: number; seconds?: number; pushed?: unknown[] }>('scan', { body: {} }) const parts = [`抓取完成(${r.seconds ?? 0} 秒)`] parts.push(r.newPosts ? `新增 ${r.newPosts} 条帖子` : '没有新帖子') if (r.newCodes) parts.push(`${r.newCodes} 个新赠链`) if (r.pushed?.length) parts.push(`推送了 ${r.pushed.length} 个`) setNotice({ text: parts.join(',') }) } catch (err) { setNotice({ text: err instanceof Error ? err.message : '抓取失败', error: true }) } finally { setScanning(false) qc.invalidateQueries({ queryKey: ['feed'] }) qc.invalidateQueries({ queryKey: ['status'] }) } } const data = feed.data const posts = data?.posts ?? [] const usable = data?.usable ?? [] return (

Surf 盲盒赠链雷达

{notice && (

{notice.text}

)} setShowSettings(true)} />
{VIEWS.map((v) => ( setView(v.id)}> {v.label} ))}
{SOURCES.map((s) => ( setSource(s.id)}> {s.label} ))}
{feed.isLoading && (
加载中…
)} {feed.isError && (

读取失败:{(feed.error as Error)?.message}

)} {!feed.isLoading && posts.length === 0 && !feed.isError && (

这里还没有内容

)} {posts.map((post) => ( ))} {posts.length >= 200 &&

只显示最近 200 条,可以用搜索找更早的

}
{showSettings && (
setShowSettings(false)}>
e.stopPropagation()} >

设置

)}
) } /* ------------------------------------------------------------- pieces -- */ function Chip({ active, small, onClick, children }: { active: boolean; small?: boolean; onClick: () => void; children: ReactNode }) { return ( ) } function Stat({ label, value }: { label: string; value: string }) { return (

{label}

{value}

) } function Pill({ tone, children }: { tone: 'ok' | 'warn' | 'bad' | 'off'; children: ReactNode }) { const dot = { ok: 'bg-emerald-400', warn: 'bg-amber-400', bad: 'bg-red-400', off: 'bg-zinc-500' }[tone] return ( {children} ) } function SourceStrip({ status }: { status?: StatusResponse }) { if (!status) return

连接中…

const x = status.x const t = status.telegram const xTone = !x.connected ? 'off' : x.expired ? 'bad' : x.pausedUntil ? 'warn' : 'ok' const xText = !x.connected ? 'X 未登录' : x.expired ? 'X 登录失效' : x.pausedUntil ? 'X 限流中' : 'X 实时搜索' const tTone = t.phase === 'live' ? 'ok' : t.phase === 'needs-login' || t.phase === 'error' ? 'bad' : t.phase === 'off' ? 'off' : 'warn' const tText = t.phase === 'live' ? 'Telegram 实时' : t.phase === 'needs-login' ? 'Telegram 需登录' : t.phase === 'off' ? 'Telegram 未开启' : 'Telegram 连接中' return (

{xText} {tText} {status.push.configured ? '企业微信推送' : '未设置推送'}

) } /** Problems the user should act on, shown in plain words. */ function Alerts({ status, onOpenSettings }: { status?: StatusResponse; onOpenSettings: () => void }) { if (!status) return null const items: string[] = [] if (!status.x.connected) items.push('X 还没登录,现在只能读少数账号的主页,大部分赠链会漏掉。') else if (status.x.expired) items.push('X 登录已失效,请重新粘贴 Cookie。') if (status.telegram.phase === 'needs-login') items.push(`Telegram 需要重新登录${status.telegram.error ? `(${status.telegram.error})` : ''}。`) if (!status.push.configured) items.push('还没设置企业微信推送地址。') else if (status.push.lastError) items.push(`推送失败:${status.push.lastError}`) if (status.backup.blockSave) items.push('读取备份失败,为了不覆盖旧数据,本次运行暂停了备份。') if (!items.length) return null return (
{items.map((t, i) => (

• {t}

))}
) } function UsableBoard({ codes, loading }: { codes: UsableCode[]; loading: boolean }) { const now = useTick(1000) const sorted = codes if (loading) return null return (

现在还能领({codes.length})

每 90 秒复查一次名额
{sorted.length === 0 ? (

暂时没有还有名额的赠链。出现新的会自动推送到企业微信,也会显示在这里。

) : (
{sorted.map((c) => { const link = `https://asksurf.ai/mystery-box/g/${c.code}` return (
{c.code} 剩 {c.seats} 份 {c.expires_at ? {countdown(c.expires_at, now)}后到期 : null} {c.tier && {c.tier}} {c.source === 'telegram' && } {c.author_name || c.author || ''} {c.first_seen ? ` · ${timeAgo(c.first_seen * 1000)}发现` : ''} {c.url && ( )}
) })}
)}
) }