Created
April 7, 2020 13:43
-
-
Save ayoisaiah/6a7b5c8938d3e70057ed99aea8fff556 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useEffect } from 'react'; | |
import { Chat, Channel, ChannelHeader, Thread, Window, ChannelList, ChannelListTeam, MessageList, MessageTeam, MessageInput, } from 'stream-chat-react'; | |
import { StreamChat } from 'stream-chat'; | |
import rug from 'random-username-generator'; | |
import axios from 'axios'; | |
import 'stream-chat-react/dist/css/index.css'; | |
let chatClient; | |
function App() { | |
const [channel, setChannel] = useState(null); | |
useEffect(() => { | |
const username = rug.generate(); | |
async function getToken() { | |
try { | |
const response = await axios.post('<YOUR_NGROK_URL>/join', { | |
username, | |
}); | |
const { token } = response.data; | |
const apiKey = response.data.api_key; | |
chatClient = new StreamChat(apiKey); | |
await chatClient.setUser( | |
{ | |
id: username, | |
name: username, | |
}, | |
token | |
); | |
const channel = chatClient.channel('team', 'general'); | |
await channel.watch(); | |
setChannel(channel); | |
} catch (err) { | |
console.log(err); | |
return; | |
} | |
} | |
getToken(); | |
}, []); | |
if (channel) { | |
return ( | |
<Chat client={chatClient} theme="team light"> | |
<ChannelList | |
options={{ | |
subscribe: true, | |
state: true, | |
}} | |
List={ChannelListTeam} | |
/> | |
<Channel channel={channel}> | |
<Window> | |
<ChannelHeader /> | |
<MessageList Message={MessageTeam} /> | |
<MessageInput focus /> | |
</Window> | |
<Thread Message={MessageTeam} /> | |
</Channel> | |
</Chat> | |
); | |
} | |
return <div>Loading...</div>; | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment