diff --git a/components/screens/HistoryStackScreen/HistoryScreen.tsx b/components/screens/HistoryStackScreen/HistoryScreen.tsx index 7e908da..3c2be06 100644 --- a/components/screens/HistoryStackScreen/HistoryScreen.tsx +++ b/components/screens/HistoryStackScreen/HistoryScreen.tsx @@ -123,30 +123,6 @@ const HistoryScreen = () => { ); } - if (flashcards.length === 0) { - return ( - - - {t('no_flashcards_added')} - - - - ); - } - const handleDeleteFlashcard = async (id: string) => { await deleteFlashcard(id); setFlashcards(prevFlashcards => prevFlashcards.filter(card => card.id !== id)); @@ -179,6 +155,11 @@ const HistoryScreen = () => { + {flashcards.length === 0 ? + + {t('no_flashcards_added')} + + : null} {sortedDates.map(date => ( {date}: diff --git a/components/screens/ReadStackScreen/ReadingScreen.tsx b/components/screens/ReadStackScreen/ReadingScreen.tsx index 3d22bf4..f4a9559 100644 --- a/components/screens/ReadStackScreen/ReadingScreen.tsx +++ b/components/screens/ReadStackScreen/ReadingScreen.tsx @@ -17,7 +17,8 @@ import { LanguageContext } from '../../../contexts/LanguageContext'; import { ActivityIndicator } from 'react-native-paper'; import { getWordTimeStamps } from '../../../services/whisper'; import { useTranslation } from 'react-i18next'; -import { useHardestWord } from '../../../services/chatGpt'; +import { translatePassage, useHardestWord } from '../../../services/chatGpt'; +import RomanizeButton from '../../RomanizeButton'; type ReadingScreenRouteProp = RouteProp; @@ -37,7 +38,11 @@ const ReadingScreen: React.FC = ({ route }) => { const [pausedToOpenDefinition, setPausedToOpenDefinition] = useState(false); const [flashingWordIndex, setFlashingWordIndex] = useState(null); - const { targetLanguage } = useContext(LanguageContext); + const [translationVisible, setTranslationVisible] = useState(false); + const [translationLoading, setTranslationLoading] = useState(false); + const [translation, setTranslation] = useState(); + + const { targetLanguage, nativeLanguage } = useContext(LanguageContext); const { pauseAudio, @@ -102,6 +107,24 @@ const ReadingScreen: React.FC = ({ route }) => { } }, [audioFile, reading, wordTimestamps, duration]); + useEffect(() => { + const fetchTranslation = async () => { + if (!reading?.passage) return + setTranslationLoading(true) + const translation = await translatePassage({ + passage: reading.passage, + language: targetLanguage, + translateTo: nativeLanguage, + }); + console.log('translation: ', translation) + setTranslation(translation ?? undefined); + setTranslationLoading(false) + }; + if (!translation) { + fetchTranslation(); + } + }, [reading?.passage]); + useEffect(() => { if (wordTimestamps) { const interval = setInterval(() => { @@ -177,7 +200,18 @@ const ReadingScreen: React.FC = ({ route }) => { return ( - {reading.title} + + + {reading.title} + + {translationVisible && translationLoading ? + + : setTranslationVisible(!translationVisible)}/> } + + { translationVisible ? + {translation} + : null} + {passage.split('\n').map((line, index) => ( = ({ }, [isFlashing]); const color = isHighlighted ? `text-[${theme.colors.tomato}]` : theme.classes.textPrimary; - const backgroundColor = flash ? 'bg-purple-300' : 'transparent'; + const backgroundColor = flash ? 'bg-purple-300' : ''; return ( diff --git a/services/chatGpt.ts b/services/chatGpt.ts index ade6ba6..5f293a4 100644 --- a/services/chatGpt.ts +++ b/services/chatGpt.ts @@ -295,3 +295,33 @@ export async function useHardestWord(passage: string) { throw error; } } + + +export async function translatePassage({ + passage, + language, + translateTo, +}: { + passage: string + language: LanguageCode + translateTo: LanguageCode +}) { + try { + const response = await openai.chat.completions.create({ + model: "gpt-4o-mini", + messages: [ + { + role: "user", + content: + `Translate the follow ${language} passage into ${translateTo}: \n\n${passage}. Only respond with the translated passage.` } + ], + temperature: 0.7, + n: 1 + }); + + return response.choices[0].message.content + } catch (error) { + console.error('Error getting hardest word:', error); + throw error; + } +}