2021-05-23 22:40:42 +02:00
|
|
|
import { ParseHTML } from '@components/Parse'
|
2022-05-07 00:52:32 +02:00
|
|
|
import CustomText from '@components/Text'
|
2022-11-04 23:38:29 +01:00
|
|
|
import getLanguage from '@helpers/getLanguage'
|
2021-05-19 20:40:16 +02:00
|
|
|
import { useTranslateQuery } from '@utils/queryHooks/translate'
|
|
|
|
import { StyleConstants } from '@utils/styles/constants'
|
|
|
|
import { useTheme } from '@utils/styles/ThemeManager'
|
2022-02-01 22:27:29 +01:00
|
|
|
import * as Localization from 'expo-localization'
|
2022-12-03 20:47:11 +01:00
|
|
|
import React, { useContext, useEffect, useState } from 'react'
|
2021-05-19 20:40:16 +02:00
|
|
|
import { useTranslation } from 'react-i18next'
|
2022-05-07 00:52:32 +02:00
|
|
|
import { Pressable } from 'react-native'
|
2021-05-19 23:28:01 +02:00
|
|
|
import { Circle } from 'react-native-animated-spinkit'
|
2022-06-09 01:47:38 +02:00
|
|
|
import detectLanguage from 'react-native-language-detection'
|
2022-12-03 20:47:11 +01:00
|
|
|
import StatusContext from './Context'
|
2021-05-19 20:40:16 +02:00
|
|
|
|
2022-12-03 20:47:11 +01:00
|
|
|
const TimelineTranslate = () => {
|
|
|
|
const { status, highlighted } = useContext(StatusContext)
|
|
|
|
if (!status || !highlighted) return null
|
2021-05-19 20:40:16 +02:00
|
|
|
|
2022-12-03 20:47:11 +01:00
|
|
|
const { t } = useTranslation('componentTimeline')
|
|
|
|
const { colors } = useTheme()
|
2021-05-19 20:40:16 +02:00
|
|
|
|
2022-12-03 20:47:11 +01:00
|
|
|
const text = status.spoiler_text ? [status.spoiler_text, status.content] : [status.content]
|
2021-05-19 23:28:01 +02:00
|
|
|
|
2022-12-03 20:47:11 +01:00
|
|
|
for (const i in text) {
|
|
|
|
for (const emoji of status.emojis) {
|
|
|
|
text[i] = text[i].replaceAll(`:${emoji.shortcode}:`, ' ')
|
|
|
|
}
|
|
|
|
text[i] = text[i]
|
|
|
|
.replace(/(<([^>]+)>)/gi, ' ')
|
|
|
|
.replace(/@.*? /gi, ' ')
|
|
|
|
.replace(/#.*? /gi, ' ')
|
|
|
|
.replace(/http(s):\/\/.*? /gi, ' ')
|
|
|
|
}
|
2021-05-23 22:40:42 +02:00
|
|
|
|
2022-12-03 20:47:11 +01:00
|
|
|
const [detectedLanguage, setDetectedLanguage] = useState<string>('')
|
|
|
|
useEffect(() => {
|
|
|
|
const detect = async () => {
|
|
|
|
const result = await detectLanguage(text.join(`\n\n`)).catch(() => {
|
|
|
|
// No need to log language detection failure
|
|
|
|
})
|
|
|
|
result?.detected && setDetectedLanguage(result.detected.slice(0, 2))
|
2021-05-19 23:28:01 +02:00
|
|
|
}
|
2022-12-03 20:47:11 +01:00
|
|
|
detect()
|
|
|
|
}, [])
|
2021-05-19 20:40:16 +02:00
|
|
|
|
2022-12-03 20:47:11 +01:00
|
|
|
const settingsLanguage = getLanguage()
|
|
|
|
const targetLanguage = settingsLanguage?.startsWith('en')
|
|
|
|
? Localization.locale || settingsLanguage || 'en'
|
|
|
|
: settingsLanguage || Localization.locale || 'en'
|
2022-06-09 01:47:38 +02:00
|
|
|
|
2022-12-03 20:47:11 +01:00
|
|
|
const [enabled, setEnabled] = useState(false)
|
|
|
|
const { refetch, data, isLoading, isSuccess, isError } = useTranslateQuery({
|
|
|
|
source: detectedLanguage,
|
|
|
|
target: targetLanguage,
|
|
|
|
text,
|
|
|
|
options: { enabled }
|
|
|
|
})
|
2022-06-09 01:47:38 +02:00
|
|
|
|
2022-12-03 20:47:11 +01:00
|
|
|
if (!detectedLanguage) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
if (Localization.locale.slice(0, 2).includes(detectedLanguage)) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
if (settingsLanguage?.slice(0, 2).includes(detectedLanguage)) {
|
|
|
|
return null
|
|
|
|
}
|
2022-06-10 19:41:39 +02:00
|
|
|
|
2022-12-03 20:47:11 +01:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Pressable
|
|
|
|
style={{
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center',
|
|
|
|
paddingVertical: StyleConstants.Spacing.S,
|
|
|
|
paddingBottom: isSuccess ? 0 : undefined
|
|
|
|
}}
|
|
|
|
onPress={() => {
|
|
|
|
if (enabled) {
|
|
|
|
if (!isSuccess) {
|
|
|
|
refetch()
|
2021-05-19 23:28:01 +02:00
|
|
|
}
|
2022-12-03 20:47:11 +01:00
|
|
|
} else {
|
|
|
|
setEnabled(true)
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<CustomText
|
|
|
|
fontStyle='M'
|
|
|
|
style={{
|
|
|
|
color: isLoading || isSuccess ? colors.secondary : isError ? colors.red : colors.blue
|
2021-05-19 20:40:16 +02:00
|
|
|
}}
|
|
|
|
>
|
2022-12-03 20:47:11 +01:00
|
|
|
{isError
|
|
|
|
? t('shared.translate.failed')
|
|
|
|
: isSuccess
|
|
|
|
? typeof data?.error === 'string'
|
|
|
|
? t(`shared.translate.${data.error}`)
|
|
|
|
: t('shared.translate.succeed', {
|
|
|
|
provider: data?.provider,
|
|
|
|
source: data?.sourceLanguage
|
|
|
|
})
|
|
|
|
: t('shared.translate.default')}
|
|
|
|
</CustomText>
|
|
|
|
<CustomText>
|
|
|
|
{__DEV__ ? ` Source: ${detectedLanguage}; Target: ${targetLanguage}` : undefined}
|
|
|
|
</CustomText>
|
|
|
|
{isLoading ? (
|
|
|
|
<Circle
|
|
|
|
size={StyleConstants.Font.Size.M}
|
|
|
|
color={colors.disabled}
|
|
|
|
style={{ marginLeft: StyleConstants.Spacing.S }}
|
|
|
|
/>
|
|
|
|
) : null}
|
|
|
|
</Pressable>
|
|
|
|
{data && data.error === undefined
|
|
|
|
? data.text.map((d, i) => <ParseHTML key={i} content={d} size={'M'} numberOfLines={999} />)
|
|
|
|
: null}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
2021-05-19 20:40:16 +02:00
|
|
|
|
|
|
|
export default TimelineTranslate
|