Skip to content

Commit abc89ec

Browse files
feat(react): adjust tutorial application
1 parent eb4b51e commit abc89ec

File tree

2 files changed

+58
-52
lines changed

2 files changed

+58
-52
lines changed

examples/react-chat-ai-tutorial/src/App.tsx

Lines changed: 4 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,7 @@
11
import type { ChannelFilters, ChannelOptions, ChannelSort } from 'stream-chat';
2-
import {
3-
Chat,
4-
Channel,
5-
MessageList,
6-
useCreateChatClient,
7-
ChannelList,
8-
Window,
9-
MessageInput,
10-
useChatContext,
11-
} from 'stream-chat-react';
12-
import { Composer } from './components/Composer';
13-
import { MessageBubble } from './components/MessageBubble';
14-
import { AIStateIndicator } from './components/AIStateIndicator';
15-
import { useEffect } from 'react';
16-
import { nanoid } from 'nanoid';
17-
import { ChannelListItem } from './components/ChannelListItem';
2+
import { Chat, useCreateChatClient } from 'stream-chat-react';
3+
4+
import { ChatContent } from './components/ChatContent';
185

196
const userToken = import.meta.env.VITE_STREAM_USER_TOKEN;
207
const apiKey = import.meta.env.VITE_STREAM_API_KEY;
@@ -43,41 +30,6 @@ const filters: ChannelFilters = {
4330
const options: ChannelOptions = { limit: 5 };
4431
const sort: ChannelSort = { pinned_at: 1, last_message_at: -1, updated_at: -1 };
4532

46-
const ChatContent = () => {
47-
const { setActiveChannel, client, channel } = useChatContext();
48-
49-
useEffect(() => {
50-
if (!channel) {
51-
setActiveChannel(
52-
client.channel('messaging', `ai-${nanoid()}`, {
53-
members: [client.userID as string],
54-
// @ts-expect-error fix - this is a hack that allows a custom upload function to run
55-
own_capabilities: ['upload-file'],
56-
}),
57-
);
58-
}
59-
}, [channel]);
60-
61-
return (
62-
<>
63-
<ChannelList
64-
Preview={ChannelListItem}
65-
setActiveChannelOnMount={false}
66-
filters={filters}
67-
sort={sort}
68-
options={options}
69-
/>
70-
<Channel initializeOnMount={false} Message={MessageBubble}>
71-
<Window>
72-
<MessageList />
73-
<AIStateIndicator />
74-
<MessageInput Input={Composer} />
75-
</Window>
76-
</Channel>
77-
</>
78-
);
79-
};
80-
8133
function App() {
8234
const chatClient = useCreateChatClient({
8335
apiKey: apiKey!,
@@ -93,7 +45,7 @@ function App() {
9345

9446
return (
9547
<Chat client={chatClient}>
96-
<ChatContent />
48+
<ChatContent filters={filters} sort={sort} options={options} />
9749
</Chat>
9850
);
9951
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import {
2+
Channel,
3+
MessageList,
4+
ChannelList,
5+
Window,
6+
MessageInput,
7+
useChatContext,
8+
type ChannelListProps,
9+
} from 'stream-chat-react';
10+
import { Composer } from './Composer';
11+
import { MessageBubble } from './MessageBubble';
12+
import { AIStateIndicator } from './AIStateIndicator';
13+
import { useEffect } from 'react';
14+
import { nanoid } from 'nanoid';
15+
import { ChannelListItem } from './ChannelListItem';
16+
17+
export const ChatContent = ({
18+
filters,
19+
options,
20+
sort,
21+
}: Pick<ChannelListProps, 'options' | 'sort' | 'filters'>) => {
22+
const { setActiveChannel, client, channel } = useChatContext();
23+
24+
useEffect(() => {
25+
if (!channel) {
26+
setActiveChannel(
27+
client.channel('messaging', `ai-${nanoid()}`, {
28+
members: [client.userID as string],
29+
// @ts-expect-error fix - this is a hack that allows a custom upload function to run
30+
own_capabilities: ['upload-file'],
31+
}),
32+
);
33+
}
34+
}, [channel]);
35+
36+
return (
37+
<>
38+
<ChannelList
39+
Preview={ChannelListItem}
40+
setActiveChannelOnMount={false}
41+
filters={filters}
42+
sort={sort}
43+
options={options}
44+
/>
45+
<Channel initializeOnMount={false} Message={MessageBubble}>
46+
<Window>
47+
<MessageList />
48+
<AIStateIndicator />
49+
<MessageInput Input={Composer} />
50+
</Window>
51+
</Channel>
52+
</>
53+
);
54+
};

0 commit comments

Comments
 (0)