tooot/src/components/Timeline/Shared/Translate.tsx

131 lines
3.5 KiB
TypeScript
Raw Normal View History

2021-05-23 22:40:42 +02:00
import analytics from '@components/analytics'
import { ParseHTML } from '@components/Parse'
2021-05-19 20:40:16 +02:00
import { useTranslateQuery } from '@utils/queryHooks/translate'
import { getSettingsLanguage } from '@utils/slices/settingsSlice'
import { StyleConstants } from '@utils/styles/constants'
import { useTheme } from '@utils/styles/ThemeManager'
2021-05-19 23:28:01 +02:00
import React, { useState } from 'react'
2021-05-19 20:40:16 +02:00
import { useTranslation } from 'react-i18next'
2021-05-19 23:28:01 +02:00
import { Pressable, StyleSheet, Text } from 'react-native'
import { Circle } from 'react-native-animated-spinkit'
2021-05-19 20:40:16 +02:00
import { useSelector } from 'react-redux'
export interface Props {
highlighted: boolean
status: Mastodon.Status
}
const TimelineTranslate = React.memo(
({ highlighted, status }: Props) => {
if (!highlighted) {
return null
}
if (!status.language) {
return null
}
2021-05-19 23:28:01 +02:00
const { t } = useTranslation('componentTimeline')
const { theme } = useTheme()
2021-05-19 20:40:16 +02:00
const tootLanguage = status.language.slice(0, 2)
2021-05-19 23:28:01 +02:00
2021-05-23 22:40:42 +02:00
const settingsLanguage = useSelector(getSettingsLanguage)
2021-05-19 20:40:16 +02:00
2021-05-30 15:40:06 +02:00
if (settingsLanguage?.includes(tootLanguage)) {
2021-05-19 20:40:16 +02:00
return null
}
2021-05-23 22:40:42 +02:00
let text = status.spoiler_text
? [status.spoiler_text, status.content]
: [status.content]
for (const i in text) {
2021-05-19 23:28:01 +02:00
for (const emoji of status.emojis) {
2021-05-23 22:40:42 +02:00
text[i] = text[i].replaceAll(`:${emoji.shortcode}:`, '')
2021-05-19 23:28:01 +02:00
}
}
2021-05-19 20:40:16 +02:00
2021-05-19 23:28:01 +02:00
const [enabled, setEnabled] = useState(false)
const { refetch, data, isLoading, isSuccess, isError } = useTranslateQuery({
2021-05-19 20:40:16 +02:00
source: status.language,
2021-05-23 22:40:42 +02:00
target: settingsLanguage,
text,
2021-05-19 23:28:01 +02:00
options: { enabled }
2021-05-19 20:40:16 +02:00
})
return (
<>
2021-05-19 23:28:01 +02:00
<Pressable
style={[styles.button, { paddingBottom: isSuccess ? 0 : undefined }]}
onPress={() => {
if (enabled) {
if (!isSuccess) {
2021-05-23 22:40:42 +02:00
analytics('timeline_shared_translate_retry', {
language: status.language
})
2021-05-19 23:28:01 +02:00
refetch()
}
} else {
2021-05-23 22:40:42 +02:00
analytics('timeline_shared_translate', {
language: status.language
})
2021-05-19 23:28:01 +02:00
setEnabled(true)
}
2021-05-19 20:40:16 +02:00
}}
>
2021-05-19 23:28:01 +02:00
<Text
style={{
...StyleConstants.FontStyle.M,
color:
isLoading || isSuccess
2021-05-23 22:40:42 +02:00
? theme.secondary
2021-05-19 23:28:01 +02:00
: isError
? theme.red
: theme.blue
}}
>
{isError
2021-05-23 22:40:42 +02:00
? t('shared.translate.failed')
: isSuccess
? t('shared.translate.succeed', {
provider: data?.provider,
source: data?.sourceLanguage
})
2021-05-19 23:28:01 +02:00
: t('shared.translate.default')}
2021-05-23 22:40:42 +02:00
{__DEV__ ? ` Source: ${status.language}` : undefined}
2021-05-19 23:28:01 +02:00
</Text>
{isLoading ? (
<Circle
size={StyleConstants.Font.Size.M}
color={theme.disabled}
style={{ marginLeft: StyleConstants.Spacing.S }}
/>
) : null}
</Pressable>
2021-05-23 22:40:42 +02:00
{data
? data.text.map((d, i) => (
<ParseHTML
key={i}
content={d}
size={'M'}
numberOfLines={999}
selectable
/>
))
: null}
2021-05-19 20:40:16 +02:00
</>
)
},
() => true
)
2021-05-19 23:28:01 +02:00
const styles = StyleSheet.create({
button: {
flexDirection: 'row',
alignItems: 'center',
paddingVertical: StyleConstants.Spacing.S
}
})
2021-05-19 20:40:16 +02:00
export default TimelineTranslate