migrate the bookmark and draft queries to the appview client
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import {type AppBskyFeedDefs} from '@atproto/api'
|
||||
import {type AtUriString} from '@atproto/syntax'
|
||||
import {useMutation, useQueryClient} from '@tanstack/react-query'
|
||||
|
||||
import {isNetworkError} from '#/lib/strings/errors'
|
||||
@@ -8,7 +9,8 @@ import {
|
||||
optimisticallyDeleteBookmark,
|
||||
optimisticallySaveBookmark,
|
||||
} from '#/state/queries/bookmarks/useBookmarksQuery'
|
||||
import {useAgent} from '#/state/session'
|
||||
import {useAppviewClient} from '#/state/session'
|
||||
import {app} from '#/lexicons'
|
||||
|
||||
type MutationArgs =
|
||||
| {action: 'create'; post: AppBskyFeedDefs.PostView}
|
||||
@@ -23,20 +25,20 @@ type MutationArgs =
|
||||
|
||||
export function useBookmarkMutation() {
|
||||
const qc = useQueryClient()
|
||||
const agent = useAgent()
|
||||
const client = useAppviewClient()
|
||||
|
||||
return useMutation({
|
||||
async mutationFn(args: MutationArgs) {
|
||||
if (args.action === 'create') {
|
||||
updatePostShadow(qc, args.post.uri, {bookmarked: true})
|
||||
await agent.app.bsky.bookmark.createBookmark({
|
||||
uri: args.post.uri,
|
||||
await client.call(app.bsky.bookmark.createBookmark, {
|
||||
uri: args.post.uri as AtUriString,
|
||||
cid: args.post.cid,
|
||||
})
|
||||
} else if (args.action === 'delete') {
|
||||
updatePostShadow(qc, args.uri, {bookmarked: false})
|
||||
await agent.app.bsky.bookmark.deleteBookmark({
|
||||
uri: args.uri,
|
||||
await client.call(app.bsky.bookmark.deleteBookmark, {
|
||||
uri: args.uri as AtUriString,
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
@@ -1,9 +1,4 @@
|
||||
import {
|
||||
type $Typed,
|
||||
type AppBskyBookmarkGetBookmarks,
|
||||
AppBskyFeedDefs,
|
||||
AtUri,
|
||||
} from '@atproto/api'
|
||||
import {type $Typed, AppBskyFeedDefs, AtUri} from '@atproto/api'
|
||||
import {
|
||||
type InfiniteData,
|
||||
type QueryClient,
|
||||
@@ -16,28 +11,28 @@ import {
|
||||
embedViewRecordToPostView,
|
||||
getEmbeddedPost,
|
||||
} from '#/state/queries/util'
|
||||
import {useAgent} from '#/state/session'
|
||||
import {useAppviewClient} from '#/state/session'
|
||||
import {app} from '#/lexicons'
|
||||
import * as bsky from '#/types/bsky'
|
||||
|
||||
export const bookmarksQueryKeyRoot = 'bookmarks'
|
||||
export const createBookmarksQueryKey = () => [bookmarksQueryKeyRoot]
|
||||
|
||||
export function useBookmarksQuery() {
|
||||
const agent = useAgent()
|
||||
const client = useAppviewClient()
|
||||
|
||||
return useInfiniteQuery<
|
||||
AppBskyBookmarkGetBookmarks.OutputSchema,
|
||||
app.bsky.bookmark.getBookmarks.$OutputBody,
|
||||
Error,
|
||||
InfiniteData<AppBskyBookmarkGetBookmarks.OutputSchema>,
|
||||
InfiniteData<app.bsky.bookmark.getBookmarks.$OutputBody>,
|
||||
QueryKey,
|
||||
string | undefined
|
||||
>({
|
||||
queryKey: createBookmarksQueryKey(),
|
||||
async queryFn({pageParam}) {
|
||||
const res = await agent.app.bsky.bookmark.getBookmarks({
|
||||
return await client.call(app.bsky.bookmark.getBookmarks, {
|
||||
cursor: pageParam,
|
||||
})
|
||||
return res.data
|
||||
},
|
||||
initialPageParam: undefined,
|
||||
getNextPageParam: lastPage => lastPage.cursor,
|
||||
@@ -45,7 +40,7 @@ export function useBookmarksQuery() {
|
||||
}
|
||||
|
||||
export async function truncateAndInvalidate(qc: QueryClient) {
|
||||
qc.setQueriesData<InfiniteData<AppBskyBookmarkGetBookmarks.OutputSchema>>(
|
||||
qc.setQueriesData<InfiniteData<app.bsky.bookmark.getBookmarks.$OutputBody>>(
|
||||
{queryKey: [bookmarksQueryKeyRoot]},
|
||||
data => {
|
||||
if (data) {
|
||||
@@ -64,7 +59,7 @@ export async function optimisticallySaveBookmark(
|
||||
qc: QueryClient,
|
||||
post: AppBskyFeedDefs.PostView,
|
||||
) {
|
||||
qc.setQueriesData<InfiniteData<AppBskyBookmarkGetBookmarks.OutputSchema>>(
|
||||
qc.setQueriesData<InfiniteData<app.bsky.bookmark.getBookmarks.$OutputBody>>(
|
||||
{
|
||||
queryKey: [bookmarksQueryKeyRoot],
|
||||
},
|
||||
@@ -75,19 +70,22 @@ export async function optimisticallySaveBookmark(
|
||||
pages: data.pages.map((page, index) => {
|
||||
if (index === 0) {
|
||||
post.$type = 'app.bsky.feed.defs#postView'
|
||||
/*
|
||||
* The optimistic entry is synthesized from an `@atproto/api`
|
||||
* `PostView`, whose string fields are unbranded, so it is asserted
|
||||
* to the vendored view type the query data is now keyed on.
|
||||
*/
|
||||
const bookmark = {
|
||||
createdAt: new Date().toISOString(),
|
||||
subject: {
|
||||
uri: post.uri,
|
||||
cid: post.cid,
|
||||
},
|
||||
item: post as $Typed<AppBskyFeedDefs.PostView>,
|
||||
} as unknown as app.bsky.bookmark.defs.BookmarkView
|
||||
return {
|
||||
...page,
|
||||
bookmarks: [
|
||||
{
|
||||
createdAt: new Date().toISOString(),
|
||||
subject: {
|
||||
uri: post.uri,
|
||||
cid: post.cid,
|
||||
},
|
||||
item: post as $Typed<AppBskyFeedDefs.PostView>,
|
||||
},
|
||||
...page.bookmarks,
|
||||
],
|
||||
bookmarks: [bookmark, ...page.bookmarks],
|
||||
}
|
||||
}
|
||||
return page
|
||||
@@ -101,7 +99,7 @@ export async function optimisticallyDeleteBookmark(
|
||||
qc: QueryClient,
|
||||
{uri}: {uri: string},
|
||||
) {
|
||||
qc.setQueriesData<InfiniteData<AppBskyBookmarkGetBookmarks.OutputSchema>>(
|
||||
qc.setQueriesData<InfiniteData<app.bsky.bookmark.getBookmarks.$OutputBody>>(
|
||||
{
|
||||
queryKey: [bookmarksQueryKeyRoot],
|
||||
},
|
||||
@@ -125,7 +123,7 @@ export function* findAllPostsInQueryData(
|
||||
uri: string,
|
||||
): Generator<AppBskyFeedDefs.PostView, undefined> {
|
||||
const queryDatas = queryClient.getQueriesData<
|
||||
InfiniteData<AppBskyBookmarkGetBookmarks.OutputSchema>
|
||||
InfiniteData<app.bsky.bookmark.getBookmarks.$OutputBody>
|
||||
>({
|
||||
queryKey: [bookmarksQueryKeyRoot],
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user