From 10f48d8947bf27726d62136d21c12cf48b16f4e3 Mon Sep 17 00:00:00 2001 From: Paul Frazee Date: Tue, 28 Nov 2023 16:36:41 -0800 Subject: [PATCH] De-closure feed --- src/state/queries/feed.ts | 118 ++++++++++++++++++-------------------- 1 file changed, 56 insertions(+), 62 deletions(-) diff --git a/src/state/queries/feed.ts b/src/state/queries/feed.ts index b5d491a5ce..c081b012dd 100644 --- a/src/state/queries/feed.ts +++ b/src/state/queries/feed.ts @@ -6,6 +6,7 @@ import { QueryKey, useMutation, useQueryClient, + QueryFunctionContext, } from '@tanstack/react-query' import { AtUri, @@ -136,30 +137,32 @@ export function getFeedTypeFromUri(uri: string) { } export function useFeedSourceInfoQuery({uri}: {uri: string}) { - const type = getFeedTypeFromUri(uri) - return useQuery({ staleTime: STALE.INFINITY, queryKey: feedSourceInfoQueryKey({uri}), - queryFn: async () => { - let view: FeedSourceInfo - - if (type === 'feed') { - const res = await getAgent().app.bsky.feed.getFeedGenerator({feed: uri}) - view = hydrateFeedGenerator(res.data.view) - } else { - const res = await getAgent().app.bsky.graph.getList({ - list: uri, - limit: 1, - }) - view = hydrateList(res.data.list) - } - - return view - }, + queryFn: feedSourceInfoQueryFn, }) } +async function feedSourceInfoQueryFn({queryKey}: QueryFunctionContext) { + let view: FeedSourceInfo + const [_, uri] = queryKey as ReturnType + const type = getFeedTypeFromUri(uri) + + if (type === 'feed') { + const res = await getAgent().app.bsky.feed.getFeedGenerator({feed: uri}) + view = hydrateFeedGenerator(res.data.view) + } else { + const res = await getAgent().app.bsky.graph.getList({ + list: uri, + limit: 1, + }) + view = hydrateList(res.data.list) + } + + return view +} + export const isFeedPublicQueryKey = ({uri}: {uri: string}) => [ 'isFeedPublic', uri, @@ -168,30 +171,32 @@ export const isFeedPublicQueryKey = ({uri}: {uri: string}) => [ export function useIsFeedPublicQuery({uri}: {uri: string}) { return useQuery({ queryKey: isFeedPublicQueryKey({uri}), - queryFn: async ({queryKey}) => { - const [, uri] = queryKey - try { - const res = await getAgent().app.bsky.feed.getFeed({ - feed: uri, - limit: 1, - }) - return Boolean(res.data.feed) - } catch (e: any) { - const msg = e.toString() as string - - if (msg.includes('missing jwt')) { - return false - } else if (msg.includes('This feed requires being logged-in')) { - // e.g. https://github.com/bluesky-social/atproto/blob/99ab1ae55c463e8d5321a1eaad07a175bdd56fea/packages/bsky/src/feed-gen/best-of-follows.ts#L13 - return false - } - - return true - } - }, + queryFn: isFeedPublicQueryFn, }) } +async function isFeedPublicQueryFn({queryKey}: QueryFunctionContext) { + const [, uri] = queryKey as ReturnType + try { + const res = await getAgent().app.bsky.feed.getFeed({ + feed: uri, + limit: 1, + }) + return Boolean(res.data.feed) + } catch (e: any) { + const msg = e.toString() as string + + if (msg.includes('missing jwt')) { + return false + } else if (msg.includes('This feed requires being logged-in')) { + // e.g. https://github.com/bluesky-social/atproto/blob/99ab1ae55c463e8d5321a1eaad07a175bdd56fea/packages/bsky/src/feed-gen/best-of-follows.ts#L13 + return false + } + + return true + } +} + export const useGetPopularFeedsQueryKey = ['getPopularFeeds'] export function useGetPopularFeedsQuery() { @@ -203,18 +208,22 @@ export function useGetPopularFeedsQuery() { string | undefined >({ queryKey: useGetPopularFeedsQueryKey, - queryFn: async ({pageParam}) => { - const res = await getAgent().app.bsky.unspecced.getPopularFeedGenerators({ - limit: 10, - cursor: pageParam, - }) - return res.data - }, + queryFn: getPopularFeedsQueryFn, initialPageParam: undefined, getNextPageParam: lastPage => lastPage.cursor, }) } +async function getPopularFeedsQueryFn({ + pageParam, +}: QueryFunctionContext) { + const res = await getAgent().app.bsky.unspecced.getPopularFeedGenerators({ + limit: 10, + cursor: pageParam, + }) + return res.data +} + export function useSearchPopularFeedsMutation() { return useMutation({ mutationFn: async (query: string) => { @@ -271,22 +280,7 @@ export function usePinnedFeedsInfos(): FeedSourceInfo[] { reqs.push( queryClient.fetchQuery({ queryKey: feedSourceInfoQueryKey({uri}), - queryFn: async () => { - const type = getFeedTypeFromUri(uri) - - if (type === 'feed') { - const res = await getAgent().app.bsky.feed.getFeedGenerator({ - feed: uri, - }) - return hydrateFeedGenerator(res.data.view) - } else { - const res = await getAgent().app.bsky.graph.getList({ - list: uri, - limit: 1, - }) - return hydrateList(res.data.list) - } - }, + queryFn: feedSourceInfoQueryFn, }), ) }