tooot/src/components/Timelines/Timeline/Shared/HeaderNotification.tsx

158 lines
4.4 KiB
TypeScript
Raw Normal View History

import client from '@api/client'
import { Feather } from '@expo/vector-icons'
2021-01-01 16:48:16 +01:00
import haptics from '@components/haptics'
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'
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'
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
? 'un'
: ''
}follow`
}).then(res => {
2020-12-15 00:01:48 +01:00
if (res.body.id === (updateData && updateData.id) || data!.id) {
setUpdateData(res.body)
2020-12-30 14:33:33 +01:00
haptics('Success')
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 })
return Promise.reject()
}
})
}, [data, updateData])
useEffect(() => {
if (notification.type === 'follow') {
refetch()
}
}, [notification.type])
const relationshipIcon = useMemo(() => {
switch (status) {
case 'idle':
case 'loading':
return (
<Chase size={StyleConstants.Font.Size.L} color={theme.secondary} />
)
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
? 'user-check'
2020-12-15 00:01:48 +01:00
: data!.requested
? 'loader'
: 'user-plus'
}
color={
updateData
? updateData.following
? theme.primary
: theme.secondary
2020-12-15 00:01:48 +01:00
: data!.following
? 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
}
/>
<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}
/>
</View>
</View>
2020-12-13 01:24:25 +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
},
meta: {
flexDirection: 'row',
alignItems: 'center',
marginTop: StyleConstants.Spacing.XS,
marginBottom: StyleConstants.Spacing.S
},
relationship: {
2021-01-01 23:10:47 +01:00
flex: 1,
flexDirection: 'row',
justifyContent: 'center'
}
})
export default React.memo(TimelineHeaderNotification, () => true)