-
Notifications
You must be signed in to change notification settings - Fork 4
/
WidgetContainer.jsx
51 lines (43 loc) · 1.36 KB
/
WidgetContainer.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { useState, useEffect, useMemo } from "react";
import { Widget } from "./Widget";
import { nanoid } from "nanoid";
export const WidgetContainer = ({license = "", greeting = ""}) => {
const [messages, setMessages] = useState([]);
useEffect( () => {
if ( greeting && messages.length === 0 ) {
setMessages(messages.concat({
_id: nanoid(),
message: greeting,
sender: "remote",
direction: "incoming",
}));
}
},[greeting, messages]);
const remoteName = useMemo( () => {
if ( license === "123" ) {
return "Chatscope";
} else if (license === "456" ) {
return "ChatKitty";
} else if (license === "789" ) {
return "EvilNull";
}
},[license]);
const handleSend = (message) => {
const newMessages = [
{
_id: nanoid(),
message,
sender: "me",
direction: "outgoing",
},
{
_id: nanoid(),
message: `ECHO: ${message}`,
sender: "remote",
direction: "incoming",
}
];
setMessages(messages.concat(newMessages));
};
return <Widget remoteName={remoteName} messages={messages} onSend={handleSend} />
};