-
Notifications
You must be signed in to change notification settings - Fork 295
Expand file tree
/
Copy pathThreadListItemUI.tsx
More file actions
143 lines (127 loc) Β· 4.67 KB
/
ThreadListItemUI.tsx
File metadata and controls
143 lines (127 loc) Β· 4.67 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
import React, { useCallback, useEffect, useMemo } from 'react';
import clsx from 'clsx';
import type { ThreadState } from 'stream-chat';
import type { ComponentPropsWithoutRef } from 'react';
import { Timestamp } from '../../Message/Timestamp';
import { Avatar, type AvatarProps, AvatarStack } from '../../Avatar';
import { useChannelPreviewInfo } from '../../ChannelPreview';
import { useChatContext, useTranslationContext } from '../../../context';
import { useThreadsViewContext } from '../../ChatView';
import { useThreadListItemContext } from './ThreadListItem';
import { useStateStore } from '../../../store';
import { Badge } from '../../Badge';
import { SummarizedMessagePreview } from '../../SummarizedMessagePreview';
import { NAV_SIDEBAR_DESKTOP_BREAKPOINT } from '../../Chat';
export type ThreadListItemUIProps = ComponentPropsWithoutRef<'button'> & {
resetHighlighting?: () => void;
};
export const ThreadListItemUI = ({
resetHighlighting,
...props
}: ThreadListItemUIProps) => {
const { client } = useChatContext();
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const thread = useThreadListItemContext()!;
const selector = useCallback(
(nextValue: ThreadState) => ({
channel: nextValue.channel,
deletedAt: nextValue.deletedAt,
latestReply: nextValue.replies.at(-1),
ownUnreadMessageCount:
(client.userID && nextValue.read[client.userID]?.unreadMessageCount) || 0,
parentMessage: nextValue.parentMessage,
participants: nextValue.participants,
replyCount: nextValue.replyCount,
}),
[client],
);
const {
channel,
deletedAt,
latestReply,
ownUnreadMessageCount,
parentMessage,
participants,
replyCount,
} = useStateStore(thread.state, selector);
const { displayTitle: channelDisplayTitle } = useChannelPreviewInfo({ channel });
const { t } = useTranslationContext('ThreadListItemUI');
const { closeMobileNav } = useChatContext('ThreadListItemUI');
const { activeThread, setActiveThread } = useThreadsViewContext();
const avatarProps: Partial<AvatarProps> | undefined = deletedAt
? undefined
: ({
imageUrl: latestReply?.user?.image,
userName: latestReply?.user?.name || latestReply?.user?.id,
} as const);
const displayInfo = useMemo(() => {
if (!participants) return [];
return participants.slice(0, 3).map((participant) => ({
id: participant.user?.id ?? undefined,
imageUrl: participant.user?.image,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
userName: participant.user?.name || participant.user!.id,
}));
}, [participants]);
useEffect(() => {
if (!resetHighlighting) return;
const reset = resetHighlighting;
const timeout = setTimeout(() => {
reset();
}, 2000);
return () => clearTimeout(timeout);
}, [resetHighlighting]);
return (
<div className='str-chat__thread-list-item-container'>
<button
aria-pressed={activeThread === thread}
className={clsx('str-chat__thread-list-item', {
'str-chat__thread-list-item--highlighted':
typeof resetHighlighting !== 'undefined',
})}
data-thread-id={thread.id}
onClick={() => {
if (
typeof window !== 'undefined' &&
window.innerWidth < NAV_SIDEBAR_DESKTOP_BREAKPOINT
) {
closeMobileNav();
}
setActiveThread(thread);
}}
role='option'
{...props}
>
<Avatar size='xl' {...avatarProps} />
<div className='str-chat__thread-list-item__content'>
<div className='str-chat__thread-list-item__content-leading'>
<span className='str-chat__thread-list-item__title'>
{channelDisplayTitle}
</span>
<SummarizedMessagePreview
latestMessage={parentMessage}
participantCount={participants?.length}
/>
</div>
<div className='str-chat__thread-list-item__content-trailing'>
<div className='str-chat__thread-list-item__reply-information'>
<AvatarStack displayInfo={displayInfo} size='sm' />
<span className='str-chat__thread-list-item__reply-count'>
{t('replyCount', { count: replyCount })}
</span>
</div>
<Timestamp
customClass='str-chat__thread-list-item__timestamp'
timestamp={latestReply?.created_at}
/>
</div>
</div>
{ownUnreadMessageCount > 0 && (
<Badge size='md' variant='primary'>
{ownUnreadMessageCount}
</Badge>
)}
</button>
</div>
);
};