Rename translation hooks
This commit is contained in:
@@ -5,7 +5,7 @@ import {type ChatBskyConvoDefs, RichText} from '@atproto/api'
|
|||||||
import {msg} from '@lingui/core/macro'
|
import {msg} from '@lingui/core/macro'
|
||||||
import {useLingui} from '@lingui/react'
|
import {useLingui} from '@lingui/react'
|
||||||
|
|
||||||
import {useTranslate} from '#/lib/hooks/useTranslate'
|
import {useGoogleTranslate} from '#/lib/hooks/useGoogleTranslate'
|
||||||
import {richTextToString} from '#/lib/strings/rich-text-helpers'
|
import {richTextToString} from '#/lib/strings/rich-text-helpers'
|
||||||
import {useConvoActive} from '#/state/messages/convo'
|
import {useConvoActive} from '#/state/messages/convo'
|
||||||
import {useLanguagePrefs} from '#/state/preferences'
|
import {useLanguagePrefs} from '#/state/preferences'
|
||||||
@@ -41,7 +41,7 @@ export let MessageContextMenu = ({
|
|||||||
const reportControl = usePromptControl()
|
const reportControl = usePromptControl()
|
||||||
const blockOrDeleteControl = usePromptControl()
|
const blockOrDeleteControl = usePromptControl()
|
||||||
const langPrefs = useLanguagePrefs()
|
const langPrefs = useLanguagePrefs()
|
||||||
const translate = useTranslate()
|
const translate = useGoogleTranslate()
|
||||||
|
|
||||||
const isFromSelf = message.sender?.did === currentAccount?.did
|
const isFromSelf = message.sender?.did === currentAccount?.did
|
||||||
|
|
||||||
@@ -54,12 +54,12 @@ export let MessageContextMenu = ({
|
|||||||
true,
|
true,
|
||||||
)
|
)
|
||||||
|
|
||||||
Clipboard.setStringAsync(str)
|
void Clipboard.setStringAsync(str)
|
||||||
Toast.show(_(msg`Copied to clipboard`), 'clipboard-check')
|
Toast.show(_(msg`Copied to clipboard`), 'clipboard-check')
|
||||||
}, [_, message.text, message.facets])
|
}, [_, message.text, message.facets])
|
||||||
|
|
||||||
const onPressTranslateMessage = useCallback(() => {
|
const onPressTranslateMessage = useCallback(() => {
|
||||||
translate(message.text, langPrefs.primaryLanguage)
|
void translate(message.text, langPrefs.primaryLanguage)
|
||||||
|
|
||||||
ax.metric('translate', {
|
ax.metric('translate', {
|
||||||
sourceLanguages: [],
|
sourceLanguages: [],
|
||||||
|
|||||||
@@ -6,10 +6,9 @@ import {getTranslatorLink} from '#/locale/helpers'
|
|||||||
import {IS_ANDROID} from '#/env'
|
import {IS_ANDROID} from '#/env'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Will always link out to Google Translate. If inline translation is desired,
|
* @deprecated Will always link out to Google Translate. Prefer `useTranslate`.
|
||||||
* use `useTranslateOnDevice`
|
|
||||||
*/
|
*/
|
||||||
export function useTranslate() {
|
export function useGoogleTranslate() {
|
||||||
const openLink = useOpenLink()
|
const openLink = useOpenLink()
|
||||||
|
|
||||||
return useCallback(
|
return useCallback(
|
||||||
@@ -4,8 +4,7 @@ import {getLocales} from 'expo-localization'
|
|||||||
import {type TranslationTaskResult} from '@bsky.app/expo-translate-text/build/ExpoTranslateText.types'
|
import {type TranslationTaskResult} from '@bsky.app/expo-translate-text/build/ExpoTranslateText.types'
|
||||||
import {useFocusEffect} from '@react-navigation/native'
|
import {useFocusEffect} from '@react-navigation/native'
|
||||||
|
|
||||||
import {useOpenLink} from '#/lib/hooks/useOpenLink'
|
import {useGoogleTranslate} from '#/lib/hooks/useGoogleTranslate'
|
||||||
import {getTranslatorLink} from '#/locale/helpers'
|
|
||||||
import {logger} from '#/logger'
|
import {logger} from '#/logger'
|
||||||
import {useLanguagePrefs} from '#/state/preferences'
|
import {useLanguagePrefs} from '#/state/preferences'
|
||||||
import {useAnalytics} from '#/analytics'
|
import {useAnalytics} from '#/analytics'
|
||||||
@@ -111,9 +110,9 @@ export function Provider({children}: React.PropsWithChildren<unknown>) {
|
|||||||
Record<string, TranslationState>
|
Record<string, TranslationState>
|
||||||
>({})
|
>({})
|
||||||
const [refCounts, setRefCounts] = useState<Record<string, number>>({})
|
const [refCounts, setRefCounts] = useState<Record<string, number>>({})
|
||||||
const openLink = useOpenLink()
|
|
||||||
const ax = useAnalytics()
|
const ax = useAnalytics()
|
||||||
const {primaryLanguage} = useLanguagePrefs()
|
const {primaryLanguage} = useLanguagePrefs()
|
||||||
|
const googleTranslate = useGoogleTranslate()
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setTranslationState(prev => {
|
setTranslationState(prev => {
|
||||||
@@ -171,11 +170,6 @@ export function Provider({children}: React.PropsWithChildren<unknown>) {
|
|||||||
sourceLangCode?: string,
|
sourceLangCode?: string,
|
||||||
options?: Options,
|
options?: Options,
|
||||||
) => {
|
) => {
|
||||||
const translateUrl = getTranslatorLink(
|
|
||||||
text,
|
|
||||||
targetLangCode,
|
|
||||||
sourceLangCode,
|
|
||||||
)
|
|
||||||
if (options?.googleTranslate) {
|
if (options?.googleTranslate) {
|
||||||
ax.metric('translate:result', {
|
ax.metric('translate:result', {
|
||||||
method: 'google-translate',
|
method: 'google-translate',
|
||||||
@@ -183,7 +177,7 @@ export function Provider({children}: React.PropsWithChildren<unknown>) {
|
|||||||
sourceLanguage: sourceLangCode ?? null,
|
sourceLanguage: sourceLangCode ?? null,
|
||||||
targetLanguage: targetLangCode,
|
targetLanguage: targetLangCode,
|
||||||
})
|
})
|
||||||
await openLink(translateUrl)
|
await googleTranslate(text, targetLangCode, sourceLangCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut)
|
LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut)
|
||||||
@@ -228,10 +222,10 @@ export function Provider({children}: React.PropsWithChildren<unknown>) {
|
|||||||
...prev,
|
...prev,
|
||||||
[key]: {status: 'idle'},
|
[key]: {status: 'idle'},
|
||||||
}))
|
}))
|
||||||
await openLink(translateUrl)
|
await googleTranslate(text, targetLangCode, sourceLangCode)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[ax, openLink, primaryLanguage],
|
[ax, googleTranslate, primaryLanguage],
|
||||||
)
|
)
|
||||||
|
|
||||||
const ctx = useMemo(
|
const ctx = useMemo(
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import {useCallback, useContext, useMemo} from 'react'
|
import {useCallback, useContext, useMemo} from 'react'
|
||||||
|
|
||||||
import {useOpenLink} from '#/lib/hooks/useOpenLink'
|
import {useGoogleTranslate} from '#/lib/hooks/useGoogleTranslate'
|
||||||
import {getTranslatorLink} from '#/locale/helpers'
|
|
||||||
import {useLanguagePrefs} from '#/state/preferences'
|
import {useLanguagePrefs} from '#/state/preferences'
|
||||||
import {useAnalytics} from '#/analytics'
|
import {useAnalytics} from '#/analytics'
|
||||||
import {Context} from './context'
|
import {Context} from './context'
|
||||||
@@ -29,9 +28,9 @@ export function useTranslateOnDevice() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function Provider({children}: React.PropsWithChildren<unknown>) {
|
export function Provider({children}: React.PropsWithChildren<unknown>) {
|
||||||
const openLink = useOpenLink()
|
|
||||||
const ax = useAnalytics()
|
const ax = useAnalytics()
|
||||||
const {primaryLanguage} = useLanguagePrefs()
|
const {primaryLanguage} = useLanguagePrefs()
|
||||||
|
const googleTranslate = useGoogleTranslate()
|
||||||
|
|
||||||
const translate = useCallback(
|
const translate = useCallback(
|
||||||
async (
|
async (
|
||||||
@@ -47,14 +46,9 @@ export function Provider({children}: React.PropsWithChildren<unknown>) {
|
|||||||
sourceLanguage: sourceLangCode ?? null,
|
sourceLanguage: sourceLangCode ?? null,
|
||||||
targetLanguage: targetLangCode,
|
targetLanguage: targetLangCode,
|
||||||
})
|
})
|
||||||
const translateUrl = getTranslatorLink(
|
await googleTranslate(text, targetLangCode, sourceLangCode)
|
||||||
text,
|
|
||||||
targetLangCode,
|
|
||||||
sourceLangCode,
|
|
||||||
)
|
|
||||||
await openLink(translateUrl)
|
|
||||||
},
|
},
|
||||||
[ax, openLink, primaryLanguage],
|
[ax, googleTranslate, primaryLanguage],
|
||||||
)
|
)
|
||||||
|
|
||||||
const ctx = useMemo(
|
const ctx = useMemo(
|
||||||
|
|||||||
Reference in New Issue
Block a user