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

274 lines
6.6 KiB
TypeScript
Raw Normal View History

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-18 23:58:53 +01:00
import { useMutation, useQueryClient } from 'react-query'
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-13 14:04:25 +01:00
import getCurrentTab from '@utils/getCurrentTab'
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'
prevState?: boolean
}) => {
let res
switch (type) {
case 'favourite':
case 'reblog':
case 'bookmark':
res = await client({
method: 'post',
instance: 'local',
url: `statuses/${id}/${prevState ? 'un' : ''}${type}`
2020-11-28 17:07:30 +01:00
}) // bug in response from Mastodon
if (!res.body[stateKey] === prevState) {
2020-11-28 17:07:30 +01:00
toast({ type: 'success', content: '功能成功' })
return Promise.resolve(res.body)
} else {
2020-11-28 17:07:30 +01:00
toast({ type: 'error', content: '功能错误' })
return Promise.reject()
}
break
}
}
export interface Props {
2020-12-18 23:58:53 +01:00
queryKey: QueryKey.Timeline
status: Mastodon.Status
}
2020-12-03 01:28:56 +01:00
const TimelineActions: React.FC<Props> = ({ queryKey, status }) => {
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-18 23:58:53 +01:00
queryClient.setQueryData(queryKey, (old: any) => {
old.pages.map((paging: any) => ({
2020-11-28 17:07:30 +01:00
toots: paging.toots.map((toot: any) => {
if (toot.id === id) {
2020-12-18 23:58:53 +01:00
console.log(toot[stateKey])
2020-11-28 17:07:30 +01:00
toot[stateKey] =
typeof prevState === 'boolean' ? !prevState : true
}
return toot
}),
pointer: paging.pointer
}))
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
},
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-12-07 12:31:40 +01:00
const onPressReply = useCallback(() => {
navigation.navigate(getCurrentTab(navigation), {
screen: 'Screen-Shared-Compose',
params: {
2020-12-13 01:24:25 +01:00
type: status.visibility === 'direct' ? 'conversation' : 'reply',
2020-12-07 12:31:40 +01:00
incomingStatus: status
}
})
}, [])
2020-11-28 17:07:30 +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
}),
[status.reblogged]
)
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-11-30 00:24:53 +01:00
fontSize: StyleConstants.Font.Size.M,
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'
color={
status.visibility === 'public' || status.visibility === 'unlisted'
? iconColorAction(status.reblogged)
: theme.disabled
}
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
/>
),
[]
)
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-11-28 17:07:30 +01:00
onPress={
status.visibility === 'public' || status.visibility === 'unlisted'
? onPressReblog
: null
}
2020-11-28 17:07:30 +01:00
children={childrenReblog}
/>
<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',
2020-11-30 00:24:53 +01:00
marginTop: StyleConstants.Spacing.M
},
action: {
width: '20%',
flexDirection: 'row',
2020-11-23 00:07:32 +01:00
justifyContent: 'center'
}
})
2020-12-03 01:28:56 +01:00
export default TimelineActions