diff options
author | Eric Bailey <git@esb.lol> | 2023-11-12 18:26:02 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-12 16:26:02 -0800 |
commit | c584a3378d66459c04eee7d98560920e09c5f09f (patch) | |
tree | eafe583f664254ab8af530ec3c67859851e6dc46 /src/state/queries | |
parent | d9e0a927c1c98ebd6aa3885ab517af27e7de2522 (diff) | |
download | voidsky-c584a3378d66459c04eee7d98560920e09c5f09f.tar.zst |
Refactor My Feeds (#1877)
* Refactor My Feeds screen * Remove unused feed UI models * Add back PTR
Diffstat (limited to 'src/state/queries')
-rw-r--r-- | src/state/queries/feed.ts | 70 |
1 files changed, 68 insertions, 2 deletions
diff --git a/src/state/queries/feed.ts b/src/state/queries/feed.ts index 0ba323314..5754d2c70 100644 --- a/src/state/queries/feed.ts +++ b/src/state/queries/feed.ts @@ -1,5 +1,17 @@ -import {useQuery} from '@tanstack/react-query' -import {AtUri, RichText, AppBskyFeedDefs, AppBskyGraphDefs} from '@atproto/api' +import { + useQuery, + useInfiniteQuery, + InfiniteData, + QueryKey, + useMutation, +} from '@tanstack/react-query' +import { + AtUri, + RichText, + AppBskyFeedDefs, + AppBskyGraphDefs, + AppBskyUnspeccedGetPopularFeedGenerators, +} from '@atproto/api' import {sanitizeDisplayName} from '#/lib/strings/display-names' import {sanitizeHandle} from '#/lib/strings/handles' @@ -10,6 +22,7 @@ type FeedSourceInfo = type: 'feed' uri: string cid: string + href: string avatar: string | undefined displayName: string description: RichText @@ -22,6 +35,7 @@ type FeedSourceInfo = type: 'list' uri: string cid: string + href: string avatar: string | undefined displayName: string description: RichText @@ -42,10 +56,16 @@ const feedSourceNSIDs = { function hydrateFeedGenerator( view: AppBskyFeedDefs.GeneratorView, ): FeedSourceInfo { + const urip = new AtUri(view.uri) + const collection = + urip.collection === 'app.bsky.feed.generator' ? 'feed' : 'lists' + const href = `/profile/${urip.hostname}/${collection}/${urip.rkey}` + return { type: 'feed', uri: view.uri, cid: view.cid, + href, avatar: view.avatar, displayName: view.displayName ? sanitizeDisplayName(view.displayName) @@ -62,10 +82,16 @@ function hydrateFeedGenerator( } function hydrateList(view: AppBskyGraphDefs.ListView): FeedSourceInfo { + const urip = new AtUri(view.uri) + const collection = + urip.collection === 'app.bsky.feed.generator' ? 'feed' : 'lists' + const href = `/profile/${urip.hostname}/${collection}/${urip.rkey}` + return { type: 'list', uri: view.uri, cid: view.cid, + href, avatar: view.avatar, description: new RichText({ text: view.description || '', @@ -104,3 +130,43 @@ export function useFeedSourceInfoQuery({uri}: {uri: string}) { }, }) } + +export const useGetPopularFeedsQueryKey = ['getPopularFeeds'] + +export function useGetPopularFeedsQuery() { + const {agent} = useSession() + + return useInfiniteQuery< + AppBskyUnspeccedGetPopularFeedGenerators.OutputSchema, + Error, + InfiniteData<AppBskyUnspeccedGetPopularFeedGenerators.OutputSchema>, + QueryKey, + string | undefined + >({ + queryKey: useGetPopularFeedsQueryKey, + queryFn: async ({pageParam}) => { + const res = await agent.app.bsky.unspecced.getPopularFeedGenerators({ + limit: 10, + cursor: pageParam, + }) + return res.data + }, + initialPageParam: undefined, + getNextPageParam: lastPage => lastPage.cursor, + }) +} + +export function useSearchPopularFeedsMutation() { + const {agent} = useSession() + + return useMutation({ + mutationFn: async (query: string) => { + const res = await agent.app.bsky.unspecced.getPopularFeedGenerators({ + limit: 10, + query: query, + }) + + return res.data.feeds + }, + }) +} |