2020-12-03 01:28:56 +01:00
|
|
|
import React, { useCallback, useMemo } from 'react'
|
2020-12-01 00:44:28 +01:00
|
|
|
import { ActionSheetIOS, Pressable, StyleSheet, Text, View } from 'react-native'
|
2020-12-26 23:27:53 +01:00
|
|
|
import { useMutation, useQueryClient } from 'react-query'
|
2020-11-06 00:59:33 +01:00
|
|
|
import { Feather } from '@expo/vector-icons'
|
|
|
|
|
2020-12-13 14:04:25 +01:00
|
|
|
import client from '@api/client'
|
|
|
|
import { useTheme } from '@utils/styles/ThemeManager'
|
|
|
|
import { toast } from '@components/toast'
|
|
|
|
import { StyleConstants } from '@utils/styles/constants'
|
2020-12-07 12:31:40 +01:00
|
|
|
import { useNavigation } from '@react-navigation/native'
|
2020-12-19 18:21:37 +01:00
|
|
|
import { findIndex } from 'lodash'
|
|
|
|
import { TimelineData } from '../../Timeline'
|
2020-11-06 00:59:33 +01:00
|
|
|
|
|
|
|
const fireMutation = async ({
|
|
|
|
id,
|
|
|
|
type,
|
|
|
|
stateKey,
|
|
|
|
prevState
|
|
|
|
}: {
|
|
|
|
id: string
|
2020-12-03 01:28:56 +01:00
|
|
|
type: 'favourite' | 'reblog' | 'bookmark'
|
|
|
|
stateKey: 'favourited' | 'reblogged' | 'bookmarked'
|
2020-11-06 00:59:33 +01:00
|
|
|
prevState?: boolean
|
|
|
|
}) => {
|
|
|
|
let res
|
|
|
|
switch (type) {
|
|
|
|
case 'favourite':
|
|
|
|
case 'reblog':
|
|
|
|
case 'bookmark':
|
|
|
|
res = await client({
|
|
|
|
method: 'post',
|
|
|
|
instance: 'local',
|
2020-12-05 01:55:53 +01:00
|
|
|
url: `statuses/${id}/${prevState ? 'un' : ''}${type}`
|
2020-11-28 17:07:30 +01:00
|
|
|
}) // bug in response from Mastodon
|
2020-11-06 00:59:33 +01:00
|
|
|
|
|
|
|
if (!res.body[stateKey] === prevState) {
|
2020-11-28 17:07:30 +01:00
|
|
|
toast({ type: 'success', content: '功能成功' })
|
2020-11-06 00:59:33 +01:00
|
|
|
return Promise.resolve(res.body)
|
|
|
|
} else {
|
2020-11-28 17:07:30 +01:00
|
|
|
toast({ type: 'error', content: '功能错误' })
|
2020-11-06 00:59:33 +01:00
|
|
|
return Promise.reject()
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface Props {
|
2020-12-18 23:58:53 +01:00
|
|
|
queryKey: QueryKey.Timeline
|
2020-11-21 00:40:55 +01:00
|
|
|
status: Mastodon.Status
|
2020-12-19 18:21:37 +01:00
|
|
|
reblog: boolean
|
2020-11-06 00:59:33 +01:00
|
|
|
}
|
|
|
|
|
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()
|
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()
|
|
|
|
const { mutate } = useMutation(fireMutation, {
|
2020-11-28 17:07:30 +01:00
|
|
|
onMutate: ({ id, type, stateKey, prevState }) => {
|
2020-12-18 23:58:53 +01:00
|
|
|
queryClient.cancelQueries(queryKey)
|
|
|
|
const oldData = queryClient.getQueryData(queryKey)
|
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, [
|
2020-12-27 18:43:49 +01:00
|
|
|
queryKey[0] === 'Notifications'
|
|
|
|
? 'status.id'
|
|
|
|
: reblog
|
|
|
|
? 'reblog.id'
|
|
|
|
: 'id',
|
2020-12-19 18:21:37 +01:00
|
|
|
id
|
|
|
|
])
|
|
|
|
if (tempIndex >= 0) {
|
|
|
|
tootIndex = tempIndex
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if (pageIndex >= 0 && tootIndex >= 0) {
|
2020-12-27 18:43:49 +01:00
|
|
|
if (
|
|
|
|
(type === 'favourite' && queryKey[0] === 'Favourites') ||
|
|
|
|
(type === 'bookmark' && queryKey[0] === 'Bookmarks')
|
|
|
|
) {
|
|
|
|
old!.pages[pageIndex].toots.splice(tootIndex, 1)
|
2020-12-19 18:21:37 +01:00
|
|
|
} else {
|
2020-12-27 18:43:49 +01:00
|
|
|
if (queryKey[0] === 'Notifications') {
|
|
|
|
old!.pages[pageIndex].toots[tootIndex].status[stateKey] =
|
|
|
|
typeof prevState === 'boolean' ? !prevState : true
|
|
|
|
} else {
|
|
|
|
if (reblog) {
|
|
|
|
old!.pages[pageIndex].toots[tootIndex].reblog![stateKey] =
|
|
|
|
typeof prevState === 'boolean' ? !prevState : true
|
|
|
|
} else {
|
|
|
|
old!.pages[pageIndex].toots[tootIndex][stateKey] =
|
|
|
|
typeof prevState === 'boolean' ? !prevState : true
|
|
|
|
}
|
|
|
|
}
|
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-06 00:59:33 +01:00
|
|
|
}
|
2020-11-28 17:07:30 +01:00
|
|
|
|
|
|
|
return oldData
|
2020-11-06 00:59:33 +01:00
|
|
|
},
|
2020-11-28 17:07:30 +01:00
|
|
|
onError: (err, _, oldData) => {
|
|
|
|
toast({ type: 'error', content: '请重试' })
|
2020-12-18 23:58:53 +01:00
|
|
|
queryClient.setQueryData(queryKey, oldData)
|
2020-11-06 00:59:33 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
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,
|
|
|
|
visibilityLock: status.visibility === 'direct'
|
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
|
|
|
id: status.id,
|
|
|
|
type: 'reblog',
|
|
|
|
stateKey: 'reblogged',
|
|
|
|
prevState: 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
|
|
|
id: status.id,
|
|
|
|
type: 'favourite',
|
|
|
|
stateKey: 'favourited',
|
|
|
|
prevState: status.favourited
|
|
|
|
}),
|
|
|
|
[status.favourited]
|
|
|
|
)
|
|
|
|
const onPressBookmark = useCallback(
|
|
|
|
() =>
|
2020-12-18 23:58:53 +01:00
|
|
|
mutate({
|
2020-11-28 17:07:30 +01:00
|
|
|
id: status.id,
|
|
|
|
type: 'bookmark',
|
|
|
|
stateKey: 'bookmarked',
|
|
|
|
prevState: status.bookmarked
|
|
|
|
}),
|
|
|
|
[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'
|
|
|
|
]
|
|
|
|
},
|
|
|
|
() => {},
|
|
|
|
() => {}
|
|
|
|
),
|
|
|
|
[]
|
|
|
|
)
|
2020-11-28 17:07:30 +01:00
|
|
|
|
|
|
|
const childrenReply = useMemo(
|
|
|
|
() => (
|
|
|
|
<>
|
|
|
|
<Feather
|
|
|
|
name='message-circle'
|
|
|
|
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,
|
2020-12-29 00:21:05 +01:00
|
|
|
...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(
|
|
|
|
() => (
|
|
|
|
<Feather
|
|
|
|
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(
|
|
|
|
() => (
|
|
|
|
<Feather
|
|
|
|
name='heart'
|
|
|
|
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(
|
|
|
|
() => (
|
|
|
|
<Feather
|
|
|
|
name='bookmark'
|
|
|
|
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(
|
|
|
|
() => (
|
|
|
|
<Feather
|
|
|
|
name='share-2'
|
|
|
|
color={iconColor}
|
2020-11-30 00:24:53 +01:00
|
|
|
size={StyleConstants.Font.Size.M + 2}
|
2020-11-28 17:07:30 +01:00
|
|
|
/>
|
|
|
|
),
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
|
2020-11-06 00:59:33 +01:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<View style={styles.actions}>
|
2020-11-28 17:07:30 +01:00
|
|
|
<Pressable
|
|
|
|
style={styles.action}
|
|
|
|
onPress={onPressReply}
|
|
|
|
children={childrenReply}
|
|
|
|
/>
|
2020-11-06 00:59:33 +01:00
|
|
|
|
|
|
|
<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
|
|
|
/>
|
2020-11-06 00:59:33 +01:00
|
|
|
|
|
|
|
<Pressable
|
|
|
|
style={styles.action}
|
2020-11-28 17:07:30 +01:00
|
|
|
onPress={onPressFavourite}
|
|
|
|
children={childrenFavourite}
|
|
|
|
/>
|
2020-11-06 00:59:33 +01:00
|
|
|
|
|
|
|
<Pressable
|
|
|
|
style={styles.action}
|
2020-11-28 17:07:30 +01:00
|
|
|
onPress={onPressBookmark}
|
|
|
|
children={childrenBookmark}
|
|
|
|
/>
|
2020-11-06 00:59:33 +01:00
|
|
|
|
2020-11-28 17:07:30 +01:00
|
|
|
<Pressable
|
|
|
|
style={styles.action}
|
|
|
|
onPress={onPressShare}
|
|
|
|
children={childrenShare}
|
|
|
|
/>
|
2020-11-06 00:59:33 +01:00
|
|
|
</View>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
actions: {
|
|
|
|
width: '100%',
|
|
|
|
flex: 1,
|
|
|
|
flexDirection: 'row',
|
2020-11-30 00:24:53 +01:00
|
|
|
marginTop: StyleConstants.Spacing.M
|
2020-11-06 00:59:33 +01:00
|
|
|
},
|
|
|
|
action: {
|
|
|
|
width: '20%',
|
|
|
|
flexDirection: 'row',
|
2020-11-23 00:07:32 +01:00
|
|
|
justifyContent: 'center'
|
2020-11-06 00:59:33 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-12-03 01:28:56 +01:00
|
|
|
export default TimelineActions
|