-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommentItem.tsx
More file actions
270 lines (239 loc) · 8.76 KB
/
CommentItem.tsx
File metadata and controls
270 lines (239 loc) · 8.76 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
"use client";
import { useRef, useState } from "react";
import Image from "next/image";
import { MessageCircleMore } from "lucide-react";
import type { Comment } from "../types/comment";
import type { Dictionary } from "../types/dictionary";
import { getAvatarSrc } from "../lib/avatar";
import CommentVoteBar from "./CommentVoteBar";
import CommentForm from "./CommentForm";
import ReportButton from "./ReportButton";
import ShareButton from "./ShareButton";
type CommentDict = Pick<Dictionary, "comment" | "post">;
function formatTimeAgo(dateString: string): string {
const date = new Date(dateString);
const now = new Date();
const seconds = Math.floor((now.getTime() - date.getTime()) / 1000);
if (seconds < 60) return `${seconds}s`;
if (seconds < 3600) return `${Math.floor(seconds / 60)}m`;
if (seconds < 86400) return `${Math.floor(seconds / 3600)}h`;
if (seconds < 2592000) return `${Math.floor(seconds / 86400)}d`;
return date.toLocaleDateString();
}
function CommentThread({
comment,
dict,
onAddReply,
}: {
comment: Comment;
dict: CommentDict;
onAddReply: (parentId: number, content: string) => void;
}) {
const [collapsed, setCollapsed] = useState(false);
const [showReplyBox, setShowReplyBox] = useState(false);
const hoverBlockId = String(comment.id);
const hoverActiveRef = useRef(false);
const [isHoverActive, setIsHoverActive] = useState(false);
const authorUsername = comment.author?.username ?? comment.username ?? "user";
const authorAvatarUrl =
comment.author?.avatar_url ?? comment.avatar_url ?? null;
const hasReplies = comment.replies && comment.replies.length > 0;
const toggleCollapsed = () => {
setCollapsed((prev) => {
const next = !prev;
if (next) setShowReplyBox(false);
return next;
});
};
const updateHoverFromTarget = (target: EventTarget | null) => {
const el = target as HTMLElement | null;
const blockEl = el?.closest?.(
"[data-thread-hover-block]",
) as HTMLElement | null;
const blockId = blockEl?.getAttribute("data-thread-hover-block");
const next = !(blockId && blockId !== hoverBlockId);
if (hoverActiveRef.current !== next) {
hoverActiveRef.current = next;
setIsHoverActive(next);
}
};
const handleMouseEnter = (e: React.MouseEvent<HTMLDivElement>) => {
updateHoverFromTarget(e.target);
};
const handleMouseMove = (e: React.MouseEvent<HTMLDivElement>) => {
updateHoverFromTarget(e.target);
};
const handleMouseLeave = () => {
hoverActiveRef.current = false;
setIsHoverActive(false);
};
const lineBgClass = isHoverActive ? "bg-zinc-400" : "bg-zinc-300";
const borderHoverClass = isHoverActive
? "border-zinc-400"
: "border-zinc-300";
return (
<div
className="w-full"
onMouseEnter={handleMouseEnter}
onMouseMove={handleMouseMove}
onMouseLeave={handleMouseLeave}
>
<div className="flex w-full gap-2">
{/* Thread column */}
<div className="flex flex-col items-center w-8.5">
{!collapsed && (
<>
{/* Avatar */}
<Image
src={getAvatarSrc(authorAvatarUrl)}
alt={authorUsername}
width={34}
height={34}
className="rounded-full object-cover"
unoptimized
/>
{hasReplies && (
<div
className={`flex-1 w-px cursor-pointer ${lineBgClass}`}
onClick={toggleCollapsed}
/>
)}
</>
)}
</div>
{/* Comment content */}
<div className="flex-1 min-w-0">
{collapsed ? (
<button
onClick={() => setCollapsed(false)}
className="flex items-center gap-2 py-2 -ml-8 -mt-1 mb-6 text-xs text-zinc-500 hover:text-zinc-800 font-medium"
type="button"
>
<span className="flex items-center justify-center w-4 h-4 text-[9px] rounded-full border border-zinc-300 text-zinc-400 bg-white">
+
</span>
{authorUsername}
{hasReplies && (
<span className="opacity-60">
[+{comment.replies?.length} replies]
</span>
)}
</button>
) : (
<>
<div className="flex items-center gap-2 mt-2">
<span className="text-xs font-semibold hover:underline">
{authorUsername}
</span>
<span className="text-xs text-zinc-500">
• {formatTimeAgo(comment.created_at)}
</span>
</div>
<div className="text-sm mb-2 mt-5 whitespace-pre-wrap leading-relaxed">
{comment.content}
</div>
<div className="relative flex items-center flex-wrap gap-1 -ml-2 mb-2">
{hasReplies && (
<button
onClick={toggleCollapsed}
className="absolute top-1/2 -translate-y-1/2 ml-2 z-10 flex items-center justify-center w-4 h-4 text-[9px] rounded-full border border-zinc-600 text-zinc-400 bg-white transition"
style={{ left: "-2.05rem" }}
type="button"
aria-label={collapsed ? "Expand thread" : "Collapse thread"}
>
{collapsed ? "+" : "−"}
</button>
)}
<CommentVoteBar
voteableId={comment.id}
initialUpvotes={comment.upvotes}
initialDownvotes={comment.downvotes}
userVote={comment.user_vote}
/>
<button
onClick={() => setShowReplyBox(!showReplyBox)}
className="rounded-full h-8 px-3 text-xs font-semibold transition-colors inline-flex items-center justify-center gap-1.5 text-gray-700 hover:bg-gray-100"
type="button"
>
<MessageCircleMore className="h-4 w-4" />
<span>{dict.comment.reply}</span>
</button>
<ShareButton
type="comment"
id={comment.post_id}
slug={comment.id.toString()}
dict={dict}
className="rounded-full h-8 px-3 text-xs font-semibold transition-colors inline-flex items-center justify-center gap-1.5 text-gray-700 hover:bg-gray-100"
/>
<ReportButton commentId={comment.id} dict={dict} />
</div>
{showReplyBox && (
<div className="mt-2 mb-4 max-w-xl">
<CommentForm
postId={comment.post_id}
parentId={comment.id}
onSubmit={(content) => {
onAddReply(comment.id, content);
setShowReplyBox(false);
}}
onCancel={() => setShowReplyBox(false)}
placeholder={dict.comment.reply}
buttonText={dict.comment.reply}
dict={dict}
/>
</div>
)}
</>
)}
</div>
</div>
{/* Replies */}
{!collapsed && hasReplies && (
<div className="pl-10.5" data-thread-hover-block={hoverBlockId}>
<div className="pl-0.5">
{comment.replies!.map((reply, index) => {
const isLast = index === comment.replies!.length - 1;
return (
<div
key={reply.id}
className={index === 0 ? "relative pt-2" : "relative"}
>
{!isLast && (
<div
className={`absolute -left-7 top-0 bottom-0 w-px cursor-pointer ${lineBgClass}`}
onClick={toggleCollapsed}
/>
)}
<div
className={`absolute -left-7 -top-[0.1px] h-5 w-7 border-l border-b rounded-bl-4xl cursor-pointer ${borderHoverClass}`}
onClick={toggleCollapsed}
/>
<CommentThread
comment={reply}
dict={dict}
onAddReply={onAddReply}
/>
</div>
);
})}
</div>
</div>
)}
</div>
);
}
export default function CommentItem({
comment,
dict,
onAddReply,
}: {
comment: Comment;
dict: CommentDict;
onAddReply: (parentId: number, content: string) => void;
}) {
return (
<div className="mb-4">
<CommentThread comment={comment} dict={dict} onAddReply={onAddReply} />
</div>
);
}