import { type FunctionComponent, memo, useCallback, useEffect, useRef } from 'react'; import { useDispatch } from 'react-redux'; import { Button } from '@/components/Button'; import { Dictionary } from '@/components/Dictionary'; import { Modal } from '@/components/Modal'; import { useIsCompactLayout } from '@/hooks/useIsCompactLayout'; import EyeFill from '@/icons/EyeFill.svg'; import { hoveredWordSlice, selectHoveredWord, useTranslate, useTypedSelector } from '@/state'; import { WordsTable } from './components'; import styles from './WordsModal.module.scss'; interface Props { className?: string; isOpen: boolean; onClose: () => void; onPreview: () => void; } const WordsModalBase: FunctionComponent = ({ className, isOpen, onClose, onPreview }) => { const dispatch = useDispatch(); const translate = useTranslate(); const hoveredWord = useTypedSelector(selectHoveredWord); const showsPreviewButton = useIsCompactLayout(); const keepsHighlightOnClose = useRef(false); const handlePreview = useCallback(() => { keepsHighlightOnClose.current = true; onPreview(); }, [onPreview]); useEffect(() => { if (!isOpen) { return; } return () => { if (!keepsHighlightOnClose.current) { dispatch(hoveredWordSlice.actions.clear()); } keepsHighlightOnClose.current = false; }; }, [dispatch, isOpen]); return ( {translate('words.preview')} ) } isOpen={isOpen} title={translate('words')} onClose={onClose} >
); }; export const WordsModal = memo(WordsModalBase);