Skip to content

Commit

Permalink
Adjust into a component + one class
Browse files Browse the repository at this point in the history
  • Loading branch information
rebeccaalpert committed Jan 10, 2025
1 parent e98c0df commit 45d741e
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import ChatbotFooter, { ChatbotFootnote } from '@patternfly/chatbot/dist/dynamic
import MessageBar from '@patternfly/chatbot/dist/dynamic/MessageBar';
import MessageBox from '@patternfly/chatbot/dist/dynamic/MessageBox';
import Message from '@patternfly/chatbot/dist/dynamic/Message';
import Compare from '@patternfly/chatbot/dist/dynamic/Compare';
import ChatbotConversationHistoryNav from '@patternfly/chatbot/dist/dynamic/ChatbotConversationHistoryNav';

import ChatbotHeader, {
Expand Down Expand Up @@ -107,6 +108,19 @@ This demo displays an embedded ChatBot. Embedded ChatBots are meant to be placed
### Comparing ChatBots

To let users compare how different ChatBots respond to the same prompt, you can add multiple ChatBots within the same window. The following demo illustrates a comparison view pattern that allows users to toggle between different conversations in a single ChatBot window.
<br /><br />
Your code structure should look like this:

```noLive
<Page ... >
<div className="pf-chatbot__compare-container">
<Compare ... />
<ChatbotFooter ... >
<MessageBar ... />
</ChatbotFooter>
</div>
</Page>
```

```js file="./EmbeddedComparisonChatbot.tsx" isFullscreen

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import {
PageSidebarBody,
PageSidebar,
MastheadToggle,
PageToggleButton,
ToggleGroup,
ToggleGroupItem
PageToggleButton
} from '@patternfly/react-core';
import Chatbot, { ChatbotDisplayMode } from '@patternfly/chatbot/dist/dynamic/Chatbot';
import ChatbotContent from '@patternfly/chatbot/dist/dynamic/ChatbotContent';
Expand All @@ -21,6 +19,7 @@ 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 } from '@patternfly/chatbot/dist/dynamic/ChatbotHeader';
import Compare from '@patternfly/chatbot/dist/dynamic/Compare';
import { BarsIcon } from '@patternfly/react-icons';
import userAvatar from '../Messages/user_avatar.svg';
import patternflyAvatar from '../Messages/patternfly_avatar.jpg';
Expand Down Expand Up @@ -113,7 +112,7 @@ export const CompareChild = ({ name, input, hasNewInput, setIsSendButtonDisabled

return (
<Chatbot displayMode={displayMode}>
<ChatbotHeader className="compare-header">
<ChatbotHeader>
<ChatbotHeaderMain>{name}</ChatbotHeaderMain>
</ChatbotHeader>
<ChatbotContent>
Expand All @@ -132,40 +131,9 @@ export const CompareChild = ({ name, input, hasNewInput, setIsSendButtonDisabled
export const EmbeddedComparisonChatbotDemo: React.FunctionComponent = () => {
const [input, setInput] = React.useState<string>();
const [hasNewInput, setHasNewInput] = React.useState(false);
const [isSelected, setIsSelected] = React.useState('toggle-group-chatbot-1');
const [showFirstChatbot, setShowFirstChatbot] = React.useState(true);
const [showSecondChatbot, setShowSecondChatbot] = React.useState(false);
const [isSidebarOpen, setIsSidebarOpen] = React.useState(false);
const [isSendButtonDisabled, setIsSendButtonDisabled] = React.useState(false);

React.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) => {
const id = event.currentTarget.id;
setIsSelected(id);
setShowSecondChatbot(!showSecondChatbot);
setShowFirstChatbot(!showFirstChatbot);
};

const handleSend = (value: string) => {
setInput(value);
setHasNewInput(!hasNewInput);
Expand Down Expand Up @@ -204,46 +172,26 @@ export const EmbeddedComparisonChatbotDemo: React.FunctionComponent = () => {
return (
<Page masthead={masthead} sidebar={sidebar} isContentFilled>
<div className="pf-chatbot__compare-container">
<div className="pf-chatbot__compare-mobile-controls">
<ToggleGroup aria-label="Select which chatbot to display">
<ToggleGroupItem
className="pf-chatbot__compare-toggle"
text="ChatBot 1"
buttonId="toggle-group-chatbot-1"
isSelected={isSelected === 'toggle-group-chatbot-1'}
onChange={handleChildToggleClick}
/>
<ToggleGroupItem
className="pf-chatbot__compare-toggle"
text="ChatBot 2"
buttonId="toggle-group-chatbot-2"
isSelected={isSelected === 'toggle-group-chatbot-2'}
onChange={handleChildToggleClick}
/>
</ToggleGroup>
</div>
<div className="pf-chatbot__compare">
<div
className={`pf-chatbot__compare-item ${!showFirstChatbot ? 'pf-chatbot__compare-item-hidden' : undefined}`}
>
<Compare
firstChild={
<CompareChild
input={input}
hasNewInput={hasNewInput}
name="ChatBot 1"
setIsSendButtonDisabled={setIsSendButtonDisabled}
/>
</div>
<div
className={`pf-chatbot__compare-item ${!showSecondChatbot ? 'pf-chatbot__compare-item-hidden' : undefined}`}
>
}
secondChild={
<CompareChild
input={input}
hasNewInput={hasNewInput}
name="ChatBot 2"
setIsSendButtonDisabled={setIsSendButtonDisabled}
/>
</div>
</div>
}
firstChildDisplayName="ChatBot 1"
secondChildDisplayName="ChatBot 2"
/>
<ChatbotFooter>
<MessageBar
onSendMessage={handleSend}
Expand Down
98 changes: 98 additions & 0 deletions packages/module/src/Compare/Compare.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import React, { PropsWithChildren } from 'react';
import { Flex, Stack, StackItem, ToggleGroup, ToggleGroupItem, Truncate } from '@patternfly/react-core';

Check failure on line 2 in packages/module/src/Compare/Compare.tsx

View workflow job for this annotation

GitHub Actions / call-build-lint-test-workflow / lint

'Flex' is defined but never used

Check failure on line 2 in packages/module/src/Compare/Compare.tsx

View workflow job for this annotation

GitHub Actions / call-build-lint-test-workflow / lint

'Stack' is defined but never used

Check failure on line 2 in packages/module/src/Compare/Compare.tsx

View workflow job for this annotation

GitHub Actions / call-build-lint-test-workflow / lint

'StackItem' is defined but never used

Check failure on line 2 in packages/module/src/Compare/Compare.tsx

View workflow job for this annotation

GitHub Actions / call-build-lint-test-workflow / lint

'Truncate' is defined but never used

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<any, MouseEvent> | React.KeyboardEvent<Element>) => void;

Check warning on line 16 in packages/module/src/Compare/Compare.tsx

View workflow job for this annotation

GitHub Actions / call-build-lint-test-workflow / lint

Unexpected any. Specify a different type
}

export const Compare = ({
firstChild,
secondChild,
firstChildDisplayName,
secondChildDisplayName,
onToggleClick,
toggleGroupAriaLabel = 'Select which chatbot to display'
}: PropsWithChildren<CompareProps>) => {
const [isSelected, setIsSelected] = React.useState('toggle-group-chatbot-1');
const [showFirstChatbot, setShowFirstChatbot] = React.useState(true);
const [showSecondChatbot, setShowSecondChatbot] = React.useState(false);

React.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 | React.MouseEvent<any, MouseEvent> | React.KeyboardEvent<Element>
) => {
const id = event.currentTarget.id;
setIsSelected(id);
setShowSecondChatbot(!showSecondChatbot);
setShowFirstChatbot(!showFirstChatbot);
onToggleClick && onToggleClick(event);
};

return (
<>
<div className="pf-chatbot__compare-mobile-controls">
<ToggleGroup aria-label={toggleGroupAriaLabel}>
<ToggleGroupItem
className="pf-chatbot__compare-toggle"
text={firstChildDisplayName}
buttonId="toggle-group-chatbot-1"
isSelected={isSelected === 'toggle-group-chatbot-1'}
onChange={handleChildToggleClick}
/>
<ToggleGroupItem
className="pf-chatbot__compare-toggle"
text={secondChildDisplayName}
buttonId="toggle-group-chatbot-2"
isSelected={isSelected === 'toggle-group-chatbot-2'}
onChange={handleChildToggleClick}
/>
</ToggleGroup>
</div>
<div className="pf-chatbot__compare">
<div
className={`pf-chatbot__compare-item ${!showFirstChatbot ? 'pf-chatbot__compare-item-hidden' : undefined}`}
>
{firstChild}
</div>
<div
className={`pf-chatbot__compare-item ${!showSecondChatbot ? 'pf-chatbot__compare-item-hidden' : undefined}`}
>
{secondChild}
</div>
</div>
</>
);
};

export default Compare;
2 changes: 2 additions & 0 deletions packages/module/src/Compare/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default } from './Compare';
export * from './Compare';
3 changes: 3 additions & 0 deletions packages/module/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ export * from './ChatbotWelcomePrompt';
export { default as CodeModal } from './CodeModal';
export * from './CodeModal';

export { default as Compare } from './Compare';
export * from './Compare';

export { default as FileDetails } from './FileDetails';
export * from './FileDetails';

Expand Down

0 comments on commit 45d741e

Please sign in to comment.