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