File size: 5,257 Bytes
5936c9d
 
 
 
94b4ce6
5936c9d
94b4ce6
5936c9d
 
 
 
94b4ce6
 
5936c9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94b4ce6
 
5936c9d
 
 
 
 
 
 
 
94b4ce6
 
5936c9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94b4ce6
5936c9d
 
94b4ce6
 
5936c9d
94b4ce6
5936c9d
94b4ce6
5936c9d
94b4ce6
 
5936c9d
 
94b4ce6
5936c9d
94b4ce6
5936c9d
94b4ce6
5936c9d
94b4ce6
5936c9d
 
 
 
 
94b4ce6
5936c9d
 
 
 
 
 
 
94b4ce6
 
 
5936c9d
 
 
 
 
 
94b4ce6
 
5936c9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94b4ce6
 
 
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { ExternalLink, Eye, Heart, MessageCircle, Repeat2, Send } from 'lucide-react'
import { formatCount, timeAgo } from '@/lib/format'
import type { Post } from '@/lib/types'
import CodeBadge from './CodeBadge'

const KIND_LABEL: Record<string, string> = { gift: '赠链', box: '盲盒链接', talk: '讨论' }

function sourceLabel(post: Post) {
  if (post.source === 'telegram') return 'Telegram'
  if (post.source === 'manual') return '手动添加'
  return 'X'
}

/** Render text with links clickable and the gift links highlighted. */
function RichText({ text }: { text: string }) {
  const parts = text.split(/(https?:\/\/[^\s<>"'))】]+)/g)
  return (
    <p className="whitespace-pre-wrap break-words text-[14px] leading-6 text-fg-base">
      {parts.map((part, i) =>
        /^https?:\/\//.test(part) ? (
          <a
            key={i}
            href={part}
            target="_blank"
            rel="noreferrer"
            className={
              /mystery-box\/g\//i.test(part)
                ? 'rounded bg-brand-10 px-1 font-mono text-[13px] text-brand-100 hover:underline'
                : 'text-brand-100 hover:underline'
            }
          >
            {part.replace(/^https?:\/\//, '').slice(0, 60)}
            {part.replace(/^https?:\/\//, '').length > 60 ? '…' : ''}
          </a>
        ) : (
          <span key={i}>{part}</span>
        )
      )}
    </p>
  )
}

export default function PostCard({ post }: { post: Post }) {
  const media = (post.media || []).filter((m) => m?.url).slice(0, 4)
  const author = post.author_name || post.author || '未知'
  const handle = post.source === 'x' && post.author ? `@${post.author}` : null
  const created = post.created_at ? post.created_at * 1000 : null
  const codes = post.codes || []
  const usable = codes.some((c) => c.usable)
  const text = post.text || ''

  return (
    <article
      className={`rounded-[14px] border bg-[var(--bg-subtle)] p-4 transition-colors ${
        usable ? 'border-brand-60' : 'border-[var(--border-strong)]'
      }`}
    >
      <header className="flex items-start justify-between gap-3">
        <div className="min-w-0">
          <div className="flex flex-wrap items-center gap-x-2 gap-y-0.5">
            <span className="truncate text-sm font-bold">{author}</span>
            {handle && <span className="text-xs text-fg-muted">{handle}</span>}
          </div>
          <div className="mt-0.5 flex flex-wrap items-center gap-x-2 text-[11px] text-fg-muted">
            <span className="inline-flex items-center gap-1">
              {post.source === 'telegram' && <Send className="h-3 w-3" />}
              {sourceLabel(post)}
            </span>
            {post.via && post.source === 'telegram' && <span className="truncate">· {post.via}</span>}
            {created && <span>· {timeAgo(created)}</span>}
          </div>
        </div>
        <div className="flex shrink-0 items-center gap-2">
          <span
            className={`rounded-full px-2 py-0.5 text-[11px] font-medium ${
              post.kind === 'gift' ? 'bg-brand-10 text-brand-100' : 'bg-[var(--bg-subtle-hover)] text-fg-muted'
            }`}
          >
            {KIND_LABEL[post.kind] || post.kind}
          </span>
          {post.url && (
            <a
              href={post.url}
              target="_blank"
              rel="noreferrer"
              className="text-fg-muted hover:text-fg-base"
              title="打开原帖"
            >
              <ExternalLink className="h-4 w-4" />
            </a>
          )}
        </div>
      </header>

      {text && (
        <div className="mt-3">
          <RichText text={text.length > 900 ? `${text.slice(0, 900)}…` : text} />
        </div>
      )}

      {media.length > 0 && (
        <div className={`mt-3 grid gap-1.5 ${media.length > 1 ? 'grid-cols-2' : 'grid-cols-1'}`}>
          {media.map((m, i) => (
            <a key={i} href={m.url} target="_blank" rel="noreferrer" className="block overflow-hidden rounded-lg">
              <img src={m.url} alt="" loading="lazy" className="max-h-72 w-full object-cover" />
            </a>
          ))}
        </div>
      )}

      {codes.length > 0 && (
        <div className="mt-3 flex flex-wrap gap-2">
          {codes.map((code) => (
            <CodeBadge key={code.code} info={code} />
          ))}
        </div>
      )}

      {post.source === 'x' && (post.views || post.likes || post.reposts || post.replies) ? (
        <footer className="mt-3 flex flex-wrap gap-4 text-[11px] text-fg-muted">
          <span className="inline-flex items-center gap-1">
            <MessageCircle className="h-3 w-3" />
            {formatCount(post.replies)}
          </span>
          <span className="inline-flex items-center gap-1">
            <Repeat2 className="h-3 w-3" />
            {formatCount(post.reposts)}
          </span>
          <span className="inline-flex items-center gap-1">
            <Heart className="h-3 w-3" />
            {formatCount(post.likes)}
          </span>
          <span className="inline-flex items-center gap-1">
            <Eye className="h-3 w-3" />
            {formatCount(post.views)}
          </span>
        </footer>
      ) : null}
    </article>
  )
}