Use truncateAndInvalidate strategy
This commit is contained in:
@@ -3,7 +3,7 @@ import {useMutation, useQueryClient} from '@tanstack/react-query'
|
|||||||
import {isNetworkError} from '#/lib/strings/errors'
|
import {isNetworkError} from '#/lib/strings/errors'
|
||||||
import {logger} from '#/logger'
|
import {logger} from '#/logger'
|
||||||
import {updatePostShadow} from '#/state/cache/post-shadow'
|
import {updatePostShadow} from '#/state/cache/post-shadow'
|
||||||
import {bookmarksQueryKeyRoot} from '#/state/queries/bookmarks/useBookmarksQuery'
|
import {truncateAndInvalidate as invalidateBookmarksQuery} from '#/state/queries/bookmarks/useBookmarksQuery'
|
||||||
import {useAgent} from '#/state/session'
|
import {useAgent} from '#/state/session'
|
||||||
|
|
||||||
type MutationArgs =
|
type MutationArgs =
|
||||||
@@ -30,7 +30,7 @@ export function useBookmarkMutation() {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
onSuccess() {
|
onSuccess() {
|
||||||
qc.invalidateQueries({queryKey: [bookmarksQueryKeyRoot]})
|
invalidateBookmarksQuery(qc)
|
||||||
},
|
},
|
||||||
onError(e, args) {
|
onError(e, args) {
|
||||||
if (args.action === 'create') {
|
if (args.action === 'create') {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import {type AppBskyBookmarkGetBookmarks} from '@atproto/api'
|
import {type AppBskyBookmarkGetBookmarks} from '@atproto/api'
|
||||||
import {
|
import {
|
||||||
type InfiniteData,
|
type InfiniteData,
|
||||||
|
type QueryClient,
|
||||||
type QueryKey,
|
type QueryKey,
|
||||||
useInfiniteQuery,
|
useInfiniteQuery,
|
||||||
} from '@tanstack/react-query'
|
} from '@tanstack/react-query'
|
||||||
@@ -31,3 +32,19 @@ export function useBookmarksQuery() {
|
|||||||
getNextPageParam: lastPage => lastPage.cursor,
|
getNextPageParam: lastPage => lastPage.cursor,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function truncateAndInvalidate(qc: QueryClient) {
|
||||||
|
qc.setQueriesData<InfiniteData<AppBskyBookmarkGetBookmarks.OutputSchema>>(
|
||||||
|
{queryKey: [bookmarksQueryKeyRoot]},
|
||||||
|
data => {
|
||||||
|
if (data) {
|
||||||
|
return {
|
||||||
|
pageParams: data.pageParams.slice(0, 1),
|
||||||
|
pages: data.pages.slice(0, 1),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return data
|
||||||
|
},
|
||||||
|
)
|
||||||
|
return qc.invalidateQueries({queryKey: [bookmarksQueryKeyRoot]})
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user