import { SuggestionProps } from '@tiptap/suggestion' import clsx from 'clsx' import { Contract } from 'common/contract' import { FIXED_ANTE } from 'common/economy' import { formatMoney } from 'common/util/format' import { forwardRef, useEffect, useImperativeHandle, useState } from 'react' import { useUser } from 'web/hooks/use-user' import { contractPath } from 'web/lib/firebase/contracts' import { Avatar } from '../avatar' import { Col } from '../layout/col' import { Row } from '../layout/row' // copied from https://tiptap.dev/api/nodes/mention#usage const M = forwardRef((props: SuggestionProps, ref) => { const { items: contracts, command, query } = props const user = useUser() 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 createOptions = [ { text: `Create yes/no`, // TODO: Get these commmands to work onClick: () => command({ id: 'new', label: 'new' } as any), }, { text: `Create free response`, onClick: () => command({ id: 'new', label: 'new' } as any), }, ] const choiceLength = contracts.length > 0 ? contracts.length : createOptions.length const onUp = () => setSelectedIndex((i) => (i + choiceLength - 1) % choiceLength) const onDown = () => setSelectedIndex((i) => (i + 1) % choiceLength) 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 ? (
{query}
{createOptions.map((option, i) => ( ))} ) : ( contracts.map((contract, i) => ( )) )}
) }) // Just to keep the formatting pretty export { M as MentionList }