2020-12-27 00:32:51 +01:00
|
|
|
import client from '@api/client'
|
2020-12-12 18:22:22 +01:00
|
|
|
import { Feather } from '@expo/vector-icons'
|
2021-01-01 16:48:16 +01:00
|
|
|
import haptics from '@components/haptics'
|
|
|
|
import openLink from '@components/openLink'
|
|
|
|
import { ParseEmojis } from '@components/Parse'
|
|
|
|
import relativeTime from '@components/relativeTime'
|
2020-12-27 00:32:51 +01:00
|
|
|
import { toast } from '@components/toast'
|
2020-12-13 14:04:25 +01:00
|
|
|
import { relationshipFetch } from '@utils/fetches/relationshipFetch'
|
2021-01-01 16:48:16 +01:00
|
|
|
import { StyleConstants } from '@utils/styles/constants'
|
2020-12-27 00:32:51 +01:00
|
|
|
import { useTheme } from '@utils/styles/ThemeManager'
|
2021-01-01 16:48:16 +01:00
|
|
|
import React, { useCallback, useEffect, useMemo, useState } from 'react'
|
|
|
|
import { Pressable, StyleSheet, Text, View } from 'react-native'
|
|
|
|
import { Chase } from 'react-native-animated-spinkit'
|
|
|
|
import { useQuery } from 'react-query'
|
2020-12-12 18:22:22 +01:00
|
|
|
|
|
|
|
export interface Props {
|
|
|
|
notification: Mastodon.Notification
|
|
|
|
}
|
|
|
|
|
|
|
|
const TimelineHeaderNotification: React.FC<Props> = ({ notification }) => {
|
2020-12-30 11:52:47 +01:00
|
|
|
const actualAccount = notification.status
|
|
|
|
? notification.status.account
|
|
|
|
: notification.account
|
|
|
|
const name = actualAccount.display_name || actualAccount.username
|
|
|
|
const emojis = actualAccount.emojis
|
|
|
|
const account = actualAccount.acct
|
2020-12-12 18:22:22 +01:00
|
|
|
const { theme } = useTheme()
|
|
|
|
|
|
|
|
const [since, setSince] = useState(relativeTime(notification.created_at))
|
|
|
|
|
|
|
|
const { status, data, refetch } = useQuery(
|
|
|
|
['Relationship', { id: notification.account.id }],
|
|
|
|
relationshipFetch,
|
|
|
|
{
|
|
|
|
enabled: false
|
|
|
|
}
|
|
|
|
)
|
|
|
|
const [updateData, setUpdateData] = useState<
|
|
|
|
Mastodon.Relationship | undefined
|
|
|
|
>()
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setTimeout(() => {
|
|
|
|
setSince(relativeTime(notification.created_at))
|
|
|
|
}, 1000)
|
|
|
|
}, [since])
|
|
|
|
|
2020-12-20 18:41:28 +01:00
|
|
|
const applicationOnPress = useCallback(
|
|
|
|
async () =>
|
|
|
|
notification.status?.application.website &&
|
|
|
|
(await openLink(notification.status.application.website)),
|
|
|
|
[]
|
|
|
|
)
|
2020-12-12 18:22:22 +01:00
|
|
|
|
|
|
|
const relationshipOnPress = useCallback(() => {
|
|
|
|
client({
|
|
|
|
method: 'post',
|
|
|
|
instance: 'local',
|
|
|
|
url: `accounts/${notification.account.id}/${
|
|
|
|
updateData
|
|
|
|
? updateData.following || updateData.requested
|
|
|
|
? 'un'
|
|
|
|
: ''
|
2020-12-15 00:01:48 +01:00
|
|
|
: data!.following || data!.requested
|
2020-12-12 18:22:22 +01:00
|
|
|
? 'un'
|
|
|
|
: ''
|
|
|
|
}follow`
|
|
|
|
}).then(res => {
|
2020-12-15 00:01:48 +01:00
|
|
|
if (res.body.id === (updateData && updateData.id) || data!.id) {
|
2020-12-12 18:22:22 +01:00
|
|
|
setUpdateData(res.body)
|
2020-12-30 14:33:33 +01:00
|
|
|
haptics('Success')
|
2020-12-12 18:22:22 +01:00
|
|
|
return Promise.resolve()
|
|
|
|
} else {
|
2020-12-30 14:33:33 +01:00
|
|
|
haptics('Error')
|
2020-12-12 18:22:22 +01:00
|
|
|
toast({ type: 'error', content: '请重试', autoHide: false })
|
|
|
|
return Promise.reject()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}, [data, updateData])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (notification.type === 'follow') {
|
|
|
|
refetch()
|
|
|
|
}
|
|
|
|
}, [notification.type])
|
|
|
|
const relationshipIcon = useMemo(() => {
|
|
|
|
switch (status) {
|
|
|
|
case 'idle':
|
|
|
|
case 'loading':
|
2020-12-27 00:32:51 +01:00
|
|
|
return (
|
|
|
|
<Chase size={StyleConstants.Font.Size.L} color={theme.secondary} />
|
|
|
|
)
|
2020-12-12 18:22:22 +01:00
|
|
|
case 'success':
|
|
|
|
return (
|
|
|
|
<Pressable onPress={relationshipOnPress}>
|
|
|
|
<Feather
|
|
|
|
name={
|
|
|
|
updateData
|
|
|
|
? updateData.following
|
|
|
|
? 'user-check'
|
|
|
|
: updateData.requested
|
|
|
|
? 'loader'
|
|
|
|
: 'user-plus'
|
2020-12-15 00:01:48 +01:00
|
|
|
: data!.following
|
2020-12-12 18:22:22 +01:00
|
|
|
? 'user-check'
|
2020-12-15 00:01:48 +01:00
|
|
|
: data!.requested
|
2020-12-12 18:22:22 +01:00
|
|
|
? 'loader'
|
|
|
|
: 'user-plus'
|
|
|
|
}
|
|
|
|
color={
|
|
|
|
updateData
|
|
|
|
? updateData.following
|
|
|
|
? theme.primary
|
|
|
|
: theme.secondary
|
2020-12-15 00:01:48 +01:00
|
|
|
: data!.following
|
2020-12-12 18:22:22 +01:00
|
|
|
? theme.primary
|
|
|
|
: theme.secondary
|
|
|
|
}
|
|
|
|
size={StyleConstants.Font.Size.M + 2}
|
|
|
|
/>
|
|
|
|
</Pressable>
|
|
|
|
)
|
|
|
|
default:
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}, [status, data, updateData])
|
|
|
|
|
|
|
|
return (
|
|
|
|
<View style={styles.base}>
|
2020-12-13 01:24:25 +01:00
|
|
|
<View style={styles.nameAndMeta}>
|
2020-12-30 11:52:47 +01:00
|
|
|
<View style={styles.nameAndAccount}>
|
2021-01-01 16:48:16 +01:00
|
|
|
<Text numberOfLines={1}>
|
|
|
|
<ParseEmojis content={name} emojis={emojis} fontBold />
|
|
|
|
</Text>
|
2020-12-13 01:24:25 +01:00
|
|
|
<Text
|
|
|
|
style={[styles.account, { color: theme.secondary }]}
|
|
|
|
numberOfLines={1}
|
|
|
|
>
|
|
|
|
@{account}
|
|
|
|
</Text>
|
2020-12-12 18:22:22 +01:00
|
|
|
</View>
|
|
|
|
|
|
|
|
<View style={styles.meta}>
|
|
|
|
<View>
|
|
|
|
<Text style={[styles.created_at, { color: theme.secondary }]}>
|
|
|
|
{since}
|
|
|
|
</Text>
|
|
|
|
</View>
|
|
|
|
{notification.status?.visibility === 'private' && (
|
|
|
|
<Feather
|
|
|
|
name='lock'
|
|
|
|
size={StyleConstants.Font.Size.S}
|
|
|
|
color={theme.secondary}
|
|
|
|
style={styles.visibility}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{notification.status?.application &&
|
|
|
|
notification.status?.application.name !== 'Web' && (
|
|
|
|
<View>
|
|
|
|
<Text
|
|
|
|
onPress={applicationOnPress}
|
|
|
|
style={[styles.application, { color: theme.secondary }]}
|
|
|
|
>
|
|
|
|
发自于 - {notification.status?.application.name}
|
|
|
|
</Text>
|
|
|
|
</View>
|
|
|
|
)}
|
|
|
|
</View>
|
|
|
|
</View>
|
2020-12-13 01:24:25 +01:00
|
|
|
|
2020-12-12 18:22:22 +01:00
|
|
|
{notification.type === 'follow' && (
|
|
|
|
<View style={styles.relationship}>{relationshipIcon}</View>
|
|
|
|
)}
|
|
|
|
</View>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
base: {
|
|
|
|
flex: 1,
|
|
|
|
flexDirection: 'row'
|
|
|
|
},
|
2020-12-13 01:24:25 +01:00
|
|
|
nameAndMeta: {
|
|
|
|
width: '80%'
|
2020-12-12 18:22:22 +01:00
|
|
|
},
|
2020-12-30 11:52:47 +01:00
|
|
|
nameAndAccount: {
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center'
|
2020-12-12 18:22:22 +01:00
|
|
|
},
|
|
|
|
account: {
|
|
|
|
flexShrink: 1,
|
|
|
|
marginLeft: StyleConstants.Spacing.XS
|
|
|
|
},
|
|
|
|
meta: {
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center',
|
|
|
|
marginTop: StyleConstants.Spacing.XS,
|
|
|
|
marginBottom: StyleConstants.Spacing.S
|
|
|
|
},
|
|
|
|
created_at: {
|
2020-12-29 00:21:05 +01:00
|
|
|
...StyleConstants.FontStyle.S
|
2020-12-12 18:22:22 +01:00
|
|
|
},
|
|
|
|
visibility: {
|
|
|
|
marginLeft: StyleConstants.Spacing.S
|
|
|
|
},
|
|
|
|
application: {
|
2020-12-29 00:21:05 +01:00
|
|
|
...StyleConstants.FontStyle.S,
|
2020-12-12 18:22:22 +01:00
|
|
|
marginLeft: StyleConstants.Spacing.S
|
|
|
|
},
|
|
|
|
relationship: {
|
|
|
|
flexBasis: '20%',
|
|
|
|
flexDirection: 'row',
|
|
|
|
justifyContent: 'center'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
export default React.memo(TimelineHeaderNotification, () => true)
|