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