De-closure feed

This commit is contained in:
Paul Frazee
2023-11-28 16:36:41 -08:00
parent 3eafdbefbd
commit 10f48d8947
+56 -62
View File
@@ -6,6 +6,7 @@ import {
QueryKey, QueryKey,
useMutation, useMutation,
useQueryClient, useQueryClient,
QueryFunctionContext,
} from '@tanstack/react-query' } from '@tanstack/react-query'
import { import {
AtUri, AtUri,
@@ -136,30 +137,32 @@ export function getFeedTypeFromUri(uri: string) {
} }
export function useFeedSourceInfoQuery({uri}: {uri: string}) { export function useFeedSourceInfoQuery({uri}: {uri: string}) {
const type = getFeedTypeFromUri(uri)
return useQuery({ return useQuery({
staleTime: STALE.INFINITY, staleTime: STALE.INFINITY,
queryKey: feedSourceInfoQueryKey({uri}), queryKey: feedSourceInfoQueryKey({uri}),
queryFn: async () => { queryFn: feedSourceInfoQueryFn,
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
},
}) })
} }
async function feedSourceInfoQueryFn({queryKey}: QueryFunctionContext) {
let view: FeedSourceInfo
const [_, uri] = queryKey as ReturnType<typeof feedSourceInfoQueryKey>
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}) => [ export const isFeedPublicQueryKey = ({uri}: {uri: string}) => [
'isFeedPublic', 'isFeedPublic',
uri, uri,
@@ -168,30 +171,32 @@ export const isFeedPublicQueryKey = ({uri}: {uri: string}) => [
export function useIsFeedPublicQuery({uri}: {uri: string}) { export function useIsFeedPublicQuery({uri}: {uri: string}) {
return useQuery({ return useQuery({
queryKey: isFeedPublicQueryKey({uri}), queryKey: isFeedPublicQueryKey({uri}),
queryFn: async ({queryKey}) => { queryFn: isFeedPublicQueryFn,
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
}
},
}) })
} }
async function isFeedPublicQueryFn({queryKey}: QueryFunctionContext) {
const [, uri] = queryKey as ReturnType<typeof isFeedPublicQueryKey>
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 const useGetPopularFeedsQueryKey = ['getPopularFeeds']
export function useGetPopularFeedsQuery() { export function useGetPopularFeedsQuery() {
@@ -203,18 +208,22 @@ export function useGetPopularFeedsQuery() {
string | undefined string | undefined
>({ >({
queryKey: useGetPopularFeedsQueryKey, queryKey: useGetPopularFeedsQueryKey,
queryFn: async ({pageParam}) => { queryFn: getPopularFeedsQueryFn,
const res = await getAgent().app.bsky.unspecced.getPopularFeedGenerators({
limit: 10,
cursor: pageParam,
})
return res.data
},
initialPageParam: undefined, initialPageParam: undefined,
getNextPageParam: lastPage => lastPage.cursor, getNextPageParam: lastPage => lastPage.cursor,
}) })
} }
async function getPopularFeedsQueryFn({
pageParam,
}: QueryFunctionContext<QueryKey, string | undefined>) {
const res = await getAgent().app.bsky.unspecced.getPopularFeedGenerators({
limit: 10,
cursor: pageParam,
})
return res.data
}
export function useSearchPopularFeedsMutation() { export function useSearchPopularFeedsMutation() {
return useMutation({ return useMutation({
mutationFn: async (query: string) => { mutationFn: async (query: string) => {
@@ -271,22 +280,7 @@ export function usePinnedFeedsInfos(): FeedSourceInfo[] {
reqs.push( reqs.push(
queryClient.fetchQuery({ queryClient.fetchQuery({
queryKey: feedSourceInfoQueryKey({uri}), queryKey: feedSourceInfoQueryKey({uri}),
queryFn: async () => { queryFn: feedSourceInfoQueryFn,
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)
}
},
}), }),
) )
} }