-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathEmbeddedComparisonChatbot.tsx
More file actions
210 lines (197 loc) · 7.36 KB
/
Copy pathEmbeddedComparisonChatbot.tsx
File metadata and controls
210 lines (197 loc) · 7.36 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import { useState, useRef, useCallback, useEffect, FunctionComponent } from 'react';
import {
Page,
Masthead,
MastheadMain,
MastheadBrand,
MastheadLogo,
PageSidebarBody,
PageSidebar,
MastheadToggle,
PageToggleButton
} from '@patternfly/react-core';
import Chatbot, { ChatbotDisplayMode } from '@patternfly/chatbot/dist/dynamic/Chatbot';
import ChatbotContent from '@patternfly/chatbot/dist/dynamic/ChatbotContent';
import ChatbotWelcomePrompt from '@patternfly/chatbot/dist/dynamic/ChatbotWelcomePrompt';
import ChatbotFooter from '@patternfly/chatbot/dist/dynamic/ChatbotFooter';
import MessageBar from '@patternfly/chatbot/dist/dynamic/MessageBar';
import MessageBox from '@patternfly/chatbot/dist/dynamic/MessageBox';
import Message, { MessageProps } from '@patternfly/chatbot/dist/dynamic/Message';
import ChatbotHeader, { ChatbotHeaderMain, ChatbotHeaderTitle } from '@patternfly/chatbot/dist/dynamic/ChatbotHeader';
import Compare from '@patternfly/chatbot/dist/dynamic/Compare';
import { RhUiMenuBarsIcon } from '@patternfly/react-icons';
import userAvatar from '../Messages/user_avatar.svg';
import '@patternfly/react-core/dist/styles/base.css';
import '@patternfly/chatbot/dist/css/main.css';
export const CompareChild = ({ name, input, hasNewInput, setIsSendButtonDisabled }) => {
const [messages, setMessages] = useState<MessageProps[]>([]);
const [announcement, setAnnouncement] = useState<string>();
const scrollToBottomRef = useRef<HTMLDivElement>(null);
const displayMode = ChatbotDisplayMode.embedded;
// you will likely want to come up with your own unique id function; this is for demo purposes only
const generateId = () => {
const id = Date.now() + Math.random();
return id.toString();
};
const handleSend = useCallback(
(input: string) => {
const date = new Date();
const newMessages: MessageProps[] = [];
messages.forEach((message) => newMessages.push(message));
newMessages.push({
avatar: userAvatar,
avatarProps: { isBordered: true },
id: generateId(),
name: 'You',
role: 'user',
content: input,
timestamp: `${date?.toLocaleDateString()} ${date?.toLocaleTimeString()}`
});
newMessages.push({
id: generateId(),
name,
role: 'bot',
timestamp: `${date?.toLocaleDateString()} ${date?.toLocaleTimeString()}`,
isLoading: true
});
setMessages(newMessages);
// make announcement to assistive devices that new messages have been added
setAnnouncement(`Message from You: ${input}. Message from ${name} is loading.`);
// this is for demo purposes only; in a real situation, there would be an API response we would wait for
setTimeout(() => {
const loadedMessages: MessageProps[] = [];
// we can't use structuredClone since messages contains functions, but we can't mutate
// items that are going into state or the UI won't update correctly
newMessages.forEach((message) => loadedMessages.push(message));
loadedMessages.pop();
loadedMessages.push({
id: generateId(),
role: 'bot',
content: `API response from ${name} goes here`,
name,
isLoading: false,
actions: {
// eslint-disable-next-line no-console
positive: { onClick: () => console.log('Good response') },
// eslint-disable-next-line no-console
negative: { onClick: () => console.log('Bad response') },
// eslint-disable-next-line no-console
copy: { onClick: () => console.log('Copy') },
// eslint-disable-next-line no-console
download: { onClick: () => console.log('Download') },
// eslint-disable-next-line no-console
listen: { onClick: () => console.log('Listen') }
},
timestamp: date.toLocaleString()
});
setMessages(loadedMessages);
// make announcement to assistive devices that new message has loaded
setAnnouncement(`Message from ${name}: API response goes here`);
setIsSendButtonDisabled(false);
}, 5000);
},
[messages, name, setIsSendButtonDisabled]
);
useEffect(() => {
if (input) {
handleSend(input);
}
}, [hasNewInput, input]);
// Auto-scrolls to the latest message
useEffect(() => {
// don't scroll the first load, but scroll if there's a current stream or a new source has popped up
if (messages.length > 0) {
scrollToBottomRef.current?.scrollIntoView();
}
}, [messages]);
return (
<Chatbot displayMode={displayMode}>
<ChatbotHeader>
<ChatbotHeaderMain>
<ChatbotHeaderTitle>{name}</ChatbotHeaderTitle>
</ChatbotHeaderMain>
</ChatbotHeader>
<ChatbotContent>
<MessageBox ariaLabel={`Scrollable message log for ${name}`} announcement={announcement}>
<ChatbotWelcomePrompt title="Hi, ChatBot User!" description="How can I help you today?" />
{messages.map((message) => (
<Message key={message.id} {...message} />
))}
<div ref={scrollToBottomRef}></div>
</MessageBox>
</ChatbotContent>
</Chatbot>
);
};
export const EmbeddedComparisonChatbotDemo: FunctionComponent = () => {
const [input, setInput] = useState<string>();
const [hasNewInput, setHasNewInput] = useState(false);
const [isSidebarOpen, setIsSidebarOpen] = useState(false);
const [isSendButtonDisabled, setIsSendButtonDisabled] = useState(false);
const handleSend = (value: string) => {
setInput(value);
setHasNewInput(!hasNewInput);
setIsSendButtonDisabled(true);
};
const masthead = (
<Masthead>
<MastheadMain>
<MastheadToggle>
<PageToggleButton
variant="plain"
aria-label="Global navigation"
isSidebarOpen={isSidebarOpen}
onSidebarToggle={() => setIsSidebarOpen(!isSidebarOpen)}
id="fill-nav-toggle"
>
<RhUiMenuBarsIcon />
</PageToggleButton>
</MastheadToggle>
<MastheadBrand>
<MastheadLogo href="https://patternfly.org" target="_blank">
Logo
</MastheadLogo>
</MastheadBrand>
</MastheadMain>
</Masthead>
);
const sidebar = (
<PageSidebar isSidebarOpen={isSidebarOpen} id="fill-sidebar">
<PageSidebarBody>Navigation</PageSidebarBody>
</PageSidebar>
);
return (
<Page masthead={masthead} sidebar={sidebar} isContentFilled>
<div className="pf-chatbot__compare-container">
<Compare
firstChild={
<CompareChild
input={input}
hasNewInput={hasNewInput}
name="ChatBot 1"
setIsSendButtonDisabled={setIsSendButtonDisabled}
/>
}
secondChild={
<CompareChild
input={input}
hasNewInput={hasNewInput}
name="ChatBot 2"
setIsSendButtonDisabled={setIsSendButtonDisabled}
/>
}
firstChildDisplayName="ChatBot 1"
secondChildDisplayName="ChatBot 2"
/>
<ChatbotFooter>
<MessageBar
onSendMessage={handleSend}
hasAttachButton={false}
alwayShowSendButton
isSendButtonDisabled={isSendButtonDisabled}
/>
</ChatbotFooter>
</div>
</Page>
);
};