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

122 lines
3.0 KiB
TypeScript
Raw Normal View History

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'
2021-01-11 21:36:57 +01:00
import {
QueryKeyTimeline,
useTimelineMutation
} from '@utils/queryHooks/timeline'
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 { useTranslation } from 'react-i18next'
2021-01-01 23:10:47 +01:00
import { Pressable, StyleSheet, View } from 'react-native'
2021-01-11 21:36:57 +01:00
import { useQueryClient } from 'react-query'
2021-01-01 23:10:47 +01:00
import HeaderSharedAccount from './HeaderShared/Account'
import HeaderSharedCreated from './HeaderShared/Created'
2021-01-11 21:36:57 +01:00
import HeaderSharedMuted from './HeaderShared/Muted'
2020-11-22 00:46:23 +01:00
export interface Props {
2021-01-07 19:13:09 +01:00
queryKey: QueryKeyTimeline
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-19 01:13:45 +01:00
const { t } = useTranslation('componentTimeline')
2020-12-18 23:58:53 +01:00
const queryClient = useQueryClient()
2021-01-11 21:36:57 +01:00
const mutation = useTimelineMutation({
queryClient,
onMutate: true,
onError: (err: any, _, oldData) => {
2020-12-30 14:33:33 +01:00
haptics('Error')
toast({
type: 'error',
message: t('common:toastMessage.error.message', {
2021-01-19 01:13:45 +01:00
function: t(`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-11 21:36:57 +01:00
const actionOnPress = useCallback(
() =>
mutation.mutate({
type: 'deleteItem',
source: 'conversations',
queryKey,
id: conversation.id
}),
[]
)
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}
2021-01-13 01:03:46 +01:00
size={StyleConstants.Font.Size.L}
2020-12-13 01:24:25 +01:00
/>
),
[]
)
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}
2021-01-11 21:36:57 +01:00
<HeaderSharedMuted muted={conversation.last_status?.muted} />
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-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