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

166 lines
4.7 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'
2020-12-30 14:33:33 +01:00
import AttachmentAudio from '@components/Timelines/Timeline/Shared/Attachment/Audio'
import AttachmentImage from '@components/Timelines/Timeline/Shared/Attachment/Image'
import AttachmentUnsupported from '@components/Timelines/Timeline/Shared/Attachment/Unsupported'
import AttachmentVideo from '@components/Timelines/Timeline/Shared/Attachment/Video'
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-12-25 18:20:09 +01:00
import { IImageInfo } from 'react-native-image-zoom-viewer/built/image-viewer.type'
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
}
2020-12-30 14:33:33 +01:00
const TimelineAttachment: React.FC<Props> = ({ status }) => {
2021-01-01 23:10:47 +01:00
const { t } = useTranslation('timeline')
2020-12-25 18:20:09 +01:00
const [sensitiveShown, setSensitiveShown] = useState(status.sensitive)
2020-12-03 01:28:56 +01:00
const onPressBlurView = useCallback(() => {
2020-12-26 14:40:10 +01:00
layoutAnimation()
2020-12-03 01:28:56 +01:00
setSensitiveShown(false)
2020-12-30 14:33:33 +01:00
haptics('Medium')
}, [])
const onPressShow = useCallback(() => {
setSensitiveShown(true)
haptics('Medium')
2020-12-03 01:28:56 +01:00
}, [])
2020-12-25 18:20:09 +01:00
let imageUrls: (IImageInfo & {
preview_url: Mastodon.AttachmentImage['preview_url']
remote_url?: Mastodon.AttachmentImage['remote_url']
imageIndex: number
})[] = []
const navigation = useNavigation()
const navigateToImagesViewer = (imageIndex: number) =>
navigation.navigate('Screen-Shared-ImagesViewer', {
imageUrls,
imageIndex
})
const attachments = useMemo(
() =>
status.media_attachments.map((attachment, index) => {
switch (attachment.type) {
case 'image':
imageUrls.push({
url: attachment.url,
width: attachment.meta?.original?.width,
height: attachment.meta?.original?.height,
preview_url: attachment.preview_url,
remote_url: attachment.remote_url,
imageIndex: index
})
return (
<AttachmentImage
key={index}
sensitiveShown={sensitiveShown}
image={attachment}
imageIndex={index}
navigateToImagesViewer={navigateToImagesViewer}
/>
)
case 'video':
return (
<AttachmentVideo
key={index}
sensitiveShown={sensitiveShown}
video={attachment}
/>
)
case 'gifv':
return (
<AttachmentVideo
key={index}
sensitiveShown={sensitiveShown}
video={attachment}
/>
)
case 'audio':
2020-12-25 21:09:43 +01:00
return (
<AttachmentAudio
key={index}
sensitiveShown={sensitiveShown}
audio={attachment}
/>
)
2020-12-25 18:20:09 +01:00
default:
2020-12-30 14:33:33 +01:00
return (
<AttachmentUnsupported
key={index}
sensitiveShown={sensitiveShown}
attachment={attachment}
/>
)
2020-12-25 18:20:09 +01:00
}
}),
[sensitiveShown]
)
2020-10-30 20:03:44 +01:00
return (
2020-12-28 16:54:19 +01:00
<View style={styles.base}>
2020-12-25 18:20:09 +01:00
{attachments}
2020-12-25 21:09:43 +01:00
{status.sensitive &&
(sensitiveShown ? (
<Pressable style={styles.sensitiveBlur}>
2020-12-28 17:30:20 +01:00
<Button
type='text'
2021-01-01 23:10:47 +01:00
content={t('shared.attachment.sensitive.button')}
2020-12-28 17:30:20 +01:00
overlay
2020-12-25 21:09:43 +01:00
onPress={onPressBlurView}
2020-12-28 17:30:20 +01:00
/>
2020-12-25 21:09:43 +01:00
</Pressable>
) : (
2020-12-28 17:30:20 +01:00
<Button
type='icon'
content='eye-off'
round
overlay
2020-12-30 14:33:33 +01:00
onPress={onPressShow}
2020-12-28 17:30:20 +01:00
style={{
position: 'absolute',
top: StyleConstants.Spacing.S,
left: StyleConstants.Spacing.S
}}
/>
2020-12-25 21:09:43 +01:00
))}
2020-10-30 20:03:44 +01:00
</View>
)
}
2020-12-03 01:28:56 +01:00
const styles = StyleSheet.create({
2020-12-25 18:20:09 +01:00
base: {
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'
},
2020-12-28 16:54:19 +01:00
container: {
flexBasis: '50%',
aspectRatio: 16 / 9
},
2020-12-25 18:20:09 +01:00
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
}
})
export default React.memo(TimelineAttachment, () => true)