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'
|
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'
|
2021-01-01 23:10:47 +01:00
|
|
|
import { Pressable, StyleSheet, View } from 'react-native'
|
2021-01-01 16:48:16 +01:00
|
|
|
import { Chase } from 'react-native-animated-spinkit'
|
|
|
|
import { useQuery } from 'react-query'
|
2021-01-01 23:10:47 +01:00
|
|
|
import HeaderSharedApplication from './HeaderShared/Application'
|
|
|
|
import HeaderSharedVisibility from './HeaderShared/Visibility'
|
|
|
|
import HeaderSharedCreated from './HeaderShared/Created'
|
|
|
|
import HeaderSharedAccount from './HeaderShared/Account'
|
2020-12-12 18:22:22 +01:00
|
|
|
|
|
|
|
export interface Props {
|
|
|
|
notification: Mastodon.Notification
|
|
|
|
}
|
|
|
|
|
|
|
|
const TimelineHeaderNotification: React.FC<Props> = ({ notification }) => {
|
|
|
|
const { theme } = useTheme()
|
|
|
|
|
|
|
|
const { status, data, refetch } = useQuery(
|
|
|
|
['Relationship', { id: notification.account.id }],
|
|
|
|
relationshipFetch,
|
|
|
|
{
|
|
|
|
enabled: false
|
|
|
|
}
|
|
|
|
)
|
|
|
|
const [updateData, setUpdateData] = useState<
|
|
|
|
Mastodon.Relationship | undefined
|
|
|
|
>()
|
|
|
|
|
|
|
|
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')
|
2021-01-01 23:10:47 +01:00
|
|
|
toast({ type: 'error', message: '请重试', autoHide: false })
|
2020-12-12 18:22:22 +01:00
|
|
|
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}>
|
2021-01-01 23:10:47 +01:00
|
|
|
<View style={styles.accountAndMeta}>
|
|
|
|
<HeaderSharedAccount
|
|
|
|
account={
|
|
|
|
notification.status
|
|
|
|
? notification.status.account
|
|
|
|
: notification.account
|
|
|
|
}
|
|
|
|
/>
|
2020-12-12 18:22:22 +01:00
|
|
|
<View style={styles.meta}>
|
2021-01-01 23:10:47 +01:00
|
|
|
<HeaderSharedCreated created_at={notification.created_at} />
|
|
|
|
<HeaderSharedVisibility
|
|
|
|
visibility={notification.status?.visibility}
|
|
|
|
/>
|
|
|
|
<HeaderSharedApplication
|
|
|
|
application={notification.status?.application}
|
|
|
|
/>
|
2020-12-12 18:22:22 +01:00
|
|
|
</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'
|
|
|
|
},
|
2021-01-01 23:10:47 +01:00
|
|
|
accountAndMeta: {
|
|
|
|
flex: 4
|
2020-12-12 18:22:22 +01:00
|
|
|
},
|
|
|
|
meta: {
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center',
|
|
|
|
marginTop: StyleConstants.Spacing.XS,
|
|
|
|
marginBottom: StyleConstants.Spacing.S
|
|
|
|
},
|
|
|
|
relationship: {
|
2021-01-01 23:10:47 +01:00
|
|
|
flex: 1,
|
2020-12-12 18:22:22 +01:00
|
|
|
flexDirection: 'row',
|
|
|
|
justifyContent: 'center'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
export default React.memo(TimelineHeaderNotification, () => true)
|