import React, { useState, useEffect, useRef } from "react" import { Replacement } from "../interfaces"; interface WordChipProps { word: string; logprob: number; threshold: number; replacements: Replacement[]; onReplace: (newWord: string) => Promise; } export function WordChip({ word, logprob, threshold, replacements, onReplace }: WordChipProps) { const [isExpanded, setIsExpanded] = useState(false); const [selectedIndex, setSelectedIndex] = useState(-1); 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 = async (newWord: string) => { console.log("handleReplacement", newWord); await onReplace(newWord); setIsExpanded(false); // Close the dropdown }; console.log(`word: ->${word}<-`); let w1; let w2; let w3; // if word contains a newline, render a
if (word.includes("\n")) { [w1, w3] = word.split("\n"); w2 = "\n"; console.log(`split: ${w1} | ${w2} | ${w3}`); } else { w1 = word; w2 = ""; w3 = ""; } // sort replacements by logprob (make sure not to mutate the original array) const sortedReplacements = [...replacements].sort((a, b) => b.logprob - a.logprob) // convert logprobs to probabilities const withProbabilities = sortedReplacements.map(r => ({ ...r, probability: Math.exp(r.logprob)*100 })) return ( {w1} {w2 &&
} {w3} {isExpanded && (
{withProbabilities.map((option, index) => (
handleReplacement(option.text)} onMouseEnter={() => setSelectedIndex(index)} style={{ padding: "5px 10px", cursor: "pointer", color: "black", backgroundColor: selectedIndex === index ? "#f0f0f0" : "white", whiteSpace: "nowrap" }} > {option.text} {option.probability.toFixed(1)}%
))}
)}
) }