diff --git a/src/state/queries/post-also-liked.ts b/src/state/queries/post-also-liked.ts index b528fab99..089045721 100644 --- a/src/state/queries/post-also-liked.ts +++ b/src/state/queries/post-also-liked.ts @@ -1,6 +1,7 @@ -import {type AppBskyFeedDefs} from '@atproto/api' +import {type AppBskyFeedDefs, AtUri} from '@atproto/api' import { type InfiniteData, + type QueryClient, useInfiniteQuery, useQueryClient, } from '@tanstack/react-query' @@ -8,6 +9,7 @@ import { import {STALE} from '#/state/queries' import {precachePost} from '#/state/queries/post' import {useAgent} from '#/state/session' +import {embedViewRecordToPostView, getEmbeddedPost} from './util' const ALSO_LIKED_URL = 'https://foryou.club/also-liked' const ALSO_LIKED_PAGE_SIZE = 10 @@ -123,3 +125,43 @@ export function usePostAlsoLikedQuery( }, }) } + +export function* findAllPostsInQueryData( + queryClient: QueryClient, + uri: string, +): Generator { + const atUri = new AtUri(uri) + const queryDatas = queryClient.getQueriesData>({ + queryKey: [RQKEY_ROOT], + }) + + for (const [_queryKey, queryData] of queryDatas) { + if (!queryData?.pages) { + continue + } + + for (const page of queryData.pages) { + for (const post of page.posts) { + if (uriMatches(atUri, post)) { + yield post + } + + const quotedPost = getEmbeddedPost(post.embed) + if (quotedPost && uriMatches(atUri, quotedPost)) { + yield embedViewRecordToPostView(quotedPost) + } + } + } + } +} + +function uriMatches( + atUri: AtUri, + record: {uri: string; author: AppBskyFeedDefs.PostView['author']}, +) { + if (atUri.host.startsWith('did:')) { + return atUri.href === record.uri + } + + return atUri.host === record.author.handle && record.uri.endsWith(atUri.rkey) +} diff --git a/src/state/queries/usePostThread/queryCache.ts b/src/state/queries/usePostThread/queryCache.ts index bddda65aa..0e643e90c 100644 --- a/src/state/queries/usePostThread/queryCache.ts +++ b/src/state/queries/usePostThread/queryCache.ts @@ -18,6 +18,7 @@ import {findAllPostsInQueryData as findAllPostsInBookmarksQueryData} from '#/sta import {findAllPostsInQueryData as findAllPostsInExploreFeedPreviewsQueryData} from '#/state/queries/explore-feed-previews' import {findAllPostsInQueryData as findAllPostsInNotifsQueryData} from '#/state/queries/notifications/feed' import {findAllPostsInQueryData as findAllPostsInPostQueryData} from '#/state/queries/post' +import {findAllPostsInQueryData as findAllPostsInAlsoLikedQueryData} from '#/state/queries/post-also-liked' import {findAllPostsInQueryData as findAllPostsInFeedQueryData} from '#/state/queries/post-feed' import {findAllPostsInQueryData as findAllPostsInQuoteQueryData} from '#/state/queries/post-quotes' import {findAllPostsInQueryData as findAllPostsInSearchQueryData} from '#/state/queries/search-posts' @@ -263,6 +264,9 @@ export function* getThreadPlaceholderCandidates( for (let post of findAllPostsInSearchQueryData(queryClient, uri)) { yield postViewToThreadPlaceholder(post) } + for (let post of findAllPostsInAlsoLikedQueryData(queryClient, uri)) { + yield postViewToThreadPlaceholder(post) + } for (let post of findAllPostsInPostQueryData(queryClient, uri)) { yield postViewToThreadPlaceholder(post) }