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

57 lines
1.7 KiB
TypeScript
Raw Normal View History

2021-01-24 02:25:43 +01:00
import analytics from '@components/analytics'
2021-01-18 00:23:40 +01:00
import GracefullyImage from '@components/GracefullyImage'
import { StyleConstants } from '@utils/styles/constants'
2021-01-16 00:00:31 +01:00
import React, { useCallback } from 'react'
2021-03-27 18:16:10 +01:00
import { StyleSheet, View } from 'react-native'
2021-01-18 00:23:40 +01:00
import attachmentAspectRatio from './aspectRatio'
2020-12-25 18:20:09 +01:00
export interface Props {
2021-01-18 00:23:40 +01:00
total: number
index: number
2020-12-25 18:20:09 +01:00
sensitiveShown: boolean
image: Mastodon.AttachmentImage
2021-03-06 23:42:29 +01:00
navigateToImagesViewer: (imageIndex: string) => void
2020-12-25 18:20:09 +01:00
}
2021-02-27 16:33:54 +01:00
const AttachmentImage = React.memo(
({ total, index, sensitiveShown, image, navigateToImagesViewer }: Props) => {
const onPress = useCallback(() => {
analytics('timeline_shared_attachment_image_press', { id: image.id })
2021-03-06 23:42:29 +01:00
navigateToImagesViewer(image.id)
2021-02-27 16:33:54 +01:00
}, [])
2020-12-25 18:20:09 +01:00
2021-02-27 16:33:54 +01:00
return (
2021-03-27 18:16:10 +01:00
<View style={styles.base}>
<GracefullyImage
2021-04-09 21:43:12 +02:00
accessibilityLabel={image.description}
2021-03-27 18:16:10 +01:00
hidden={sensitiveShown}
uri={{ original: image.preview_url, remote: image.remote_url }}
blurhash={image.blurhash}
onPress={onPress}
2022-02-12 22:16:29 +01:00
style={{
aspectRatio:
total > 1 ||
!image.meta?.original?.width ||
!image.meta?.original?.height
? attachmentAspectRatio({ total, index })
2022-02-14 21:07:10 +01:00
: image.meta.original.height / image.meta.original.width > 1
? 1
2022-02-12 22:16:29 +01:00
: image.meta.original.width / image.meta.original.height
}}
2021-03-27 18:16:10 +01:00
/>
</View>
2021-02-27 16:33:54 +01:00
)
},
(prev, next) => prev.sensitiveShown === next.sensitiveShown
)
2020-12-25 18:20:09 +01:00
const styles = StyleSheet.create({
2020-12-28 16:54:19 +01:00
base: {
2020-12-25 18:20:09 +01:00
flex: 1,
flexBasis: '50%',
padding: StyleConstants.Spacing.XS / 2
}
})
2021-02-27 16:33:54 +01:00
export default AttachmentImage