tooot/src/components/ParseContent.tsx

134 lines
3.3 KiB
TypeScript
Raw Normal View History

2020-11-28 17:07:30 +01:00
import React, { useCallback } from 'react'
2020-11-23 00:07:32 +01:00
import { Text } from 'react-native'
2020-10-31 21:04:46 +01:00
import HTMLView, { HTMLViewNode } from 'react-native-htmlview'
2020-10-28 00:02:37 +01:00
import { useNavigation } from '@react-navigation/native'
2020-11-23 00:07:32 +01:00
import Emojis from 'src/components/Timelines/Timeline/Shared/Emojis'
import { useTheme } from 'src/utils/styles/ThemeManager'
2020-11-05 21:47:50 +01:00
// Prevent going to the same hashtag multiple times
2020-10-31 21:04:46 +01:00
const renderNode = ({
2020-11-23 00:07:32 +01:00
theme,
2020-10-31 21:04:46 +01:00
node,
index,
navigation,
mentions,
showFullLink
}: {
2020-11-23 00:07:32 +01:00
theme: any
node: any
2020-10-31 21:04:46 +01:00
index: number
navigation: any
mentions?: Mastodon.Mention[]
2020-10-31 21:04:46 +01:00
showFullLink: boolean
}) => {
2020-10-28 00:02:37 +01:00
if (node.name == 'a') {
const classes = node.attribs.class
const href = node.attribs.href
if (classes) {
if (classes.includes('hashtag')) {
return (
<Text
key={index}
2020-11-23 00:07:32 +01:00
style={{ color: theme.link }}
2020-10-28 00:02:37 +01:00
onPress={() => {
const tag = href.split(new RegExp(/\/tag\/(.*)|\/tags\/(.*)/))
2020-11-23 00:07:32 +01:00
navigation.push('Screen-Shared-Hashtag', {
2020-10-28 00:02:37 +01:00
hashtag: tag[1] || tag[2]
})
}}
>
{node.children[0].data}
{node.children[1]?.children[0].data}
</Text>
)
2020-11-05 21:47:50 +01:00
} else if (classes.includes('mention') && mentions) {
2020-10-28 00:02:37 +01:00
return (
<Text
key={index}
2020-11-23 00:07:32 +01:00
style={{ color: theme.link }}
2020-10-28 00:02:37 +01:00
onPress={() => {
const username = href.split(new RegExp(/@(.*)/))
const usernameIndex = mentions.findIndex(
m => m.username === username[1]
)
2020-11-23 00:07:32 +01:00
navigation.push('Screen-Shared-Account', {
2020-10-28 00:02:37 +01:00
id: mentions[usernameIndex].id
})
}}
>
{node.children[0].data}
{node.children[1]?.children[0].data}
</Text>
)
}
} else {
const domain = href.split(new RegExp(/:\/\/(.*?)\//))
return (
<Text
key={index}
2020-11-23 00:07:32 +01:00
style={{ color: theme.link }}
2020-10-28 00:02:37 +01:00
onPress={() => {
2020-11-23 00:07:32 +01:00
navigation.navigate('Screen-Shared-Webview', {
2020-10-28 00:02:37 +01:00
uri: href,
domain: domain[1]
})
}}
>
{showFullLink ? href : domain[1]}
</Text>
)
}
}
}
2020-10-31 21:04:46 +01:00
export interface Props {
content: string
2020-11-23 00:07:32 +01:00
size: number
emojis?: Mastodon.Emoji[]
mentions?: Mastodon.Mention[]
2020-10-31 21:04:46 +01:00
showFullLink?: boolean
linesTruncated?: number
}
const ParseContent: React.FC<Props> = ({
2020-10-28 00:02:37 +01:00
content,
2020-11-23 00:07:32 +01:00
size,
2020-10-28 00:02:37 +01:00
emojis,
mentions,
2020-10-29 14:52:28 +01:00
showFullLink = false,
linesTruncated = 10
2020-10-31 21:04:46 +01:00
}) => {
2020-10-28 00:02:37 +01:00
const navigation = useNavigation()
2020-11-23 00:07:32 +01:00
const { theme } = useTheme()
2020-10-28 00:02:37 +01:00
2020-11-28 17:07:30 +01:00
const renderNodeCallback = useCallback(
(node, index) =>
renderNode({ theme, node, index, navigation, mentions, showFullLink }),
[]
)
const textComponent = useCallback(
({ children }) =>
emojis && children ? (
<Emojis content={children.toString()} emojis={emojis} size={size} />
) : (
<Text>{children}</Text>
),
[]
)
const rootComponent = useCallback(({ children }) => {
return <Text numberOfLines={linesTruncated}>{children}</Text>
}, [])
2020-10-28 00:02:37 +01:00
return (
<HTMLView
value={content}
2020-11-28 17:07:30 +01:00
TextComponent={textComponent}
RootComponent={rootComponent}
renderNode={renderNodeCallback}
2020-10-28 00:02:37 +01:00
/>
)
}
2020-10-31 21:04:46 +01:00
export default ParseContent