Spaces:
Sleeping
Sleeping
Download frontend/src/components/CodeBadge.tsx from Ethscriptions/cut: direct link, hf CLI and curl.
- Browser
- Download file 3.17 kB
-
https://huggingface.co/spaces/Ethscriptions/cut/resolve/main/frontend/src/components/CodeBadge.tsx
- Command line
-
hf download hf://spaces/Ethscriptions/cut/frontend/src/components/CodeBadge.tsx
-
curl -L -o CodeBadge.tsx https://huggingface.co/spaces/Ethscriptions/cut/resolve/main/frontend/src/components/CodeBadge.tsx
3.17 kB
| 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> | |
| ) | |
| } | |