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' ;
@@ -86,6 +93,34 @@ const ResponsiveChannelPanels = () => {
8693 ) ;
8794} ;
8895
96+ const HeaderStartContent = ( ) => {
97+ const { client } = useChatContext ( ) ;
98+ const { channel } = useChannelStateContext ( ) ;
99+ const members = useChannelMembersState ( channel ) ;
100+ const membership = members [ client . userID ! ] as ChannelMemberResponse | undefined ;
101+
102+ const isMember = typeof membership ?. channel_role === 'string' ;
103+ const canJoin = channel . data ?. own_capabilities ?. includes ( 'join-channel' ) ;
104+
105+ const handleClick = useCallback ( ( ) => {
106+ if ( isMember ) {
107+ channel . removeMembers ( [ client . userID ! ] ) . then ( ( ) => {
108+ channel . watch ( ) ;
109+ } ) ;
110+ } else {
111+ channel . addMembers ( [ client . userID ! ] ) ;
112+ }
113+ } , [ isMember ] ) ;
114+
115+ if ( ! canJoin ) return null ;
116+
117+ return (
118+ < Button onClick = { handleClick } variant = 'secondary' appearance = 'outline' size = 'sm' >
119+ { isMember ? 'Leave' : 'Join' }
120+ </ Button >
121+ ) ;
122+ } ;
123+
89124export const ChannelsPanels = ( {
90125 filters,
91126 iconOnly,
@@ -128,6 +163,7 @@ export const ChannelsPanels = ({
128163 } }
129164 >
130165 < ChannelList
166+ onRemovedFromChannel = { ( ) => { } }
131167 customActiveChannel = { initialChannelId }
132168 filters = { filters }
133169 options = { options }
@@ -137,7 +173,7 @@ export const ChannelsPanels = ({
137173 </ WithComponents >
138174 </ div >
139175 < SidebarResizeHandle layoutRef = { channelsLayoutRef } />
140- < WithComponents overrides = { { TypingIndicator } } >
176+ < WithComponents overrides = { { TypingIndicator, HeaderStartContent } } >
141177 < Channel >
142178 < ResponsiveChannelPanels />
143179 </ Channel >
0 commit comments