tooot/src/components/Timeline/Notifications.tsx

163 lines
5.0 KiB
TypeScript
Raw Normal View History

2021-01-24 02:25:43 +01:00
import analytics from '@components/analytics'
2021-02-08 23:47:20 +01:00
import TimelineActioned from '@components/Timeline/Shared/Actioned'
import TimelineActions from '@components/Timeline/Shared/Actions'
import TimelineAttachment from '@components/Timeline/Shared/Attachment'
import TimelineAvatar from '@components/Timeline/Shared/Avatar'
import TimelineCard from '@components/Timeline/Shared/Card'
import TimelineContent from '@components/Timeline/Shared/Content'
import TimelineHeaderNotification from '@components/Timeline/Shared/HeaderNotification'
import TimelinePoll from '@components/Timeline/Shared/Poll'
2020-12-30 14:33:33 +01:00
import { useNavigation } from '@react-navigation/native'
2021-01-24 02:25:43 +01:00
import { StackNavigationProp } from '@react-navigation/stack'
2021-01-07 19:13:09 +01:00
import { QueryKeyTimeline } from '@utils/queryHooks/timeline'
2021-02-20 19:12:44 +01:00
import { getInstanceAccount } from '@utils/slices/instancesSlice'
2020-12-13 14:04:25 +01:00
import { StyleConstants } from '@utils/styles/constants'
2021-02-08 23:47:20 +01:00
import { useTheme } from '@utils/styles/ThemeManager'
2021-02-17 21:39:38 +01:00
import { uniqBy } from 'lodash'
2020-12-30 14:33:33 +01:00
import React, { useCallback } from 'react'
import { Pressable, StyleSheet, View } from 'react-native'
2020-12-20 17:53:24 +01:00
import { useSelector } from 'react-redux'
2021-03-14 01:35:38 +01:00
import TimelineFullConversation from './Shared/FullConversation'
export interface Props {
notification: Mastodon.Notification
2021-01-07 19:13:09 +01:00
queryKey: QueryKeyTimeline
2020-12-13 23:02:54 +01:00
highlighted?: boolean
}
2020-12-13 23:02:54 +01:00
const TimelineNotifications: React.FC<Props> = ({
notification,
queryKey,
highlighted = false
}) => {
2021-02-08 23:47:20 +01:00
const { theme } = useTheme()
2021-02-20 19:12:44 +01:00
const instanceAccount = useSelector(
getInstanceAccount,
2021-02-10 00:40:44 +01:00
(prev, next) => prev?.id === next?.id
)
2021-01-24 02:25:43 +01:00
const navigation = useNavigation<
2021-01-30 01:29:15 +01:00
StackNavigationProp<Nav.TabLocalStackParamList>
2021-01-24 02:25:43 +01:00
>()
const actualAccount = notification.status
? notification.status.account
: notification.account
2021-01-24 02:25:43 +01:00
const onPress = useCallback(() => {
analytics('timeline_notification_press')
notification.status &&
2021-01-30 01:29:15 +01:00
navigation.push('Tab-Shared-Toot', {
2021-02-13 01:26:02 +01:00
toot: notification.status,
rootQueryKey: queryKey
2021-01-24 02:25:43 +01:00
})
}, [])
2020-12-27 18:43:49 +01:00
return (
2021-02-08 23:47:20 +01:00
<Pressable
2021-03-14 01:35:38 +01:00
style={[
styles.notificationView,
{
backgroundColor: theme.backgroundDefault,
2021-03-14 01:35:38 +01:00
paddingBottom: notification.status
? 0
: StyleConstants.Spacing.Global.PagePadding
}
]}
2021-02-08 23:47:20 +01:00
onPress={onPress}
>
{notification.type !== 'mention' ? (
<TimelineActioned
action={notification.type}
account={notification.account}
notification
/>
) : null}
2020-12-27 18:43:49 +01:00
2020-12-30 11:52:47 +01:00
<View
style={{
opacity:
notification.type === 'follow' ||
notification.type === 'follow_request' ||
notification.type === 'mention' ||
notification.type === 'status'
2020-12-30 11:52:47 +01:00
? 1
: 0.5
}}
>
<View style={styles.header}>
<TimelineAvatar queryKey={queryKey} account={actualAccount} />
2021-01-12 00:12:44 +01:00
<TimelineHeaderNotification
queryKey={queryKey}
notification={notification}
/>
2020-12-13 23:02:54 +01:00
</View>
2020-12-30 11:52:47 +01:00
{notification.status ? (
<View
style={{
paddingTop: highlighted ? StyleConstants.Spacing.S : 0,
paddingLeft: highlighted
? 0
: StyleConstants.Avatar.M + StyleConstants.Spacing.S
}}
>
{notification.status.content.length > 0 && (
<TimelineContent
status={notification.status}
highlighted={highlighted}
/>
)}
{notification.status.poll && (
<TimelinePoll
queryKey={queryKey}
2021-01-11 21:36:57 +01:00
statusId={notification.status.id}
2020-12-30 11:52:47 +01:00
poll={notification.status.poll}
reblog={false}
2021-02-20 19:12:44 +01:00
sameAccount={notification.account.id === instanceAccount?.id}
2020-12-30 11:52:47 +01:00
/>
)}
{notification.status.media_attachments.length > 0 && (
2020-12-30 14:33:33 +01:00
<TimelineAttachment status={notification.status} />
2020-12-30 11:52:47 +01:00
)}
{notification.status.card && (
<TimelineCard card={notification.status.card} />
)}
2021-03-14 21:48:28 +01:00
<TimelineFullConversation
queryKey={queryKey}
status={notification.status}
/>
2020-12-30 11:52:47 +01:00
</View>
) : null}
</View>
2020-12-13 23:02:54 +01:00
2021-03-16 23:15:37 +01:00
{notification.status ? (
<TimelineActions
queryKey={queryKey}
status={notification.status}
highlighted={highlighted}
accts={uniqBy(
([notification.status.account] as Mastodon.Account[] &
Mastodon.Mention[])
.concat(notification.status.mentions)
2021-03-20 17:51:38 +01:00
.filter(d => d?.id !== instanceAccount?.id),
d => d?.id
).map(d => d?.acct)}
2021-03-16 23:15:37 +01:00
reblog={false}
/>
) : null}
2020-12-27 18:43:49 +01:00
</Pressable>
)
}
const styles = StyleSheet.create({
notificationView: {
2021-03-14 01:35:38 +01:00
padding: StyleConstants.Spacing.Global.PagePadding
},
2020-12-13 23:02:54 +01:00
header: {
flex: 1,
2020-12-13 23:02:54 +01:00
width: '100%',
flexDirection: 'row'
}
})
2020-11-23 00:07:32 +01:00
export default TimelineNotifications