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'
|
2021-01-03 02:00:26 +01:00
|
|
|
import Icon from '@components/Icon'
|
2021-01-04 18:29:02 +01:00
|
|
|
import { TimelineData } from '@components/Timelines/Timeline'
|
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-04 18:29:02 +01:00
|
|
|
import { findIndex } from 'lodash'
|
2021-01-01 16:48:16 +01:00
|
|
|
import React, { useCallback, useMemo } from 'react'
|
2021-01-04 10:50:24 +01:00
|
|
|
import { useTranslation } from 'react-i18next'
|
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 }) => {
|
2021-01-04 10:50:24 +01:00
|
|
|
const { t } = useTranslation()
|
|
|
|
|
2020-12-18 23:58:53 +01:00
|
|
|
const queryClient = useQueryClient()
|
2021-01-04 10:50:24 +01:00
|
|
|
const fireMutation = useCallback(() => {
|
|
|
|
return client({
|
2021-01-01 23:10:47 +01:00
|
|
|
method: 'delete',
|
|
|
|
instance: 'local',
|
|
|
|
url: `conversations/${conversation.id}`
|
|
|
|
})
|
|
|
|
}, [])
|
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')
|
2021-01-04 18:29:02 +01:00
|
|
|
queryClient.setQueryData<TimelineData>(queryKey, old => {
|
|
|
|
let tootIndex = -1
|
|
|
|
const pageIndex = findIndex(old?.pages, page => {
|
|
|
|
const tempIndex = findIndex(page.toots, ['id', conversation.id])
|
|
|
|
if (tempIndex >= 0) {
|
|
|
|
tootIndex = tempIndex
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if (pageIndex >= 0 && tootIndex >= 0) {
|
|
|
|
old!.pages[pageIndex].toots.splice(tootIndex, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return old
|
|
|
|
})
|
2020-12-13 01:24:25 +01:00
|
|
|
|
|
|
|
return oldData
|
|
|
|
},
|
2021-01-04 10:50:24 +01:00
|
|
|
onError: (err: any, _, oldData) => {
|
2020-12-30 14:33:33 +01:00
|
|
|
haptics('Error')
|
2021-01-04 10:50:24 +01:00
|
|
|
toast({
|
|
|
|
type: 'error',
|
|
|
|
message: t('common:toastMessage.error.message', {
|
|
|
|
function: t(`timeline:shared.header.conversation.delete.function`)
|
|
|
|
}),
|
|
|
|
...(err.status &&
|
|
|
|
typeof err.status === 'number' &&
|
|
|
|
err.data &&
|
|
|
|
err.data.error &&
|
|
|
|
typeof err.data.error === 'string' && {
|
|
|
|
description: err.data.error
|
|
|
|
}),
|
|
|
|
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(
|
|
|
|
() => (
|
2021-01-03 02:00:26 +01:00
|
|
|
<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
|
|
|
</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: {
|
2020-12-29 00:21:05 +01:00
|
|
|
...StyleConstants.FontStyle.S
|
2020-11-22 00:46:23 +01:00
|
|
|
},
|
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
|