Skip to content

Commit 7ffb2aa

Browse files
Join/Leave button + extended filters
1 parent feb28f0 commit 7ffb2aa

2 files changed

Lines changed: 51 additions & 4 deletions

File tree

examples/vite/src/App.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ import {
6767
import { ConfigurableMessageActions } from './CustomMessageActions';
6868
import { SidebarToggle } from './Sidebar/SidebarToggle.tsx';
6969

70+
const PUBLIC_VITE_EXAMPLE_API_KEY = 'xzwhhgtazy6h';
71+
7072
init({ data });
7173

7274
const parseUserIdFromToken = (token: string) => {
@@ -125,7 +127,7 @@ const useUser = () => {
125127
}, [userId]);
126128

127129
const environment =
128-
import.meta.env.VITE_STREAM_API_KEY === 'xzwhhgtazy6h'
130+
apiKey === PUBLIC_VITE_EXAMPLE_API_KEY
129131
? 'public-shared-chat-redesign'
130132
: 'shared-chat-redesign';
131133

@@ -256,6 +258,7 @@ const App = () => {
256258
...(userName && { name: userName }),
257259
},
258260
});
261+
259262
const searchController = useMemo(() => {
260263
if (!chatClient) return undefined;
261264

@@ -285,6 +288,14 @@ const App = () => {
285288
members: { $in: [userId] },
286289
},
287290
{ type: 'public' },
291+
// public example channels
292+
{
293+
cid: {
294+
$in: ['random', 'general', 'music', 'jokes'].map(
295+
(channelId) => `messaging:${channelId}`,
296+
),
297+
},
298+
},
288299
],
289300
}),
290301
[userId],

examples/vite/src/ChatLayout/Panels.tsx

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import 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';
49
import {
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

2633
import { 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+
89124
export 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

Comments
 (0)