-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathPostCard.tsx
More file actions
181 lines (158 loc) · 5.43 KB
/
Copy pathPostCard.tsx
File metadata and controls
181 lines (158 loc) · 5.43 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
"use client";
import { useEffect, useState } from "react";
export interface Post {
id: string | number;
author: string;
username?: string;
content: string;
tip_total?: string | number;
like_count?: string | number;
timestamp?: string | number;
created_at?: string;
imageUrl?: string;
}
interface PostCardProps {
post: Post;
query?: string;
onLike?: () => void;
onTip?: () => void;
isLiked?: boolean;
isTipping?: boolean;
tourAnchor?: boolean;
}
function escapeRegExp(value: string): string {
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
function highlightText(text: string, query = "") {
const trimmed = query.trim();
if (!trimmed) return text;
const parts = text.split(new RegExp(`(${escapeRegExp(trimmed)})`, "gi"));
return parts.map((part, index) =>
part.toLowerCase() === trimmed.toLowerCase() ? (
<mark
key={`${part}-${index}`}
className="rounded bg-yellow-300 px-1 text-black font-semibold"
>
{part}
</mark>
) : (
part
)
);
}
export function getPostDate(post: Post): Date | null {
const raw = post.created_at ?? post.timestamp;
if (raw === undefined || raw === null || raw === "") return null;
const numeric = Number(raw);
if (Number.isFinite(numeric)) {
return new Date(numeric < 1_000_000_000_000 ? numeric * 1000 : numeric);
}
const date = new Date(raw);
return Number.isNaN(date.getTime()) ? null : date;
}
export function getPostTipTotal(post: Post): number {
const value = Number(post.tip_total ?? 0);
return Number.isFinite(value) ? value : 0;
}
export function getPostLikeCount(post: Post): number {
const value = Number(post.like_count ?? 0);
return Number.isFinite(value) ? value : 0;
}
function formatDate(post: Post): string {
const date = getPostDate(post);
return date ? date.toLocaleDateString() : "Unknown date";
}
export function formatAuthor(author: string): string {
return author.length > 16 ? `${author.slice(0, 6)}...${author.slice(-4)}` : author;
}
export function PostCard({
post,
query,
onLike,
onTip,
isLiked,
isTipping,
tourAnchor,
}: PostCardProps) {
const likeCount = getPostLikeCount(post);
const [animateLikeCount, setAnimateLikeCount] = useState(false);
const [previousLikeCount, setPreviousLikeCount] = useState(likeCount);
useEffect(() => {
if (likeCount === previousLikeCount) {
return;
}
setAnimateLikeCount(true);
setPreviousLikeCount(likeCount);
const timeout = window.setTimeout(() => {
setAnimateLikeCount(false);
}, 220);
return () => window.clearTimeout(timeout);
}, [likeCount, previousLikeCount]);
return (
<article className="rounded-xl border border-[var(--border)] bg-[var(--muted)] p-3 md:p-4 lg:p-5 shadow-lg transition-all duration-300 hover:border-violet-500/40 hover:shadow-violet-950/10">
<div className="mb-3 flex flex-wrap items-center justify-between gap-2 text-sm text-[var(--text-muted)]">
<span
className="font-medium hover:text-[var(--foreground)] transition-colors"
title={post.author}
>
By {formatAuthor(post.author)}
</span>
<time className="text-xs text-[var(--text-muted)]">{formatDate(post)}</time>
</div>
<p className="whitespace-pre-wrap break-words leading-relaxed text-[var(--foreground)] mb-4 text-base">
{highlightText(post.content, query)}
</p>
<div
className="flex items-center gap-6 border-t border-[var(--border)]/40 pt-3"
data-tour={tourAnchor ? "post-actions" : undefined}
>
{/* Like Button */}
<button
onClick={onLike}
className={`flex items-center gap-2 text-sm font-medium transition-colors ${
isLiked
? "text-[var(--accent-coral)]"
: "text-[var(--text-muted)] hover:text-[var(--accent-coral)]"
}`}
aria-label={isLiked ? "Unlike post" : "Like post"}
>
<span
className={`text-lg transition-transform duration-200 ${animateLikeCount ? "scale-110" : "scale-100"}`}
>
{isLiked ? "❤️" : "🤍"}
</span>
<span className={animateLikeCount ? "like-count-animate" : ""}>{likeCount}</span>
</button>
{/* Tip Button */}
<button
onClick={onTip}
disabled={isTipping}
className="flex items-center gap-2 text-sm font-medium text-[var(--text-muted)] hover:text-[var(--accent-teal)] transition-colors disabled:opacity-50"
aria-label="Tip creator"
>
<span className="text-lg">💰</span>
<span>Tips: {getPostTipTotal(post)}</span>
</button>
</div>
</article>
);
}
export function PostCardSkeleton() {
return (
<div className="animate-pulse rounded-xl border border-[var(--border)] bg-[var(--muted)] p-3 md:p-4 lg:p-5 space-y-4">
<div className="flex justify-between">
<div className="h-4 w-28 rounded bg-zinc-800" />
<div className="h-4 w-20 rounded bg-zinc-800" />
</div>
<div className="space-y-3">
<div className="h-4 w-full rounded bg-zinc-800" />
<div className="h-4 w-4/5 rounded bg-zinc-800" />
<div className="h-4 w-2/3 rounded bg-zinc-800" />
</div>
<div className="border-t border-[var(--border)]/40 pt-3 flex gap-6">
<div className="h-5 w-12 rounded bg-zinc-800" />
<div className="h-5 w-20 rounded bg-zinc-800" />
</div>
</div>
);
}