import React, { useEffect, useState } from 'react'; import Popup from '@/components/Popup'; import { Position } from '@/utils/sel'; import { getAPIBaseUrl } from '@/services/environment'; import { useSettingsStore } from '@/store/settingsStore'; import { useTranslation } from '@/hooks/useTranslation'; import { useAuth } from '@/context/AuthContext'; const LANGUAGES = { AUTO: 'Auto Detect', EN: 'English', DE: 'German', FR: 'French', ES: 'Spanish', IT: 'Italian', EL: 'Greek', PT: 'Portuguese', NL: 'Dutch', PL: 'Polish', UK: 'Ukrainian', RU: 'Russian', AR: 'Arabic', TR: 'Turkish', HI: 'Hindi', ID: 'Indonesian', VI: 'Vietnamese', KO: 'Korean', JA: 'Japanese', 'ZH-HANS': 'Chinese (Simplified)', 'ZH-HANT': 'Chinese (Traditional)', }; const DEEPL_API_ENDPOINT = getAPIBaseUrl() + '/deepl/translate'; interface DeepLPopupProps { text: string; position: Position; trianglePosition: Position; popupWidth: number; popupHeight: number; } const DeepLPopup: React.FC = ({ text, position, trianglePosition, popupWidth, popupHeight, }) => { const _ = useTranslation(); const { token } = useAuth(); const { settings, setSettings } = useSettingsStore(); const [sourceLang, setSourceLang] = useState('AUTO'); const [targetLang, setTargetLang] = useState(settings.globalReadSettings.translateTargetLang); const [translation, setTranslation] = useState(null); const [detectedSourceLang, setDetectedSourceLang] = useState(null); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const handleSourceLangChange = (event: React.ChangeEvent) => { setSourceLang(event.target.value); }; const handleTargetLangChange = (event: React.ChangeEvent) => { settings.globalReadSettings.translateTargetLang = event.target.value; setSettings(settings); setTargetLang(event.target.value); }; useEffect(() => { const fetchTranslation = async () => { setLoading(true); setError(null); setTranslation(null); try { const response = await fetch(DEEPL_API_ENDPOINT, { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token ?? ''}`, }, body: JSON.stringify({ text: [text], target_lang: targetLang.toUpperCase(), source_lang: sourceLang === 'AUTO' ? undefined : sourceLang.toUpperCase(), }), }); if (!response.ok) { throw new Error('Failed to fetch translation'); } const data = await response.json(); const translatedText = data.translations[0]?.text; const detectedSource = data.translations[0]?.detected_source_language; if (!translatedText) { throw new Error('No translation found'); } if (sourceLang === 'AUTO' && detectedSource) { setDetectedSourceLang(detectedSource); } setTranslation(translatedText); } catch (err) { console.error(err); setError(_('Unable to fetch the translation. Try again later.')); } finally { setLoading(false); } }; fetchTranslation(); // eslint-disable-next-line react-hooks/exhaustive-deps }, [text, token, sourceLang, targetLang]); return (

{_('Original Text')}

{text}

{_('Translated Text')}

{loading ? (

{_('Loading...')}

) : error ? (

{error}

) : (

{translation || 'No translation available.'}

Translated by DeepL.
)}
); }; export default DeepLPopup;