Bluesky app fork with some witchin' additions 馃挮
at main 54 lines 2.0 kB view raw
1import {useCallback} from 'react' 2import * as IntentLauncher from 'expo-intent-launcher' 3 4import {getTranslatorLink} from '#/locale/helpers' 5import {IS_ANDROID} from '#/env' 6import {useOpenLink} from './useOpenLink' 7 8export function useTranslate() { 9 const openLink = useOpenLink() 10 11 return useCallback( 12 async (text: string, language: string) => { 13 const translateUrl = getTranslatorLink(text, language) 14 if (IS_ANDROID) { 15 try { 16 // use getApplicationIconAsync to determine if the translate app is installed 17 if ( 18 !(await IntentLauncher.getApplicationIconAsync( 19 'com.google.android.apps.translate', 20 )) 21 ) { 22 throw new Error('Translate app not installed') 23 } 24 25 // TODO: this should only be called one at a time, use something like 26 // RQ's `scope` - otherwise can trigger the browser to open unexpectedly when the call throws -sfn 27 await IntentLauncher.startActivityAsync( 28 'android.intent.action.PROCESS_TEXT', 29 { 30 type: 'text/plain', 31 extra: { 32 'android.intent.extra.PROCESS_TEXT': text, 33 'android.intent.extra.PROCESS_TEXT_READONLY': true, 34 }, 35 // note: to skip the intermediate app select, we need to specify a 36 // `className`. however, this isn't safe to hardcode, we'd need to query the 37 // package manager for the correct activity. this requires native code, so 38 // skip for now -sfn 39 // packageName: 'com.google.android.apps.translate', 40 // className: 'com.google.android.apps.translate.TranslateActivity', 41 }, 42 ) 43 } catch (err) { 44 if (__DEV__) console.error(err) 45 // most likely means they don't have the translate app 46 await openLink(translateUrl) 47 } 48 } else { 49 await openLink(translateUrl) 50 } 51 }, 52 [openLink], 53 ) 54}