Skip to content

Commit 76d91b3

Browse files
committed
Update remote work demo to use talkjs-react
1 parent 3d68852 commit 76d91b3

File tree

4 files changed

+2000
-1850
lines changed

4 files changed

+2000
-1850
lines changed

react/remote-work-demo/package-lock.json

+18-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

react/remote-work-demo/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
6+
"@talkjs/react": "^0.1.3",
67
"@testing-library/jest-dom": "^5.17.0",
78
"@testing-library/react": "^13.4.0",
89
"@testing-library/user-event": "^13.5.0",
910
"react": "^18.2.0",
1011
"react-dom": "^18.2.0",
1112
"react-feather": "^2.0.10",
1213
"react-scripts": "5.0.1",
13-
"talkjs": "^0.16.4",
14+
"talkjs": "^0.16.6",
1415
"web-vitals": "^2.1.4"
1516
},
1617
"scripts": {

react/remote-work-demo/src/App.jsx

+56-80
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,71 @@
11
import talkJsConfig from "./talkJsConfig";
2-
import { useState, useEffect, useRef } from "react";
2+
import { useState, useRef, useCallback } from "react";
33
import Talk from "talkjs";
44
import CategoryCollapse from "./components/CategoryCollapse";
55
import ConversationListItem from "./components/ConversationListItem";
66
import ChatHeader from "./components/ChatHeader";
7+
import { Session, Chatbox } from "@talkjs/react";
78

89
function App() {
9-
/*
10-
* The three values of the currentConversation object pertain to the UI,
11-
* and they are essential for rendering the contents of the components properly.
12-
*/
13-
const [currentConversation, setCurrentConversation] = useState({
14-
id: "",
15-
subject: "",
16-
avatar: "",
17-
});
10+
const initialConversation = talkJsConfig.conversations.channels[0];
11+
const [currentConversation, setCurrentConversation] =
12+
useState(initialConversation);
1813

19-
const talkjsContainer = useRef(null); //This is used to create a ref for the mounting of the TalkJS UI
20-
const [talkLoaded, setTalkLoaded] = useState(false); // This is used to check whether or not TalkJS has loaded,
14+
const sessionRef = useRef(null);
15+
const chatboxRef = useRef(null);
2116

22-
const [sessionChatbox, setSessionChatbox] = useState(null); // This is used to store the session chatbox
2317
const [mobileChannelSelected, setMobileChannelSelected] = useState(true); //This is used to control whether or not to display the chatbox or the inbox while on mobile displays
24-
2518
const [unreadMessages, setUnreadMessages] = useState([]); //This is used to create the unread effect in the conversationlist
2619

27-
useEffect(() => {
28-
Talk.ready.then(() => setTalkLoaded(true));
29-
}, [talkLoaded]);
20+
const changeConversation = (conversation) => {
21+
if (sessionRef.current?.isAlive) {
22+
const talkJsConversation = sessionRef.current.getOrCreateConversation(
23+
conversation.id
24+
);
3025

31-
useEffect(() => {
32-
if (talkLoaded) {
3326
const me = new Talk.User({
3427
id: talkJsConfig.userId,
3528
name: "Eulalia Van Helgen",
3629
photoUrl: "https://talkjs.com/new-web/avatar-7.jpg",
3730
role: "default",
3831
});
3932

40-
const other = new Talk.User({
41-
id: "remoteWorkOther",
42-
name: "TalkJS",
43-
photoUrl: "https://talkjs.com/new-web/avatar-talkjs.jpg",
44-
welcomeMessage:
45-
"Hi there 👋 \nThis is our chat demo and you can test it out in any way you like. Play with some of the chat features, kick the tyres a little, and experience what you could easily build with TalkJS. Also consider checking out our Docs: https://talkjs.com/docs/",
46-
role: "default",
47-
});
48-
49-
window.talkSession = new Talk.Session({
50-
appId: talkJsConfig.appId,
51-
me: me,
52-
});
53-
54-
const defaultConv =
55-
window.talkSession.getOrCreateConversation("remoteWorkDefault");
56-
defaultConv.setParticipant(me);
57-
defaultConv.setParticipant(other);
58-
defaultConv.setAttributes({
59-
subject: "welcome",
60-
welcomeMessages: ["Welcome to the TalkJS team chat demo!"],
61-
});
62-
63-
const chatbox = window.talkSession.createChatbox({
64-
theme: "team_chat",
65-
conversation: defaultConv,
66-
showChatHeader: false,
67-
});
68-
69-
setCurrentConversation({
70-
id: "remoteWorkDefault",
71-
subject: "TalkJS",
72-
avatar: "https://talkjs.com/new-web/avatar-talkjs.jpg",
73-
});
74-
setSessionChatbox(chatbox);
75-
chatbox.select(defaultConv);
76-
chatbox.mount(talkjsContainer.current);
77-
78-
window.talkSession.unreads.on("change", function (unreadConversations) {
79-
setUnreadMessages(unreadConversations);
80-
});
33+
talkJsConversation.setParticipant(me);
34+
talkJsConversation.setAttributes(conversation);
35+
setMobileChannelSelected(true);
36+
setCurrentConversation(conversation);
37+
if (chatboxRef.current?.isAlive) {
38+
chatboxRef.current.select(talkJsConversation);
39+
}
8140
}
82-
}, [talkLoaded]);
41+
};
8342

84-
const changeConversation = (conversation) => {
85-
const talkJsConversation = window.talkSession.getOrCreateConversation(
86-
conversation.id
87-
);
43+
const syncUser = useCallback(
44+
() =>
45+
new Talk.User({
46+
id: talkJsConfig.userId,
47+
name: "Eulalia Van Helgen",
48+
photoUrl: "https://talkjs.com/new-web/avatar-7.jpg",
49+
role: "default",
50+
}),
51+
[]
52+
);
8853

89-
const me = new Talk.User({
90-
id: talkJsConfig.userId,
91-
name: "Eulalia Van Helgen",
92-
photoUrl: "https://talkjs.com/new-web/avatar-7.jpg",
54+
const syncConversation = useCallback((session) => {
55+
const other = new Talk.User({
56+
id: "remoteWorkOther",
57+
name: "TalkJS",
58+
photoUrl: "https://talkjs.com/new-web/avatar-talkjs.jpg",
59+
welcomeMessage:
60+
"Hi there 👋 \nThis is our chat demo and you can test it out in any way you like. Play with some of the chat features, kick the tyres a little, and experience what you could easily build with TalkJS. Also consider checking out our Docs: https://talkjs.com/docs/",
9361
role: "default",
9462
});
9563

96-
talkJsConversation.setParticipant(me);
97-
talkJsConversation.setAttributes(conversation);
98-
setMobileChannelSelected(true);
99-
setCurrentConversation(conversation);
100-
sessionChatbox.select(talkJsConversation);
101-
};
64+
const defaultConv = session.getOrCreateConversation("remoteWorkWelcome");
65+
defaultConv.setParticipant(session.me);
66+
defaultConv.setParticipant(other);
67+
return defaultConv;
68+
}, []);
10269

10370
return (
10471
<div className="w-full h-screen flex flex-row bg-gray-900 text-white border-none">
@@ -172,11 +139,20 @@ function App() {
172139
setMobileChannelSelected={setMobileChannelSelected}
173140
/>
174141
</div>
175-
176-
<div
177-
className="h-full w-full overflow-hidden rounded-b-xl lg:rounded-none lg:rounded-br-xl"
178-
ref={talkjsContainer}
179-
></div>
142+
<Session
143+
appId={talkJsConfig.appId}
144+
syncUser={syncUser}
145+
sessionRef={sessionRef}
146+
onUnreadsChange={(unreads) => setUnreadMessages(unreads)}
147+
>
148+
<Chatbox
149+
chatboxRef={chatboxRef}
150+
syncConversation={syncConversation}
151+
className="h-full w-full overflow-hidden rounded-b-xl lg:rounded-none lg:rounded-br-xl"
152+
showChatHeader={false}
153+
theme="team_chat"
154+
/>
155+
</Session>
180156
</div>
181157
</div>
182158
);

0 commit comments

Comments
 (0)