|
1 | 1 | import talkJsConfig from "./talkJsConfig";
|
2 |
| -import { useState, useEffect, useRef } from "react"; |
| 2 | +import { useState, useRef, useCallback } from "react"; |
3 | 3 | import Talk from "talkjs";
|
4 | 4 | import CategoryCollapse from "./components/CategoryCollapse";
|
5 | 5 | import ConversationListItem from "./components/ConversationListItem";
|
6 | 6 | import ChatHeader from "./components/ChatHeader";
|
| 7 | +import { Session, Chatbox } from "@talkjs/react"; |
7 | 8 |
|
8 | 9 | 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); |
18 | 13 |
|
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); |
21 | 16 |
|
22 |
| - const [sessionChatbox, setSessionChatbox] = useState(null); // This is used to store the session chatbox |
23 | 17 | 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 |
| - |
25 | 18 | const [unreadMessages, setUnreadMessages] = useState([]); //This is used to create the unread effect in the conversationlist
|
26 | 19 |
|
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 | + ); |
30 | 25 |
|
31 |
| - useEffect(() => { |
32 |
| - if (talkLoaded) { |
33 | 26 | const me = new Talk.User({
|
34 | 27 | id: talkJsConfig.userId,
|
35 | 28 | name: "Eulalia Van Helgen",
|
36 | 29 | photoUrl: "https://talkjs.com/new-web/avatar-7.jpg",
|
37 | 30 | role: "default",
|
38 | 31 | });
|
39 | 32 |
|
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 | + } |
81 | 40 | }
|
82 |
| - }, [talkLoaded]); |
| 41 | + }; |
83 | 42 |
|
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 | + ); |
88 | 53 |
|
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/", |
93 | 61 | role: "default",
|
94 | 62 | });
|
95 | 63 |
|
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 | + }, []); |
102 | 69 |
|
103 | 70 | return (
|
104 | 71 | <div className="w-full h-screen flex flex-row bg-gray-900 text-white border-none">
|
@@ -172,11 +139,20 @@ function App() {
|
172 | 139 | setMobileChannelSelected={setMobileChannelSelected}
|
173 | 140 | />
|
174 | 141 | </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> |
180 | 156 | </div>
|
181 | 157 | </div>
|
182 | 158 | );
|
|
0 commit comments