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

163 lines
3.9 KiB
TypeScript
Raw Normal View History

2020-12-13 01:24:25 +01:00
import { Feather } from '@expo/vector-icons'
import React, { useCallback, useMemo } from 'react'
import { Pressable, StyleSheet, Text, View } from 'react-native'
2020-12-18 23:58:53 +01:00
import { useMutation, useQueryClient } from 'react-query'
2020-12-13 14:04:25 +01:00
import client from '@api/client'
import { toast } from '@components/toast'
2020-11-22 00:46:23 +01:00
2020-12-13 14:04:25 +01:00
import relativeTime from '@utils/relativeTime'
import { StyleConstants } from '@utils/styles/constants'
import { useTheme } from '@utils/styles/ThemeManager'
import Emojis from '@components/Timelines/Timeline/Shared/Emojis'
2020-11-22 00:46:23 +01:00
export interface Props {
2020-12-18 23:58:53 +01:00
queryKey: QueryKey.Timeline
2020-12-13 01:24:25 +01:00
id: string
2020-11-22 00:46:23 +01:00
account: Mastodon.Account
created_at?: Mastodon.Status['created_at']
}
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()
}
}
const HeaderConversation: React.FC<Props> = ({
queryKey,
id,
account,
created_at
}) => {
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
2020-12-18 23:58:53 +01:00
queryClient.setQueryData(queryKey, (old: any) =>
old.pages.map((paging: any) => ({
2020-12-13 01:24:25 +01:00
toots: paging.toots.filter((toot: any) => toot.id !== id),
pointer: paging.pointer
}))
)
return oldData
},
onError: (err, _, oldData) => {
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-18 23:58:53 +01:00
const actionOnPress = useCallback(() => mutate({ 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}>
<View style={styles.name}>
{account.emojis ? (
<Emojis
content={account.display_name || account.username}
emojis={account.emojis}
2020-12-13 01:24:25 +01:00
size={StyleConstants.Font.Size.M}
fontBold={true}
2020-11-22 00:46:23 +01:00
/>
) : (
2020-12-13 01:24:25 +01:00
<Text
numberOfLines={1}
style={[styles.nameWithoutEmoji, { color: theme.primary }]}
>
2020-11-22 00:46:23 +01:00
{account.display_name || account.username}
</Text>
)}
2020-12-13 01:24:25 +01:00
<Text
style={[styles.account, { color: theme.secondary }]}
numberOfLines={1}
>
@{account.acct}
</Text>
2020-11-22 00:46:23 +01:00
</View>
2020-12-13 01:24:25 +01:00
2020-11-22 00:46:23 +01:00
{created_at && (
2020-12-13 01:24:25 +01:00
<View style={styles.meta}>
<Text style={[styles.created_at, { color: theme.secondary }]}>
{relativeTime(created_at)}
</Text>
2020-11-22 00:46:23 +01:00
</View>
)}
</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
},
name: {
2020-12-13 01:24:25 +01:00
flexDirection: 'row'
},
account: {
flexShrink: 1,
marginLeft: StyleConstants.Spacing.XS
},
nameWithoutEmoji: {
fontSize: StyleConstants.Font.Size.M,
fontWeight: StyleConstants.Font.Weight.Bold
},
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: {
2020-12-13 01:24:25 +01:00
fontSize: StyleConstants.Font.Size.S
2020-11-22 00:46:23 +01:00
},
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