-
Notifications
You must be signed in to change notification settings - Fork 296
Expand file tree
/
Copy pathThreadHeader.tsx
More file actions
132 lines (120 loc) · 4.73 KB
/
ThreadHeader.tsx
File metadata and controls
132 lines (120 loc) · 4.73 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
import React from 'react';
import { useChannelStateContext } from '../../context/ChannelStateContext';
import { useTranslationContext } from '../../context/TranslationContext';
import { useStateStore } from '../../store';
import { useChannelPreviewInfo } from '../ChannelPreview/hooks/useChannelPreviewInfo';
import { TypingIndicatorHeader } from '../TypingIndicator/TypingIndicatorHeader';
import { useThreadContext } from '../Threads';
import { useChatContext } from '../../context/ChatContext';
import { useTypingContext } from '../../context/TypingContext';
import type { LocalMessage } from 'stream-chat';
import type { ThreadState } from 'stream-chat';
import { Button } from '../Button';
import { IconCrossMedium, IconLayoutAlignLeft } from '../Icons';
import { ToggleSidebarButton } from '../Button/ToggleSidebarButton';
import { useChatViewContext } from '../ChatView';
const threadStateSelector = ({ replyCount }: ThreadState) => ({ replyCount });
/** Fallback when channel has no display title: parent message author (name only). */
const displayNameFromParentMessage = (message: LocalMessage): string | undefined =>
message.user?.name ?? undefined;
/** Subtitle: replyCount, threadDisplayName, defaultSubtitle (name · reply count), and when typing also TypingIndicatorHeader. */
const ThreadHeaderSubtitle = ({
replyCount,
threadDisplayName,
threadList,
}: {
replyCount: number | undefined;
threadDisplayName: string | undefined;
threadList: boolean;
}) => {
const { t } = useTranslationContext();
const { channelConfig, thread } = useChannelStateContext('ThreadHeaderSubtitle');
const threadInstance = useThreadContext();
const parentId = threadInstance?.id ?? thread?.id;
const { client } = useChatContext('ThreadHeaderSubtitle');
const { typing = {} } = useTypingContext('ThreadHeaderSubtitle');
const typingInThread = Object.values(typing).filter(
({ parent_id, user }) => user?.id !== client.user?.id && parent_id === parentId,
);
const hasTyping = channelConfig?.typing_events !== false && typingInThread.length > 0;
const replyCountText = t('replyCount', { count: replyCount ?? 0 });
const defaultSubtitle = threadDisplayName
? `${threadDisplayName} · ${replyCountText}`
: replyCountText;
return (
<div className='str-chat__thread-header-subtitle'>
<span
className='str-chat__subtitle-content-transition'
key={hasTyping ? 'typing' : 'default'}
>
{hasTyping ? (
<TypingIndicatorHeader threadList={threadList} />
) : (
<>{defaultSubtitle}</>
)}
</span>
</div>
);
};
export type ThreadHeaderProps = {
/** Callback for closing the thread */
closeThread: (event?: React.BaseSyntheticEvent) => void;
/** The thread parent message */
thread: LocalMessage;
/** UI component to display menu icon, defaults to IconLayoutAlignLeft*/
MenuIcon?: React.ComponentType;
/** Override the thread display title */
overrideTitle?: string;
};
export const ThreadHeader = (props: ThreadHeaderProps) => {
const { closeThread, MenuIcon = IconLayoutAlignLeft, overrideTitle, thread } = props;
const { t } = useTranslationContext();
const { channel } = useChannelStateContext();
const { activeChatView } = useChatViewContext();
const { displayTitle: channelDisplayTitle } = useChannelPreviewInfo({ channel });
const threadInstance = useThreadContext();
const { replyCount: replyCountThreadInstance } =
useStateStore(threadInstance?.state, threadStateSelector) ?? {};
const replyCount = threadInstance
? replyCountThreadInstance
: thread
? (thread.reply_count ?? 0)
: 0;
// Subtitle: channel display title (from parent or hook), with override and fallback to parent message author
const threadDisplayName =
overrideTitle ??
channelDisplayTitle ??
displayNameFromParentMessage(thread) ??
undefined;
return (
<div className='str-chat__thread-header'>
{activeChatView === 'threads' && (
<ToggleSidebarButton canCollapse={!!threadInstance} mode='expand'>
<MenuIcon />
</ToggleSidebarButton>
)}
<div className='str-chat__thread-header-details'>
<div className='str-chat__thread-header-title'>{t('Thread')}</div>
<ThreadHeaderSubtitle
replyCount={replyCount}
threadDisplayName={threadDisplayName}
threadList
/>
</div>
{!threadInstance && (
<Button
appearance='ghost'
aria-label={t('aria/Close thread')}
circular
className='str-chat__close-thread-button'
data-testid='close-thread-button'
onClick={closeThread}
size='md'
variant='secondary'
>
<IconCrossMedium />
</Button>
)}
</div>
);
};