Skip to content

Commit

Permalink
Disable send button when input is blank
Browse files Browse the repository at this point in the history
Updated Virtual Assistant to pull in onChange and used it to modify the send button state.
  • Loading branch information
rebeccaalpert committed Nov 7, 2024
1 parent a9610da commit 6357c68
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"@patternfly/react-core": "^6.0.0",
"@patternfly/react-icons": "^6.0.0",
"@patternfly/react-styles": "^6.0.0",
"@patternfly/virtual-assistant": "^2.1.0-prerelease.8",
"@patternfly/virtual-assistant": "^2.1.0-prerelease.11",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"sirv-cli": "^2.0.2"
Expand Down
12 changes: 12 additions & 0 deletions src/app/BaseChatbot/BaseChatbot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ interface Source {

const BaseChatbot: React.FunctionComponent = () => {
const { chatbots } = useLoaderData() as { chatbots: CannedChatbot[] };
const [isSendButtonDisabled, setIsSendButtonDisabled] = React.useState(true);
const [messages, setMessages] = React.useState<MessageProps[]>([]);
const [currentMessage, setCurrentMessage] = React.useState<string[]>([]);
const [currentSources, setCurrentSources] = React.useState<Source[]>();
Expand Down Expand Up @@ -167,6 +168,7 @@ const BaseChatbot: React.FunctionComponent = () => {
}

const handleSend = async (input: string) => {
setIsSendButtonDisabled(true);
const date = new Date();
const newMessages = structuredClone(messages);
if (currentMessage.length > 0) {
Expand Down Expand Up @@ -229,6 +231,14 @@ const BaseChatbot: React.FunctionComponent = () => {
setHasStopButton(false);
};

const handleChange = (event: React.ChangeEvent<HTMLTextAreaElement>, value: string) => {
if (value !== '') {
setIsSendButtonDisabled(false);
return;
}
setIsSendButtonDisabled(true);
};

return (
<Chatbot displayMode={displayMode}>
<ChatbotHeader>
Expand Down Expand Up @@ -281,6 +291,8 @@ const BaseChatbot: React.FunctionComponent = () => {
hasStopButton={hasStopButton}
handleStopButton={handleStopButton}
alwayShowSendButton
onChange={handleChange}
isSendButtonDisabled={isSendButtonDisabled}
/>
<ChatbotFootnote label="Verify all information from this tool. LLMs make mistakes." />
</ChatbotFooter>
Expand Down
2 changes: 1 addition & 1 deletion src/app/Compare/CompareChild.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ const CompareChild: React.FunctionComponent<CompareChildProps> = ({

// fixme this is getting too large; we should refactor
async function fetchData(userMessage: string) {
updateStatus(order === 'first' ? 'child1' : 'child2', { isMessageStreaming: true });
if (controller) {
controller.abort();
}
Expand Down Expand Up @@ -169,7 +170,6 @@ const CompareChild: React.FunctionComponent<CompareChildProps> = ({
throw new Error('Other');
}
}
updateStatus(order === 'first' ? 'child1' : 'child2', { isMessageStreaming: true });

// start reading the streaming message
const reader = response.body.getReader();
Expand Down
12 changes: 12 additions & 0 deletions src/app/Compare/CompareLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const CompareLayout: React.FunctionComponent = () => {
const { chatbots } = useLoaderData() as { chatbots: CannedChatbot[] };

// state
const [isSendButtonDisabled, setIsSendButtonDisabled] = React.useState(true);
const [input, setInput] = React.useState<string>();
const [hasNewInput, setHasNewInput] = React.useState(false);
const [firstController, setFirstController] = React.useState<AbortController>();
Expand Down Expand Up @@ -96,6 +97,7 @@ export const CompareLayout: React.FunctionComponent = () => {
const handleSend = (value: string) => {
setInput(value);
setHasNewInput(!hasNewInput);
setIsSendButtonDisabled(true);
};

const changeSearchParams = (_event, value: string, order: string) => {
Expand All @@ -117,6 +119,14 @@ export const CompareLayout: React.FunctionComponent = () => {
setHasStopButton(false);
};

const handleChange = (event: React.ChangeEvent<HTMLTextAreaElement>, value: string) => {
if (value !== '') {
setIsSendButtonDisabled(false);
return;
}
setIsSendButtonDisabled(true);
};

return (
firstChatbot &&
secondChatbot && (
Expand Down Expand Up @@ -175,6 +185,8 @@ export const CompareLayout: React.FunctionComponent = () => {
hasStopButton={hasStopButton}
handleStopButton={handleStopButton}
alwayShowSendButton
onChange={handleChange}
isSendButtonDisabled={isSendButtonDisabled}
/>
<ChatbotFootnote label="Verify all information from this tool. LLMs make mistakes." />
</ChatbotFooter>
Expand Down

0 comments on commit 6357c68

Please sign in to comment.