Move forceGoogleTranslate param to useTranslate hook
This commit is contained in:
@@ -137,6 +137,7 @@ let PostMenuItems = ({
|
||||
const openLink = useOpenLink()
|
||||
const {clearTranslation, translate, translationState} = useTranslate({
|
||||
key: post.uri,
|
||||
forceGoogleTranslate,
|
||||
})
|
||||
const navigation = useNavigation<NavigationProp>()
|
||||
const {mutedWordsDialogControl} = useGlobalDialogsControlContext()
|
||||
@@ -261,7 +262,6 @@ let PostMenuItems = ({
|
||||
void translate({
|
||||
text: record.text,
|
||||
targetLangCode: langPrefs.primaryLanguage,
|
||||
forceGoogleTranslate,
|
||||
})
|
||||
|
||||
if (
|
||||
|
||||
@@ -7,6 +7,7 @@ export const Context = createContext<{
|
||||
translate: (
|
||||
parameters: TranslationFunctionParams & {
|
||||
key: string
|
||||
forceGoogleTranslate: boolean
|
||||
},
|
||||
) => Promise<void>
|
||||
clearTranslation: (key: string) => void
|
||||
|
||||
@@ -77,7 +77,13 @@ async function attemptTranslation(
|
||||
*
|
||||
* Web uses index.web.ts which always opens Google Translate.
|
||||
*/
|
||||
export function useTranslate({key}: {key: string}) {
|
||||
export function useTranslate({
|
||||
key,
|
||||
forceGoogleTranslate = false,
|
||||
}: {
|
||||
key: string
|
||||
forceGoogleTranslate?: boolean
|
||||
}) {
|
||||
const context = useContext(Context)
|
||||
if (!context) {
|
||||
throw new Error(
|
||||
@@ -94,14 +100,14 @@ export function useTranslate({key}: {key: string}) {
|
||||
|
||||
const translate = useCallback(
|
||||
async (params: TranslationFunctionParams) => {
|
||||
return context.translate({...params, key})
|
||||
return context.translate({...params, key, forceGoogleTranslate})
|
||||
},
|
||||
[key, context],
|
||||
[context, forceGoogleTranslate, key],
|
||||
)
|
||||
|
||||
const clearTranslation = useCallback(
|
||||
() => context.clearTranslation(key),
|
||||
[key, context],
|
||||
[context, key],
|
||||
)
|
||||
|
||||
return {
|
||||
|
||||
@@ -3,7 +3,7 @@ import {useCallback, useContext, useMemo} from 'react'
|
||||
import {useGoogleTranslate} from '#/lib/hooks/useGoogleTranslate'
|
||||
import {useAnalytics} from '#/analytics'
|
||||
import {Context} from './context'
|
||||
import {type TranslationState} from './types'
|
||||
import {type TranslationFunctionParams, type TranslationState} from './types'
|
||||
|
||||
const translationState: Record<string, TranslationState> = {}
|
||||
const acquireTranslation = (_key: string) => {
|
||||
@@ -14,7 +14,12 @@ const clearTranslation = (_key: string) => {}
|
||||
/**
|
||||
* Web always opens Google Translate.
|
||||
*/
|
||||
export function useTranslate(key: string) {
|
||||
export function useTranslate({
|
||||
key,
|
||||
}: {
|
||||
key: string
|
||||
forceGoogleTranslate?: boolean
|
||||
}) {
|
||||
const context = useContext(Context)
|
||||
if (!context) {
|
||||
throw new Error(
|
||||
@@ -24,42 +29,23 @@ export function useTranslate(key: string) {
|
||||
|
||||
// Always call hooks in consistent order
|
||||
const translate = useCallback(
|
||||
async (params: {
|
||||
text: string
|
||||
targetLangCode: string
|
||||
sourceLangCode?: string
|
||||
}) => {
|
||||
if (!key) {
|
||||
throw new Error(
|
||||
'translate requires a key. Either pass key to useTranslate() or use context.translate() with key parameter',
|
||||
)
|
||||
}
|
||||
return context.translate({...params, key})
|
||||
async (params: TranslationFunctionParams) => {
|
||||
return context.translate({...params, key, forceGoogleTranslate: true})
|
||||
},
|
||||
[key, context],
|
||||
)
|
||||
|
||||
const clearTranslation = useCallback(() => {
|
||||
if (!key) {
|
||||
throw new Error(
|
||||
'clearTranslation requires a key. Either pass key to useTranslate() or use context.clearTranslation() with key parameter',
|
||||
)
|
||||
}
|
||||
return context.clearTranslation(key)
|
||||
}, [key, context])
|
||||
|
||||
// If a key is provided, return wrapped versions that automatically use the key
|
||||
if (key) {
|
||||
return {
|
||||
translationState: context.translationState[key] ?? {
|
||||
status: 'idle' as const,
|
||||
},
|
||||
translate,
|
||||
clearTranslation,
|
||||
}
|
||||
return {
|
||||
translationState: context.translationState[key] ?? {
|
||||
status: 'idle' as const,
|
||||
},
|
||||
translate,
|
||||
clearTranslation,
|
||||
}
|
||||
|
||||
return context
|
||||
}
|
||||
|
||||
export function Provider({children}: React.PropsWithChildren<unknown>) {
|
||||
|
||||
@@ -23,10 +23,6 @@ export type TranslationFunctionParams = {
|
||||
* The source language of the text. Will auto-detect if not provided.
|
||||
*/
|
||||
sourceLangCode?: string
|
||||
/**
|
||||
* Whether to force the use of Google Translate. Default is false.
|
||||
*/
|
||||
forceGoogleTranslate?: boolean
|
||||
}
|
||||
|
||||
export type TranslationFunction = (
|
||||
|
||||
Reference in New Issue
Block a user