tooot/src/screens/Shared/Announcements.tsx

310 lines
8.7 KiB
TypeScript
Raw Normal View History

2021-01-01 16:48:16 +01:00
import client from '@api/client'
import Button from '@components/Button'
import haptics from '@components/haptics'
import { ParseHTML } from '@components/Parse'
import relativeTime from '@components/relativeTime'
2021-01-01 23:10:47 +01:00
import { useBottomTabBarHeight } from '@react-navigation/bottom-tabs'
2021-01-07 19:13:09 +01:00
import hookAnnouncement 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-01 23:10:47 +01:00
import { useTranslation } from 'react-i18next'
2020-12-23 01:31:11 +01:00
import {
Dimensions,
Image,
Pressable,
StyleSheet,
Text,
View
} from 'react-native'
import { FlatList, ScrollView } from 'react-native-gesture-handler'
import { SafeAreaView } from 'react-native-safe-area-context'
2021-01-07 19:13:09 +01:00
import { useMutation } from 'react-query'
2021-01-07 22:18:14 +01:00
import { SharedAnnouncementsProp } from './sharedScreens'
2020-12-23 01:31:11 +01:00
const fireMutation = async ({
announcementId,
type,
name,
me
}: {
announcementId: Mastodon.Announcement['id']
type: 'reaction' | 'dismiss'
name?: Mastodon.AnnouncementReaction['name']
me?: boolean
}) => {
switch (type) {
case 'reaction':
2021-01-07 19:13:09 +01:00
return client<{}>({
2020-12-23 01:31:11 +01:00
method: me ? 'delete' : 'put',
instance: 'local',
url: `announcements/${announcementId}/reactions/${name}`
})
case 'dismiss':
2021-01-07 19:13:09 +01:00
return client<{}>({
2020-12-23 01:31:11 +01:00
method: 'post',
instance: 'local',
url: `announcements/${announcementId}/dismiss`
})
}
}
2021-01-07 22:18:14 +01:00
const ScreenSharedAnnouncements: React.FC<SharedAnnouncementsProp> = ({
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
}) => {
2020-12-25 18:20:09 +01:00
const { theme } = useTheme()
2020-12-23 01:31:11 +01:00
const bottomTabBarHeight = useBottomTabBarHeight()
const [index, setIndex] = useState(0)
2021-01-01 23:10:47 +01:00
const { t, i18n } = useTranslation()
2020-12-23 01:31:11 +01:00
2021-01-07 19:13:09 +01:00
const { data, refetch } = hookAnnouncement({
showAll,
options: {
select: announcements =>
announcements.filter(announcement =>
showAll ? announcement : !announcement.read
)
}
2020-12-23 01:31:11 +01:00
})
2020-12-27 16:25:29 +01:00
const queryMutation = useMutation(fireMutation, {
2020-12-23 01:31:11 +01:00
onSettled: () => {
2020-12-30 14:33:33 +01:00
haptics('Success')
2020-12-23 01:31:11 +01:00
refetch()
}
})
useEffect(() => {
2020-12-23 15:57:20 +01:00
if (!showAll && data?.length === 0) {
2020-12-23 01:31:11 +01:00
navigation.goBack()
}
}, [data])
const renderItem = useCallback(
({ item, index }: { item: Mastodon.Announcement; index: number }) => (
2020-12-27 16:25:29 +01:00
<View
key={index}
style={[
styles.announcementContainer,
{ backgroundColor: theme.background }
]}
>
2020-12-23 01:31:11 +01:00
<Pressable
style={styles.pressable}
onPress={() => navigation.goBack()}
/>
<View
style={[
styles.announcement,
{
borderColor: theme.primary,
backgroundColor: theme.background
}
]}
>
2020-12-25 21:51:46 +01:00
<Text style={[styles.published, { color: theme.secondary }]}>
2021-01-01 23:10:47 +01:00
{relativeTime(item.published_at, i18n.language)}
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.primary,
backgroundColor: reaction.me
? theme.disabled
: theme.background
}
]}
2020-12-23 01:31:11 +01:00
onPress={() =>
2020-12-27 16:25:29 +01:00
queryMutation.mutate({
2020-12-23 01:31:11 +01:00
announcementId: item.id,
type: 'reaction',
name: reaction.name,
me: reaction.me
})
}
>
{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.primary }]}
>
{reaction.count}
</Text>
) : null}
</Pressable>
))}
{/* <Pressable
style={[styles.reaction, { borderColor: theme.primary }]}
onPress={() => invisibleTextInputRef.current?.focus()}
>
<Icon
name='Plus'
2020-12-23 01:31:11 +01:00
size={StyleConstants.Font.Size.M}
color={theme.primary}
/>
</Pressable> */}
</View>
) : null}
2020-12-26 23:05:17 +01:00
<Button
type='text'
2020-12-27 16:25:29 +01:00
content={item.read ? '已读' : '标记阅读'}
loading={queryMutation.isLoading}
disabled={item.read}
onPress={() =>
!item.read &&
queryMutation.mutate({
type: 'dismiss',
announcementId: item.id
})
}
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))
},
[]
)
return (
2020-12-25 18:20:09 +01:00
<SafeAreaView style={[styles.base, { backgroundColor: theme.background }]}>
2020-12-23 01:31:11 +01:00
<View style={[styles.header, { height: bottomTabBarHeight }]}>
<Text style={[styles.headerText, { color: theme.primary }]}></Text>
</View>
<FlatList
horizontal
data={data}
pagingEnabled
renderItem={renderItem}
showsHorizontalScrollIndicator={false}
onMomentumScrollEnd={onMomentumScrollEnd}
/>
<View style={[styles.indicators, { height: bottomTabBarHeight }]}>
{data && data.length > 1 ? (
<>
{data.map((d, i) => (
<View
key={i}
style={[
styles.indicator,
{
borderColor: theme.primary,
backgroundColor: i === index ? theme.primary : undefined,
marginLeft: i === data.length ? 0 : StyleConstants.Spacing.S
}
]}
/>
))}
</>
) : null}
</View>
</SafeAreaView>
)
}
const styles = StyleSheet.create({
base: {
flex: 1
},
invisibleTextInput: { ...StyleSheet.absoluteFillObject },
header: {
justifyContent: 'center',
alignItems: 'center'
},
headerText: {
...StyleConstants.FontStyle.L,
2020-12-23 01:31:11 +01:00
fontWeight: StyleConstants.Font.Weight.Bold
},
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',
alignItems: 'center'
},
indicator: {
width: StyleConstants.Spacing.S,
height: StyleConstants.Spacing.S,
borderRadius: StyleConstants.Spacing.S,
borderWidth: 1
}
})
export default ScreenSharedAnnouncements