import { SuggestionProps } from '@tiptap/suggestion' import clsx from 'clsx' import { Contract } from 'common/contract' import { forwardRef, useEffect, useImperativeHandle, useState } from 'react' import { contractPath } from 'web/lib/firebase/contracts' import { Avatar } from '../avatar' // copied from https://tiptap.dev/api/nodes/mention#usage const M = forwardRef((props: SuggestionProps, ref) => { const { items: contracts, command } = props const [selectedIndex, setSelectedIndex] = useState(0) useEffect(() => setSelectedIndex(0), [contracts]) const submitUser = (index: number) => { const contract = contracts[index] if (contract) command({ id: contract.id, label: contractPath(contract) } as any) } const onUp = () => setSelectedIndex((i) => (i + contracts.length - 1) % contracts.length) const onDown = () => setSelectedIndex((i) => (i + 1) % contracts.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 (
{!contracts.length ? ( No results... ) : ( contracts.map((contract, i) => ( )) )}
) }) // Just to keep the formatting pretty export { M as MentionList }