-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathMessageThreadReplyInChannelButtonIndicator.tsx
More file actions
86 lines (81 loc) · 2.66 KB
/
MessageThreadReplyInChannelButtonIndicator.tsx
File metadata and controls
86 lines (81 loc) · 2.66 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
import React, { useEffect, useRef } from 'react';
import type { LocalMessage } from 'stream-chat';
import { formatMessage } from 'stream-chat';
import {
useChannelActionContext,
useChannelStateContext,
useChatContext,
useMessageContext,
useTranslationContext,
} from '../../context';
export const MessageThreadReplyInChannelButtonIndicator = () => {
const { client } = useChatContext();
const { t } = useTranslationContext();
const { channel } = useChannelStateContext();
const { openThread } = useChannelActionContext();
const { message } = useMessageContext();
const parentMessageRef = useRef<LocalMessage | null | undefined>(undefined);
const querySearchParent = () =>
channel
.getClient()
.search({ cid: channel.cid }, { id: message.parent_id })
.then(({ results }) => {
if (!results.length) {
throw new Error('Thread has not been found');
}
parentMessageRef.current = formatMessage(results[0].message);
})
.catch((error: Error) => {
client.notifications.addError({
message: t('Thread has not been found'),
options: {
originalError: error,
type: 'api:message:search:not-found',
},
origin: {
context: { threadReply: message },
emitter: 'MessageThreadReplyInChannelButtonIndicator',
},
});
});
useEffect(() => {
if (
parentMessageRef.current ||
parentMessageRef.current === null ||
!message.parent_id
)
return;
const localMessage = channel.state.findMessage(message.parent_id);
if (localMessage) {
parentMessageRef.current = localMessage;
return;
}
}, [channel, message]);
if (!message.parent_id) return null;
return (
<div className='str-chat__message-is-thread-reply-button-wrapper'>
<button
className='str-chat__message-is-thread-reply-button'
data-testid='message-is-thread-reply-button'
onClick={async () => {
if (!parentMessageRef.current) {
// search query is performed here in order to prevent multiple search queries in useEffect
// due to the message list 3x remounting its items
await querySearchParent();
if (parentMessageRef.current) {
openThread(parentMessageRef.current);
} else {
// prevent further search queries if the message is not found in the DB
parentMessageRef.current = null;
}
return;
}
openThread(parentMessageRef.current);
}}
type='button'
>
{t('Thread reply')}
</button>
</div>
);
};