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

131 lines
3.3 KiB
TypeScript
Raw Normal View History

2020-12-13 14:04:25 +01:00
import client from '@api/client'
2021-01-01 16:48:16 +01:00
import haptics from '@components/haptics'
import Icon from '@components/Icon'
2020-12-13 14:04:25 +01:00
import { toast } from '@components/toast'
import { StyleConstants } from '@utils/styles/constants'
import { useTheme } from '@utils/styles/ThemeManager'
2021-01-01 16:48:16 +01:00
import React, { useCallback, useMemo } 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 { useMutation, useQueryClient } from 'react-query'
2021-01-01 23:10:47 +01:00
import HeaderSharedAccount from './HeaderShared/Account'
import HeaderSharedCreated from './HeaderShared/Created'
2020-11-22 00:46:23 +01:00
export interface Props {
2020-12-18 23:58:53 +01:00
queryKey: QueryKey.Timeline
2020-12-27 16:25:29 +01:00
conversation: Mastodon.Conversation
2020-11-22 00:46:23 +01:00
}
2020-12-27 16:25:29 +01:00
const HeaderConversation: React.FC<Props> = ({ queryKey, conversation }) => {
2020-12-18 23:58:53 +01:00
const queryClient = useQueryClient()
2021-01-01 23:10:47 +01:00
const fireMutation = useCallback(async () => {
const res = await client({
method: 'delete',
instance: 'local',
url: `conversations/${conversation.id}`
})
if (!res.body.error) {
toast({ type: 'success', message: '删除私信成功' })
return Promise.resolve()
} else {
toast({
type: 'error',
message: '删除私信失败,请重试',
autoHide: false
})
return Promise.reject()
}
}, [])
2020-12-18 23:58:53 +01:00
const { mutate } = useMutation(fireMutation, {
2020-12-13 01:24:25 +01:00
onMutate: () => {
2020-12-18 23:58:53 +01:00
queryClient.cancelQueries(queryKey)
const oldData = queryClient.getQueryData(queryKey)
2020-12-13 01:24:25 +01:00
2021-01-01 16:48:16 +01:00
haptics('Success')
2020-12-18 23:58:53 +01:00
queryClient.setQueryData(queryKey, (old: any) =>
old.pages.map((paging: any) => ({
2020-12-27 16:25:29 +01:00
toots: paging.toots.filter(
(toot: Mastodon.Conversation) => toot.id !== conversation.id
),
2020-12-13 01:24:25 +01:00
pointer: paging.pointer
}))
)
return oldData
},
onError: (err, _, oldData) => {
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-18 23:58:53 +01:00
queryClient.setQueryData(queryKey, oldData)
2020-12-13 01:24:25 +01:00
}
})
const { theme } = useTheme()
2021-01-01 23:10:47 +01:00
const actionOnPress = useCallback(() => mutate(), [])
2020-12-13 01:24:25 +01:00
const actionChildren = useMemo(
() => (
<Icon
name='Trash'
2020-12-13 01:24:25 +01:00
color={theme.secondary}
size={StyleConstants.Font.Size.M + 2}
/>
),
[]
)
2020-11-22 00:46:23 +01:00
return (
2020-12-13 01:24:25 +01:00
<View style={styles.base}>
2021-01-01 23:10:47 +01:00
<View style={styles.nameAndMeta}>
<HeaderSharedAccount account={conversation.accounts[0]} />
2020-12-27 16:25:29 +01:00
<View style={styles.meta}>
2021-01-01 23:10:47 +01:00
{conversation.last_status?.created_at ? (
<HeaderSharedCreated
created_at={conversation.last_status?.created_at}
/>
) : null}
2020-12-27 16:25:29 +01:00
{conversation.unread && (
<Icon name='Circle' color={theme.blue} style={styles.unread} />
2020-12-27 16:25:29 +01:00
)}
</View>
2020-11-22 00:46:23 +01:00
</View>
2020-12-13 01:24:25 +01:00
<Pressable
style={styles.action}
onPress={actionOnPress}
children={actionChildren}
/>
2020-11-22 00:46:23 +01:00
</View>
)
}
const styles = StyleSheet.create({
2020-12-13 01:24:25 +01:00
base: {
flex: 1,
flexDirection: 'row'
},
2021-01-01 23:10:47 +01:00
nameAndMeta: {
flex: 4
2020-12-13 01:24:25 +01:00
},
meta: {
2020-11-22 00:46:23 +01:00
flexDirection: 'row',
2020-12-13 01:24:25 +01:00
alignItems: 'center',
marginTop: StyleConstants.Spacing.XS,
marginBottom: StyleConstants.Spacing.S
2020-11-22 00:46:23 +01:00
},
created_at: {
...StyleConstants.FontStyle.S
2020-11-22 00:46:23 +01:00
},
2020-12-27 16:25:29 +01:00
unread: {
marginLeft: StyleConstants.Spacing.XS
},
2020-12-13 01:24:25 +01:00
action: {
2021-01-01 23:10:47 +01:00
flex: 1,
2020-12-13 01:24:25 +01:00
flexDirection: 'row',
justifyContent: 'center'
2020-11-22 00:46:23 +01:00
}
})
export default HeaderConversation