tooot/src/components/Timelines/Timeline/Conversation.tsx

139 lines
3.8 KiB
TypeScript
Raw Normal View History

2021-01-11 21:36:57 +01:00
import client from '@api/client'
2020-11-22 00:46:23 +01:00
import { useNavigation } from '@react-navigation/native'
2021-01-11 21:36:57 +01:00
import { QueryKeyTimeline } from '@utils/queryHooks/timeline'
import { getLocalAccount } from '@utils/slices/instancesSlice'
2020-12-13 14:04:25 +01:00
import { StyleConstants } from '@utils/styles/constants'
2021-01-11 21:36:57 +01:00
import { useTheme } from '@utils/styles/ThemeManager'
import React, { useCallback } from 'react'
import { Pressable, StyleSheet, View } from 'react-native'
2020-12-27 16:25:29 +01:00
import { useMutation, useQueryClient } from 'react-query'
2021-01-11 21:36:57 +01:00
import { useSelector } from 'react-redux'
import TimelineActions from './Shared/Actions'
import TimelineAvatar from './Shared/Avatar'
import TimelineContent from './Shared/Content'
import TimelineHeaderConversation from './Shared/HeaderConversation'
import TimelinePoll from './Shared/Poll'
2020-11-22 00:46:23 +01:00
export interface Props {
2020-12-27 16:25:29 +01:00
conversation: Mastodon.Conversation
2021-01-07 19:13:09 +01:00
queryKey: QueryKeyTimeline
2020-12-13 01:24:25 +01:00
highlighted?: boolean
2020-11-22 00:46:23 +01:00
}
2020-12-27 16:25:29 +01:00
2020-12-13 01:24:25 +01:00
const TimelineConversation: React.FC<Props> = ({
2020-12-27 16:25:29 +01:00
conversation,
2020-12-13 01:24:25 +01:00
queryKey,
highlighted = false
}) => {
2021-01-11 21:36:57 +01:00
const localAccount = useSelector(getLocalAccount)
const { theme } = useTheme()
2020-12-27 16:25:29 +01:00
const queryClient = useQueryClient()
2021-01-07 19:13:09 +01:00
const fireMutation = useCallback(() => {
return client<Mastodon.Conversation>({
method: 'post',
instance: 'local',
url: `conversations/${conversation.id}/read`
})
}, [])
2020-12-27 16:25:29 +01:00
const { mutate } = useMutation(fireMutation, {
onSettled: () => {
queryClient.invalidateQueries(queryKey)
}
})
2020-11-22 00:46:23 +01:00
const navigation = useNavigation()
2020-12-27 18:43:49 +01:00
const onPress = useCallback(() => {
2020-12-27 16:25:29 +01:00
if (conversation.last_status) {
2021-01-07 19:13:09 +01:00
conversation.unread && mutate()
2020-12-27 18:43:49 +01:00
navigation.push('Screen-Shared-Toot', {
2020-12-27 16:25:29 +01:00
toot: conversation.last_status
})
}
}, [])
2020-12-13 01:24:25 +01:00
2020-12-27 18:43:49 +01:00
return (
<Pressable
style={[
styles.base,
conversation.unread && {
borderLeftWidth: StyleConstants.Spacing.XS,
borderLeftColor: theme.blue,
paddingLeft:
StyleConstants.Spacing.Global.PagePadding -
StyleConstants.Spacing.XS
}
]}
onPress={onPress}
>
2020-12-27 18:43:49 +01:00
<View style={styles.header}>
<TimelineAvatar
queryKey={queryKey}
account={conversation.accounts[0]}
/>
<TimelineHeaderConversation
queryKey={queryKey}
conversation={conversation}
/>
</View>
{conversation.last_status ? (
2021-01-22 01:34:20 +01:00
<>
<View
style={{
paddingTop: highlighted ? StyleConstants.Spacing.S : 0,
paddingLeft: highlighted
? 0
: StyleConstants.Avatar.M + StyleConstants.Spacing.S
}}
>
<TimelineContent
status={conversation.last_status}
highlighted={highlighted}
/>
{conversation.last_status.poll && (
<TimelinePoll
queryKey={queryKey}
statusId={conversation.last_status.id}
poll={conversation.last_status.poll}
reblog={false}
sameAccount={conversation.last_status.id === localAccount?.id}
/>
)}
</View>
<View
style={{
paddingLeft: highlighted
? 0
: StyleConstants.Avatar.M + StyleConstants.Spacing.S
}}
>
<TimelineActions
2021-01-11 21:36:57 +01:00
queryKey={queryKey}
2021-01-22 01:34:20 +01:00
status={conversation.last_status}
2021-01-11 21:36:57 +01:00
reblog={false}
/>
2021-01-22 01:34:20 +01:00
</View>
</>
2020-12-27 18:43:49 +01:00
) : null}
</Pressable>
)
2020-11-22 00:46:23 +01:00
}
const styles = StyleSheet.create({
base: {
2020-11-22 00:46:23 +01:00
flex: 1,
flexDirection: 'column',
2021-01-23 02:41:50 +01:00
padding: StyleConstants.Spacing.Global.PagePadding,
paddingBottom: 0
2020-11-22 00:46:23 +01:00
},
2020-12-13 23:02:54 +01:00
header: {
2020-11-22 00:46:23 +01:00
flex: 1,
2020-12-13 23:02:54 +01:00
width: '100%',
2020-11-22 00:46:23 +01:00
flexDirection: 'row'
}
})
export default TimelineConversation