import { SuggestionProps } from '@tiptap/suggestion' import clsx from 'clsx' import { User } from 'common/user' import { forwardRef, useEffect, useImperativeHandle, useState } from 'react' import { Avatar } from '../avatar' // copied from https://tiptap.dev/api/nodes/mention#usage export const MentionList = forwardRef((props: SuggestionProps, ref) => { const { items: users, command } = props const [selectedIndex, setSelectedIndex] = useState(0) useEffect(() => setSelectedIndex(0), [users]) const submitUser = (index: number) => { const user = users[index] if (user) command({ id: user.id, label: user.username } as any) } const onUp = () => setSelectedIndex((i) => (i + users.length - 1) % users.length) const onDown = () => setSelectedIndex((i) => (i + 1) % users.length) const onEnter = () => submitUser(selectedIndex) useImperativeHandle(ref, () => ({ onKeyDown: ({ event }: any) => { if (event.key === 'ArrowUp') { onUp() return true } if (event.key === 'ArrowDown') { onDown() return true } if (event.key === 'Enter') { onEnter() return true } return false }, })) return (
{!users.length ? ( No results... ) : ( users.map((user, i) => ( )) )}
) })