From b59c8e22af0d92988d2145ea28299230f3b71180 Mon Sep 17 00:00:00 2001 From: Samuel Newman Date: Wed, 29 May 2024 07:08:46 +0300 Subject: Native translation expo module (#4098) * translation expo module * add `onClose` and `onReplacementAction` * rm onReplacementAction * make all props published * make translation api available globally w/o wrapper (#4110) * conditionally import the translation module * only use native translation if language is probably supported * open native translation via dropdown menu --------- Co-authored-by: Hailey Co-authored-by: Dan Abramov --- src/view/com/post-thread/PostThreadItem.tsx | 27 +++++++++++++++++++++++---- src/view/com/util/forms/PostDropdownBtn.tsx | 21 +++++++++++++++++---- src/view/shell/index.tsx | 2 ++ 3 files changed, 42 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/view/com/post-thread/PostThreadItem.tsx b/src/view/com/post-thread/PostThreadItem.tsx index c44875b37..548b73af6 100644 --- a/src/view/com/post-thread/PostThreadItem.tsx +++ b/src/view/com/post-thread/PostThreadItem.tsx @@ -30,6 +30,11 @@ import {useSession} from 'state/session' import {PostThreadFollowBtn} from 'view/com/post-thread/PostThreadFollowBtn' import {atoms as a} from '#/alf' import {RichText} from '#/components/RichText' +import { + isAvailable as isNativeTranslationAvailable, + isLanguageSupported, + NativeTranslationModule, +} from '../../../../modules/expo-bluesky-translate' import {ContentHider} from '../../../components/moderation/ContentHider' import {LabelsOnMyPost} from '../../../components/moderation/LabelsOnMe' import {PostAlerts} from '../../../components/moderation/PostAlerts' @@ -317,6 +322,7 @@ let PostThreadItemLoaded = ({ @@ -620,26 +626,39 @@ function PostOuterWrapper({ function ExpandedPostDetails({ post, + record, needsTranslation, translatorUrl, }: { post: AppBskyFeedDefs.PostView + record?: AppBskyFeedPost.Record needsTranslation: boolean translatorUrl: string }) { const pal = usePalette('default') const {_} = useLingui() const openLink = useOpenLink() - const onTranslatePress = React.useCallback( - () => openLink(translatorUrl), - [openLink, translatorUrl], - ) + + const text = record?.text || '' + + const onTranslatePress = React.useCallback(() => { + if ( + isNativeTranslationAvailable && + isLanguageSupported(record?.langs?.at(0)) + ) { + NativeTranslationModule.presentAsync(text) + } else { + openLink(translatorUrl) + } + }, [openLink, text, translatorUrl, record]) + return ( {niceDate(post.indexedAt)} {needsTranslation && ( <> · + { - openLink(translatorUrl) - }, [openLink, translatorUrl]) + const onPressTranslate = React.useCallback(() => { + if ( + isNativeTranslationAvailable && + isLanguageSupported(record?.langs?.at(0)) + ) { + const text = richTextToString(richText, true) + NativeTranslationModule.presentAsync(text) + } else { + openLink(translatorUrl) + } + }, [openLink, record?.langs, richText, translatorUrl]) const onHidePost = React.useCallback(() => { hidePost({uri: postUri}) @@ -246,7 +259,7 @@ let PostDropdownBtn = ({ + onPress={onPressTranslate}> {_(msg`Translate`)} diff --git a/src/view/shell/index.tsx b/src/view/shell/index.tsx index 7d080e57b..317ac0bde 100644 --- a/src/view/shell/index.tsx +++ b/src/view/shell/index.tsx @@ -33,6 +33,7 @@ import {ErrorBoundary} from 'view/com/util/ErrorBoundary' import {MutedWordsDialog} from '#/components/dialogs/MutedWords' import {SigninDialog} from '#/components/dialogs/Signin' import {Outlet as PortalOutlet} from '#/components/Portal' +import {NativeTranslationView} from '../../../modules/expo-bluesky-translate' import {RoutesContainer, TabsNavigator} from '../../Navigation' import {Composer} from './Composer' import {DrawerContent} from './Drawer' @@ -93,6 +94,7 @@ function ShellInner() { + -- cgit 1.4.1