import { Toggle, Tooltip } from "@usememos/ui"; import { useEffect, useId, useRef, useState } from "react"; import { emojiName } from "@/lib/emoji"; import { cn } from "@/lib/utils"; import type { Memo } from "@/types/proto/api/memo_service_pb"; import { useReactionActions } from "./hooks"; interface Props { memo: Memo; reactionType: string; count: number; names: string[]; } const ReactionView = ({ memo, reactionType, count, names }: Props) => { const { hasReacted, handleReactionClick, isPending, canReact, canWithdraw } = useReactionActions({ memo }); const pressed = hasReacted(reactionType); const clickable = canReact || (canWithdraw && pressed); const [open, setOpen] = useState(false); const triggerId = useId(); const timer = useRef | undefined>(undefined); const longPressed = useRef(false); const clearTimer = () => clearTimeout(timer.current); useEffect(() => clearTimer, []); const label = ( {reactionType} {count} ); const accessibleName = `${emojiName(reactionType)}: ${count} ยท ${names.join(", ")}`; return ( { if (event.pointerType !== "touch") return; longPressed.current = false; clearTimer(); timer.current = setTimeout(() => { longPressed.current = true; setOpen(true); }, 500); }} onPointerUp={() => { clearTimer(); if (longPressed.current) timer.current = setTimeout(() => setOpen(false), 2000); }} onPointerCancel={() => { clearTimer(); longPressed.current = false; setOpen(false); }} onPointerLeave={() => { clearTimer(); if (longPressed.current) setOpen(false); }} onContextMenu={(event) => { if (longPressed.current) event.preventDefault(); }} onClickCapture={(event) => { if (longPressed.current) { event.preventDefault(); event.stopPropagation(); longPressed.current = false; } }} render={ clickable ? ( { handleReactionClick(reactionType); }} /> ) : ( ) } > {label}

{names.join(", ")}

); }; export default ReactionView;