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

157 lines
4.0 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 { ParseEmojis } from '@components/Parse'
import relativeTime from '@components/relativeTime'
2020-12-13 14:04:25 +01:00
import { toast } from '@components/toast'
2021-01-01 16:48:16 +01:00
import { Feather } from '@expo/vector-icons'
2020-12-13 14:04:25 +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, useMemo } from 'react'
import { Pressable, StyleSheet, Text, View } from 'react-native'
import { useMutation, useQueryClient } from 'react-query'
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-13 01:24:25 +01:00
const fireMutation = async ({ id }: { id: string }) => {
const res = await client({
method: 'delete',
instance: 'local',
url: `conversations/${id}`
})
2020-12-20 17:53:24 +01:00
2020-12-13 01:24:25 +01:00
if (!res.body.error) {
toast({ type: 'success', content: '删除私信成功' })
return Promise.resolve()
} else {
toast({
type: 'error',
content: '删除私信失败,请重试',
autoHide: false
})
return Promise.reject()
}
}
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()
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')
2020-12-13 01:24:25 +01:00
toast({ type: 'error', content: '请重试', 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()
2020-12-27 16:25:29 +01:00
const actionOnPress = useCallback(() => mutate({ id: conversation.id }), [])
2020-12-13 01:24:25 +01:00
const actionChildren = useMemo(
() => (
<Feather
name='trash'
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}>
2020-11-22 00:46:23 +01:00
<View style={styles.nameAndDate}>
2021-01-01 16:48:16 +01:00
<View style={styles.namdAndAccount}>
<Text numberOfLines={1}>
<ParseEmojis
2020-12-27 16:25:29 +01:00
content={
conversation.accounts[0].display_name ||
conversation.accounts[0].username
}
emojis={conversation.accounts[0].emojis}
2021-01-01 16:48:16 +01:00
fontBold
2020-11-22 00:46:23 +01:00
/>
2021-01-01 16:48:16 +01:00
</Text>
2020-12-13 01:24:25 +01:00
<Text
style={[styles.account, { color: theme.secondary }]}
numberOfLines={1}
>
2020-12-27 16:25:29 +01:00
@{conversation.accounts[0].acct}
2020-12-13 01:24:25 +01:00
</Text>
2020-11-22 00:46:23 +01:00
</View>
2020-12-27 16:25:29 +01:00
<View style={styles.meta}>
{conversation.last_status?.created_at && (
2020-12-13 01:24:25 +01:00
<Text style={[styles.created_at, { color: theme.secondary }]}>
2020-12-27 16:25:29 +01:00
{relativeTime(conversation.last_status?.created_at)}
2020-12-13 01:24:25 +01:00
</Text>
2020-12-27 16:25:29 +01:00
)}
{conversation.unread && (
<Feather name='circle' color={theme.blue} style={styles.unread} />
)}
</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'
},
2020-11-22 00:46:23 +01:00
nameAndDate: {
2020-12-13 01:24:25 +01:00
width: '80%'
2020-11-22 00:46:23 +01:00
},
2021-01-01 16:48:16 +01:00
namdAndAccount: {
2020-12-30 11:52:47 +01:00
flexDirection: 'row',
alignItems: 'center'
2020-12-13 01:24:25 +01:00
},
account: {
flexShrink: 1,
marginLeft: StyleConstants.Spacing.XS
},
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: {
flexBasis: '20%',
flexDirection: 'row',
justifyContent: 'center'
2020-11-22 00:46:23 +01:00
}
})
export default HeaderConversation