-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathDiscussionCard.tsx
More file actions
159 lines (148 loc) · 4.39 KB
/
DiscussionCard.tsx
File metadata and controls
159 lines (148 loc) · 4.39 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
import React from "react";
import { motion } from "framer-motion";
import { MessageCircle, ThumbsUp, Calendar, Tag, User } from "lucide-react";
export interface Discussion {
id: string;
title: string;
body: string;
author: {
login: string;
avatar_url: string;
html_url: string;
};
category: {
name: string;
emoji: string;
};
created_at: string;
updated_at: string;
comments: number;
reactions: {
total_count: number;
};
html_url: string;
labels: Array<{
name: string;
color: string;
}>;
}
interface DiscussionCardProps {
discussion: Discussion;
index: number;
}
export default function DiscussionCard({
discussion,
index,
}: DiscussionCardProps): JSX.Element {
const handleCardClick = () => {
window.open(discussion.html_url, "_blank", "noopener,noreferrer");
};
const formatDate = (dateString: string) => {
const date = new Date(dateString);
return date.toLocaleDateString("en-US", {
year: "numeric",
month: "short",
day: "numeric",
});
};
const truncateText = (text: string, maxLength: number) => {
if (!text || text.length <= maxLength) return text;
return text.substring(0, maxLength).trim() + "...";
};
const formatCount = (count: number) => {
if (count >= 1000) {
return (count / 1000).toFixed(1) + "k";
}
return count.toString();
};
return (
<motion.div
className="discussion-card-new"
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.5, delay: index * 0.1 }}
whileHover={{ boxShadow: "0 4px 20px rgba(0,0,0,0.08)" }}
onClick={handleCardClick}
role="button"
tabIndex={0}
onKeyDown={(e) =>
(e.key === "Enter" || e.key === " ") && handleCardClick()
}
aria-label={`Open discussion: ${discussion.title}`}
>
<div className="discussion-card-header">
<div className="discussion-category-badge">
<Tag size={14} />
<span>{discussion.category.name}</span>
</div>
<div className="discussion-date-badge">
<Calendar size={14} />
<span>{formatDate(discussion.created_at)}</span>
</div>
</div>
<div className="discussion-main-content">
<h3 className="discussion-title-new">
<a
href={discussion.html_url}
target="_blank"
rel="noopener noreferrer"
onClick={(e) => e.stopPropagation()}
>
{discussion.title}
</a>
</h3>
{discussion.body && (
<p className="discussion-excerpt">
{truncateText(
discussion.body.replace(/[#*`\[\]]/g, "").replace(/\n/g, " "),
150,
)}
</p>
)}
{discussion.labels.length > 0 && (
<div className="discussion-keywords">
{discussion.labels.slice(0, 4).map((label, idx) => (
<span key={idx} className="keyword-tag">
{label.name}
</span>
))}
</div>
)}
</div>
<div className="discussion-card-footer">
<div className="discussion-author-info">
{discussion.author.avatar_url ? (
<img
src={discussion.author.avatar_url}
alt={discussion.author.login}
className="author-avatar-small"
onError={(e) => {
const target = e.currentTarget;
target.style.display = "none";
const fallback = target.nextElementSibling;
if (fallback) fallback.style.display = "flex";
}}
/>
) : null}
<div
className="author-avatar-fallback"
style={{ display: discussion.author.avatar_url ? "none" : "flex" }}
>
{discussion.author.login.charAt(0).toUpperCase()}
</div>
<span className="author-name-new">{discussion.author.login}</span>
</div>
<div className="discussion-engagement">
<div className="engagement-item">
<MessageCircle size={16} />
<span>{formatCount(discussion.comments)}</span>
</div>
<div className="engagement-item">
<ThumbsUp size={16} />
<span>{formatCount(discussion.reactions.total_count)}</span>
</div>
</div>
</div>
</motion.div>
);
}