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

287 lines
7.3 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'
2020-12-07 12:31:40 +01:00
import { useNavigation } from '@react-navigation/native'
2021-01-11 21:36:57 +01:00
import {
MutationVarsTimelineUpdateStatusProperty,
QueryKeyTimeline,
useTimelineMutation
} from '@utils/queryHooks/timeline'
2021-01-01 16:48:16 +01:00
import { StyleConstants } from '@utils/styles/constants'
import { useTheme } from '@utils/styles/ThemeManager'
import React, { useCallback, useMemo } from 'react'
2021-01-01 23:10:47 +01:00
import { useTranslation } from 'react-i18next'
2021-01-13 01:03:46 +01:00
import {
Platform,
Pressable,
Share,
StyleSheet,
Text,
View
} from 'react-native'
2021-01-11 21:36:57 +01:00
import { useQueryClient } from 'react-query'
export interface Props {
2021-01-07 19:13:09 +01:00
queryKey: QueryKeyTimeline
status: Mastodon.Status
2020-12-19 18:21:37 +01:00
reblog: boolean
}
2020-12-27 18:43:49 +01:00
const TimelineActions: React.FC<Props> = ({ queryKey, status, reblog }) => {
2020-12-07 12:31:40 +01:00
const navigation = useNavigation()
2021-01-01 23:10:47 +01:00
const { t } = useTranslation()
2020-11-23 00:07:32 +01:00
const { theme } = useTheme()
const iconColor = theme.secondary
const iconColorAction = (state: boolean) =>
state ? theme.primary : theme.secondary
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,
2021-01-12 00:12:44 +01:00
onSuccess: (_, params) => {
const theParams = params as MutationVarsTimelineUpdateStatusProperty
if (
2021-01-13 01:03:46 +01:00
// Un-bookmark from bookmarks page
2021-01-12 00:12:44 +01:00
(queryKey[1].page === 'Bookmarks' &&
theParams.payload.property === 'bookmarked') ||
2021-01-13 01:03:46 +01:00
// Un-favourite from favourites page
2021-01-12 00:12:44 +01:00
(queryKey[1].page === 'Favourites' &&
2021-01-13 01:03:46 +01:00
theParams.payload.property === 'favourited') ||
// Un-reblog from following page
(queryKey[1].page === 'Following' &&
theParams.payload.property === 'reblogged' &&
theParams.payload.currentValue === true)
2021-01-12 00:12:44 +01:00
) {
queryClient.invalidateQueries(queryKey)
2021-01-13 01:03:46 +01:00
} else if (theParams.payload.property === 'reblogged') {
// When reblogged, update cache of following page
const tempQueryKey: QueryKeyTimeline = [
'Timeline',
{ page: 'Following' }
]
queryClient.invalidateQueries(tempQueryKey)
} else if (theParams.payload.property === 'favourited') {
// When favourited, update favourited page
const tempQueryKey: QueryKeyTimeline = [
'Timeline',
{ page: 'Favourites' }
]
queryClient.invalidateQueries(tempQueryKey)
} else if (theParams.payload.property === 'bookmarked') {
// When bookmarked, update bookmark page
const tempQueryKey: QueryKeyTimeline = [
'Timeline',
{ page: 'Bookmarks' }
]
queryClient.invalidateQueries(tempQueryKey)
2021-01-12 00:12:44 +01:00
}
},
2021-01-11 21:36:57 +01:00
onError: (err: any, params, oldData) => {
const correctParam = params as MutationVarsTimelineUpdateStatusProperty
2020-12-30 14:33:33 +01:00
haptics('Error')
2021-01-01 23:10:47 +01:00
toast({
type: 'error',
message: t('common:toastMessage.error.message', {
2021-01-11 21:36:57 +01:00
function: t(
`timeline:shared.actions.${correctParam.payload.property}.function`
)
}),
...(err.status &&
typeof err.status === 'number' &&
err.data &&
err.data.error &&
typeof err.data.error === 'string' && {
description: err.data.error
})
2021-01-01 23:10:47 +01:00
})
2020-12-18 23:58:53 +01:00
queryClient.setQueryData(queryKey, oldData)
}
})
2020-12-28 16:54:19 +01:00
const onPressReply = useCallback(
() =>
2020-12-29 16:19:04 +01:00
navigation.navigate('Screen-Shared-Compose', {
type: 'reply',
2021-01-14 22:53:01 +01:00
incomingStatus: status,
queryKey
2020-12-28 16:54:19 +01:00
}),
[]
)
const onPressReblog = useCallback(
() =>
2021-01-11 21:36:57 +01:00
mutation.mutate({
type: 'updateStatusProperty',
queryKey,
id: status.id,
reblog,
payload: {
property: 'reblogged',
currentValue: status.reblogged
}
2020-12-28 16:54:19 +01:00
}),
[status.reblogged]
)
2020-11-28 17:07:30 +01:00
const onPressFavourite = useCallback(
() =>
2021-01-11 21:36:57 +01:00
mutation.mutate({
type: 'updateStatusProperty',
queryKey,
id: status.id,
reblog,
payload: {
property: 'favourited',
currentValue: status.favourited
}
2020-11-28 17:07:30 +01:00
}),
[status.favourited]
)
const onPressBookmark = useCallback(
() =>
2021-01-11 21:36:57 +01:00
mutation.mutate({
type: 'updateStatusProperty',
queryKey,
id: status.id,
reblog,
payload: {
property: 'bookmarked',
currentValue: status.bookmarked
}
2020-11-28 17:07:30 +01:00
}),
[status.bookmarked]
)
2021-01-13 01:03:46 +01:00
const onPressShare = useCallback(() => {
switch (Platform.OS) {
case 'ios':
return Share.share({
url: status.uri
})
case 'android':
return Share.share({
message: status.uri
})
}
}, [])
2020-11-28 17:07:30 +01:00
const childrenReply = useMemo(
() => (
<>
<Icon
name='MessageCircle'
2020-11-28 17:07:30 +01:00
color={iconColor}
2021-01-13 01:03:46 +01:00
size={StyleConstants.Font.Size.L}
2020-11-28 17:07:30 +01:00
/>
{status.replies_count > 0 && (
<Text
style={{
color: theme.secondary,
...StyleConstants.FontStyle.M,
2020-11-30 00:24:53 +01:00
marginLeft: StyleConstants.Spacing.XS
2020-11-28 17:07:30 +01:00
}}
>
{status.replies_count}
</Text>
)}
</>
),
[status.replies_count]
)
const childrenReblog = useMemo(
() => (
<Icon
name='Repeat'
2020-12-26 14:40:10 +01:00
color={
status.visibility === 'private' || status.visibility === 'direct'
? theme.disabled
: iconColorAction(status.reblogged)
}
2021-01-13 01:03:46 +01:00
size={StyleConstants.Font.Size.L}
2020-11-28 17:07:30 +01:00
/>
),
[status.reblogged]
)
const childrenFavourite = useMemo(
() => (
<Icon
name='Heart'
2020-11-28 17:07:30 +01:00
color={iconColorAction(status.favourited)}
2021-01-13 01:03:46 +01:00
size={StyleConstants.Font.Size.L}
2020-11-28 17:07:30 +01:00
/>
),
[status.favourited]
)
const childrenBookmark = useMemo(
() => (
<Icon
name='Bookmark'
2020-11-28 17:07:30 +01:00
color={iconColorAction(status.bookmarked)}
2021-01-13 01:03:46 +01:00
size={StyleConstants.Font.Size.L}
2020-11-28 17:07:30 +01:00
/>
),
[status.bookmarked]
)
const childrenShare = useMemo(
() => (
2021-01-13 01:03:46 +01:00
<Icon name='Share2' color={iconColor} size={StyleConstants.Font.Size.L} />
2020-11-28 17:07:30 +01:00
),
[]
)
return (
<>
<View style={styles.actions}>
2020-11-28 17:07:30 +01:00
<Pressable
style={styles.action}
onPress={onPressReply}
children={childrenReply}
/>
<Pressable
style={styles.action}
2020-12-26 00:40:27 +01:00
onPress={onPressReblog}
2020-11-28 17:07:30 +01:00
children={childrenReblog}
2020-12-28 16:54:19 +01:00
disabled={
status.visibility === 'private' || status.visibility === 'direct'
}
2020-11-28 17:07:30 +01:00
/>
<Pressable
style={styles.action}
2020-11-28 17:07:30 +01:00
onPress={onPressFavourite}
children={childrenFavourite}
/>
<Pressable
style={styles.action}
2020-11-28 17:07:30 +01:00
onPress={onPressBookmark}
children={childrenBookmark}
/>
2020-11-28 17:07:30 +01:00
<Pressable
style={styles.action}
onPress={onPressShare}
children={childrenShare}
/>
</View>
</>
)
}
const styles = StyleSheet.create({
actions: {
width: '100%',
flex: 1,
flexDirection: 'row',
2021-01-01 16:48:16 +01:00
marginTop: StyleConstants.Spacing.S
},
action: {
width: '20%',
flexDirection: 'row',
2021-01-01 16:48:16 +01:00
justifyContent: 'center',
2021-01-13 01:03:46 +01:00
alignItems: 'center',
2021-01-01 16:48:16 +01:00
paddingVertical: StyleConstants.Spacing.S
}
})
2020-12-03 01:28:56 +01:00
export default TimelineActions