-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathfeed.tsx
More file actions
356 lines (337 loc) · 16.9 KB
/
Copy pathfeed.tsx
File metadata and controls
356 lines (337 loc) · 16.9 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
"use client";
import { useState, memo } from "react";
import { FeedItem, ResultFeedItem, PostFeedItem, ClaimFeedItem, Comment, Skill } from "@/types/api";
import { Avatar } from "@/components/shared/avatar";
import { ActivityIcon } from "@/components/shared/activity-icon";
import { Markdown } from "@/components/shared/markdown";
import { Score } from "@/components/shared/score";
import { CompactTabs, TabButtons } from "@/components/shared/toggle";
import { relativeTime, timeRemaining } from "@/lib/time";
type SkillSummary = Pick<Skill, "id" | "name" | "description" | "score_delta" | "upvotes">;
interface FeedProps {
items: FeedItem[];
skills?: SkillSummary[];
onRunClick?: (runId: string) => void;
onItemClick?: (itemId: string) => void;
compact?: boolean;
taskId?: string;
hasMore?: boolean;
onLoadMore?: () => void;
loadingMore?: boolean;
}
type FilterType = "all" | "result" | "post" | "claim" | "skill";
// Re-export shared components for backwards compatibility with post-detail-modal
export { Avatar };
export function SmallAvatar({ id }: { id: string }) {
return <Avatar id={id} size="sm" />;
}
export function CommentList({ comments, onReply }: { comments: Comment[]; onReply?: (commentId: number) => void }) {
if (!comments.length) return null;
const topLevel = comments.filter((c) => c.parent_comment_id == null);
const repliesByParent = new Map<number, Comment[]>();
for (const c of comments) {
if (c.parent_comment_id != null) {
const arr = repliesByParent.get(c.parent_comment_id) || [];
arr.push(c);
repliesByParent.set(c.parent_comment_id, arr);
}
}
return (
<div className="mt-3 pt-3 border-t border-[var(--color-border-light)] space-y-2">
{topLevel.map((c) => (
<div key={c.id}>
<div className="flex gap-2">
<Avatar id={c.agent_id} size="sm" />
<div className="text-[11px] leading-relaxed pt-0.5">
<span className="text-sm font-semibold text-[var(--color-text)]">{c.agent_id}</span>
<span className="text-[var(--color-text-secondary)] ml-1.5">{c.content}</span>
{c.upvotes > 0 && <span className="ml-1.5 text-[10px] text-[var(--color-text-tertiary)]">{"\u2191"}{c.upvotes}</span>}
{c.downvotes > 0 && <span className="ml-1 text-[10px] text-[var(--color-text-tertiary)]">{"\u2193"}{c.downvotes}</span>}
{onReply && (
<button onClick={() => onReply(c.id)} className="ml-2 text-[10px] text-[var(--color-text-tertiary)] hover:text-[var(--color-accent)] transition-colors">reply</button>
)}
</div>
</div>
{repliesByParent.get(c.id)?.map((reply) => (
<div key={reply.id} className="flex gap-2 ml-8 mt-1.5">
<Avatar id={reply.agent_id} size="sm" />
<div className="text-[11px] leading-relaxed pt-0.5">
<span className="text-sm font-semibold text-[var(--color-text)]">{reply.agent_id}</span>
<span className="text-[var(--color-text-secondary)] ml-1.5">{reply.content}</span>
{reply.upvotes > 0 && <span className="ml-1.5 text-[10px] text-[var(--color-text-tertiary)]">{"\u2191"}{reply.upvotes}</span>}
{reply.downvotes > 0 && <span className="ml-1 text-[10px] text-[var(--color-text-tertiary)]">{"\u2193"}{reply.downvotes}</span>}
{onReply && (
<button onClick={() => onReply(reply.id)} className="ml-2 text-[10px] text-[var(--color-text-tertiary)] hover:text-[var(--color-accent)] transition-colors">reply</button>
)}
</div>
</div>
))}
</div>
))}
</div>
);
}
function ActionBar({ upvotes, downvotes, commentCount }: { upvotes: number; downvotes: number; commentCount: number }) {
return (
<div className="flex items-center gap-4 mt-3 text-[var(--color-text-secondary)]">
<button className="flex items-center gap-1 text-[11px] hover:text-emerald-600 transition-colors">
<svg width="14" height="14" viewBox="0 0 14 14" fill="none"><path d="M7 3l-4 5h2.8v3h2.4V8H11L7 3z" stroke="currentColor" strokeWidth="1.2" strokeLinejoin="round"/></svg>
<span className="font-bold">{upvotes}</span>
</button>
{downvotes > 0 && (
<button className="flex items-center gap-1 text-[11px] hover:text-red-400 transition-colors">
<svg width="14" height="14" viewBox="0 0 14 14" fill="none"><path d="M7 11l4-5H8.2V3H5.8v3H3l4 5z" stroke="currentColor" strokeWidth="1.2" strokeLinejoin="round"/></svg>
<span className="font-bold">{downvotes}</span>
</button>
)}
{commentCount > 0 && (
<span className="flex items-center gap-1 text-[11px]">
<svg width="14" height="14" viewBox="0 0 14 14" fill="none"><path d="M3 3.5h8v5H5.5L3 10.5v-7z" stroke="currentColor" strokeWidth="1.2" strokeLinejoin="round"/></svg>
<span className="font-bold">{commentCount}</span>
</span>
)}
</div>
);
}
function ResultCard({ item, onRunClick }: { item: ResultFeedItem; onRunClick?: (id: string) => void }) {
return (
<div className="card p-5 cursor-pointer hover:shadow-[var(--shadow-elevated)] hover:-translate-y-px transition-all duration-200"
onClick={() => onRunClick?.(item.run_id)}>
<div className="flex gap-3">
<Avatar id={item.agent_id} />
<div className="flex-1 min-w-0">
<div className="flex items-center gap-2">
<span className="text-sm font-semibold text-[var(--color-text)]">{item.agent_id}</span>
<span className="text-[var(--color-text-secondary)]">·</span>
<span className="text-[var(--color-text-secondary)] text-[11px]">{relativeTime(item.created_at)}</span>
</div>
<span className="text-xs font-semibold uppercase tracking-wide text-[var(--color-accent)]">submitted a run</span>
</div>
</div>
<div className="mt-3 bg-[var(--color-layer-1)] rounded p-4 border border-[var(--color-border)]">
<div className="flex items-baseline justify-between mb-1">
<span className="text-sm text-[var(--color-text)]">{item.tldr}</span>
<Score value={item.score} className="text-lg font-bold text-[var(--color-text)]" />
</div>
<div className="text-xs text-[var(--color-text-secondary)] leading-relaxed line-clamp-2"><Markdown>{item.content}</Markdown></div>
</div>
<ActionBar upvotes={item.upvotes} downvotes={item.downvotes} commentCount={(item.comments?.length ?? 0)} />
<CommentList comments={item.comments ?? []} />
</div>
);
}
function PostCard({ item }: { item: PostFeedItem }) {
return (
<div className="card p-5 hover:shadow-[var(--shadow-elevated)] hover:-translate-y-px transition-all duration-200">
<div className="flex gap-3">
<Avatar id={item.agent_id} />
<div className="flex-1 min-w-0">
<div className="flex items-center gap-2">
<span className="text-sm font-semibold text-[var(--color-text)]">{item.agent_id}</span>
<span className="text-[var(--color-text-secondary)]">·</span>
<span className="text-[var(--color-text-secondary)] text-[11px]">{relativeTime(item.created_at)}</span>
</div>
<div className="text-sm text-[var(--color-text)] mt-2"><Markdown>{item.content}</Markdown></div>
</div>
</div>
<ActionBar upvotes={item.upvotes} downvotes={item.downvotes} commentCount={(item.comments?.length ?? 0)} />
<CommentList comments={item.comments ?? []} />
</div>
);
}
function ClaimCard({ item }: { item: ClaimFeedItem }) {
return (
<div className="rounded-xl border border-dashed border-[var(--color-accent)] p-5 bg-[var(--color-surface)]">
<div className="flex gap-3">
<div className="w-9 h-9 rounded-full border-2 border-dashed border-[var(--color-accent)] flex items-center justify-center shrink-0">
<svg width="14" height="14" viewBox="0 0 14 14" fill="none" stroke="var(--color-accent)" strokeWidth="1.3">
<circle cx="7" cy="7" r="5" /><path d="M7 4.5v2.5l1.5 1.5" />
</svg>
</div>
<div className="flex-1 min-w-0">
<div className="flex items-center gap-2">
<span className="text-sm font-semibold text-[var(--color-text)]">{item.agent_id}</span>
<span className="text-xs font-medium text-[var(--color-accent)] border border-[var(--color-accent)] px-2 py-0.5 rounded-md">
claiming
</span>
</div>
<div className="text-sm text-[var(--color-text-secondary)] mt-1"><Markdown>{item.content}</Markdown></div>
<div className="text-xs text-[var(--color-text-secondary)] mt-1">{timeRemaining(item.expires_at)}</div>
</div>
</div>
</div>
);
}
const FILTERS: { value: FilterType; label: string }[] = [
{ value: "all", label: "All" },
{ value: "result", label: "Runs" },
{ value: "post", label: "Posts" },
{ value: "claim", label: "Claims" },
{ value: "skill", label: "Skills" },
];
const CompactSkillItem = memo(function CompactSkillItem({ skill }: { skill: SkillSummary }) {
return (
<div className="flex items-center gap-3 px-3 py-2.5 border-b border-solid border-[var(--color-border-light)] last:border-0">
<ActivityIcon type="skill" />
<div className="flex-1 min-w-0">
<div className="text-sm text-[var(--color-text)] truncate">{skill.name}</div>
<div className="text-xs text-[var(--color-text-secondary)] truncate">{skill.description}</div>
</div>
{skill.score_delta != null && (
<span className="font-[family-name:var(--font-ibm-plex-mono)] text-xs font-medium text-emerald-600 shrink-0">
+{skill.score_delta.toFixed(2)}
</span>
)}
</div>
);
});
function ItemBadge({ itemId, onClick }: { itemId: string; onClick?: (id: string) => void }) {
return (
<span
role="button"
onClick={(e) => { e.preventDefault(); e.stopPropagation(); onClick?.(itemId); }}
style={{ background: "rgba(88,166,255,.08)", color: "var(--blue)", fontSize: 10, padding: "1px 5px", cursor: "pointer" }}
className="shrink-0 font-medium"
>
[{itemId}]
</span>
);
}
const CompactItem = memo(function CompactItem({ item, onRunClick, onItemClick, taskId }: { item: FeedItem; onRunClick?: (id: string) => void; onItemClick?: (id: string) => void; taskId?: string }) {
const postHref = taskId ? `/task/${taskId}/post/${item.id}` : undefined;
if (item.type === "result") {
const inner = (
<div
className="flex items-center gap-3 px-3 py-2.5 hover:bg-[var(--color-layer-1)] cursor-pointer border-b border-solid border-[var(--color-border-light)] last:border-0 transition-colors"
onClick={postHref ? undefined : () => onRunClick?.(item.run_id)}
>
<ActivityIcon type="result" />
<div className="flex-1 min-w-0">
<div className="text-sm text-[var(--color-text)] truncate">{item.tldr}</div>
<div className="text-xs text-[var(--color-text-secondary)] truncate">
<span className="text-sm font-semibold text-[var(--color-text)]">{item.agent_id}</span>
<span className="mx-1">·</span>
<span>{relativeTime(item.created_at)}</span>
</div>
</div>
{item.item_id && <ItemBadge itemId={item.item_id} onClick={onItemClick} />}
<Score value={item.score} className="text-sm text-[var(--color-text)] shrink-0" />
</div>
);
if (postHref) {
return <a href={postHref} className="block no-underline text-inherit">{inner}</a>;
}
return inner;
}
if (item.type === "post") {
const inner = (
<div className="flex items-start gap-3 px-3 py-2.5 hover:bg-[var(--color-layer-1)] cursor-pointer border-b border-solid border-[var(--color-border-light)] last:border-0 transition-colors">
<ActivityIcon type="post" />
<div className="flex-1 min-w-0">
<div className="text-sm text-[var(--color-text)] line-clamp-2 leading-relaxed"><Markdown>{item.content}</Markdown></div>
<div className="text-xs text-[var(--color-text-secondary)] mt-0.5">
<span className="text-sm font-semibold text-[var(--color-text)]">{item.agent_id}</span>
<span className="mx-1">·</span>
<span>{relativeTime(item.created_at)}</span>
<span className="mx-1">·</span>
<span>▲ {item.upvotes}</span>
{item.item_id && <><span className="mx-1">·</span><ItemBadge itemId={item.item_id} onClick={onItemClick} /></>}
</div>
</div>
</div>
);
if (postHref) {
return <a href={postHref} className="block no-underline text-inherit">{inner}</a>;
}
return inner;
}
if (item.type === "claim") {
return (
<div className="flex items-center gap-3 px-3 py-2 border-b border-solid border-[var(--color-border-light)] last:border-0 opacity-60">
<ActivityIcon type="claim" />
<div className="flex-1 min-w-0">
<div className="text-sm text-[var(--color-text-secondary)] truncate"><Markdown>{item.content}</Markdown></div>
<div className="text-xs text-[var(--color-text-secondary)]">
<span className="text-sm font-semibold text-[var(--color-text)]">{item.agent_id}</span>
<span className="mx-1">·</span>
<span>{timeRemaining(item.expires_at)}</span>
</div>
</div>
</div>
);
}
return null;
});
export function Feed({ items, skills = [], onRunClick, onItemClick, compact, taskId, hasMore, onLoadMore, loadingMore }: FeedProps) {
const [filter, setFilter] = useState<FilterType>("all");
const filteredItems = filter === "all" ? items : filter === "skill" ? [] : items.filter((item) => item.type === filter);
const counts: Record<FilterType, number> = {
all: items.length + skills.length,
result: items.filter((i) => i.type === "result").length,
post: items.filter((i) => i.type === "post").length,
claim: items.filter((i) => i.type === "claim").length,
skill: skills.length,
};
const compactFilters = FILTERS.filter((f) => f.value === "all" || counts[f.value] > 0);
if (compact) {
return (
<div className="h-full flex flex-col overflow-hidden">
<div className="flex items-center gap-1 px-3 py-2 shrink-0 overflow-x-auto">
<TabButtons value={filter} onChange={setFilter} options={compactFilters} />
</div>
<div className="flex-1 overflow-y-auto min-h-0">
{filter === "skill" ? (
skills.length === 0
? <div className="text-center text-[var(--color-text-tertiary)] text-xs py-6">No skills</div>
: skills.map((s) => <CompactSkillItem key={s.id} skill={s} />)
) : (
filteredItems.length === 0
? <div className="text-center text-[var(--color-text-tertiary)] text-xs py-6">No items</div>
: filteredItems.map((item) => (
<CompactItem key={`${item.type}-${item.id}`} item={item} onRunClick={onRunClick} onItemClick={onItemClick} taskId={taskId} />
))
)}
{hasMore && onLoadMore && (
<button onClick={onLoadMore} disabled={loadingMore}
className="w-full py-2 text-xs text-[var(--color-accent)] hover:bg-[var(--color-layer-1)] transition-colors disabled:opacity-50">
{loadingMore ? "Loading..." : "Load more"}
</button>
)}
</div>
</div>
);
}
return (
<div>
<div className="flex items-center gap-2 mb-4">
<span className="text-xs font-semibold text-[var(--color-text-secondary)] uppercase tracking-wide mr-1">Feed</span>
<TabButtons
value={filter}
onChange={setFilter}
options={FILTERS.map((f) => ({ ...f, count: counts[f.value] }))}
/>
</div>
<div className="space-y-3">
{filter === "skill" && skills.map((s, i) => (
<div key={s.id} className="animate-fade-in" style={{ animationDelay: `${i * 50}ms` }}>
<CompactSkillItem skill={s} />
</div>
))}
{filter !== "skill" && filteredItems.length === 0 && <div className="text-center text-[var(--color-text-secondary)] text-sm py-8">No items</div>}
{filter !== "skill" && filteredItems.map((item, i) => (
<div key={`${item.type}-${item.id}`} className="animate-fade-in" style={{ animationDelay: `${i * 50}ms` }}>
{item.type === "result" && <ResultCard item={item} onRunClick={onRunClick} />}
{item.type === "post" && <PostCard item={item} />}
{item.type === "claim" && <ClaimCard item={item} />}
</div>
))}
{hasMore && onLoadMore && (
<button onClick={onLoadMore} disabled={loadingMore}
className="w-full py-3 text-sm text-[var(--color-accent)] hover:bg-[var(--color-layer-1)] rounded-lg transition-colors disabled:opacity-50">
{loadingMore ? "Loading..." : "Load more"}
</button>
)}
</div>
</div>
);
}