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

302 lines
7.9 KiB
TypeScript
Raw Normal View History

2020-12-13 14:04:25 +01:00
import client from '@api/client'
2021-01-01 16:48:16 +01:00
import haptics from '@components/haptics'
import Icon from '@components/Icon'
2021-01-01 16:48:16 +01:00
import { TimelineData } from '@components/Timelines/Timeline'
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-07 19:13:09 +01:00
import { QueryKeyTimeline } from '@utils/queryHooks/timeline'
2021-01-01 16:48:16 +01:00
import { StyleConstants } from '@utils/styles/constants'
import { useTheme } from '@utils/styles/ThemeManager'
2020-12-19 18:21:37 +01:00
import { findIndex } from 'lodash'
2021-01-01 16:48:16 +01:00
import React, { useCallback, useMemo } from 'react'
2021-01-01 23:10:47 +01:00
import { useTranslation } from 'react-i18next'
2021-01-01 16:48:16 +01:00
import { ActionSheetIOS, Pressable, StyleSheet, Text, View } from 'react-native'
import { useMutation, 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-01 23:10:47 +01:00
const fireMutation = useCallback(
async ({
type,
state
}: {
type: 'favourite' | 'reblog' | 'bookmark'
stateKey: 'favourited' | 'reblogged' | 'bookmarked'
state?: boolean
}) => {
2021-01-07 19:13:09 +01:00
return client<Mastodon.Status>({
2021-01-01 23:10:47 +01:00
method: 'post',
instance: 'local',
url: `statuses/${status.id}/${state ? 'un' : ''}${type}`
}) // bug in response from Mastodon
},
[]
)
2020-12-18 23:58:53 +01:00
const { mutate } = useMutation(fireMutation, {
2021-01-01 23:10:47 +01:00
onMutate: ({ type, stateKey, state }) => {
2020-12-18 23:58:53 +01:00
queryClient.cancelQueries(queryKey)
const oldData = queryClient.getQueryData(queryKey)
2020-11-28 17:07:30 +01:00
2021-01-01 16:48:16 +01:00
haptics('Success')
2020-11-28 17:07:30 +01:00
switch (type) {
case 'favourite':
case 'reblog':
case 'bookmark':
2020-12-19 18:21:37 +01:00
queryClient.setQueryData<TimelineData>(queryKey, old => {
let tootIndex = -1
const pageIndex = findIndex(old?.pages, page => {
const tempIndex = findIndex(page.toots, [
2021-01-07 19:13:09 +01:00
queryKey[1].page === 'Notifications'
2020-12-27 18:43:49 +01:00
? 'status.id'
: reblog
? 'reblog.id'
: 'id',
2021-01-01 23:10:47 +01:00
status.id
2020-12-19 18:21:37 +01:00
])
if (tempIndex >= 0) {
tootIndex = tempIndex
return true
} else {
return false
}
})
if (pageIndex >= 0 && tootIndex >= 0) {
2020-12-27 18:43:49 +01:00
if (
2021-01-07 19:13:09 +01:00
(type === 'favourite' && queryKey[1].page === 'Favourites') ||
(type === 'bookmark' && queryKey[1].page === 'Bookmarks')
2020-12-27 18:43:49 +01:00
) {
old!.pages[pageIndex].toots.splice(tootIndex, 1)
2020-12-19 18:21:37 +01:00
} else {
2021-01-07 19:13:09 +01:00
if (queryKey[1].page === 'Notifications') {
2020-12-27 18:43:49 +01:00
old!.pages[pageIndex].toots[tootIndex].status[stateKey] =
2021-01-01 23:10:47 +01:00
typeof state === 'boolean' ? !state : true
2020-12-27 18:43:49 +01:00
} else {
if (reblog) {
old!.pages[pageIndex].toots[tootIndex].reblog![stateKey] =
2021-01-01 23:10:47 +01:00
typeof state === 'boolean' ? !state : true
2020-12-27 18:43:49 +01:00
} else {
old!.pages[pageIndex].toots[tootIndex][stateKey] =
2021-01-01 23:10:47 +01:00
typeof state === 'boolean' ? !state : true
2020-12-27 18:43:49 +01:00
}
}
2020-12-19 18:21:37 +01:00
}
}
2020-12-18 23:58:53 +01:00
return old
})
2020-11-28 17:07:30 +01:00
break
}
2020-11-28 17:07:30 +01:00
return oldData
},
onError: (err: any, { type }, oldData) => {
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-01 23:10:47 +01:00
function: t(`timeline:shared.actions.${type}.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',
incomingStatus: status
2020-12-28 16:54:19 +01:00
}),
[]
)
const onPressReblog = useCallback(
() =>
2020-12-18 23:58:53 +01:00
mutate({
2020-11-28 17:07:30 +01:00
type: 'reblog',
stateKey: 'reblogged',
2021-01-01 23:10:47 +01:00
state: status.reblogged
2020-12-28 16:54:19 +01:00
}),
[status.reblogged]
)
2020-11-28 17:07:30 +01:00
const onPressFavourite = useCallback(
() =>
2020-12-18 23:58:53 +01:00
mutate({
2020-11-28 17:07:30 +01:00
type: 'favourite',
stateKey: 'favourited',
2021-01-01 23:10:47 +01:00
state: status.favourited
2020-11-28 17:07:30 +01:00
}),
[status.favourited]
)
const onPressBookmark = useCallback(
() =>
2020-12-18 23:58:53 +01:00
mutate({
2020-11-28 17:07:30 +01:00
type: 'bookmark',
stateKey: 'bookmarked',
2021-01-01 23:10:47 +01:00
state: status.bookmarked
2020-11-28 17:07:30 +01:00
}),
[status.bookmarked]
)
2020-12-03 01:28:56 +01:00
const onPressShare = useCallback(
() =>
ActionSheetIOS.showShareActionSheetWithOptions(
{
url: status.uri,
excludedActivityTypes: [
'com.apple.UIKit.activity.Mail',
'com.apple.UIKit.activity.Print',
'com.apple.UIKit.activity.SaveToCameraRoll',
'com.apple.UIKit.activity.OpenInIBooks'
]
},
2021-01-01 16:48:16 +01:00
() => haptics('Error'),
() => haptics('Success')
2020-12-03 01:28:56 +01:00
),
[]
)
2020-11-28 17:07:30 +01:00
const childrenReply = useMemo(
() => (
<>
<Icon
name='MessageCircle'
2020-11-28 17:07:30 +01:00
color={iconColor}
2020-11-30 00:24:53 +01:00
size={StyleConstants.Font.Size.M + 2}
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)
}
2020-11-30 00:24:53 +01:00
size={StyleConstants.Font.Size.M + 2}
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)}
2020-11-30 00:24:53 +01:00
size={StyleConstants.Font.Size.M + 2}
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)}
2020-11-30 00:24:53 +01:00
size={StyleConstants.Font.Size.M + 2}
2020-11-28 17:07:30 +01:00
/>
),
[status.bookmarked]
)
const childrenShare = useMemo(
() => (
<Icon
name='Share2'
2020-11-28 17:07:30 +01:00
color={iconColor}
2020-11-30 00:24:53 +01:00
size={StyleConstants.Font.Size.M + 2}
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',
paddingVertical: StyleConstants.Spacing.S
}
})
2020-12-03 01:28:56 +01:00
export default TimelineActions