tooot/src/screens/Tabs/Shared/Account/Attachments.tsx

132 lines
4.1 KiB
TypeScript
Raw Normal View History

2021-01-16 00:00:31 +01:00
import GracefullyImage from '@components/GracefullyImage'
import Icon from '@components/Icon'
import { useNavigation } from '@react-navigation/native'
2021-01-24 02:25:43 +01:00
import { StackNavigationProp } from '@react-navigation/stack'
2021-08-29 15:25:38 +02:00
import { TabLocalStackParamList } from '@utils/navigation/navigators'
2021-01-16 00:00:31 +01:00
import { useTimelineQuery } from '@utils/queryHooks/timeline'
import { StyleConstants } from '@utils/styles/constants'
import { useTheme } from '@utils/styles/ThemeManager'
import React, { useCallback, useEffect } from 'react'
2022-11-29 23:44:11 +01:00
import { Dimensions, ListRenderItem, Pressable, StyleSheet, View } from 'react-native'
2021-01-16 00:00:31 +01:00
import { FlatList } from 'react-native-gesture-handler'
import Animated, { useAnimatedStyle, withTiming } from 'react-native-reanimated'
export interface Props {
account: Mastodon.Account | undefined
}
const AccountAttachments = React.memo(
({ account }: Props) => {
2022-11-29 23:44:11 +01:00
const navigation = useNavigation<StackNavigationProp<TabLocalStackParamList>>()
2022-02-12 14:51:01 +01:00
const { colors } = useTheme()
2021-01-16 00:00:31 +01:00
2021-01-31 03:09:35 +01:00
const DISPLAY_AMOUNT = 6
2021-01-16 00:00:31 +01:00
const width =
2022-11-29 23:44:11 +01:00
(Dimensions.get('screen').width - StyleConstants.Spacing.Global.PagePadding * 2) / 4
2021-01-16 00:00:31 +01:00
const queryKeyParams = {
page: 'Account_Attachments' as 'Account_Attachments',
account: account?.id
}
2021-02-11 01:33:31 +01:00
const { data, refetch } = useTimelineQuery({
2021-01-16 00:00:31 +01:00
...queryKeyParams,
options: { enabled: false }
})
useEffect(() => {
if (account?.id) {
refetch()
}
}, [account])
2021-01-31 03:09:35 +01:00
const flattenData = data?.pages
? data.pages
2021-02-11 01:33:31 +01:00
.flatMap(d => [...d.body])
.filter(status => !(status as Mastodon.Status).sensitive)
2021-01-31 03:09:35 +01:00
.splice(0, DISPLAY_AMOUNT)
: []
2021-01-16 00:00:31 +01:00
const renderItem = useCallback<ListRenderItem<Mastodon.Status>>(
({ item, index }) => {
2021-01-31 03:09:35 +01:00
if (index === DISPLAY_AMOUNT - 1) {
2021-01-16 00:00:31 +01:00
return (
<Pressable
2021-01-24 02:25:43 +01:00
onPress={() => {
2022-11-29 23:44:11 +01:00
account && navigation.push('Tab-Shared-Attachments', { account })
2021-01-24 02:25:43 +01:00
}}
2021-01-16 00:00:31 +01:00
children={
<View
style={{
marginHorizontal: StyleConstants.Spacing.Global.PagePadding,
2022-02-12 14:51:01 +01:00
backgroundColor: colors.backgroundOverlayInvert,
2021-01-16 00:00:31 +01:00
width: width,
height: width,
justifyContent: 'center',
alignItems: 'center'
}}
children={
<Icon
name='MoreHorizontal'
2022-02-12 14:51:01 +01:00
color={colors.primaryOverlay}
2021-01-16 00:00:31 +01:00
size={StyleConstants.Font.Size.L * 1.5}
/>
}
/>
}
/>
)
} else {
return (
<GracefullyImage
uri={{
2022-11-29 23:44:11 +01:00
original: item.media_attachments[0]?.preview_url || item.media_attachments[0]?.url,
2022-02-14 22:10:07 +01:00
remote: item.media_attachments[0]?.remote_url
2021-01-16 00:00:31 +01:00
}}
2022-05-03 00:49:19 +02:00
blurhash={
2022-11-29 23:44:11 +01:00
item.media_attachments[0] && (item.media_attachments[0].blurhash || undefined)
2022-05-03 00:49:19 +02:00
}
2021-01-16 00:00:31 +01:00
dimension={{ width: width, height: width }}
style={{ marginLeft: StyleConstants.Spacing.Global.PagePadding }}
2022-11-29 23:44:11 +01:00
onPress={() => navigation.push('Tab-Shared-Toot', { toot: item })}
2021-01-16 00:00:31 +01:00
/>
)
}
},
[account]
)
const styleContainer = useAnimatedStyle(() => {
if (flattenData.length) {
return {
2022-11-29 23:44:11 +01:00
height: withTiming(width + StyleConstants.Spacing.Global.PagePadding * 2),
2021-01-16 00:00:31 +01:00
paddingVertical: StyleConstants.Spacing.Global.PagePadding,
borderTopWidth: 1,
2022-02-12 14:51:01 +01:00
borderTopColor: colors.border
2021-01-16 00:00:31 +01:00
}
} else {
return {}
}
}, [flattenData.length])
return (
<Animated.View style={[styles.base, styleContainer]}>
<FlatList
horizontal
2021-01-31 03:09:35 +01:00
data={flattenData}
2021-01-16 00:00:31 +01:00
renderItem={renderItem}
showsHorizontalScrollIndicator={false}
/>
</Animated.View>
)
},
(_, next) => next.account === undefined
)
const styles = StyleSheet.create({
base: {
flex: 1
}
})
export default AccountAttachments