-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathChannelPreviewOverlay.tsx
More file actions
85 lines (77 loc) · 2.59 KB
/
Copy pathChannelPreviewOverlay.tsx
File metadata and controls
85 lines (77 loc) · 2.59 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
import { useCallback, useState } from 'react';
import type { ChannelMemberResponse } from 'stream-chat';
import {
Button,
IconMessageBubbles,
LoadingIndicator,
useChannelMembersState,
useChannelStateContext,
useChatContext,
useNotificationApi,
} from 'stream-chat-react';
import './ChannelPreviewOverlay.scss';
export const useChannelMembershipState = () => {
const { client } = useChatContext();
const { channel } = useChannelStateContext();
const members = useChannelMembersState(channel);
const membership = members[client.userId!] as ChannelMemberResponse | undefined;
const isMember = typeof membership?.channel_role === 'string';
const canJoin = channel.data?.own_capabilities?.includes('join-channel');
return { canJoin, channel, client, isMember };
};
export const ChannelPreviewOverlay = () => {
const { canJoin, channel, client, isMember } = useChannelMembershipState();
const { addNotification } = useNotificationApi();
const [joining, setJoining] = useState(false);
const handleJoin = useCallback(async () => {
setJoining(true);
try {
await channel.addMembers([client.userId!]);
} catch (error) {
addNotification({
emitter: 'ChannelPreviewOverlay',
incident: {
domain: 'api',
entity: 'channel',
operation: 'join',
},
message: 'Failed to join the group',
severity: 'error',
error: error instanceof Error ? error : new Error(String(error)),
});
} finally {
setJoining(false);
}
}, [addNotification, channel, client.userId]);
if (isMember) return null;
return (
<div className='app-channel-preview-overlay'>
<div className='app-channel-preview-overlay__content'>
<IconMessageBubbles />
<div className='app-channel-preview-overlay__text'>
<p className='app-channel-preview-overlay__title'>
{canJoin ? "You're previewing this group" : 'This is a private group'}
</p>
<p className='app-channel-preview-overlay__description'>
{canJoin
? 'Join to send messages and follow the conversation'
: 'It is not possible to join this group'}
</p>
</div>
{canJoin && (
<Button
appearance='solid'
autoFocus
className='app-channel-preview-overlay__join-button'
disabled={joining}
onClick={handleJoin}
size='md'
variant='primary'
>
{joining ? <LoadingIndicator /> : 'Join Group'}
</Button>
)}
</div>
</div>
);
};