-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathChatChannel.tsx
79 lines (72 loc) · 2.38 KB
/
ChatChannel.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
import _ from 'lodash';
import React, { useEffect } from 'react';
import { Outlet, useNavigate, useParams } from 'react-router';
import { Helmet } from 'react-helmet';
import cn from 'classnames';
import ChatInput from '@/chat/ChatInput/ChatInput';
import ChatWindow from '@/chat/ChatWindow';
import Layout from '@/components/Layout/Layout';
import { ViewProps } from '@/types/groups';
import { useChatPerms, useChatState, useMessagesForChat } from '@/state/chat';
import {
useRouteGroup,
useVessel,
useGroup,
useChannel,
useAmAdmin,
GROUP_ADMIN,
} from '@/state/groups/groups';
import ChannelHeader from '@/channels/ChannelHeader';
import useRecentChannel from '@/logic/useRecentChannel';
import { canReadChannel } from '@/logic/utils';
function ChatChannel({ title }: ViewProps) {
const navigate = useNavigate();
const { chShip, chName } = useParams();
const chFlag = `${chShip}/${chName}`;
const nest = `chat/${chFlag}`;
const groupFlag = useRouteGroup();
const { setRecentChannel } = useRecentChannel(groupFlag);
useEffect(() => {
useChatState.getState().initialize(chFlag);
setRecentChannel(nest);
}, [chFlag, nest, setRecentChannel]);
const messages = useMessagesForChat(chFlag);
const perms = useChatPerms(chFlag);
const vessel = useVessel(groupFlag, window.our);
const canWrite =
perms.writers.length === 0 ||
_.intersection(perms.writers, vessel.sects).length !== 0;
const { sendMessage } = useChatState.getState();
const channel = useChannel(groupFlag, nest);
const group = useGroup(groupFlag);
useEffect(() => {
if (channel && !canReadChannel(channel, vessel)) {
navigate('../../activity');
setRecentChannel('');
}
}, [channel, vessel, navigate, setRecentChannel]);
return (
<Layout
className="flex-1 bg-white"
aside={<Outlet />}
header={<ChannelHeader flag={groupFlag} nest={nest} />}
footer={
<div className={cn(canWrite ? 'border-t-2 border-gray-50 p-4' : '')}>
{canWrite ? (
<ChatInput whom={chFlag} sendMessage={sendMessage} showReply />
) : null}
</div>
}
>
<Helmet>
<title>
{channel && group
? `${channel.meta.title} in ${group.meta.title} ${title}`
: title}
</title>
</Helmet>
<ChatWindow whom={chFlag} messages={messages} />
</Layout>
);
}
export default ChatChannel;