/
Thread.tsx
134 lines (118 loc) · 4.37 KB
/
Thread.tsx
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
import type { IMessage } from '@rocket.chat/core-typings';
import { css } from '@rocket.chat/css-in-js';
import { Box, Modal, Skeleton } from '@rocket.chat/fuselage';
import { useLocalStorage } from '@rocket.chat/fuselage-hooks';
import { useLayoutContextualBarExpanded, useToastMessageDispatch, useTranslation, useUserId } from '@rocket.chat/ui-contexts';
import type { ReactElement } from 'react';
import React from 'react';
import VerticalBar from '../../../../components/VerticalBar';
import { useRoom, useRoomSubscription } from '../../contexts/RoomContext';
import { useTabBarClose } from '../../contexts/ToolboxContext';
import ChatProvider from '../../providers/ChatProvider';
import MessageProvider from '../../providers/MessageProvider';
import ThreadChat from './components/ThreadChat';
import ThreadSkeleton from './components/ThreadSkeleton';
import ThreadTitle from './components/ThreadTitle';
import { useGoToThreadList } from './hooks/useGoToThreadList';
import { useThreadMainMessageQuery } from './hooks/useThreadMainMessageQuery';
import { useToggleFollowingThreadMutation } from './hooks/useToggleFollowingThreadMutation';
type ThreadProps = {
tmid: IMessage['_id'];
};
const Thread = ({ tmid }: ThreadProps): ReactElement => {
const goToThreadList = useGoToThreadList();
const closeTabBar = useTabBarClose();
const mainMessageQueryResult = useThreadMainMessageQuery(tmid, {
onDelete: () => {
closeTabBar();
},
});
const room = useRoom();
const subscription = useRoomSubscription();
const t = useTranslation();
const dispatchToastMessage = useToastMessageDispatch();
const canExpand = useLayoutContextualBarExpanded();
const [expanded, setExpanded] = useLocalStorage('expand-threads', false);
const uid = useUserId();
const following = uid ? mainMessageQueryResult.data?.replies?.includes(uid) ?? false : false;
const toggleFollowingMutation = useToggleFollowingThreadMutation({
onError: (error) => {
dispatchToastMessage({ type: 'error', message: error });
},
});
const handleBackdropClick = () => {
closeTabBar();
};
const handleGoBack = () => {
goToThreadList();
};
const handleToggleExpand = () => {
setExpanded((expanded) => !expanded);
};
const handleToggleFollowing = () => {
toggleFollowingMutation.mutate({ tmid, follow: !following });
};
const handleClose = () => {
closeTabBar();
};
return (
<VerticalBar.InnerContent>
{canExpand && expanded && <Modal.Backdrop onClick={handleBackdropClick} />}
<Box flexGrow={1} position={expanded ? 'static' : 'relative'}>
<VerticalBar
rcx-thread-view
className={
canExpand && expanded
? css`
max-width: 855px !important;
@media (min-width: 780px) and (max-width: 1135px) {
max-width: calc(100% - var(--sidebar-width)) !important;
}
`
: undefined
}
position={expanded ? 'fixed' : 'absolute'}
display='flex'
flexDirection='column'
width='full'
overflow='hidden'
zIndex={100}
insetBlock={0}
>
<VerticalBar.Header>
<VerticalBar.Action name='arrow-back' title={t('Back_to_threads')} onClick={handleGoBack} />
{(mainMessageQueryResult.isLoading && <Skeleton width='100%' />) ||
(mainMessageQueryResult.isSuccess && <ThreadTitle mainMessage={mainMessageQueryResult.data} />) ||
null}
<VerticalBar.Actions>
{canExpand && (
<VerticalBar.Action
name={expanded ? 'arrow-collapse' : 'arrow-expand'}
title={expanded ? t('Collapse') : t('Expand')}
onClick={handleToggleExpand}
/>
)}
<VerticalBar.Action
name={following ? 'bell' : 'bell-off'}
title={following ? t('Following') : t('Not_Following')}
disabled={!mainMessageQueryResult.isSuccess || toggleFollowingMutation.isLoading}
onClick={handleToggleFollowing}
/>
<VerticalBar.Close onClick={handleClose} />
</VerticalBar.Actions>
</VerticalBar.Header>
{(mainMessageQueryResult.isLoading && <ThreadSkeleton />) ||
(mainMessageQueryResult.isSuccess && (
<ChatProvider tmid={tmid}>
<MessageProvider rid={room._id} broadcast={subscription?.broadcast ?? false}>
<ThreadChat mainMessage={mainMessageQueryResult.data} />
</MessageProvider>
</ChatProvider>
)) ||
null}
</VerticalBar>
</Box>
</VerticalBar.InnerContent>
);
};
export default Thread;