import React, { useCallback, useImperativeHandle, useRef, useState } from 'react' import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' import { faHighlighter } from '@fortawesome/free-solid-svg-icons' interface HighlightButtonProps { onHighlight: (text: string) => void highlightColor?: string } export interface HighlightButtonRef { updateSelection: (text: string) => void clearSelection: () => void } export const HighlightButton = React.forwardRef( ({ onHighlight, highlightColor = '#ffff00' }, ref) => { const currentSelectionRef = useRef('') const [hasSelection, setHasSelection] = useState(false) const handleClick = useCallback( (e: React.MouseEvent) => { e.preventDefault() e.stopPropagation() if (currentSelectionRef.current) { onHighlight(currentSelectionRef.current) } }, [onHighlight] ) // Expose methods to update selection useImperativeHandle(ref, () => ({ updateSelection: (text: string) => { currentSelectionRef.current = text setHasSelection(!!text) }, clearSelection: () => { currentSelectionRef.current = '' setHasSelection(false) } })) return ( ) } ) HighlightButton.displayName = 'HighlightButton'