Bluesky app fork with some witchin' additions 💫

Prevent over-eager translate button (#10007)

authored by samuel.fm and committed by

GitHub 20bf2cd1 fe8e8ce7

+13 -11
+13 -11
src/components/Post/Translated/index.tsx
··· 5 5 6 6 import {HITSLOP_30} from '#/lib/constants' 7 7 import {useGoogleTranslate} from '#/lib/hooks/useGoogleTranslate' 8 - import {guessLanguage, useTranslate} from '#/lib/translation' 8 + import {useTranslate} from '#/lib/translation' 9 9 import {type TranslationFunction} from '#/lib/translation' 10 - import {codeToLanguageName, languageName} from '#/locale/helpers' 10 + import { 11 + codeToLanguageName, 12 + isPostInLanguage, 13 + languageName, 14 + } from '#/locale/helpers' 11 15 import {LANGUAGES} from '#/locale/languages' 12 16 import {useLanguagePrefs} from '#/state/preferences' 13 17 import {atoms as a, native, useTheme, web} from '#/alf' ··· 36 40 key: post.uri, 37 41 }) 38 42 39 - const postLanguage = useMemo(() => guessLanguage(postText), [postText]) 40 - const needsTranslation = postLanguage !== langPrefs.primaryLanguage 43 + const needsTranslation = useMemo(() => { 44 + if (hideTranslateLink) return false 45 + return !isPostInLanguage(post, [langPrefs.primaryLanguage]) 46 + }, [hideTranslateLink, post, langPrefs.primaryLanguage]) 41 47 42 48 switch (translationState.status) { 43 49 case 'loading': ··· 49 55 translate={translate} 50 56 postText={postText} 51 57 sourceLanguage={ 52 - translationState.sourceLanguage ?? postLanguage ?? null // Fallback primarily for iOS 58 + translationState.sourceLanguage ?? null // Fallback primarily for iOS 53 59 } 54 60 translatedText={translationState.translatedText} 55 61 /> ··· 65 71 ) 66 72 default: 67 73 return ( 68 - !hideTranslateLink && 69 74 needsTranslation && ( 70 75 <TranslationLink 71 76 postText={postText} 72 77 primaryLanguage={langPrefs.primaryLanguage} 73 - sourceLanguage={postLanguage} 74 78 translate={translate} 75 79 /> 76 80 ) ··· 96 100 function TranslationLink({ 97 101 postText, 98 102 primaryLanguage, 99 - sourceLanguage, 100 103 translate, 101 104 }: { 102 105 postText: string 103 106 primaryLanguage: string 104 - sourceLanguage: string | null 105 107 translate: TranslationFunction 106 108 }) { 107 109 const t = useTheme() ··· 115 117 }) 116 118 117 119 ax.metric('translate', { 118 - sourceLanguages: sourceLanguage ? [sourceLanguage] : [], 120 + sourceLanguages: [], // todo: get from post maybe? 119 121 targetLanguage: primaryLanguage, 120 122 textLength: postText.length, 121 123 }) 122 - }, [ax, postText, primaryLanguage, translate, sourceLanguage]) 124 + }, [ax, postText, primaryLanguage, translate]) 123 125 124 126 return ( 125 127 <View