tooot/src/screens/Announcements.tsx

329 lines
9.4 KiB
TypeScript
Raw Normal View History

2021-01-24 02:25:43 +01:00
import analytics from '@components/analytics'
2021-01-01 16:48:16 +01:00
import Button from '@components/Button'
import haptics from '@components/haptics'
2021-02-10 00:40:44 +01:00
import { HeaderCenter, HeaderLeft, HeaderRight } from '@components/Header'
2021-01-01 16:48:16 +01:00
import { ParseHTML } from '@components/Parse'
2021-01-14 00:43:35 +01:00
import RelativeTime from '@components/RelativeTime'
2021-01-30 01:29:15 +01:00
import { BlurView } from '@react-native-community/blur'
import { StackScreenProps } from '@react-navigation/stack'
2021-01-11 21:36:57 +01:00
import {
useAnnouncementMutation,
useAnnouncementQuery
} from '@utils/queryHooks/announcement'
2021-01-01 16:48:16 +01:00
import { StyleConstants } from '@utils/styles/constants'
import { useTheme } from '@utils/styles/ThemeManager'
2020-12-25 18:20:09 +01:00
import React, { useCallback, useEffect, useState } from 'react'
2021-01-19 01:13:45 +01:00
import { Trans, useTranslation } from 'react-i18next'
2020-12-23 01:31:11 +01:00
import {
Dimensions,
Image,
Pressable,
StyleSheet,
Text,
View
} from 'react-native'
2021-02-08 23:47:20 +01:00
import { Circle } from 'react-native-animated-spinkit'
2020-12-23 01:31:11 +01:00
import { FlatList, ScrollView } from 'react-native-gesture-handler'
import { SafeAreaView } from 'react-native-safe-area-context'
2021-01-30 01:29:15 +01:00
export type ScreenAnnouncementsProp = StackScreenProps<
Nav.RootStackParamList,
'Screen-Announcements'
>
const ScreenAnnouncements: React.FC<ScreenAnnouncementsProp> = ({
2020-12-23 15:57:20 +01:00
route: {
2021-01-07 22:18:14 +01:00
params: { showAll = false }
2020-12-23 15:57:20 +01:00
},
navigation
}) => {
2021-01-30 01:29:15 +01:00
const { mode, theme } = useTheme()
2020-12-23 01:31:11 +01:00
const [index, setIndex] = useState(0)
2021-01-19 01:13:45 +01:00
const { t } = useTranslation('sharedAnnouncements')
2020-12-23 01:31:11 +01:00
2021-01-11 21:36:57 +01:00
const query = useAnnouncementQuery({
2021-01-07 19:13:09 +01:00
showAll,
options: {
select: announcements =>
announcements.filter(announcement =>
showAll ? announcement : !announcement.read
)
}
2020-12-23 01:31:11 +01:00
})
2021-01-11 21:36:57 +01:00
const mutation = useAnnouncementMutation({
2020-12-23 01:31:11 +01:00
onSettled: () => {
2020-12-30 14:33:33 +01:00
haptics('Success')
2021-01-11 21:36:57 +01:00
query.refetch()
2020-12-23 01:31:11 +01:00
}
})
useEffect(() => {
2021-01-11 21:36:57 +01:00
if (!showAll && query.data?.length === 0) {
2020-12-23 01:31:11 +01:00
navigation.goBack()
}
2021-01-11 21:36:57 +01:00
}, [query.data])
2020-12-23 01:31:11 +01:00
const renderItem = useCallback(
({ item, index }: { item: Mastodon.Announcement; index: number }) => (
2021-01-30 01:29:15 +01:00
<View key={index} style={styles.announcementContainer}>
2020-12-23 01:31:11 +01:00
<Pressable
style={styles.pressable}
onPress={() => navigation.goBack()}
/>
<View
style={[
styles.announcement,
{
borderColor: theme.primaryDefault,
backgroundColor: theme.backgroundDefault
2020-12-23 01:31:11 +01:00
}
]}
>
2020-12-25 21:51:46 +01:00
<Text style={[styles.published, { color: theme.secondary }]}>
2021-01-19 01:13:45 +01:00
<Trans
i18nKey='sharedAnnouncements:content.published'
components={[<RelativeTime date={item.published_at} />]}
/>
2020-12-25 21:51:46 +01:00
</Text>
2020-12-23 01:31:11 +01:00
<ScrollView style={styles.scrollView} showsVerticalScrollIndicator>
2021-01-01 16:48:16 +01:00
<ParseHTML
2020-12-23 01:31:11 +01:00
content={item.content}
size='M'
emojis={item.emojis}
mentions={item.mentions}
numberOfLines={999}
/>
</ScrollView>
{item.reactions?.length ? (
<View style={styles.reactions}>
{item.reactions?.map(reaction => (
<Pressable
key={reaction.name}
2020-12-27 16:25:29 +01:00
style={[
styles.reaction,
{
borderColor: reaction.me ? theme.disabled : theme.primaryDefault,
2020-12-27 16:25:29 +01:00
backgroundColor: reaction.me
? theme.disabled
: theme.backgroundDefault
2020-12-27 16:25:29 +01:00
}
]}
2021-01-24 02:25:43 +01:00
onPress={() => {
analytics('accnouncement_reaction_press', {
current: reaction.me
})
2021-01-11 21:36:57 +01:00
mutation.mutate({
id: item.id,
2020-12-23 01:31:11 +01:00
type: 'reaction',
name: reaction.name,
me: reaction.me
})
2021-01-24 02:25:43 +01:00
}}
2020-12-23 01:31:11 +01:00
>
{reaction.url ? (
<Image
source={{ uri: reaction.url }}
style={[styles.reactionImage]}
/>
) : (
<Text style={[styles.reactionText]}>{reaction.name}</Text>
)}
{reaction.count ? (
<Text
style={[styles.reactionCount, { color: theme.primaryDefault }]}
2020-12-23 01:31:11 +01:00
>
{reaction.count}
</Text>
) : null}
</Pressable>
))}
{/* <Pressable
style={[styles.reaction, { borderColor: theme.primaryDefault }]}
2020-12-23 01:31:11 +01:00
onPress={() => invisibleTextInputRef.current?.focus()}
>
<Icon
name='Plus'
2020-12-23 01:31:11 +01:00
size={StyleConstants.Font.Size.M}
color={theme.primaryDefault}
2020-12-23 01:31:11 +01:00
/>
</Pressable> */}
</View>
) : null}
2020-12-26 23:05:17 +01:00
<Button
type='text'
2021-01-19 01:13:45 +01:00
content={
item.read ? t('content.button.read') : t('content.button.unread')
}
2021-01-11 21:36:57 +01:00
loading={mutation.isLoading}
2020-12-27 16:25:29 +01:00
disabled={item.read}
2021-01-24 02:25:43 +01:00
onPress={() => {
analytics('accnouncement_read_press')
2020-12-27 16:25:29 +01:00
!item.read &&
2021-01-24 02:25:43 +01:00
mutation.mutate({
id: item.id,
type: 'dismiss'
})
}}
2020-12-23 01:31:11 +01:00
/>
</View>
</View>
),
2020-12-27 16:25:29 +01:00
[theme]
2020-12-23 01:31:11 +01:00
)
const onMomentumScrollEnd = useCallback(
({
nativeEvent: {
contentOffset: { x },
layoutMeasurement: { width }
}
}) => {
setIndex(Math.floor(x / width))
},
[]
)
2021-01-10 02:12:14 +01:00
const ListEmptyComponent = useCallback(() => {
return (
<View
style={{
width: Dimensions.get('screen').width,
justifyContent: 'center',
alignItems: 'center'
}}
>
2021-02-08 23:47:20 +01:00
<Circle size={StyleConstants.Font.Size.L} color={theme.secondary} />
2021-01-10 02:12:14 +01:00
</View>
)
}, [])
2020-12-23 01:31:11 +01:00
return (
2021-01-30 01:29:15 +01:00
<BlurView
blurType={mode}
blurAmount={20}
style={styles.base}
reducedTransparencyFallbackColor={theme.backgroundDefault}
2021-01-30 01:29:15 +01:00
>
<SafeAreaView style={styles.base}>
2021-02-10 00:40:44 +01:00
<View
style={{
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
flexBasis: 44
2021-02-10 00:40:44 +01:00
}}
>
<HeaderLeft
content='X'
native={false}
onPress={() => navigation.goBack()}
/>
<HeaderCenter content={t('sharedAnnouncements:heading')} />
<View style={{ opacity: 0 }}>
<HeaderRight
content='MoreHorizontal'
native={false}
onPress={() => {}}
/>
</View>
</View>
2021-01-30 01:29:15 +01:00
<FlatList
horizontal
data={query.data}
pagingEnabled
renderItem={renderItem}
showsHorizontalScrollIndicator={false}
onMomentumScrollEnd={onMomentumScrollEnd}
ListEmptyComponent={ListEmptyComponent}
/>
<View style={styles.indicators}>
2021-01-30 01:29:15 +01:00
{query.data && query.data.length > 1 ? (
<>
{query.data.map((d, i) => (
<View
key={i}
style={[
styles.indicator,
{
borderColor: theme.primaryDefault,
backgroundColor: i === index ? theme.primaryDefault : undefined,
2021-01-30 01:29:15 +01:00
marginLeft:
i === query.data.length ? 0 : StyleConstants.Spacing.S
}
]}
/>
))}
</>
) : null}
</View>
</SafeAreaView>
</BlurView>
2020-12-23 01:31:11 +01:00
)
}
const styles = StyleSheet.create({
base: {
flex: 1
},
invisibleTextInput: { ...StyleSheet.absoluteFillObject },
announcementContainer: {
width: Dimensions.get('screen').width,
padding: StyleConstants.Spacing.Global.PagePadding,
justifyContent: 'center'
},
2020-12-25 21:51:46 +01:00
published: {
...StyleConstants.FontStyle.S,
2020-12-25 21:51:46 +01:00
marginBottom: StyleConstants.Spacing.S
},
2020-12-23 01:31:11 +01:00
pressable: { ...StyleSheet.absoluteFillObject },
announcement: {
flexShrink: 1,
padding: StyleConstants.Spacing.Global.PagePadding,
marginTop: StyleConstants.Spacing.Global.PagePadding,
borderWidth: 1,
borderRadius: 6
},
scrollView: {
marginBottom: StyleConstants.Spacing.Global.PagePadding / 2
},
2020-12-27 16:25:29 +01:00
reactions: {
flexDirection: 'row',
flexWrap: 'wrap',
marginBottom: StyleConstants.Spacing.Global.PagePadding / 2
},
2020-12-23 01:31:11 +01:00
reaction: {
2020-12-27 16:25:29 +01:00
borderWidth: 1,
2020-12-23 01:31:11 +01:00
padding: StyleConstants.Spacing.Global.PagePadding / 2,
marginTop: StyleConstants.Spacing.Global.PagePadding / 2,
marginBottom: StyleConstants.Spacing.Global.PagePadding / 2,
marginRight: StyleConstants.Spacing.M,
borderRadius: 6,
flexDirection: 'row'
},
reactionImage: {
width: StyleConstants.Font.LineHeight.M + 3,
height: StyleConstants.Font.LineHeight.M
},
reactionText: {
2020-12-30 14:33:33 +01:00
...StyleConstants.FontStyle.M
2020-12-23 01:31:11 +01:00
},
reactionCount: {
...StyleConstants.FontStyle.S,
2020-12-23 01:31:11 +01:00
marginLeft: StyleConstants.Spacing.S
},
indicators: {
flexDirection: 'row',
justifyContent: 'center',
2021-01-30 01:29:15 +01:00
alignItems: 'center',
minHeight: 49
2020-12-23 01:31:11 +01:00
},
indicator: {
width: StyleConstants.Spacing.S,
height: StyleConstants.Spacing.S,
borderRadius: StyleConstants.Spacing.S,
borderWidth: 1
}
})
2021-01-30 01:29:15 +01:00
export default ScreenAnnouncements