import * as Avatar from "@radix-ui/react-avatar"; import { minidenticon } from "minidenticons"; import { Ref, forwardRef, useEffect, useImperativeHandle, useState, } from "react"; import { NDKCacheUserProfile } from "@lume/types"; import { cn } from "@lume/utils"; import { useTranslation } from "react-i18next"; type MentionListRef = { onKeyDown: (props: { event: Event }) => boolean; }; const List = ( props: { items: NDKCacheUserProfile[]; command: (arg0: { id: string }) => void; }, ref: Ref, ) => { const [t] = useTranslation(); const [selectedIndex, setSelectedIndex] = useState(0); const selectItem = (index) => { const item = props.items[index]; if (item) { props.command({ id: item.pubkey }); } }; const upHandler = () => { setSelectedIndex( (selectedIndex + props.items.length - 1) % props.items.length, ); }; const downHandler = () => { setSelectedIndex((selectedIndex + 1) % props.items.length); }; const enterHandler = () => { selectItem(selectedIndex); }; useEffect(() => setSelectedIndex(0), [props.items]); useImperativeHandle(ref, () => ({ onKeyDown: ({ event }) => { if (event.key === "ArrowUp") { upHandler(); return true; } if (event.key === "ArrowDown") { downHandler(); return true; } if (event.key === "Enter") { enterHandler(); return true; } return false; }, })); return (
{props.items.length ? ( props.items.map((item, index) => ( )) ) : (
{t("global.noResult")}
)}
); }; export const MentionList = forwardRef(List);