-
Notifications
You must be signed in to change notification settings - Fork 301
Expand file tree
/
Copy pathNotificationItem.tsx
More file actions
429 lines (402 loc) · 14.7 KB
/
Copy pathNotificationItem.tsx
File metadata and controls
429 lines (402 loc) · 14.7 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
import type { ReactElement } from 'react';
import React, { useMemo } from 'react';
import classNames from 'classnames';
import { useRouter } from 'next/router';
import Link from '../utilities/Link';
import type { Notification } from '../../graphql/notifications';
import { useObjectPurify } from '../../hooks/useDomPurify';
import NotificationItemAvatar from './NotificationItemAvatar';
import { NotificationItemLead } from './NotificationItemLead';
import { NotificationCategoryBadge } from './NotificationCategoryBadge';
import { getNotificationLeadAvatar } from './leadAvatar';
import {
getNotificationCategory,
NotificationFilterCategory,
notificationMutingCopy,
NotificationType,
notificationTypeNotClickable,
} from './utils';
import { KeyboardCommand } from '../../lib/element';
import { ProfileTooltip } from '../profile/ProfileTooltip';
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuOptions,
DropdownMenuTrigger,
} from '../dropdown/DropdownMenu';
import { Button, ButtonSize, ButtonVariant } from '../buttons/Button';
import { BellDisabledIcon, BellIcon, MenuIcon, PlayIcon } from '../icons';
import { useNotificationPreference } from '../../hooks/notifications';
import {
NotificationAttachmentType,
NotificationAvatarType,
NotificationPreferenceStatus,
} from '../../graphql/notifications';
import { Image, ImageType } from '../image/Image';
import { IconSize } from '../Icon';
import { Loader } from '../Loader';
import { NotificationFollowUserButton } from './NotificationFollowUserButton';
import { NotificationSayThanksButton } from './NotificationSayThanksButton';
import {
getFullNotificationDate,
publishTimeRelativeShort,
} from '../../lib/dateFormat';
import { stripHtmlTags } from '../../lib/strings';
import { Tooltip } from '../tooltip/Tooltip';
// Strip markup + collapse whitespace so two HTML strings can be compared for
// "is this just the same text again" de-duplication. Pure + module-scoped so
// it isn't re-allocated on every row render.
const normalizeText = (html: string): string =>
stripHtmlTags(html).replace(/\s+/g, ' ').trim().toLowerCase();
export interface NotificationItemProps
extends Pick<
Notification,
| 'type'
| 'icon'
| 'title'
| 'description'
| 'avatars'
| 'attachments'
| 'numTotalAvatars'
| 'referenceId'
| 'hasThanks'
> {
isUnread?: boolean;
targetUrl: string;
createdAt?: Date;
onClick?: (
e:
| React.MouseEvent<HTMLAnchorElement>
| React.KeyboardEvent<HTMLAnchorElement>,
) => void;
}
const NotificationOptionsButton = ({
notification,
}: {
notification: Pick<Notification, 'type' | 'referenceId'>;
}): ReactElement => {
const {
preferences,
isFetching,
clearNotificationPreference,
muteNotification,
} = useNotificationPreference({
params: notification
? [
{
notificationType: notification.type,
referenceId: notification.referenceId,
},
]
: [],
});
const onItemClick = () => {
const isMuted =
preferences?.[0]?.status === NotificationPreferenceStatus.Muted;
const preferenceCommand = isMuted
? clearNotificationPreference
: muteNotification;
return preferenceCommand({
type: notification.type,
referenceId: notification.referenceId,
});
};
const Icon = (): ReactElement | null => {
if (!notification) {
return null;
}
if (isFetching) {
return <Loader />;
}
const NotifIcon =
preferences[0]?.status === NotificationPreferenceStatus.Muted
? BellIcon
: BellDisabledIcon;
return <NotifIcon />;
};
const label = useMemo((): string => {
if (!notification) {
return '';
}
if (isFetching) {
return 'Fetching your preference';
}
const isMuted =
preferences[0]?.status === NotificationPreferenceStatus.Muted;
const copy = notificationMutingCopy[notification?.type];
if (!copy) {
return '';
}
return isMuted ? copy.unmute : copy.mute;
}, [notification, preferences, isFetching]);
const options = [{ icon: <Icon />, label, action: onItemClick }];
return (
<DropdownMenu>
<DropdownMenuTrigger
asChild
tooltip={{
content: 'Options',
}}
>
<Button
// Tertiary is the flat variant — transparent, no background or
// border (Float carries a faint surface-float background).
variant={ButtonVariant.Tertiary}
icon={<MenuIcon className="rotate-90" />}
size={ButtonSize.XSmall}
/>
</DropdownMenuTrigger>
<DropdownMenuContent>
<DropdownMenuOptions options={options} />
</DropdownMenuContent>
</DropdownMenu>
);
};
function NotificationItem(props: NotificationItemProps): ReactElement | null {
const {
icon,
type,
title,
isUnread,
description,
avatars,
attachments,
onClick,
targetUrl,
numTotalAvatars,
referenceId,
hasThanks,
createdAt,
} = props;
const {
isReady,
title: memoizedTitle,
description: memoizedDescription,
} = useObjectPurify({ title, description: description ?? '' });
const router = useRouter();
const isClickable = !notificationTypeNotClickable[type];
const filteredAvatars = useMemo(() => {
return avatars?.filter((avatar) => avatar) || [];
}, [avatars]);
if (!isReady) {
return null;
}
// Lead with the human actor, not whatever avatar the backend listed first
// (squad comments/posts arrive source-first). Falls back to the source for
// source-only notifications.
const leadAvatar = getNotificationLeadAvatar(filteredAvatars);
const hasAvatar = filteredAvatars.length > 0;
// `numTotalAvatars` can arrive as 0 from the backend even when avatars are
// present, so take the larger of the two rather than `??` (which keeps 0).
const totalAvatars = Math.max(numTotalAvatars ?? 0, filteredAvatars.length);
const renderLink = onClick && isClickable;
const hasOptions = Object.keys(notificationMutingCopy).includes(type);
const [attachment] = attachments ?? [];
const category = getNotificationCategory(type);
// Badge only for notifications about you (upvotes/comments/mentions/follows/
// squad activity). Source posts & system land in `Updates` and stay clean.
const showBadge =
hasAvatar && category !== NotificationFilterCategory.Updates;
// More than three actors render as a tight overlapping stack of up to three
// rounded-square faces — sized like one avatar so the lead never grows wider
// and every row stays aligned. The exact count still lives in the title.
const showStack = filteredAvatars.length > 1 && totalAvatars > 3;
let avatarContent: ReactElement | null = null;
if (showStack) {
const faces = filteredAvatars.slice(0, 3);
avatarContent = (
<div className="flex -space-x-4">
{faces.map((avatar, index) => {
// Icon-backed types (briefs/digests/badges) never reach a >3 stack,
// but fall back to the full avatar renderer rather than a broken
// <img> if one ever does.
const isImageBacked =
avatar.type === NotificationAvatarType.User ||
avatar.type === NotificationAvatarType.Source ||
avatar.type === NotificationAvatarType.Organization;
const isFront = index === faces.length - 1;
const face = isImageBacked ? (
<Image
className="size-6 rounded-8 border-2 border-background-default object-cover"
src={avatar.image}
alt={`${avatar.name} avatar`}
/>
) : (
<NotificationItemAvatar {...avatar} />
);
return (
<span
key={avatar.referenceId ?? index}
className={classNames('relative', isFront && 'z-1')}
>
{avatar.type === NotificationAvatarType.User ? (
<ProfileTooltip
userId={avatar.referenceId}
link={{ href: avatar.targetUrl }}
>
{face}
</ProfileTooltip>
) : (
face
)}
{/* Same category badge as the single-actor lead, straddling the
front face's bottom-right corner (translate centering keeps it
from swamping the smaller stacked face). */}
{isFront && showBadge && (
<NotificationCategoryBadge
category={category}
className="bottom-0 right-0 translate-x-1/2 translate-y-1/2"
/>
)}
</span>
);
})}
</div>
);
}
const timeText = createdAt ? publishTimeRelativeShort(createdAt) : '';
const fullDate = createdAt ? getFullNotificationDate(createdAt) : '';
// Subtitle can carry two things: the comment (what was said) AND the title
// of the referenced post (which article/post it's about), so a mention or
// comment makes clear where it happened. Each is hidden when it just repeats
// the headline or the other, so we never show the same text twice (e.g.
// source-post rows whose title already is the post title).
const titleNorm = normalizeText(memoizedTitle);
const descriptionNorm = description ? normalizeText(memoizedDescription) : '';
const showDescription = !!description && descriptionNorm !== titleNorm;
const attachmentTitle = attachment?.title;
const attachmentTitleNorm = attachmentTitle
? normalizeText(attachmentTitle)
: '';
const showAttachmentTitle =
!!attachmentTitle &&
attachmentTitleNorm !== titleNorm &&
attachmentTitleNorm !== descriptionNorm;
return (
<div
className={classNames(
'relative flex min-h-14 flex-row items-start gap-3 px-4 py-3 hover:bg-surface-hover focus:bg-theme-active laptop:min-h-16 laptop:py-4',
isUnread && 'bg-surface-float',
)}
>
{renderLink && targetUrl && (
<Link href={targetUrl} passHref>
<a
role="link"
aria-label="Open notification"
className="absolute inset-0 z-0"
onClick={onClick}
title="Open notification"
data-testid="openNotification"
tabIndex={0}
onKeyDown={(e) => {
if (e.code === KeyboardCommand.Space) {
onClick(e);
router.push(targetUrl);
}
return true;
}}
>
<span />
</a>
</Link>
)}
{/* Leading avatar + colored type badge — the eye-catching, type-at-a-
glance cue (Instagram/Facebook/TikTok). System rows with no person
fall back to the plain type icon. More than three actors render as an
overlapping stack; everything else uses the shared single-actor lead. */}
<div className="flex w-10 shrink-0 items-center justify-start">
{showStack ? (
avatarContent
) : (
<NotificationItemLead type={type} icon={icon} avatar={leadAvatar} />
)}
</div>
{/* Headline (actor name bold, the rest regular for a scannable
hierarchy) with the relative time flowing inline right after it — so a
row reads "what happened" first and the time is a quiet suffix, not a
right-aligned column. Then the comment, then the post's title. */}
<div className="flex min-w-0 flex-1 flex-col gap-1 text-left">
<div className="break-words font-normal text-text-primary typo-callout [&_b]:font-bold [&_p]:m-0 [&_p]:inline [&_strong]:font-bold">
<span dangerouslySetInnerHTML={{ __html: memoizedTitle }} />
{timeText && (
<Tooltip content={fullDate}>
<time className="relative z-1 ml-1.5 whitespace-nowrap text-text-tertiary typo-footnote">
· {timeText}
</time>
</Tooltip>
)}
</div>
{(showDescription || showAttachmentTitle) && (
<div className="multi-truncate line-clamp-2 break-words text-text-tertiary typo-subhead [&_p]:m-0 [&_p]:inline">
{showDescription ? (
<span dangerouslySetInnerHTML={{ __html: memoizedDescription }} />
) : (
showAttachmentTitle && <span>{attachmentTitle}</span>
)}
</div>
)}
{/* When there's both a comment and a post, name the post on its own
line so it's clear which article it's about. */}
{showDescription && showAttachmentTitle && (
<div className="multi-truncate line-clamp-1 break-words text-text-tertiary typo-footnote">
{attachmentTitle}
</div>
)}
{type === NotificationType.UserFollow && (
<span className="relative z-1">
<NotificationFollowUserButton {...props} />
</span>
)}
{type === NotificationType.UserReceivedAward && (
<span className="relative z-1 mt-1">
<NotificationSayThanksButton
referenceId={referenceId}
hasThanks={hasThanks}
/>
</span>
)}
</div>
{/* Trailing: the post cover and/or the options menu, both top-aligned with
the title (the menu is the rightmost element, so it pins to the
top-right corner). The menu column is only reserved when the row
actually has a menu — otherwise the cover sits flush to the right edge
instead of leaving an empty gap. */}
{(attachment?.image || hasOptions) && (
<div className="flex shrink-0 items-start gap-2">
{attachment?.image && (
<span className="relative flex size-12 shrink-0">
<Image
data-testid="postImage"
loading="lazy"
type={ImageType.Post}
className="size-12 rounded-12 object-cover"
src={attachment.image}
alt={
attachment.title
? `Cover preview of: ${attachment.title}`
: 'Post cover preview'
}
/>
{attachment.type === NotificationAttachmentType.Video && (
<span className="absolute inset-0 flex items-center justify-center rounded-12 bg-overlay-tertiary-black">
<PlayIcon
secondary
size={IconSize.Small}
className="text-white"
/>
</span>
)}
</span>
)}
{hasOptions && (
<div className="relative z-1 flex w-7 shrink-0 justify-center">
<NotificationOptionsButton notification={{ type, referenceId }} />
</div>
)}
</div>
)}
</div>
);
}
export default NotificationItem;