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'
|
|
|
|
import { useMutation, useQueryCache } 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-13 01:24:25 +01:00
|
|
|
queryKey: App.QueryKey
|
|
|
|
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}`
|
|
|
|
})
|
|
|
|
console.log(res)
|
|
|
|
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
|
|
|
|
}) => {
|
|
|
|
const queryCache = useQueryCache()
|
|
|
|
const [mutateAction] = useMutation(fireMutation, {
|
|
|
|
onMutate: () => {
|
|
|
|
queryCache.cancelQueries(queryKey)
|
|
|
|
const oldData = queryCache.getQueryData(queryKey)
|
|
|
|
|
|
|
|
queryCache.setQueryData(queryKey, old =>
|
|
|
|
(old as {}[]).map((paging: any) => ({
|
|
|
|
toots: paging.toots.filter((toot: any) => toot.id !== id),
|
|
|
|
pointer: paging.pointer
|
|
|
|
}))
|
|
|
|
)
|
|
|
|
|
|
|
|
return oldData
|
|
|
|
},
|
|
|
|
onError: (err, _, oldData) => {
|
|
|
|
toast({ type: 'error', content: '请重试', autoHide: false })
|
|
|
|
queryCache.setQueryData(queryKey, oldData)
|
|
|
|
},
|
|
|
|
onSettled: () => {
|
|
|
|
queryCache.invalidateQueries(queryKey)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const { theme } = useTheme()
|
|
|
|
|
|
|
|
const actionOnPress = useCallback(() => mutateAction({ id }), [])
|
|
|
|
|
|
|
|
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
|