From 284d6e46e05b0a7d42df922384ee0bca18f13b61 Mon Sep 17 00:00:00 2001 From: Zhiyuan Zheng Date: Mon, 11 Jan 2021 21:36:57 +0100 Subject: [PATCH] Switch to shared hooks --- App.tsx | 1 - src/Index.tsx | 8 +- src/components/Button.tsx | 5 +- src/components/Instance.tsx | 8 +- src/components/Relationship/Incoming.tsx | 50 ++- src/components/Relationship/Outgoing.tsx | 180 +++++---- src/components/Timelines/Timeline.tsx | 90 ++--- .../Timelines/Timeline/Conversation.tsx | 32 +- src/components/Timelines/Timeline/Default.tsx | 11 +- .../Timelines/Timeline/Notifications.tsx | 1 + .../Timelines/Timeline/Shared/Actions.tsx | 135 ++----- .../Timeline/Shared/HeaderConversation.tsx | 60 +-- .../Timeline/Shared/HeaderDefault.tsx | 7 +- .../Shared/HeaderDefault/ActionsAccount.tsx | 61 ++- .../Shared/HeaderDefault/ActionsDomain.tsx | 47 ++- .../Shared/HeaderDefault/ActionsStatus.tsx | 158 +++----- .../Timeline/Shared/HeaderNotification.tsx | 2 + .../Timeline/Shared/HeaderShared/Muted.tsx | 30 ++ .../Timelines/Timeline/Shared/Poll.tsx | 94 ++--- src/screens/Me/Lists.tsx | 4 +- src/screens/Me/Root/Collections.tsx | 4 +- src/screens/Me/Root/MyInfo.tsx | 4 +- src/screens/Me/Root/Settings.tsx | 11 +- src/screens/Me/Switch/Root.tsx | 4 +- src/screens/Shared/Account.tsx | 4 +- .../Shared/Account/Information/Actions.tsx | 4 +- src/screens/Shared/Announcements.tsx | 37 +- src/screens/Shared/Compose.tsx | 7 +- src/screens/Shared/Compose/Root.tsx | 8 +- src/screens/Shared/Relationships/List.tsx | 6 +- src/screens/Shared/Search.tsx | 4 +- src/utils/queryHooks/account.ts | 4 +- src/utils/queryHooks/accountCheck.ts | 4 +- src/utils/queryHooks/announcement.ts | 57 ++- src/utils/queryHooks/apps.ts | 4 +- src/utils/queryHooks/emojis.ts | 4 +- src/utils/queryHooks/instance.ts | 4 +- src/utils/queryHooks/lists.ts | 4 +- src/utils/queryHooks/relationship.ts | 52 ++- src/utils/queryHooks/relationships.ts | 4 +- src/utils/queryHooks/search.ts | 4 +- src/utils/queryHooks/timeline.ts | 373 ++++++++++++------ src/utils/queryHooks/timeline/deleteItem.ts | 25 ++ .../timeline/update/conversation.ts | 27 ++ .../timeline/update/notification.ts | 24 ++ .../queryHooks/timeline/update/status.ts | 37 ++ .../timeline/updateStatusProperty.ts | 76 ++++ 47 files changed, 1053 insertions(+), 727 deletions(-) create mode 100644 src/components/Timelines/Timeline/Shared/HeaderShared/Muted.tsx create mode 100644 src/utils/queryHooks/timeline/deleteItem.ts create mode 100644 src/utils/queryHooks/timeline/update/conversation.ts create mode 100644 src/utils/queryHooks/timeline/update/notification.ts create mode 100644 src/utils/queryHooks/timeline/update/status.ts create mode 100644 src/utils/queryHooks/timeline/updateStatusProperty.ts diff --git a/App.tsx b/App.tsx index 181695fc..73ace6f4 100644 --- a/App.tsx +++ b/App.tsx @@ -7,7 +7,6 @@ import { enableScreens } from 'react-native-screens' import { QueryClient, QueryClientProvider } from 'react-query' import { Provider } from 'react-redux' import { PersistGate } from 'redux-persist/integration/react' -import checkSecureStorageVersion from '@root/startup/checkSecureStorageVersion' import dev from '@root/startup/dev' import sentry from '@root/startup/sentry' import log from '@root/startup/log' diff --git a/src/Index.tsx b/src/Index.tsx index 9b323a49..967c2471 100644 --- a/src/Index.tsx +++ b/src/Index.tsx @@ -14,7 +14,7 @@ import ScreenLocal from '@screens/Local' import ScreenMe from '@screens/Me' import ScreenNotifications from '@screens/Notifications' import ScreenPublic from '@screens/Public' -import hookTimeline from '@utils/queryHooks/timeline' +import { useTimelineQuery } from '@utils/queryHooks/timeline' import { getLocalActiveIndex, getLocalNotification, @@ -100,7 +100,7 @@ const Index: React.FC = ({ localCorrupt }) => { }, []) // On launch check if there is any unread noficiations - const queryNotification = hookTimeline({ + const queryNotification = useTimelineQuery({ page: 'Notifications', options: { enabled: localActiveIndex !== null ? true : false, @@ -112,9 +112,7 @@ const Index: React.FC = ({ localCorrupt }) => { const prevNotification = useSelector(getLocalNotification) useEffect(() => { if (queryNotification.data?.pages) { - const flattenData = queryNotification.data.pages.flatMap(d => [ - ...d?.toots - ]) + const flattenData = queryNotification.data.pages.flatMap(d => [...d]) const latestNotificationTime = flattenData.length ? (flattenData[0] as Mastodon.Notification).created_at : undefined diff --git a/src/components/Button.tsx b/src/components/Button.tsx index 200cd00a..20fae0b7 100644 --- a/src/components/Button.tsx +++ b/src/components/Button.tsx @@ -12,7 +12,6 @@ import { ViewStyle } from 'react-native' import { Chase } from 'react-native-animated-spinkit' -import Animated from 'react-native-reanimated' export interface Props { style?: StyleProp @@ -156,7 +155,7 @@ const Button: React.FC = ({ } return ( - + = ({ children={children} disabled={disabled || active || loading} /> - + ) } diff --git a/src/components/Instance.tsx b/src/components/Instance.tsx index bac20289..cae3990d 100644 --- a/src/components/Instance.tsx +++ b/src/components/Instance.tsx @@ -2,8 +2,8 @@ import Button from '@components/Button' import haptics from '@components/haptics' import Icon from '@components/Icon' import { useNavigation } from '@react-navigation/native' -import hookApps from '@utils/queryHooks/apps' -import hookInstance from '@utils/queryHooks/instance' +import { useAppsQuery } from '@utils/queryHooks/apps' +import { useInstanceQuery } from '@utils/queryHooks/instance' import { QueryKeyTimeline } from '@utils/queryHooks/timeline' import { getLocalInstances, @@ -42,11 +42,11 @@ const ComponentInstance: React.FC = ({ const [appData, setApplicationData] = useState() const localInstances = useSelector(getLocalInstances) - const instanceQuery = hookInstance({ + const instanceQuery = useInstanceQuery({ instanceDomain, options: { enabled: false, retry: false } }) - const applicationQuery = hookApps({ + const applicationQuery = useAppsQuery({ instanceDomain, options: { enabled: false, retry: false } }) diff --git a/src/components/Relationship/Incoming.tsx b/src/components/Relationship/Incoming.tsx index a2f627a8..6e3227dd 100644 --- a/src/components/Relationship/Incoming.tsx +++ b/src/components/Relationship/Incoming.tsx @@ -1,13 +1,16 @@ -import client from '@api/client' import Button from '@components/Button' import haptics from '@components/haptics' import { toast } from '@components/toast' -import { QueryKeyRelationship } from '@utils/queryHooks/relationship' +import { + QueryKeyRelationship, + useRelationshipMutation +} from '@utils/queryHooks/relationship' +import { QueryKeyTimeline } from '@utils/queryHooks/timeline' import { StyleConstants } from '@utils/styles/constants' -import React, { useCallback } from 'react' +import React from 'react' import { useTranslation } from 'react-i18next' import { StyleSheet, View } from 'react-native' -import { useMutation, useQueryClient } from 'react-query' +import { useQueryClient } from 'react-query' export interface Props { id: Mastodon.Account['id'] @@ -17,23 +20,18 @@ const RelationshipIncoming: React.FC = ({ id }) => { const { t } = useTranslation() const queryKeyRelationship: QueryKeyRelationship = ['Relationship', { id }] - + const queryKeyNotification: QueryKeyTimeline = [ + 'Timeline', + { page: 'Notifications' } + ] const queryClient = useQueryClient() - const fireMutation = useCallback( - ({ type }: { type: 'authorize' | 'reject' }) => { - return client({ - method: 'post', - instance: 'local', - url: `follow_requests/${id}/${type}` - }) - }, - [] - ) - const mutation = useMutation(fireMutation, { + const mutation = useRelationshipMutation({ onSuccess: res => { haptics('Success') - queryClient.setQueryData(queryKeyRelationship, res) - queryClient.refetchQueries(['Notifications']) + queryClient.setQueryData(queryKeyRelationship, [ + res + ]) + queryClient.refetchQueries(queryKeyNotification) }, onError: (err: any, { type }) => { haptics('Error') @@ -60,14 +58,26 @@ const RelationshipIncoming: React.FC = ({ id }) => { type='icon' content='X' loading={mutation.isLoading} - onPress={() => mutation.mutate({ type: 'reject' })} + onPress={() => + mutation.mutate({ + id, + type: 'incoming', + payload: { action: 'reject' } + }) + } />