86 lines
3.5 KiB
TypeScript
86 lines
3.5 KiB
TypeScript
import { useTranslation } from "react-i18next";
|
|
import type { ChatMentionAutocompleteVM, ChatMentionSuggestionVM } from "@hivegrid/view-models";
|
|
|
|
import { Button } from "~/ui/components/Button";
|
|
import { ButtonGroup } from "~/ui/components/ButtonGroup";
|
|
import { Card } from "~/ui/components/Card";
|
|
import { EmptyState } from "~/ui/components/EmptyState";
|
|
import { LoadingIndicatorSkeleton } from "~/ui/components/LoadingIndicatorSkeleton";
|
|
|
|
export function ChatMentionAutocomplete({
|
|
vm,
|
|
open,
|
|
loading,
|
|
activeIndex,
|
|
listboxId,
|
|
actions,
|
|
}: {
|
|
readonly vm: ChatMentionAutocompleteVM | null;
|
|
readonly open: boolean;
|
|
readonly loading: boolean;
|
|
readonly activeIndex: number;
|
|
readonly listboxId: string;
|
|
readonly actions: {
|
|
readonly onSelect: (suggestion: ChatMentionSuggestionVM) => void;
|
|
readonly onActiveIndexChange: (index: number) => void;
|
|
};
|
|
}): React.JSX.Element | null {
|
|
const { t } = useTranslation();
|
|
if (!open) return null;
|
|
const items = vm?.groups.flatMap((group) => group.items) ?? [];
|
|
let itemOffset = 0;
|
|
|
|
return (
|
|
<Card size="small" contentOverflow="wrap" data-ui-id="ui.chat.mentionAutocomplete">
|
|
<span id={`${listboxId}-help`} className="sr-only">
|
|
{t("chat.mention.help")}
|
|
</span>
|
|
<div id={listboxId} role="listbox" aria-label={t("chat.mention.label")} aria-describedby={`${listboxId}-help`} aria-busy={loading}>
|
|
{loading ? (
|
|
<LoadingIndicatorSkeleton variant="list" aria-label={t("chat.mention.loading")} />
|
|
) : items.length === 0 ? (
|
|
<EmptyState outlined title={t("chat.mention.empty")} />
|
|
) : (
|
|
<ButtonGroup align="stack">
|
|
{vm?.groups.map((group) => {
|
|
const startIndex = itemOffset;
|
|
itemOffset += group.items.length;
|
|
return (
|
|
<div key={group.kind} role="group" aria-labelledby={`${listboxId}-${group.kind}`} className="w-full">
|
|
<span id={`${listboxId}-${group.kind}`} className="text-caption-bold font-caption-bold text-text-neutral-secondary">
|
|
{t(group.labelKey)}
|
|
</span>
|
|
<ButtonGroup align="stack">
|
|
{group.items.map((item, index) => {
|
|
const absoluteIndex = startIndex + index;
|
|
const selected = absoluteIndex === activeIndex;
|
|
return (
|
|
<Button
|
|
key={`${item.kind}:${item.id}`}
|
|
id={`${listboxId}-option-${absoluteIndex}`}
|
|
role="option"
|
|
tabIndex={-1}
|
|
aria-selected={selected}
|
|
variant={selected ? "secondary" : "ghost"}
|
|
onMouseEnter={() => actions.onActiveIndexChange(absoluteIndex)}
|
|
onMouseDown={(event) => event.preventDefault()}
|
|
onClick={() => actions.onSelect(item)}
|
|
>
|
|
<span className="flex min-w-0 flex-1 items-center justify-between gap-3 text-left">
|
|
<span className="truncate">{item.label}</span>
|
|
{item.detail ? <span className="text-text-neutral-secondary truncate">{item.detail}</span> : null}
|
|
</span>
|
|
</Button>
|
|
);
|
|
})}
|
|
</ButtonGroup>
|
|
</div>
|
|
);
|
|
})}
|
|
</ButtonGroup>
|
|
)}
|
|
</div>
|
|
</Card>
|
|
);
|
|
}
|