forked from p-society/iiitbuzz
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopicThreadRow.tsx
More file actions
179 lines (167 loc) · 6.27 KB
/
Copy pathTopicThreadRow.tsx
File metadata and controls
179 lines (167 loc) · 6.27 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
import { useState, useEffect } from "react";
import { Link, useNavigate } from "react-router-dom";
import { api } from "@/lib/api";
import type { ThreadListItem, Topic } from "@/types/forum";
import { formatTimeAgo } from "@/lib/utils/date";
import { getTopicColor, getTopicColorHex } from "@/lib/utils/topicColor";
interface TopicRowProps {
topic: Topic;
threadCount: number;
latestPost?: {
title: string;
authorInitials: string;
timeAgo: string;
};
}
interface ThreadRowProps {
thread: ThreadListItem;
topicName?: string;
topicColor?: string;
}
const TopicRow = ({ topic, threadCount, latestPost }: TopicRowProps) => {
return (
<Link to={`/topic/${topic.id}`} className="block">
<div
className={`py-2 px-3 border-b border-border last:border-b-0 hover:bg-muted/40 transition-colors ${getTopicColor(topic.id)}`}
>
<div className="grid grid-cols-[2rem_1fr_auto] sm:grid-cols-[2rem_minmax(0,1fr)_4.5rem_minmax(10rem,14rem)] items-start sm:items-center gap-x-3 gap-y-1">
<div className="zone-a row-span-2 sm:row-span-1 flex items-center justify-center w-8 h-8 border border-border bg-card">
<div className="w-2.5 h-2.5 bg-foreground rotate-45" />
</div>
<div className="zone-b min-w-0">
<h3 className="font-bold text-sm truncate uppercase tracking-tight text-foreground">
{topic.topicName}
</h3>
<p className="mono-meta truncate text-muted-foreground">{topic.topicDescription}</p>
</div>
<div className="zone-c flex flex-col items-center justify-center text-center min-w-[3.5rem]">
<div className="font-bold text-sm leading-none text-foreground">{threadCount}</div>
<div className="mono-label leading-none mt-1">Threads</div>
</div>
<div className="zone-d col-start-2 col-span-2 sm:col-auto sm:col-span-1 mt-1 sm:mt-0 pt-1 sm:pt-0 border-t border-border sm:border-t-0 flex items-center gap-2 min-w-0">
{latestPost ? (
<>
<div className="h-5 w-5 flex items-center justify-center bg-foreground text-background text-[8px] font-bold border border-border flex-shrink-0 overflow-hidden">
<span>{latestPost.authorInitials}</span>
</div>
<div className="min-w-0 flex-1">
<div className="text-[11px] font-semibold truncate leading-tight text-foreground">
{latestPost.title}
</div>
<div className="mono-meta">{latestPost.timeAgo}</div>
</div>
</>
) : (
<div className="mono-meta">—</div>
)}
</div>
</div>
</div>
</Link>
);
};
const ThreadRow = ({ thread, topicName, topicColor }: ThreadRowProps) => {
const navigate = useNavigate();
const [avatarUrl, setAvatarUrl] = useState<string | null>(null);
const [imgError, setImgError] = useState(false);
const username = thread.author?.username || thread.authorName;
useEffect(() => {
if (!username) return;
api
.getUserProfile(username)
.then((res) => {
if (res.success && res.user.imageUrl) {
setAvatarUrl(res.user.imageUrl.replace("http://", "https://"));
}
})
.catch(() => {
setImgError(true);
});
}, [username]);
const initials = (username || "??").substring(0, 2).toUpperCase();
const color = topicColor || getTopicColorHex(thread.topicId);
const profileUsername = !thread.isAnonymous && username ? username : null;
const goToProfile = (event: React.MouseEvent | React.KeyboardEvent) => {
if (!profileUsername) return;
event.preventDefault();
event.stopPropagation();
navigate(`/profile/${encodeURIComponent(profileUsername)}`);
};
return (
<Link to={`/thread/${thread.id}`} className="block">
<div className="py-2 px-3 flex items-center gap-2 sm:gap-3 border-b border-border last:border-b-0 hover:bg-muted/40 transition-colors">
<div className="col-author w-8 sm:w-10 flex-shrink-0">
<div
className={`h-8 w-8 flex items-center justify-center bg-foreground text-background text-[10px] font-bold border border-border overflow-hidden ${profileUsername ? "cursor-pointer hover:opacity-85" : ""}`}
onClick={goToProfile}
onKeyDown={(event) => {
if (event.key === "Enter" || event.key === " ") {
goToProfile(event);
}
}}
role={profileUsername ? "button" : undefined}
tabIndex={profileUsername ? 0 : undefined}
>
{!imgError && avatarUrl ? (
<img
src={avatarUrl}
alt={username}
className="h-full w-full object-cover"
referrerPolicy="no-referrer"
onError={() => setImgError(true)}
/>
) : (
<span>{initials}</span>
)}
</div>
</div>
<div className="col-title flex-1 min-w-0">
<div className="flex items-center gap-1 mb-0.5 flex-wrap">
{thread.isPinned && (
<span className="tech-stamp text-[8px] sm:text-[9px] bg-foreground text-background border-foreground">
PIN
</span>
)}
<span
className="mono-label border border-current px-1 text-[8px] sm:text-[9px]"
style={{ color, borderColor: color }}
>
{topicName || thread.topicName}
</span>
</div>
<h3 className="font-bold text-xs sm:text-sm truncate text-foreground">
{thread.title || thread.threadTitle}
</h3>
<span
className={`mono-meta text-[9px] ${profileUsername ? "cursor-pointer hover:underline" : ""}`}
onClick={goToProfile}
onKeyDown={(event) => {
if (event.key === "Enter" || event.key === " ") {
goToProfile(event);
}
}}
role={profileUsername ? "button" : undefined}
tabIndex={profileUsername ? 0 : undefined}
>
{username || "Unknown"}
</span>
</div>
<div className="col-replies hidden sm:flex w-12 sm:w-16 text-center flex-shrink-0 flex flex-col justify-center">
<div className="font-bold text-sm text-foreground">{thread.replies ?? 0}</div>
<div className="mono-label text-[8px]">Replies</div>
</div>
<div className="col-last hidden sm:flex w-16 sm:w-20 text-right flex-shrink-0 flex flex-col justify-center">
<span className="mono-meta text-right text-[8px] sm:text-xs">
[ {formatTimeAgo(thread.lastActive || "").toUpperCase()} ]
</span>
</div>
</div>
</Link>
);
};
export const TopicThreadRow = (props: TopicRowProps | ThreadRowProps) => {
if ("thread" in props) {
return <ThreadRow {...props} />;
}
return <TopicRow {...props} />;
};