From 2918e863bf80aa918eb299866d5d7ad0d76e367a Mon Sep 17 00:00:00 2001 From: Rudy Fraser Date: Thu, 7 May 2026 14:31:06 -0400 Subject: [PATCH] Fix circular import: extract findAllPostsInTopicQueryData MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Move findAllPostsInTopicQueryData out of the screen file and into a state-only module. post-shadow.ts is loaded early in app boot, but its top-level import of #/screens/Topic dragged TabBar.web.tsx (and the rest of the view chain) along with it — and the chain re-entered #/alf before its 'web' export had finished initializing, throwing 'Cannot read properties of undefined (reading web)' at first paint. Same query key, same behavior; the function lives at #/state/queries/topic-feed and is shared via TOPIC_FEED_QUERY_KEY_ROOT so Topic.tsx and post-shadow can't drift. --- src/screens/Topic.tsx | 29 +++-------------------------- src/state/cache/post-shadow.ts | 2 +- src/state/queries/topic-feed.ts | 29 +++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 27 deletions(-) create mode 100644 src/state/queries/topic-feed.ts diff --git a/src/screens/Topic.tsx b/src/screens/Topic.tsx index e0407e854..e1a6f348e 100644 --- a/src/screens/Topic.tsx +++ b/src/screens/Topic.tsx @@ -4,11 +4,7 @@ import {type AppBskyFeedDefs} from '@atproto/api' import {msg} from '@lingui/core/macro' import {useLingui} from '@lingui/react' import {type NativeStackScreenProps} from '@react-navigation/native-stack' -import { - type InfiniteData, - type QueryClient, - useInfiniteQuery, -} from '@tanstack/react-query' +import {useInfiniteQuery} from '@tanstack/react-query' import {HITSLOP_10} from '#/lib/constants' import {useInitialNumToRender} from '#/lib/hooks/useInitialNumToRender' @@ -18,6 +14,7 @@ import {shareUrl} from '#/lib/sharing' import {cleanError} from '#/lib/strings/errors' import {enforceLen} from '#/lib/strings/helpers' import {useSearchPostsQuery} from '#/state/queries/search-posts' +import {TOPIC_FEED_QUERY_KEY_ROOT} from '#/state/queries/topic-feed' import {useAgent} from '#/state/session' import {Pager} from '#/view/com/pager/Pager' import {TabBar} from '#/view/com/pager/TabBar' @@ -125,7 +122,7 @@ function CuratedTopicFeed({ hasNextPage, isFetchingNextPage, } = useInfiniteQuery({ - queryKey: ['topic-feed', topicId], + queryKey: [TOPIC_FEED_QUERY_KEY_ROOT, topicId], initialPageParam: undefined as string | undefined, queryFn: async ({pageParam}) => { const params = new URLSearchParams({topicId, limit: String(PAGE_SIZE)}) @@ -211,26 +208,6 @@ function CuratedTopicFeed({ ) } -export function* findAllPostsInTopicQueryData( - queryClient: QueryClient, - uri: string, -): Generator { - type PageData = {posts: AppBskyFeedDefs.PostView[]; cursor: string | null} - const queryDatas = queryClient.getQueriesData>({ - queryKey: ['topic-feed'], - }) - for (const [_queryKey, queryData] of queryDatas) { - if (!queryData?.pages) continue - for (const page of queryData.pages) { - for (const post of page.posts) { - if (post.uri === uri) { - yield post - } - } - } - } -} - // Legacy search-based topic view for non-numeric topic params function LegacyTopicScreen({ topicParam, diff --git a/src/state/cache/post-shadow.ts b/src/state/cache/post-shadow.ts index eeb5071a1..33e78580a 100644 --- a/src/state/cache/post-shadow.ts +++ b/src/state/cache/post-shadow.ts @@ -14,8 +14,8 @@ import {findAllPostsInQueryData as findAllPostsInNotifsQueryData} from '#/state/ 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' +import {findAllPostsInTopicQueryData} from '#/state/queries/topic-feed' import {findAllPostsInQueryData as findAllPostsInThreadV2QueryData} from '#/state/queries/usePostThread/queryCache' -import {findAllPostsInTopicQueryData} from '#/screens/Topic' import {castAsShadow, type Shadow} from './types' export type {Shadow} from './types' diff --git a/src/state/queries/topic-feed.ts b/src/state/queries/topic-feed.ts new file mode 100644 index 000000000..046382397 --- /dev/null +++ b/src/state/queries/topic-feed.ts @@ -0,0 +1,29 @@ +import {type AppBskyFeedDefs} from '@atproto/api' +import {type InfiniteData, type QueryClient} from '@tanstack/react-query' + +/** + * Query key for the curated topic feed (consumed by Topic.tsx). + * Co-located here so callers like post-shadow can read from the cache + * without pulling in the screen module. + */ +export const TOPIC_FEED_QUERY_KEY_ROOT = 'topic-feed' + +export function* findAllPostsInTopicQueryData( + queryClient: QueryClient, + uri: string, +): Generator { + type PageData = {posts: AppBskyFeedDefs.PostView[]; cursor: string | null} + const queryDatas = queryClient.getQueriesData>({ + queryKey: [TOPIC_FEED_QUERY_KEY_ROOT], + }) + for (const [, queryData] of queryDatas) { + if (!queryData?.pages) continue + for (const page of queryData.pages) { + for (const post of page.posts) { + if (post.uri === uri) { + yield post + } + } + } + } +} -- knotmirror 0.0.1