|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | +import '@stream-io/stream-chat-css/dist/css/index.css'; |
| 3 | +import React, { useEffect, useState } from 'react'; |
| 4 | +import { ChannelFilters, ChannelOptions, ChannelSort, Event, StreamChat } from 'stream-chat'; |
| 5 | +import { |
| 6 | + Channel, |
| 7 | + ChannelHeader, |
| 8 | + ChannelList, |
| 9 | + Chat, |
| 10 | + MessageInput, |
| 11 | + MessageList, |
| 12 | + Thread, |
| 13 | + Window, |
| 14 | +} from '../index'; |
| 15 | +import { apiKey, StreamChatGenerics } from './utils'; |
| 16 | + |
| 17 | +const channelId = import.meta.env.E2E_ADD_MESSAGE_CHANNEL; |
| 18 | +const userId = import.meta.env.E2E_TEST_USER_1 as string; |
| 19 | +const token = import.meta.env.E2E_TEST_USER_1_TOKEN as string; |
| 20 | + |
| 21 | +if (!channelId || typeof channelId !== 'string') { |
| 22 | + throw new Error('expected ADD_MESSAGE_CHANNEL'); |
| 23 | +} |
| 24 | + |
| 25 | +// Sort in reverse order to avoid auto-selecting unread channel |
| 26 | +const sort: ChannelSort = { last_updated: 1 }; |
| 27 | +const filters: ChannelFilters = { members: { $in: [userId] }, type: 'messaging' }; |
| 28 | +const options: ChannelOptions = { limit: 10, presence: true, state: true }; |
| 29 | + |
| 30 | +const chatClient = StreamChat.getInstance<StreamChatGenerics>(apiKey); |
| 31 | +let sharedPromise = Promise.resolve(); |
| 32 | + |
| 33 | +export const BasicSetup = () => { |
| 34 | + const [connected, setConnected] = useState(false); |
| 35 | + |
| 36 | + useEffect(() => { |
| 37 | + sharedPromise.then(() => chatClient.connectUser({ id: userId }, token)); |
| 38 | + |
| 39 | + const handleConnectionChange = ({ online = false }: Event) => { |
| 40 | + setConnected(online); |
| 41 | + }; |
| 42 | + |
| 43 | + chatClient.on('connection.changed', handleConnectionChange); |
| 44 | + |
| 45 | + return () => { |
| 46 | + chatClient.off('connection.changed', handleConnectionChange); |
| 47 | + sharedPromise = chatClient.disconnectUser(); |
| 48 | + }; |
| 49 | + }, []); |
| 50 | + |
| 51 | + if (!connected) { |
| 52 | + return <p>Connecting {userId}...</p>; |
| 53 | + } |
| 54 | + |
| 55 | + return ( |
| 56 | + <Chat client={chatClient}> |
| 57 | + <ChannelList filters={filters} options={options} showChannelSearch sort={sort} /> |
| 58 | + <Channel> |
| 59 | + <Window> |
| 60 | + <ChannelHeader /> |
| 61 | + <MessageList /> |
| 62 | + <MessageInput focus /> |
| 63 | + </Window> |
| 64 | + <Thread /> |
| 65 | + </Channel> |
| 66 | + </Chat> |
| 67 | + ); |
| 68 | +}; |
0 commit comments