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

242 lines
8.2 KiB
TypeScript
Raw Normal View History

2020-12-30 14:33:33 +01:00
import Button from '@components/Button'
2021-01-01 23:10:47 +01:00
import haptics from '@components/haptics'
2021-02-08 23:47:20 +01:00
import AttachmentAudio from '@components/Timeline/Shared/Attachment/Audio'
import AttachmentImage from '@components/Timeline/Shared/Attachment/Image'
import AttachmentUnsupported from '@components/Timeline/Shared/Attachment/Unsupported'
import AttachmentVideo from '@components/Timeline/Shared/Attachment/Video'
2020-12-30 14:33:33 +01:00
import { useNavigation } from '@react-navigation/native'
2022-04-29 23:57:18 +02:00
import { StackNavigationProp } from '@react-navigation/stack'
2021-08-29 16:08:02 +02:00
import { RootStackParamList } from '@utils/navigation/navigators'
2022-06-02 00:48:14 +02:00
import { getInstanceAccount } from '@utils/slices/instancesSlice'
2020-12-30 14:33:33 +01:00
import { StyleConstants } from '@utils/styles/constants'
import layoutAnimation from '@utils/styles/layoutAnimation'
2022-09-14 22:44:23 +02:00
import React, { useState } from 'react'
2021-01-01 23:10:47 +01:00
import { useTranslation } from 'react-i18next'
2022-04-30 21:29:08 +02:00
import { Pressable, View } from 'react-native'
2022-06-02 00:48:14 +02:00
import { useSelector } from 'react-redux'
2020-10-30 20:03:44 +01:00
2020-10-31 21:04:46 +01:00
export interface Props {
2020-12-03 01:28:56 +01:00
status: Pick<Mastodon.Status, 'media_attachments' | 'sensitive'>
2020-10-31 21:04:46 +01:00
}
2021-02-27 16:33:54 +01:00
const TimelineAttachment = React.memo(
({ status }: Props) => {
const { t } = useTranslation('componentTimeline')
2021-01-01 23:10:47 +01:00
2022-06-02 00:48:14 +02:00
const account = useSelector(
getInstanceAccount,
(prev, next) =>
2022-09-14 22:44:23 +02:00
prev.preferences['reading:expand:media'] === next.preferences['reading:expand:media']
2022-06-02 00:48:14 +02:00
)
const defaultSensitive = () => {
switch (account.preferences['reading:expand:media']) {
case 'show_all':
return false
case 'hide_all':
return true
default:
return status.sensitive
}
}
const [sensitiveShown, setSensitiveShown] = useState(defaultSensitive())
2020-12-03 01:28:56 +01:00
// @ts-ignore
2022-09-14 22:44:23 +02:00
const imageUrls: RootStackParamList['Screen-ImagesViewer']['imageUrls'] =
status.media_attachments
.map(attachment => {
switch (attachment.type) {
case 'image':
return {
id: attachment.id,
preview_url: attachment.preview_url,
url: attachment.url,
remote_url: attachment.remote_url,
blurhash: attachment.blurhash,
width: attachment.meta?.original?.width,
height: attachment.meta?.original?.height
}
default:
if (
attachment.preview_url?.endsWith('.jpg') ||
attachment.preview_url?.endsWith('.jpeg') ||
attachment.preview_url?.endsWith('.png') ||
attachment.preview_url?.endsWith('.gif') ||
attachment.remote_url?.endsWith('.jpg') ||
attachment.remote_url?.endsWith('.jpeg') ||
attachment.remote_url?.endsWith('.png') ||
attachment.remote_url?.endsWith('.gif')
) {
return {
id: attachment.id,
preview_url: attachment.preview_url,
url: attachment.url,
remote_url: attachment.remote_url,
blurhash: attachment.blurhash,
width: attachment.meta?.original?.width,
height: attachment.meta?.original?.height
}
}
}
})
.filter(i => i)
2022-04-29 23:57:18 +02:00
const navigation = useNavigation<StackNavigationProp<RootStackParamList>>()
2022-04-30 21:29:08 +02:00
const navigateToImagesViewer = (id: string) => {
2022-09-14 22:44:23 +02:00
navigation.navigate('Screen-ImagesViewer', { imageUrls, id })
2022-04-30 21:29:08 +02:00
}
return (
<View>
<View
style={{
marginTop: StyleConstants.Spacing.S,
flex: 1,
flexDirection: 'row',
flexWrap: 'wrap',
justifyContent: 'center',
alignContent: 'stretch'
}}
>
{status.media_attachments.map((attachment, index) => {
switch (attachment.type) {
case 'image':
return (
<AttachmentImage
key={index}
total={status.media_attachments.length}
index={index}
sensitiveShown={sensitiveShown}
image={attachment}
navigateToImagesViewer={navigateToImagesViewer}
/>
)
2022-04-30 21:29:08 +02:00
case 'video':
return (
2022-04-30 21:29:08 +02:00
<AttachmentVideo
key={index}
total={status.media_attachments.length}
index={index}
sensitiveShown={sensitiveShown}
2022-04-30 21:29:08 +02:00
video={attachment}
/>
)
2022-04-30 21:29:08 +02:00
case 'gifv':
return (
<AttachmentVideo
key={index}
total={status.media_attachments.length}
index={index}
sensitiveShown={sensitiveShown}
video={attachment}
gifv
/>
)
case 'audio':
return (
<AttachmentAudio
key={index}
total={status.media_attachments.length}
index={index}
sensitiveShown={sensitiveShown}
audio={attachment}
/>
)
default:
if (
attachment.preview_url?.endsWith('.jpg') ||
attachment.preview_url?.endsWith('.jpeg') ||
attachment.preview_url?.endsWith('.png') ||
attachment.preview_url?.endsWith('.gif') ||
attachment.remote_url?.endsWith('.jpg') ||
attachment.remote_url?.endsWith('.jpeg') ||
attachment.remote_url?.endsWith('.png') ||
attachment.remote_url?.endsWith('.gif')
) {
return (
<AttachmentImage
key={index}
total={status.media_attachments.length}
index={index}
sensitiveShown={sensitiveShown}
// @ts-ignore
image={attachment}
navigateToImagesViewer={navigateToImagesViewer}
/>
)
} else {
return (
<AttachmentUnsupported
key={index}
total={status.media_attachments.length}
index={index}
sensitiveShown={sensitiveShown}
attachment={attachment}
/>
)
}
}
})}
</View>
2020-12-25 18:20:09 +01:00
2022-06-02 00:48:14 +02:00
{defaultSensitive() &&
2021-02-27 16:33:54 +01:00
(sensitiveShown ? (
2022-04-30 21:29:08 +02:00
<Pressable
style={{
position: 'absolute',
width: '100%',
height: '100%',
flex: 1,
justifyContent: 'center',
alignItems: 'center'
}}
>
2021-02-27 16:33:54 +01:00
<Button
type='text'
content={t('shared.attachment.sensitive.button')}
overlay
2022-04-30 21:29:08 +02:00
onPress={() => {
layoutAnimation()
setSensitiveShown(false)
haptics('Light')
}}
2021-02-27 16:33:54 +01:00
/>
</Pressable>
) : (
2020-12-28 17:30:20 +01:00
<Button
2021-02-27 16:33:54 +01:00
type='icon'
content='EyeOff'
round
2020-12-28 17:30:20 +01:00
overlay
2022-04-30 21:29:08 +02:00
onPress={() => {
setSensitiveShown(true)
haptics('Light')
}}
2021-02-27 16:33:54 +01:00
style={{
position: 'absolute',
top: StyleConstants.Spacing.S * 2,
left: StyleConstants.Spacing.S
}}
2020-12-28 17:30:20 +01:00
/>
2021-02-27 16:33:54 +01:00
))}
</View>
)
},
2022-04-30 21:29:08 +02:00
(prev, next) => {
let isEqual = true
2020-10-30 20:03:44 +01:00
2022-09-14 22:44:23 +02:00
if (prev.status.media_attachments.length !== next.status.media_attachments.length) {
2022-04-30 21:29:08 +02:00
isEqual = false
return isEqual
}
prev.status.media_attachments.forEach((attachment, index) => {
2022-09-14 22:44:23 +02:00
if (attachment.preview_url !== next.status.media_attachments[index].preview_url) {
2022-04-30 21:29:08 +02:00
isEqual = false
}
})
return isEqual
2020-12-03 01:28:56 +01:00
}
2022-04-30 21:29:08 +02:00
)
2020-12-03 01:28:56 +01:00
2021-02-27 16:33:54 +01:00
export default TimelineAttachment