import {
ChangeEvent,
KeyboardEvent,
RefObject,
useEffect,
useRef,
useState,
} from 'react';
import { Composer } from '../Composer/Composer';
import { IconButton } from '../IconButton/IconButton';
import {
AgentTreeAgent,
AgentTreeLabelStrings,
fill,
} from './agent-tree-utils';
export interface AgentTreeMessageProps {
/**
* The agent the message is for.
*/
agent: AgentTreeAgent;
/**
* Id of the composer field.
*/
id: string;
/**
* Translatable strings.
*/
labelStrings: AgentTreeLabelStrings;
/**
* Called when the bar closes. `returnFocus` is `true` when the person closed
* it from the keyboard or by sending, so focus goes back to the button that
* opened it; it is `false` for a click elsewhere.
*/
onClose: (returnFocus: boolean) => void;
/**
* Called with the message when the person sends it.
*/
onSend: (text: string) => void;
/**
* The button that opened the bar. A press on it does not count as a press
* outside, so it can toggle the bar.
*/
triggerRef: RefObject<HTMLElement | null>;
}
/**
* A floating bar for telling one agent something: the compact Composer in a
* small surface over the middle of the screen, in the top layer so nothing
* clips it. Enter or the Send button sends, Escape closes.
*/
export const AgentTreeMessage = ({
agent,
id,
labelStrings,
onClose,
onSend,
triggerRef,
}: AgentTreeMessageProps) => {
const baseClass = 'pds-agent-tree__message';
const barRef = useRef<HTMLDivElement>(null);
const [text, setText] = useState('');
const close = useRef(onClose);
close.current = onClose;
useEffect(() => {
// The field can only take focus once the popover is showing, so autoFocus
// on the textarea would be too early.
barRef.current?.showPopover?.();
barRef.current?.querySelector('textarea')?.focus();
const away = (event: PointerEvent) => {
const target = event.target as Node;
if (
barRef.current?.contains(target) ||
triggerRef.current?.contains(target)
) {
return;
}
close.current(false);
};
document.addEventListener('pointerdown', away);
return () => document.removeEventListener('pointerdown', away);
}, [triggerRef]);
const send = () => {
const message = text.trim();
if (!message) return;
onSend(message);
close.current(true);
};
const onKeyDown = (event: KeyboardEvent<HTMLTextAreaElement>) => {
if (event.key === 'Enter' && !event.shiftKey) {
event.preventDefault();
send();
}
};
return (
<div
aria-label={fill(labelStrings.messageLabel, { name: agent.name })}
className={baseClass}
onKeyDown={(event) => {
if (event.key === 'Escape') {
event.stopPropagation();
close.current(true);
}
}}
popover='manual'
ref={barRef}
role='dialog'
>
<Composer
actionsEnd={
<IconButton
ariaLabel={labelStrings.send}
disabled={!text.trim()}
iconName='paperPlane'
onClick={send}
variant='primary'
/>
}
id={id}
label={fill(labelStrings.messageLabel, { name: agent.name })}
onChange={(event: ChangeEvent<HTMLTextAreaElement>) =>
setText(event.target.value)
}
placeholder={fill(labelStrings.messagePlaceholder, {
name: agent.name,
})}
textareaProps={{ onKeyDown }}
value={text}
variant='compact'
/>
</div>
);
};
Context: AgentTree
A tree of agent cards: the lead on top, its sub-agents side by side below, their helpers under them, joined by thin connectors.
Net-new componentOpen in Storybook