-
Notifications
You must be signed in to change notification settings - Fork 293
Expand file tree
/
Copy pathArenaHighlightsFeed.tsx
More file actions
337 lines (307 loc) · 9.84 KB
/
ArenaHighlightsFeed.tsx
File metadata and controls
337 lines (307 loc) · 9.84 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import type { ReactElement } from 'react';
import React, { useEffect, useRef, useState } from 'react';
import classNames from 'classnames';
import { useViewSize, ViewSize } from '../../../hooks';
import { getLastActivityDateFormat } from '../../../lib/dateFormat';
import Link from '../../../components/utilities/Link';
import type { SentimentAnnotation, SentimentHighlightItem } from './types';
import { stripTcoLinks } from './ArenaHighlightUtils';
import { getAgentEntityPath } from './links';
const decodeHtmlEntities = (text: string): string => {
const textarea =
typeof document !== 'undefined' && document.createElement('textarea');
if (!textarea) {
return text
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, "'");
}
textarea.innerHTML = text;
return textarea.value;
};
const getSentimentColor = (score: number): string => {
if (score > 0.2) {
return 'bg-accent-avocado-default';
}
if (score < -0.2) {
return 'bg-accent-ketchup-default';
}
return 'bg-text-quaternary';
};
const Placeholder = ({ className }: { className?: string }): ReactElement => (
<div
className={classNames(
'animate-pulse rounded-8 bg-surface-float',
className,
)}
/>
);
const SentimentPill = ({
sentiment,
tab,
}: {
sentiment: SentimentAnnotation;
tab?: 'coding-agents' | 'llms';
}): ReactElement => {
const content = (
<>
<span
className={classNames(
'inline-block h-1.5 w-1.5 shrink-0 rounded-full',
getSentimentColor(sentiment.score),
)}
/>
<span className="capitalize">{sentiment.entity.replace(/_/g, ' ')}</span>
</>
);
if (!tab) {
return (
<span className="inline-flex items-center gap-1 rounded-8 bg-surface-float px-2 py-0.5 leading-none text-text-secondary typo-caption2">
{content}
</span>
);
}
return (
<Link href={getAgentEntityPath(sentiment.entity, tab)}>
<a className="inline-flex items-center gap-1 rounded-8 bg-surface-float px-2 py-0.5 leading-none text-text-secondary typo-caption2 hover:text-text-link">
{content}
</a>
</Link>
);
};
const AuthorAvatar = ({
author,
}: {
author?: SentimentHighlightItem['author'];
}): ReactElement => {
if (author?.avatarUrl) {
return (
<img
src={author.avatarUrl}
alt={author.name ?? author.handle ?? ''}
className="h-8 w-8 rounded-full bg-surface-float object-cover"
/>
);
}
const initials = (author?.name ?? author?.handle ?? '?')
.split(' ')
.map((w) => w[0])
.join('')
.slice(0, 2)
.toUpperCase();
return (
<div className="bg-surface-tertiary flex h-8 w-8 items-center justify-center rounded-full text-text-tertiary typo-caption2">
{initials}
</div>
);
};
const HighlightCard = ({
item,
tab,
}: {
item: SentimentHighlightItem;
tab?: 'coding-agents' | 'llms';
}): ReactElement => {
const { author } = item;
const cleanText = decodeHtmlEntities(stripTcoLinks(item.text));
const handleCardClick = (e: React.MouseEvent<HTMLDivElement>): void => {
if ((e.target as HTMLElement).closest('a')) {
return;
}
window.open(item.url, '_blank', 'noopener,noreferrer');
};
return (
<div
className="flex cursor-pointer gap-3 px-4 py-3 transition-colors"
onClick={handleCardClick}
role="link"
tabIndex={0}
onKeyDown={(e) => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
window.open(item.url, '_blank', 'noopener,noreferrer');
}
}}
>
<AuthorAvatar author={author} />
<div className="flex min-w-0 flex-1 flex-col gap-1.5">
<div className="flex min-w-0 items-center gap-1.5">
{author?.name && (
<span className="shrink truncate font-bold text-text-primary typo-caption1">
{author.name}
</span>
)}
{author?.handle && (
<span className="shrink truncate text-text-quaternary typo-caption2">
@{author.handle}
</span>
)}
<span className="shrink-0 text-text-disabled typo-caption2">
· {getLastActivityDateFormat(item.createdAt)}
</span>
</div>
<p className="whitespace-pre-wrap break-words text-text-secondary typo-callout">
{cleanText}
</p>
{item.sentiments.length > 0 && (
<div className="flex flex-wrap gap-1.5">
{item.sentiments.map((sentiment) => (
<SentimentPill
key={sentiment.entity}
sentiment={sentiment}
tab={tab}
/>
))}
</div>
)}
</div>
</div>
);
};
const PlaceholderCard = (): ReactElement => (
<div className="flex gap-3 px-4 py-3">
<Placeholder className="h-8 w-8 shrink-0 rounded-full" />
<div className="flex flex-1 flex-col gap-1.5">
<div className="flex items-center gap-1.5">
<Placeholder className="h-3 w-16" />
<Placeholder className="h-3 w-12" />
</div>
<Placeholder className="h-10 w-full" />
<div className="flex gap-1.5">
<Placeholder className="h-5 w-16 rounded-8" />
<Placeholder className="h-5 w-14 rounded-8" />
</div>
</div>
</div>
);
const MOBILE_FEED_LIMIT = 5;
const MAX_HIGHLIGHTS = 100;
interface ArenaHighlightsFeedProps {
items: SentimentHighlightItem[];
tab?: 'coding-agents' | 'llms';
loading?: boolean;
responsiveLimit?: boolean;
}
export const ArenaHighlightsFeed = ({
items,
tab,
loading,
responsiveLimit = true,
}: ArenaHighlightsFeedProps): ReactElement => {
const [visibleItems, setVisibleItems] = useState<SentimentHighlightItem[]>(
[],
);
const [pendingItems, setPendingItems] = useState<SentimentHighlightItem[]>(
[],
);
const [mobileExpanded, setMobileExpanded] = useState(false);
const isLaptop = useViewSize(ViewSize.LaptopL);
const initializedRef = useRef(false);
const visibleIdsRef = useRef<Set<string>>(new Set());
const scrollRef = useRef<HTMLDivElement>(null);
useEffect(() => {
if (items.length === 0) {
initializedRef.current = false;
visibleIdsRef.current = new Set();
setVisibleItems([]);
setPendingItems([]);
return;
}
if (!initializedRef.current) {
initializedRef.current = true;
const initialItems = items.slice(0, MAX_HIGHLIGHTS);
visibleIdsRef.current = new Set(
initialItems.map((item) => item.externalItemId),
);
setVisibleItems(initialItems);
setPendingItems([]);
return;
}
const newItems = items.filter(
(item) => !visibleIdsRef.current.has(item.externalItemId),
);
if (newItems.length > 0) {
setPendingItems((prev) =>
[...newItems, ...prev].slice(0, MAX_HIGHLIGHTS),
);
}
}, [items]);
const showPending = (): void => {
setVisibleItems((prev) => {
const mergedItems = [...pendingItems, ...prev].slice(0, MAX_HIGHLIGHTS);
visibleIdsRef.current = new Set(
mergedItems.map((item) => item.externalItemId),
);
return mergedItems;
});
setPendingItems([]);
scrollRef.current?.scrollTo({ top: 0, behavior: 'smooth' });
};
let displayItems = visibleItems;
let hasMoreOnMobile = false;
if (responsiveLimit) {
if (!isLaptop && !mobileExpanded) {
displayItems = visibleItems.slice(0, MOBILE_FEED_LIMIT);
}
hasMoreOnMobile =
!isLaptop && !mobileExpanded && visibleItems.length > MOBILE_FEED_LIMIT;
}
return (
<div className="relative flex h-full flex-col overflow-hidden rounded-16 border border-border-subtlest-tertiary">
<div className="flex items-center border-b border-border-subtlest-tertiary px-4 py-2.5">
<span className="font-bold text-text-primary typo-callout">
Live Highlights
</span>
</div>
{!loading && pendingItems.length > 0 && (
<div className="z-10 pointer-events-none absolute inset-x-0 top-12 flex justify-center">
<button
type="button"
onClick={showPending}
className="animate-slide-down pointer-events-auto rounded-12 bg-accent-cabbage-default px-4 py-1.5 font-bold text-white shadow-2 transition-transform typo-caption1 hover:scale-105 active:scale-95"
>
↑ +{pendingItems.length} new post
{pendingItems.length !== 1 ? 's' : ''}
</button>
</div>
)}
<div
ref={scrollRef}
className="slim-scrollbar flex min-h-0 flex-1 flex-col divide-y divide-border-subtlest-tertiary overflow-y-auto"
>
{loading &&
Array.from({ length: 4 }).map((_, i) => (
// eslint-disable-next-line react/no-array-index-key
<PlaceholderCard key={i} />
))}
{!loading && displayItems.length === 0 && (
<div className="flex min-h-[220px] items-center justify-center px-6 py-8 text-center">
<div>
<p className="font-bold text-text-primary typo-callout">
No live highlights yet
</p>
<p className="mt-1 text-text-tertiary typo-footnote">
Check back soon as new developer mentions come in.
</p>
</div>
</div>
)}
{!loading &&
displayItems.map((item) => (
<HighlightCard key={item.externalItemId} item={item} tab={tab} />
))}
</div>
{!loading && hasMoreOnMobile && (
<button
type="button"
onClick={() => setMobileExpanded(true)}
className="active:opacity-80 border-t border-border-subtlest-tertiary px-4 py-3 text-center font-bold text-text-secondary typo-callout laptopL:hidden"
>
Show all {visibleItems.length} highlights
</button>
)}
</div>
);
};