Skip to content

Commit

Permalink
fix: disable top input when no access
Browse files Browse the repository at this point in the history
Signed-off-by: Lin Wang <[email protected]>
  • Loading branch information
wanglam committed Dec 27, 2023
1 parent 510b685 commit 810d5fe
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
35 changes: 35 additions & 0 deletions public/chat_header_button.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,39 @@ describe('<HeaderChatButton />', () => {
});
expect(screen.getByLabelText('chat input')).toHaveFocus();
});

it('should disable chat input when no access', () => {
render(
<HeaderChatButton
application={applicationServiceMock.createStartContract()}
userHasAccess={false}
contentRenderers={{}}
actionExecutors={{}}
assistantActions={{} as AssistantActions}
currentAccount={{ username: 'test_user', tenant: 'test_tenant' }}
/>
);
expect(screen.getByLabelText('chat input')).toBeDisabled();
});

it('should not focus on chat input when no access and pressing global shortcut', () => {
render(
<HeaderChatButton
application={applicationServiceMock.createStartContract()}
userHasAccess={false}
contentRenderers={{}}
actionExecutors={{}}
assistantActions={{} as AssistantActions}
currentAccount={{ username: 'test_user', tenant: 'test_tenant' }}
/>
);
expect(screen.getByLabelText('chat input')).not.toHaveFocus();
fireEvent.keyDown(document.body, {
key: '/',
code: 'NumpadDivide',
charCode: 111,
metaKey: true,
});
expect(screen.getByLabelText('chat input')).not.toHaveFocus();
});
});
6 changes: 5 additions & 1 deletion public/chat_header_button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ export const HeaderChatButton = (props: HeaderChatButtonProps) => {
};

useEffect(() => {
if (!props.userHasAccess) {
return;
}
const onGlobalMouseUp = (e: KeyboardEvent) => {
if (e.metaKey && e.key === '/') {
inputRef.current?.focus();
Expand All @@ -134,7 +137,7 @@ export const HeaderChatButton = (props: HeaderChatButtonProps) => {
return () => {
document.removeEventListener('keydown', onGlobalMouseUp);
};
}, []);
}, [props.userHasAccess]);

return (
<>
Expand Down Expand Up @@ -179,6 +182,7 @@ export const HeaderChatButton = (props: HeaderChatButtonProps) => {
)}
</span>
}
disabled={!props.userHasAccess}
/>
</div>
<ChatContext.Provider value={chatContextValue}>
Expand Down

0 comments on commit 810d5fe

Please sign in to comment.