import type { MouseEvent as ReactMouseEvent, KeyboardEvent as ReactKeyboardEvent, PropsWithChildren } from 'react'; import { useEffect, useState } from 'react'; import { ToggleGroup, ToggleGroupItem } from '@patternfly/react-core'; interface CompareProps { /** First of two children to render */ firstChild: React.ReactNode; /** Second of two children to render */ secondChild: React.ReactNode; /** Display name for first child, used in mobile toggle */ firstChildDisplayName: string; /** Display name for second child, used in mobile toggle */ secondChildDisplayName: string; /** Aria label for mobile toggle group */ toggleGroupAriaLabel?: string; /** Callback for when mobile toggle is used */ onToggleClick?: (event: MouseEvent | React.MouseEvent | React.KeyboardEvent) => void; } export const Compare = ({ firstChild, secondChild, firstChildDisplayName, secondChildDisplayName, onToggleClick, toggleGroupAriaLabel = 'Select which chatbot to display' }: PropsWithChildren) => { const [isSelected, setIsSelected] = useState('toggle-group-chatbot-1'); const [showFirstChatbot, setShowFirstChatbot] = useState(true); const [showSecondChatbot, setShowSecondChatbot] = useState(false); useEffect(() => { // we want to show the first if we switch to the mobile toggle view // and reset/switch back to normal otherwise const updateChatbotVisibility = () => { if (window.innerWidth >= 901) { setShowFirstChatbot(true); setShowSecondChatbot(true); } else { setShowFirstChatbot(true); setShowSecondChatbot(false); setIsSelected('toggle-group-chatbot-1'); } }; window.addEventListener('resize', updateChatbotVisibility); return () => { window.removeEventListener('resize', updateChatbotVisibility); }; }, []); // this only happens on mobile const handleChildToggleClick = ( event: MouseEvent | ReactMouseEvent | ReactKeyboardEvent ) => { const id = event.currentTarget.id; setIsSelected(id); setShowSecondChatbot(!showSecondChatbot); setShowFirstChatbot(!showFirstChatbot); onToggleClick && onToggleClick(event); }; return ( <>
{firstChild}
{secondChild}
); }; export default Compare;