tooot/src/components/Timeline/Conversation.tsx

121 lines
4.0 KiB
TypeScript
Raw Normal View History

2021-02-20 19:12:44 +01:00
import apiInstance from '@api/instance'
2021-01-24 02:25:43 +01:00
import GracefullyImage from '@components/GracefullyImage'
2020-11-22 00:46:23 +01:00
import { useNavigation } from '@react-navigation/native'
2021-01-24 02:25:43 +01:00
import { StackNavigationProp } from '@react-navigation/stack'
2022-04-29 23:57:18 +02:00
import { TabLocalStackParamList } from '@utils/navigation/navigators'
2021-01-11 21:36:57 +01:00
import { QueryKeyTimeline } from '@utils/queryHooks/timeline'
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'
2022-04-30 21:59:13 +02:00
import { Pressable, 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 TimelineActions from './Shared/Actions'
import TimelineContent from './Shared/Content'
2022-12-03 20:47:11 +01:00
import StatusContext from './Shared/Context'
2021-01-11 21:36:57 +01:00
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
2022-12-03 20:47:11 +01:00
const TimelineConversation: React.FC<Props> = ({ conversation, queryKey, highlighted = false }) => {
const { colors } = useTheme()
2022-12-03 20:47:11 +01:00
const queryClient = useQueryClient()
const fireMutation = useCallback(() => {
return apiInstance<Mastodon.Conversation>({
method: 'post',
url: `conversations/${conversation.id}/read`
2021-01-07 19:13:09 +01:00
})
2022-12-03 20:47:11 +01:00
}, [])
const { mutate } = useMutation(fireMutation, {
onSettled: () => {
queryClient.invalidateQueries(queryKey)
}
})
2020-12-27 16:25:29 +01:00
2022-12-03 20:47:11 +01:00
const navigation = useNavigation<StackNavigationProp<TabLocalStackParamList>>()
const onPress = useCallback(() => {
if (conversation.last_status) {
conversation.unread && mutate()
navigation.push('Tab-Shared-Toot', {
toot: conversation.last_status,
rootQueryKey: queryKey
})
}
}, [])
2020-12-13 01:24:25 +01:00
2022-12-03 20:47:11 +01:00
return (
<StatusContext.Provider value={{ queryKey, status: conversation.last_status }}>
2022-04-30 21:59:13 +02:00
<Pressable
style={[
{
flex: 1,
flexDirection: 'column',
padding: StyleConstants.Spacing.Global.PagePadding,
paddingBottom: 0,
backgroundColor: colors.backgroundDefault
},
conversation.unread && {
borderLeftWidth: StyleConstants.Spacing.XS,
borderLeftColor: colors.blue,
2022-12-03 20:47:11 +01:00
paddingLeft: StyleConstants.Spacing.Global.PagePadding - StyleConstants.Spacing.XS
2022-04-30 21:59:13 +02:00
}
]}
onPress={onPress}
>
<View style={{ flex: 1, width: '100%', flexDirection: 'row' }}>
2022-12-03 20:47:11 +01:00
<View
style={{
borderRadius: 4,
overflow: 'hidden',
marginRight: StyleConstants.Spacing.S,
width: StyleConstants.Avatar.M,
height: StyleConstants.Avatar.M,
flexDirection: 'row',
flexWrap: 'wrap'
}}
>
{conversation.accounts.slice(0, 4).map(account => (
<GracefullyImage
key={account.id}
uri={{ original: account.avatar, static: account.avatar_static }}
dimension={{
width: StyleConstants.Avatar.M,
height:
conversation.accounts.length > 2
? StyleConstants.Avatar.M / 2
: StyleConstants.Avatar.M
}}
style={{ flex: 1, flexBasis: '50%' }}
/>
))}
</View>
<TimelineHeaderConversation conversation={conversation} />
2022-04-30 21:59:13 +02:00
</View>
2020-12-27 18:43:49 +01:00
2022-04-30 21:59:13 +02:00
{conversation.last_status ? (
<>
<View
style={{
paddingTop: highlighted ? StyleConstants.Spacing.S : 0,
2022-12-03 20:47:11 +01:00
paddingLeft: highlighted ? 0 : StyleConstants.Avatar.M + StyleConstants.Spacing.S
2022-04-30 21:59:13 +02:00
}}
>
2022-12-03 20:47:11 +01:00
<TimelineContent />
<TimelinePoll />
2022-04-30 21:59:13 +02:00
</View>
2022-12-03 20:47:11 +01:00
<TimelineActions />
2022-04-30 21:59:13 +02:00
</>
) : null}
</Pressable>
2022-12-03 20:47:11 +01:00
</StatusContext.Provider>
)
}
2020-11-22 00:46:23 +01:00
export default TimelineConversation