) => void;
/** The popover asks to close; a key event is present unless focus left the input. */
@@ -67,6 +69,7 @@ export default function CommandPopover({
activeIndex,
onActiveIndexChange,
isLoading = false,
+ notice,
onSelect,
onDismiss,
}: CommandPopoverProps): React.JSX.Element {
@@ -183,6 +186,11 @@ export default function CommandPopover({
)}
+ {open && notice != null && (
+
+ {notice}
+
+ )}
;
+ /** Localized placeholder for the search field. */
+ placeholder: string;
+ skills: CommandOption[];
+ /** True while any page of the catalog is still loading. */
+ isLoading: boolean;
+ isError: boolean;
+ notices: SkillsPopoverNotices;
+ /** A skill was picked; the popover has already closed and the draft keeps its text. */
+ onSelectSkill: (skill: CommandOption) => void;
+ /** Hides the popover. */
+ onClose: () => void;
+}
+
+/** The `$` popover: picks a skill to invoke on the next turn. Text typed after the `$` stays in
+ * the draft, and the pick is handed to the host rather than written into the text. */
+export default function SkillsPopover({
+ textAreaRef,
+ placeholder,
+ skills,
+ isLoading,
+ isError,
+ notices,
+ onSelectSkill,
+ onClose,
+}: SkillsPopoverProps): React.JSX.Element {
+ const [activeIndex, setActiveIndex] = useState(0);
+ const inputRef = useRef(null);
+
+ const { open, setOpen, searchValue, setSearchValue, matches } = useCombobox({
+ value: '',
+ options: skills,
+ });
+
+ const initInputRef = useInitPopoverInput({
+ inputRef,
+ textAreaRef,
+ commandChar: COMMAND_CHAR,
+ preserveTextAfterCursor: true,
+ setSearchValue,
+ setOpen,
+ });
+
+ useEffect(() => {
+ if (!open) {
+ setActiveIndex(0);
+ }
+ }, [open]);
+
+ const handleSelect = (skill: CommandOption, e?: KeyboardEvent) => {
+ e?.preventDefault();
+ setSearchValue('');
+ setOpen(false);
+ onClose();
+ onSelectSkill(skill);
+ textAreaRef.current?.focus();
+ };
+
+ const handleDismiss = (reason: CommandDismissReason, e?: KeyboardEvent) => {
+ if (reason === 'commit' && e?.key === 'Enter') {
+ e.preventDefault();
+ }
+ setOpen(false);
+ onClose();
+ if (reason !== 'blur') {
+ textAreaRef.current?.focus();
+ }
+ };
+
+ const notice = (() => {
+ if (isError) {
+ return notices.error;
+ }
+ if (isLoading || matches.length > 0) {
+ return undefined;
+ }
+ return searchValue ? notices.noMatch : notices.empty;
+ })();
+
+ return (
+ setOpen(true)}
+ matches={matches as CommandOption[]}
+ activeIndex={activeIndex}
+ onActiveIndexChange={setActiveIndex}
+ isLoading={isLoading}
+ notice={notice}
+ onSelect={handleSelect}
+ onDismiss={handleDismiss}
+ />
+ );
+}
diff --git a/packages/chat/src/components/composer/popover/__tests__/Skills.spec.tsx b/packages/chat/src/components/composer/popover/__tests__/Skills.spec.tsx
new file mode 100644
index 0000000000..a58e1a9364
--- /dev/null
+++ b/packages/chat/src/components/composer/popover/__tests__/Skills.spec.tsx
@@ -0,0 +1,101 @@
+/**
+ * @jest-environment jsdom
+ */
+import React from 'react';
+import { fireEvent, render, screen } from '@testing-library/react';
+import type { SkillsPopoverProps } from '../Skills';
+import type { CommandOption } from '../Command';
+import SkillsPopover from '../Skills';
+
+/**
+ * The `$` popover's contract: text after the `$` stays in the draft, a pick goes to the host and
+ * never into the text, Enter and Tab on a skill are consumed, and an empty, unmatched or failed
+ * list says so in a status message.
+ */
+
+const skills: CommandOption[] = [
+ { type: 'skill', value: 'brand-guidelines', label: 'Brand Guidelines' },
+ { type: 'skill', value: 'style-guide', label: 'Style Guide' },
+];
+
+const notices = { error: 'Load failed', empty: 'No skills yet', noMatch: 'No match' };
+
+beforeAll(() => {
+ Element.prototype.scrollIntoView = jest.fn();
+});
+
+beforeEach(() => {
+ document.body.innerHTML = '';
+});
+
+const renderSkills = (props: Partial = {}, draft = '$') => {
+ const textarea = document.createElement('textarea');
+ textarea.value = draft;
+ document.body.appendChild(textarea);
+ textarea.setSelectionRange(1, 1);
+ const onClose = jest.fn();
+ const onSelectSkill = jest.fn();
+ render(
+ ,
+ );
+ return { textarea, onClose, onSelectSkill };
+};
+
+const input = () => screen.getByRole('combobox');
+
+describe('SkillsPopover', () => {
+ it('keeps the text after the cursor in the draft and hands the pick to the host', () => {
+ const { textarea, onClose, onSelectSkill } = renderSkills({}, '$ please review');
+
+ expect(textarea.value).toBe(' please review');
+ fireEvent.keyDown(input(), { key: 'ArrowDown' });
+ expect(fireEvent.keyDown(input(), { key: 'Tab' })).toBe(false);
+
+ expect(onClose).toHaveBeenCalled();
+ expect(onSelectSkill).toHaveBeenCalledWith(skills[1]);
+ expect(textarea.value).toBe(' please review');
+ expect(document.activeElement).toBe(textarea);
+ });
+
+ it('lets Tab move on but consumes Enter when nothing matches', () => {
+ renderSkills();
+ fireEvent.change(input(), { target: { value: 'zzz' } });
+
+ expect(screen.getByRole('status')).toHaveTextContent('No match');
+ expect(fireEvent.keyDown(input(), { key: 'Tab' })).toBe(true);
+ });
+
+ it('consumes Enter when nothing matches and closes', () => {
+ const { onClose } = renderSkills();
+ fireEvent.change(input(), { target: { value: 'zzz' } });
+
+ expect(fireEvent.keyDown(input(), { key: 'Enter' })).toBe(false);
+ expect(onClose).toHaveBeenCalled();
+ });
+
+ it('says when there are no skills', () => {
+ renderSkills({ skills: [] });
+ expect(screen.getByRole('status')).toHaveTextContent('No skills yet');
+ });
+
+ it('says when the skills failed to load, keeping any already listed', () => {
+ renderSkills({ isError: true });
+ expect(screen.getByRole('status')).toHaveTextContent('Load failed');
+ expect(screen.getAllByRole('option')).toHaveLength(2);
+ });
+
+ it('shows no notice while the catalog is still loading', () => {
+ renderSkills({ skills: [], isLoading: true });
+ expect(screen.queryByRole('status')).toBeNull();
+ });
+});