tooot/src/components/Timeline/Conversation.tsx

162 lines
5.1 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 analytics from '@components/analytics'
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'
2021-02-20 19:12:44 +01:00
import { getInstanceAccount } 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'
2022-04-30 21:59:13 +02:00
import { isEqual } from 'lodash'
2021-01-11 21:36:57 +01:00
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 { useSelector } from 'react-redux'
import TimelineActions from './Shared/Actions'
import TimelineContent from './Shared/Content'
import TimelineHeaderConversation from './Shared/HeaderConversation'
import TimelinePoll from './Shared/Poll'
2020-11-22 00:46:23 +01:00
2021-01-24 02:25:43 +01:00
const Avatars: React.FC<{ accounts: Mastodon.Account[] }> = ({ accounts }) => {
return (
<View
style={{
borderRadius: 4,
overflow: 'hidden',
marginRight: StyleConstants.Spacing.S,
width: StyleConstants.Avatar.M,
height: StyleConstants.Avatar.M,
flexDirection: 'row',
flexWrap: 'wrap'
}}
>
{accounts.slice(0, 4).map(account => (
<GracefullyImage
key={account.id}
2022-02-12 18:25:53 +01:00
uri={{ original: account.avatar, static: account.avatar_static }}
2021-01-24 02:25:43 +01:00
dimension={{
width: StyleConstants.Avatar.M,
height:
accounts.length > 2
? StyleConstants.Avatar.M / 2
: StyleConstants.Avatar.M
}}
style={{ flex: 1, flexBasis: '50%' }}
/>
))}
</View>
)
}
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-04-30 21:59:13 +02:00
const TimelineConversation = React.memo(
({ conversation, queryKey, highlighted = false }: Props) => {
const instanceAccount = useSelector(
getInstanceAccount,
(prev, next) => prev?.id === next?.id
)
const { colors } = useTheme()
2022-04-30 21:59:13 +02:00
const queryClient = useQueryClient()
const fireMutation = useCallback(() => {
return apiInstance<Mastodon.Conversation>({
method: 'post',
url: `conversations/${conversation.id}/read`
})
}, [])
const { mutate } = useMutation(fireMutation, {
onSettled: () => {
queryClient.invalidateQueries(queryKey)
}
2021-01-07 19:13:09 +01:00
})
2020-12-27 16:25:29 +01:00
2022-04-30 21:59:13 +02:00
const navigation =
useNavigation<StackNavigationProp<TabLocalStackParamList>>()
const onPress = useCallback(() => {
analytics('timeline_conversation_press')
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-04-30 21:59:13 +02:00
return (
<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,
paddingLeft:
StyleConstants.Spacing.Global.PagePadding -
StyleConstants.Spacing.XS
}
]}
onPress={onPress}
>
<View style={{ flex: 1, width: '100%', flexDirection: 'row' }}>
<Avatars accounts={conversation.accounts} />
<TimelineHeaderConversation
queryKey={queryKey}
conversation={conversation}
/>
</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,
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 === instanceAccount?.id
}
/>
) : null}
</View>
<TimelineActions
queryKey={queryKey}
2021-01-22 01:34:20 +01:00
status={conversation.last_status}
highlighted={highlighted}
2022-04-30 21:59:13 +02:00
accts={conversation.accounts.map(account => account.acct)}
reblog={false}
2021-01-22 01:34:20 +01:00
/>
2022-04-30 21:59:13 +02:00
</>
) : null}
</Pressable>
)
2020-11-22 00:46:23 +01:00
},
2022-04-30 21:59:13 +02:00
(prev, next) => isEqual(prev.conversation, next.conversation)
)
2020-11-22 00:46:23 +01:00
export default TimelineConversation