|
| 1 | +import { useCallback, useState } from 'react'; |
| 2 | +import type { ChannelMemberResponse } from 'stream-chat'; |
| 3 | +import { |
| 4 | + Button, |
| 5 | + IconMessageBubbles, |
| 6 | + LoadingIndicator, |
| 7 | + useChannelMembersState, |
| 8 | + useChannelStateContext, |
| 9 | + useChatContext, |
| 10 | + useNotificationApi, |
| 11 | +} from 'stream-chat-react'; |
| 12 | + |
| 13 | +import './PublicChannelOverlay.scss'; |
| 14 | + |
| 15 | +export const usePublicChannelState = () => { |
| 16 | + const { client } = useChatContext(); |
| 17 | + const { channel } = useChannelStateContext(); |
| 18 | + const members = useChannelMembersState(channel); |
| 19 | + const membership = members[client.userID!] as ChannelMemberResponse | undefined; |
| 20 | + |
| 21 | + const isMember = typeof membership?.channel_role === 'string'; |
| 22 | + const canJoin = channel.data?.own_capabilities?.includes('join-channel'); |
| 23 | + |
| 24 | + return { canJoin, channel, client, isMember }; |
| 25 | +}; |
| 26 | + |
| 27 | +export const PublicChannelOverlay = () => { |
| 28 | + const { canJoin, channel, client, isMember } = usePublicChannelState(); |
| 29 | + const { addNotification } = useNotificationApi(); |
| 30 | + const [joining, setJoining] = useState(false); |
| 31 | + |
| 32 | + const handleJoin = useCallback(async () => { |
| 33 | + setJoining(true); |
| 34 | + try { |
| 35 | + await channel.addMembers([client.userID!]); |
| 36 | + } catch (error) { |
| 37 | + addNotification({ |
| 38 | + emitter: 'PublicChannelOverlay', |
| 39 | + incident: { |
| 40 | + domain: 'api', |
| 41 | + entity: 'channel', |
| 42 | + operation: 'join', |
| 43 | + }, |
| 44 | + message: 'Failed to join the group', |
| 45 | + severity: 'error', |
| 46 | + error: error instanceof Error ? error : new Error(String(error)), |
| 47 | + }); |
| 48 | + } finally { |
| 49 | + setJoining(false); |
| 50 | + } |
| 51 | + }, [addNotification, channel, client.userID]); |
| 52 | + |
| 53 | + if (isMember || !canJoin) return null; |
| 54 | + |
| 55 | + return ( |
| 56 | + <div className='app-public-channel-overlay'> |
| 57 | + <div className='app-public-channel-overlay__content'> |
| 58 | + <IconMessageBubbles /> |
| 59 | + <div className='app-public-channel-overlay__text'> |
| 60 | + <p className='app-public-channel-overlay__title'> |
| 61 | + You're previewing this group |
| 62 | + </p> |
| 63 | + <p className='app-public-channel-overlay__description'> |
| 64 | + Join to send messages and follow the conversation |
| 65 | + </p> |
| 66 | + </div> |
| 67 | + <Button |
| 68 | + appearance='solid' |
| 69 | + className='app-public-channel-overlay__join-button' |
| 70 | + disabled={joining} |
| 71 | + onClick={handleJoin} |
| 72 | + size='md' |
| 73 | + variant='primary' |
| 74 | + > |
| 75 | + {joining ? <LoadingIndicator /> : 'Join Group'} |
| 76 | + </Button> |
| 77 | + </div> |
| 78 | + </div> |
| 79 | + ); |
| 80 | +}; |
| 81 | + |
| 82 | +export const PublicChannelComposerBanner = () => { |
| 83 | + const { canJoin, isMember } = usePublicChannelState(); |
| 84 | + |
| 85 | + if (isMember || canJoin) return null; |
| 86 | + |
| 87 | + return ( |
| 88 | + <div className='app-public-channel-composer-banner'> |
| 89 | + <p className='app-public-channel-composer-banner__text'> |
| 90 | + You can only view this conversation |
| 91 | + </p> |
| 92 | + </div> |
| 93 | + ); |
| 94 | +}; |
0 commit comments