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

172 lines
5.2 KiB
TypeScript
Raw Normal View History

2021-01-24 02:25:43 +01:00
import analytics from '@components/analytics'
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'
import { StyleConstants } from '@utils/styles/constants'
import layoutAnimation from '@utils/styles/layoutAnimation'
2020-12-25 21:09:43 +01:00
import React, { useCallback, useMemo, useState } from 'react'
2021-01-01 23:10:47 +01:00
import { useTranslation } from 'react-i18next'
2020-12-28 17:30:20 +01:00
import { Pressable, StyleSheet, View } from 'react-native'
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
2021-02-27 16:33:54 +01:00
const [sensitiveShown, setSensitiveShown] = useState(status.sensitive)
const onPressBlurView = useCallback(() => {
analytics('timeline_shared_attachment_blurview_press_show')
layoutAnimation()
setSensitiveShown(false)
haptics('Light')
}, [])
const onPressShow = useCallback(() => {
analytics('timeline_shared_attachment_blurview_press_hide')
setSensitiveShown(true)
haptics('Light')
}, [])
2020-12-03 01:28:56 +01:00
2021-02-27 16:33:54 +01:00
let imageUrls: Nav.RootStackParamList['Screen-ImagesViewer']['imageUrls'] = []
const navigation = useNavigation()
2021-03-06 23:42:29 +01:00
const navigateToImagesViewer = (id: string) =>
2021-02-27 16:33:54 +01:00
navigation.navigate('Screen-ImagesViewer', {
imageUrls,
2021-03-06 23:42:29 +01:00
id
2021-02-27 16:33:54 +01:00
})
const attachments = useMemo(
() =>
status.media_attachments.map((attachment, index) => {
switch (attachment.type) {
case 'image':
imageUrls.push({
2021-03-06 23:42:29 +01:00
id: attachment.id,
2021-02-27 16:33:54 +01:00
url: attachment.url,
remote_url: attachment.remote_url,
width: attachment.meta?.original?.width,
2021-03-06 23:42:29 +01:00
height: attachment.meta?.original?.height
2021-02-27 16:33:54 +01:00
})
return (
<AttachmentImage
key={index}
total={status.media_attachments.length}
index={index}
sensitiveShown={sensitiveShown}
image={attachment}
navigateToImagesViewer={navigateToImagesViewer}
/>
)
case 'video':
return (
<AttachmentVideo
key={index}
total={status.media_attachments.length}
index={index}
sensitiveShown={sensitiveShown}
video={attachment}
/>
)
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:
return (
<AttachmentUnsupported
key={index}
total={status.media_attachments.length}
index={index}
sensitiveShown={sensitiveShown}
attachment={attachment}
/>
)
}
}),
[sensitiveShown]
)
2020-12-25 18:20:09 +01:00
2021-02-27 16:33:54 +01:00
return (
<View>
<View style={styles.container} children={attachments} />
2020-12-25 18:20:09 +01:00
2021-02-27 16:33:54 +01:00
{status.sensitive &&
(sensitiveShown ? (
<Pressable style={styles.sensitiveBlur}>
<Button
type='text'
content={t('shared.attachment.sensitive.button')}
overlay
onPress={onPressBlurView}
/>
</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
2021-02-27 16:33:54 +01:00
onPress={onPressShow}
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>
)
},
() => true
)
2020-10-30 20:03:44 +01:00
2020-12-03 01:28:56 +01:00
const styles = StyleSheet.create({
2021-01-16 00:00:31 +01:00
container: {
2020-12-25 18:20:09 +01:00
marginTop: StyleConstants.Spacing.S,
flex: 1,
flexDirection: 'row',
flexWrap: 'wrap',
2020-12-28 16:54:19 +01:00
justifyContent: 'center',
2020-12-25 18:20:09 +01:00
alignContent: 'stretch'
},
sensitiveBlur: {
2020-12-03 01:28:56 +01:00
position: 'absolute',
width: '100%',
2020-12-25 18:20:09 +01:00
height: '100%',
2020-12-03 01:28:56 +01:00
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
2020-12-25 18:20:09 +01:00
sensitiveBlurButton: {
padding: StyleConstants.Spacing.S,
borderRadius: 6
},
2020-12-03 01:28:56 +01:00
sensitiveText: {
...StyleConstants.FontStyle.M
2020-12-03 01:28:56 +01:00
}
})
2021-02-27 16:33:54 +01:00
export default TimelineAttachment