11import clsx from 'clsx' ;
2- import type { ChannelFilters , ChannelOptions , ChannelSort } from 'stream-chat' ;
3- import { useEffect , useRef } from 'react' ;
2+ import type {
3+ ChannelFilters ,
4+ ChannelMemberResponse ,
5+ ChannelOptions ,
6+ ChannelSort ,
7+ } from 'stream-chat' ;
8+ import { useCallback , useEffect , useRef } from 'react' ;
49import {
510 AIStateIndicator ,
611 Channel ,
@@ -21,6 +26,8 @@ import {
2126 useChatContext ,
2227 type ChatViewSelectorEntry ,
2328 useThreadsViewContext ,
29+ Button ,
30+ useChannelMembersState ,
2431} from 'stream-chat-react' ;
2532
2633import { useAppSettingsSelector } from '../AppSettings/state' ;
@@ -97,6 +104,34 @@ const ResponsiveChannelPanels = () => {
97104 ) ;
98105} ;
99106
107+ const HeaderStartContent = ( ) => {
108+ const { client } = useChatContext ( ) ;
109+ const { channel } = useChannelStateContext ( ) ;
110+ const members = useChannelMembersState ( channel ) ;
111+ const membership = members [ client . userID ! ] as ChannelMemberResponse | undefined ;
112+
113+ const isMember = typeof membership ?. channel_role === 'string' ;
114+ const canJoin = channel . data ?. own_capabilities ?. includes ( 'join-channel' ) ;
115+
116+ const handleClick = useCallback ( ( ) => {
117+ if ( isMember ) {
118+ channel . removeMembers ( [ client . userID ! ] ) . then ( ( ) => {
119+ channel . watch ( ) ;
120+ } ) ;
121+ } else {
122+ channel . addMembers ( [ client . userID ! ] ) ;
123+ }
124+ } , [ isMember ] ) ;
125+
126+ if ( ! canJoin ) return null ;
127+
128+ return (
129+ < Button onClick = { handleClick } variant = 'secondary' appearance = 'outline' size = 'sm' >
130+ { isMember ? 'Leave' : 'Join' }
131+ </ Button >
132+ ) ;
133+ } ;
134+
100135export const ChannelsPanels = ( {
101136 filters,
102137 iconOnly,
@@ -148,6 +183,7 @@ export const ChannelsPanels = ({
148183 } }
149184 >
150185 < ChannelList
186+ onRemovedFromChannel = { ( ) => { } }
151187 customActiveChannel = { initialChannelId }
152188 filters = { filters }
153189 options = { options }
@@ -157,7 +193,7 @@ export const ChannelsPanels = ({
157193 </ WithComponents >
158194 </ div >
159195 < SidebarResizeHandle layoutRef = { channelsLayoutRef } />
160- < WithComponents overrides = { { TypingIndicator } } >
196+ < WithComponents overrides = { { TypingIndicator, HeaderStartContent } } >
161197 < Channel >
162198 < ResponsiveChannelPanels />
163199 </ Channel >
0 commit comments