import React, { useMemo, useRef, useImperativeHandle, forwardRef, useState } from 'react' import { useI18n } from '@/lang' import Menu, { type MenuType, type Position } from '@/components/common/Menu' export interface SelectInfo { listId: string name: string index: number } const initSelectInfo = {} export interface ListMenuProps { onPlay: (selectInfo: SelectInfo) => void onCollect: (selectInfo: SelectInfo) => void onHideMenu: () => void } export interface ListMenuType { show: (selectInfo: SelectInfo, position: Position) => void } export type { Position, } export default forwardRef((props, ref) => { const t = useI18n() const [visible, setVisible] = useState(false) const menuRef = useRef(null) const selectInfoRef = useRef(initSelectInfo as SelectInfo) useImperativeHandle(ref, () => ({ show(selectInfo, position) { selectInfoRef.current = selectInfo if (visible) menuRef.current?.show(position) else { setVisible(true) requestAnimationFrame(() => { menuRef.current?.show(position) }) } }, })) const menus = useMemo(() => { return [ { action: 'play', label: t('play') }, { action: 'collect', label: t('collect') }, ] as const }, [t]) const handleMenuPress = ({ action }: typeof menus[number]) => { const selectInfo = selectInfoRef.current switch (action) { case 'play': props.onPlay(selectInfo) break case 'collect': props.onCollect(selectInfo) break default: break } } return ( visible ? : null ) })