Spaces:
Sleeping
Sleeping
File size: 3,172 Bytes
5936c9d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | import { useEffect, useState } from 'react'
import { Check, Copy } from 'lucide-react'
import { timeAgo } from '@/lib/format'
import type { CodeInfo } from '@/lib/types'
export function countdown(expiresAt: number | null | undefined, now = Date.now()) {
if (!expiresAt) return null
const s = Math.floor(expiresAt - now / 1000)
if (s <= 0) return '已到期'
const d = Math.floor(s / 86400)
const h = Math.floor((s % 86400) / 3600)
const m = Math.floor((s % 3600) / 60)
if (d > 0) return `${d}天${h}小时`
if (h > 0) return `${h}小时${m}分`
return `${m}分${s % 60}秒`
}
export function statusText(info: CodeInfo) {
if (!info.checked_at) return '检查中…'
if (info.usable) return `剩 ${info.seats ?? 0} 份`
if (info.status === 'INVALID') return '无效'
if (info.status === 'EXPIRED') return '已过期'
if ((info.seats ?? 0) <= 0) return '已领完'
if (info.expires_at && info.expires_at * 1000 < Date.now()) return '已过期'
return info.status || '不可用'
}
export function useTick(ms = 1000) {
const [now, setNow] = useState(Date.now())
useEffect(() => {
const t = setInterval(() => setNow(Date.now()), ms)
return () => clearInterval(t)
}, [ms])
return now
}
export function CopyButton({ text, label }: { text: string; label?: string }) {
const [done, setDone] = useState(false)
return (
<button
type="button"
onClick={async (e) => {
e.preventDefault()
try {
await navigator.clipboard.writeText(text)
} catch {
const el = document.createElement('textarea')
el.value = text
document.body.appendChild(el)
el.select()
document.execCommand('copy')
el.remove()
}
setDone(true)
setTimeout(() => setDone(false), 1500)
}}
className="inline-flex items-center gap-1 rounded-md px-1.5 py-0.5 text-[11px] text-fg-muted hover:bg-[var(--bg-subtle-hover)] hover:text-fg-base"
title="复制"
>
{done ? <Check className="h-3 w-3" /> : <Copy className="h-3 w-3" />}
{label}
</button>
)
}
/** One gift code inside a post: status, seats, expiry, copy. */
export default function CodeBadge({ info }: { info: CodeInfo }) {
const link = `https://asksurf.ai/mystery-box/g/${info.code}`
return (
<div
className={`flex items-center gap-2 rounded-lg border px-2.5 py-1.5 text-xs ${
info.usable ? 'border-brand-60 bg-brand-10' : 'border-[var(--border-strong)] bg-background/40'
}`}
>
<a href={link} target="_blank" rel="noreferrer" className="font-mono font-semibold hover:underline">
{info.code}
</a>
<span className={info.usable ? 'font-semibold text-brand-100' : 'text-fg-muted'}>{statusText(info)}</span>
{info.usable && info.expires_at ? <span className="text-fg-muted">· {countdown(info.expires_at)}</span> : null}
{info.tier && <span className="text-fg-muted">· {info.tier}</span>}
{info.pushed_at ? <span className="text-fg-muted" title={`推送于 ${timeAgo(info.pushed_at * 1000)}`}>· 已推送</span> : null}
<CopyButton text={link} />
</div>
)
}
|