import React, { useState, useEffect, useRef } from "react" export const WordChip = ({ word, logprob, threshold, replacements, onReplace }: { word: string, logprob: number, threshold: number, replacements: string[], onReplace: (newWord: string) => void }) => { const [isExpanded, setIsExpanded] = useState(false); const dropdownRef = useRef(null); useEffect(() => { const handleClickOutside = (event: MouseEvent) => { if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) { setIsExpanded(false); } }; document.addEventListener("mousedown", handleClickOutside); return () => { document.removeEventListener("mousedown", handleClickOutside); }; }, []); const handleClick = () => { if (logprob < threshold && replacements.length > 0) { setIsExpanded(true); } } const handleReplacement = (event: React.ChangeEvent) => { console.log("handleReplacement", event.target.value) const newWord = event.target.value onReplace(newWord) setIsExpanded(false); } return ( {word} {isExpanded && ( )} ) }