import React from 'react'; import ChatbotToggle from '@patternfly/virtual-assistant/dist/dynamic/ChatbotToggle'; import Chatbot, { ChatbotDisplayMode } from '@patternfly/virtual-assistant/dist/dynamic/Chatbot'; import ChatbotContent from '@patternfly/virtual-assistant/dist/dynamic/ChatbotContent'; import ChatbotWelcomePrompt from '@patternfly/virtual-assistant/dist/dynamic/ChatbotWelcomePrompt'; import ChatbotFooter, { ChatbotFootnote } from '@patternfly/virtual-assistant/dist/dynamic/ChatbotFooter'; import MessageBar from '@patternfly/virtual-assistant/dist/dynamic/MessageBar'; import MessageBox from '@patternfly/virtual-assistant/dist/dynamic/MessageBox'; import Message, { MessageProps } from '@patternfly/virtual-assistant/dist/dynamic/Message'; import FileDropZone from '@patternfly/virtual-assistant/dist/dynamic/FileDropZone'; import { Brand, Bullseye, DropdownGroup, DropdownItem, DropdownList, DropEvent } from '@patternfly/react-core'; import FileDetailsLabel from '@patternfly/virtual-assistant/dist/dynamic/FileDetailsLabel'; import PreviewAttachment from '@patternfly/virtual-assistant/dist/dynamic/PreviewAttachment'; import AttachmentEdit from '@patternfly/virtual-assistant/dist/dynamic/AttachmentEdit'; import ChatbotHeader, { ChatbotHeaderMenu, ChatbotHeaderTitle, ChatbotHeaderActions, ChatbotHeaderOptionsDropdown, ChatbotHeaderMain } from '@patternfly/virtual-assistant/dist/dynamic/ChatbotHeader'; import ChatbotAlert from '@patternfly/virtual-assistant/dist/dynamic/ChatbotAlert'; import ExpandIcon from '@patternfly/react-icons/dist/esm/icons/expand-icon'; import OpenDrawerRightIcon from '@patternfly/react-icons/dist/esm/icons/open-drawer-right-icon'; import OutlinedWindowRestoreIcon from '@patternfly/react-icons/dist/esm/icons/outlined-window-restore-icon'; import PFHorizontalLogoColor from '../ChatbotHeader/PF-HorizontalLogo-Color.svg'; import PFHorizontalLogoReverse from '../ChatbotHeader/PF-HorizontalLogo-Reverse.svg'; import PFIconLogoColor from '../ChatbotHeader/PF-IconLogo-Color.svg'; import PFIconLogoReverse from '../ChatbotHeader/PF-IconLogo-Reverse.svg'; import userAvatar from '../ChatbotMessage/user_avatar.jpg'; import patternflyAvatar from '../ChatbotMessage/patternfly_avatar.jpg'; interface ModalData { code: string; fileName: string; } export const BasicDemo: React.FunctionComponent = () => { const onAttachmentClose = (attachmentId: string) => { const index = messages.findIndex((message) => message.attachmentId === attachmentId); const updatedMessages: MessageProps[] = []; if (index >= 0) { messages.forEach((message) => updatedMessages.push(message)); delete updatedMessages[index].attachmentName; delete updatedMessages[index].attachmentId; delete updatedMessages[index].onAttachmentClick; delete updatedMessages[index].onAttachmentClose; setMessages(updatedMessages); } }; const initialMessages: MessageProps[] = [ { role: 'user', content: "I'm referring to this attachment for added context in our conversation.", name: 'User', avatar: userAvatar, attachmentName: 'auth-operator.yml', attachmentId: '1', onAttachmentClose, onAttachmentClick: () => { setCurrentModalData({ fileName: 'auth-operator.yml', code: 'test' }); setIsEditModalOpen(false); setIsPreviewModalOpen(true); } }, { role: 'bot', content: 'Great, I can reference this attachment throughout our conversation.', avatar: patternflyAvatar, name: 'Bot' } ]; const [error, setError] = React.useState(); const [chatbotVisible, setChatbotVisible] = React.useState(true); const [file, setFile] = React.useState(); const [isLoadingFile, setIsLoadingFile] = React.useState(false); const [messages, setMessages] = React.useState(initialMessages); const [isPreviewModalOpen, setIsPreviewModalOpen] = React.useState(false); const [isEditModalOpen, setIsEditModalOpen] = React.useState(false); const [currentModalData, setCurrentModalData] = React.useState(); const [showAlert, setShowAlert] = React.useState(false); const [displayMode, setDisplayMode] = React.useState(ChatbotDisplayMode.default); const onSelectDisplayMode = ( _event: React.MouseEvent | undefined, value: string | number | undefined ) => { setDisplayMode(value as ChatbotDisplayMode); }; const handleSend = (message) => alert(message); // Attachments // -------------------------------------------------------------------------- // example of how you can read a text file const readFile = (file: File) => new Promise((resolve, reject) => { const reader = new FileReader(); reader.onload = () => resolve(reader.result); reader.onerror = () => reject(reader.error); reader.readAsDataURL(file); // you can use reader.readAsText(file) for human-readable file types; }); // handle file drop/selection const handleFile = (fileArr: File[]) => { setIsLoadingFile(true); // any custom validation you may want if (fileArr.length > 1) { setShowAlert(true); setFile(undefined); setError('Uploaded more than one file.'); return; } // this is 25MB in bytes; size is in bytes if (fileArr[0].size > 25000000) { setShowAlert(true); setFile(undefined); setError('File is larger than 25MB.'); return; } readFile(fileArr[0]) .then((data) => { // eslint-disable-next-line no-console console.log(data); setFile(fileArr[0]); setShowAlert(false); setError(undefined); // this is just for demo purposes, to make the loading state really obvious setTimeout(() => { setIsLoadingFile(false); }, 1000); }) .catch((error: DOMException) => { setError(`Failed to read file: ${error.message}`); }); }; const handleFileDrop = (event: DropEvent, data: File[]) => { handleFile(data); }; const handleAttach = (data: File[]) => { handleFile(data); }; const onClose = () => { setFile(undefined); }; const horizontalLogo = ( ); const iconLogo = ( <> ); return ( <> { setChatbotVisible(!chatbotVisible); setIsEditModalOpen(false); setIsPreviewModalOpen(false); }} /> alert('Menu toggle clicked')} /> } isSelected={displayMode === ChatbotDisplayMode.default} > Overlay } isSelected={displayMode === ChatbotDisplayMode.docked} > Dock to window } isSelected={displayMode === ChatbotDisplayMode.fullscreen} > Fullscreen {showAlert && ( { setShowAlert(false); setError(undefined); }} title="File upload failed" > {error} )} {messages.map((message) => ( ))} {file && (
)}
{currentModalData && ( { setIsPreviewModalOpen(false); setIsEditModalOpen(true); }} onDismiss={() => setCurrentModalData(undefined)} handleModalToggle={() => setIsPreviewModalOpen(false)} displayMode={displayMode} /> )} {currentModalData && ( { setIsEditModalOpen(false); }} onCancel={() => setCurrentModalData(undefined)} handleModalToggle={() => setIsEditModalOpen(false)} displayMode={displayMode} /> )} ); };