import clsx from 'clsx'; import React, { useState, useRef, useEffect } from 'react'; import { useEnv } from '@/context/EnvContext'; import { useThemeStore } from '@/store/themeStore'; import { useReaderStore } from '@/store/readerStore'; import { useTranslation } from '@/hooks/useTranslation'; import { useResponsiveSize } from '@/hooks/useResponsiveSize'; import { useTTSControl } from '@/app/reader/hooks/useTTSControl'; import { getPopupPosition, Position } from '@/utils/sel'; import { Insets } from '@/types/misc'; import { Overlay } from '@/components/Overlay'; import Popup from '@/components/Popup'; import TTSPanel from './TTSPanel'; import TTSIcon from './TTSIcon'; import TTSBar from './TTSBar'; const POPUP_WIDTH = 282; const POPUP_HEIGHT = 160; const POPUP_PADDING = 10; interface TTSControlProps { bookKey: string; gridInsets: Insets; } const TTSControl: React.FC = ({ bookKey, gridInsets }) => { const _ = useTranslation(); const { appService } = useEnv(); const { safeAreaInsets } = useThemeStore(); const { hoveredBookKey, getViewSettings } = useReaderStore(); const viewSettings = getViewSettings(bookKey); const [showPanel, setShowPanel] = useState(false); const [panelPosition, setPanelPosition] = useState(); const [trianglePosition, setTrianglePosition] = useState(); const iconRef = useRef(null); const hoverTimeoutRef = useRef | null>(null); const backButtonTimeoutRef = useRef | null>(null); const [showIndicatorWithinTimeout, setShowIndicatorWithinTimeout] = useState(true); const [shouldMountBackButton, setShouldMountBackButton] = useState(false); const [isBackButtonVisible, setIsBackButtonVisible] = useState(false); const popupPadding = useResponsiveSize(POPUP_PADDING); const maxWidth = window.innerWidth - 2 * popupPadding; const popupWidth = Math.min(maxWidth, useResponsiveSize(POPUP_WIDTH)); const popupHeight = useResponsiveSize(POPUP_HEIGHT); const tts = useTTSControl({ bookKey, onRequestHidePanel: () => setShowPanel(false), }); useEffect(() => { if (tts.showBackToCurrentTTSLocation) { setShouldMountBackButton(true); const fadeInTimeout = setTimeout(() => { setIsBackButtonVisible(true); }, 10); return () => clearTimeout(fadeInTimeout); } else { setIsBackButtonVisible(false); if (backButtonTimeoutRef.current) { clearTimeout(backButtonTimeoutRef.current); } backButtonTimeoutRef.current = setTimeout(() => { setShouldMountBackButton(false); }, 300); return; } }, [tts.showBackToCurrentTTSLocation]); useEffect(() => { if (!iconRef.current || !showPanel) return; const parentElement = iconRef.current.parentElement; if (!parentElement) return; const resizeObserver = new ResizeObserver(() => { updatePanelPosition(); }); resizeObserver.observe(parentElement); return () => { resizeObserver.disconnect(); }; // eslint-disable-next-line react-hooks/exhaustive-deps }, [showPanel]); useEffect(() => { if (hoveredBookKey) { if (hoverTimeoutRef.current) { clearTimeout(hoverTimeoutRef.current); hoverTimeoutRef.current = null; } const showTimeout = setTimeout(() => { setShowIndicatorWithinTimeout(true); }, 100); hoverTimeoutRef.current = showTimeout; } else { if (hoverTimeoutRef.current) { clearTimeout(hoverTimeoutRef.current); hoverTimeoutRef.current = null; } const hideTimeout = setTimeout(() => { setShowIndicatorWithinTimeout(false); }, 5000); hoverTimeoutRef.current = hideTimeout; } return () => { if (hoverTimeoutRef.current) { clearTimeout(hoverTimeoutRef.current); } }; }, [hoveredBookKey]); useEffect(() => { if (tts.showTTSBar) { setShowPanel(false); } }, [tts.showTTSBar]); const updatePanelPosition = () => { if (iconRef.current) { const rect = iconRef.current.getBoundingClientRect(); const parentRect = iconRef.current.parentElement?.getBoundingClientRect() || document.documentElement.getBoundingClientRect(); const trianglePos = { dir: 'up', point: { x: rect.left + rect.width / 2 - parentRect.left, y: rect.top - 12 }, } as Position; const popupPos = getPopupPosition( trianglePos, parentRect, popupWidth, popupHeight, popupPadding, ); setPanelPosition(popupPos); setTrianglePosition(trianglePos); } }; const togglePopup = () => { updatePanelPosition(); if (!showPanel && tts.isTTSActive) { tts.refreshTtsLang(); } setShowPanel((prev) => !prev); }; const handleDismissPopup = () => { setShowPanel(false); }; return ( <> {shouldMountBackButton && (
)} {showPanel && } {(showPanel || (tts.showIndicator && showIndicatorWithinTimeout)) && (
)} {showPanel && panelPosition && trianglePosition && tts.ttsClientsInited && ( )} {tts.showIndicator && tts.showTTSBar && tts.ttsClientsInited && ( )} ); }; export default TTSControl;