1
0
mirror of https://github.com/tooot-app/app synced 2025-06-05 22:19:13 +02:00

Try to fix #16

This commit is contained in:
Zhiyuan Zheng
2021-02-13 01:26:02 +01:00
parent d2aec8d590
commit 4c8003b533
16 changed files with 195 additions and 51 deletions

View File

@ -226,6 +226,7 @@ export type MutationVarsTimelineUpdateStatusProperty = {
// This is status in general, including "status" inside conversation and notification
type: 'updateStatusProperty'
queryKey: QueryKeyTimeline
rootQueryKey?: QueryKeyTimeline
id: Mastodon.Status['id'] | Mastodon.Poll['id']
reblog?: boolean
payload:
@ -264,7 +265,8 @@ export type MutationVarsTimelineDeleteItem = {
// This is for deleting status and conversation
type: 'deleteItem'
source: 'statuses' | 'conversations'
queryKey: QueryKeyTimeline
queryKey?: QueryKeyTimeline
rootQueryKey?: QueryKeyTimeline
id: Mastodon.Conversation['id']
}
@ -366,7 +368,7 @@ const useTimelineMutation = ({
onError?: MutationOptionsTimeline['onError']
onMutate?: boolean
onSettled?: MutationOptionsTimeline['onSettled']
onSuccess?: MutationOptionsTimeline['onSuccess'] | boolean
onSuccess?: MutationOptionsTimeline['onSuccess']
}) => {
return useMutation<
{ body: Mastodon.Conversation | Mastodon.Notification | Mastodon.Status },
@ -375,27 +377,7 @@ const useTimelineMutation = ({
>(mutationFunction, {
onError,
onSettled,
...(typeof onSuccess === 'function'
? { onSuccess }
: onSuccess
? {
onSuccess: ({ body }, params) => {
queryClient.cancelQueries(params.queryKey)
haptics('Success')
switch (params.type) {
case 'updateStatusProperty':
switch (params.payload.property) {
case 'poll':
params.payload.data = (body as unknown) as Mastodon.Poll
updateStatusProperty({ queryClient, ...params })
break
}
break
}
}
}
: undefined),
onSuccess,
...(onMutate && {
onMutate: params => {
queryClient.cancelQueries(params.queryKey)

View File

@ -1,24 +1,45 @@
import { InfiniteData, QueryClient } from 'react-query'
import { QueryKeyTimeline, TimelineData } from '../timeline'
import { MutationVarsTimelineDeleteItem } from '../timeline'
const deleteItem = ({
queryClient,
queryKey,
rootQueryKey,
id
}: {
queryClient: QueryClient
queryKey: QueryKeyTimeline
id: Mastodon.Status['id']
queryKey?: MutationVarsTimelineDeleteItem['queryKey']
rootQueryKey?: MutationVarsTimelineDeleteItem['rootQueryKey']
id: MutationVarsTimelineDeleteItem['id']
}) => {
queryClient.setQueryData<InfiniteData<any> | undefined>(queryKey, old => {
if (old) {
old.pages = old.pages.map(page => {
page.body = page.body.filter((item: Mastodon.Status) => item.id !== id)
return page
})
return old
}
})
queryKey &&
queryClient.setQueryData<InfiniteData<any> | undefined>(queryKey, old => {
if (old) {
old.pages = old.pages.map(page => {
page.body = page.body.filter(
(item: Mastodon.Status) => item.id !== id
)
return page
})
return old
}
})
rootQueryKey &&
queryClient.setQueryData<InfiniteData<any> | undefined>(
rootQueryKey,
old => {
if (old) {
old.pages = old.pages.map(page => {
page.body = page.body.filter(
(item: Mastodon.Status) => item.id !== id
)
return page
})
return old
}
}
)
}
export default deleteItem

View File

@ -11,12 +11,14 @@ import updateStatus from './update/status'
const updateStatusProperty = ({
queryClient,
queryKey,
rootQueryKey,
id,
reblog,
payload
}: {
queryClient: QueryClient
queryKey: MutationVarsTimelineUpdateStatusProperty['queryKey']
rootQueryKey?: MutationVarsTimelineUpdateStatusProperty['rootQueryKey']
id: MutationVarsTimelineUpdateStatusProperty['id']
reblog?: MutationVarsTimelineUpdateStatusProperty['reblog']
payload: MutationVarsTimelineUpdateStatusProperty['payload']
@ -72,6 +74,60 @@ const updateStatusProperty = ({
return old
}
)
rootQueryKey &&
queryClient.setQueryData<InfiniteData<TimelineData> | undefined>(
rootQueryKey,
old => {
if (old) {
let foundToot = false
old.pages = old.pages.map(page => {
// Skip rest of the pages if any toot is found
if (foundToot) {
return page
} else {
if (
typeof (page.body as Mastodon.Conversation[])[0].unread ===
'boolean'
) {
const items = page.body as Mastodon.Conversation[]
const tootIndex = findIndex(items, ['last_status.id', id])
if (tootIndex >= 0) {
foundToot = true
updateConversation({ item: items[tootIndex], payload })
}
return page
} else if (
typeof (page.body as Mastodon.Notification[])[0].type ===
'string'
) {
const items = page.body as Mastodon.Notification[]
const tootIndex = findIndex(items, ['status.id', id])
if (tootIndex >= 0) {
foundToot = true
updateNotification({ item: items[tootIndex], payload })
}
} else {
const items = page.body as Mastodon.Status[]
const tootIndex = findIndex(items, [
reblog ? 'reblog.id' : 'id',
id
])
// if favouriets page and notifications page, remove the item instead
if (tootIndex >= 0) {
foundToot = true
updateStatus({ item: items[tootIndex], reblog, payload })
}
}
return page
}
})
}
return old
}
)
}
export default updateStatusProperty