Bluesky app fork with some witchin' additions 💫 witchsky.app
bluesky fork client

Fix crash with translate link on web (#9972)

authored by

DS Boyce and committed by
GitHub
8e2a5abf 6cdc1fe2

+21 -53
+21 -10
src/translation/index.tsx
··· 14 14 import {logger} from '#/logger' 15 15 import {useLanguagePrefs} from '#/state/preferences' 16 16 import {useAnalytics} from '#/analytics' 17 + import {IS_WEB} from '#/env' 17 18 18 19 type TranslationState = 19 20 | {status: 'idle'} ··· 119 120 return context 120 121 } 121 122 122 - export function Provider({children}: {children?: React.ReactNode}) { 123 + export function Provider({children}: React.PropsWithChildren<unknown>) { 123 124 const [translationState, setTranslationState] = 124 125 useState<TranslationState>(IDLE) 125 126 const openLink = useOpenLink() ··· 158 159 targetLanguage: result.targetLanguage, 159 160 }) 160 161 } catch (e) { 161 - logger.error('Failed to translate post on device', {safeMessage: e}) 162 - // On-device translation failed (language pack missing or user dismissed 163 - // the download prompt). Fall back to Google Translate. 164 - ax.metric('translate:result', { 165 - method: 'fallback-alert', 166 - os: Platform.OS, 167 - sourceLanguage: sourceLangCode ?? null, 168 - targetLanguage: targetLangCode, 169 - }) 162 + if (IS_WEB) { 163 + // Web always opens Google Translate. 164 + ax.metric('translate:result', { 165 + method: 'google-translate', 166 + os: Platform.OS, 167 + sourceLanguage: sourceLangCode ?? null, 168 + targetLanguage: targetLangCode, 169 + }) 170 + } else { 171 + logger.error('Failed to translate post on device', {safeMessage: e}) 172 + // On-device translation failed (language pack missing or user dismissed 173 + // the download prompt). Fall back to Google Translate. 174 + ax.metric('translate:result', { 175 + method: 'fallback-alert', 176 + os: Platform.OS, 177 + sourceLanguage: sourceLangCode ?? null, 178 + targetLanguage: targetLangCode, 179 + }) 180 + } 170 181 setTranslationState({status: 'idle'}) 171 182 const translateUrl = getTranslatorLink( 172 183 text,
-43
src/translation/index.web.tsx
··· 1 - import {useCallback} from 'react' 2 - import {Platform} from 'react-native' 3 - 4 - import {useOpenLink} from '#/lib/hooks/useOpenLink' 5 - import {getTranslatorLink} from '#/locale/helpers' 6 - import {useLanguagePrefs} from '#/state/preferences' 7 - import {useAnalytics} from '#/analytics' 8 - 9 - const translationState = {status: 'idle'} // No on-device translations for web. 10 - 11 - const clearTranslation = () => {} // no-op on web 12 - 13 - /** 14 - * Web always opens Google Translate. 15 - */ 16 - export function useTranslateOnDevice() { 17 - const openLink = useOpenLink() 18 - const ax = useAnalytics() 19 - const {primaryLanguage} = useLanguagePrefs() 20 - 21 - const translate = useCallback( 22 - async ( 23 - text: string, 24 - targetLangCode: string = primaryLanguage, 25 - sourceLangCode: string, 26 - ) => { 27 - const translateUrl = getTranslatorLink( 28 - text, 29 - targetLangCode, 30 - sourceLangCode, 31 - ) 32 - ax.metric('translate:result', { 33 - method: 'google-translate', 34 - os: Platform.OS, 35 - sourceLanguage: sourceLangCode ?? null, 36 - targetLanguage: targetLangCode, 37 - }) 38 - await openLink(translateUrl) 39 - }, 40 - [ax, openLink, primaryLanguage], 41 - ) 42 - return {clearTranslation, translate, translationState} 43 - }