import { useState, isValidElement, cloneElement, Children, FunctionComponent, ReactNode } from 'react'; import { MessageBar } from '@patternfly/chatbot/dist/dynamic/MessageBar'; import SourceDetailsMenuItem from '@patternfly/chatbot/dist/dynamic/SourceDetailsMenuItem'; import { Divider, DropdownGroup, DropdownItem, DropdownList } from '@patternfly/react-core'; import { BellIcon, CalendarAltIcon, ClipboardIcon, CodeIcon, UploadIcon } from '@patternfly/react-icons'; import { useDropzone } from 'react-dropzone'; export const ChatbotMessageBarDefaultAttachExample: FunctionComponent = () => { const [isOpen, setIsOpen] = useState(false); const [userFacingMenuItems, setUserFacingMenuItems] = useState([]); const handleSend = (message) => alert(message); const { open, getInputProps } = useDropzone({ multiple: true, // eslint-disable-next-line no-console onDropAccepted: () => console.log('fileUploaded') }); const onToggleClick = () => { setIsOpen(!isOpen); setUserFacingMenuItems(initialMenuItems.concat(uploadMenuItems)); }; const findMatchingElements = (elements: ReactNode[], targetValue: string) => { let matchingElements = [] as ReactNode[]; elements.forEach((element) => { if (isValidElement(element)) { // Check if the element's value matches the targetValue if (element.props.value && element.props.value.toLowerCase().includes(targetValue.toLowerCase())) { matchingElements.push(cloneElement(element, { key: element.props.value })); } // Recursively check the element's children const children = Children.toArray(element.props.children); matchingElements = matchingElements.concat(findMatchingElements(children, targetValue)); } }); return matchingElements; }; const onTextChange = (textValue: string) => { if (textValue === '') { setUserFacingMenuItems(initialMenuItems.concat(uploadMenuItems)); return; } const newMenuItems = findMatchingElements(initialMenuItems, textValue); // this is necessary because the React nodes we find traversing the recursive search // aren't correctly wrapped in a DropdownList. This leads to problems with the // auth-operator item where it winds up floating in a bad place in the DOM and never // gets removed setUserFacingMenuItems( <> {newMenuItems.length === 0 ? ( No results found ) : ( newMenuItems.map((item) => item) )} {uploadMenuItems.map((item) => item)} ); }; const initialMenuItems = [ } name="auth-operator" type="Pod" /> , }> Alerts }> Events }> Logs }> YAML - Status }> YAML - All contents ]; const uploadMenuItems = [ , } onClick={open}> Upload from computer ]; return ( <> {/* this is required for react-dropzone to work in Safari and Firefox */} { // eslint-disable-next-line no-console console.log('selected', value); setIsOpen(false); }, attachMenuInputPlaceholder: 'Search cluster resources...', onAttachMenuInputChange: onTextChange, onAttachMenuToggleClick: onToggleClick }} /> ); };