migrate starter pack and search queries to the appview client
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,10 +1,5 @@
|
||||
import {useCallback, useMemo, useRef} from 'react'
|
||||
import {
|
||||
type AppBskyFeedDefs,
|
||||
type AppBskyFeedSearchPostsV2,
|
||||
AtUri,
|
||||
moderatePost,
|
||||
} from '@atproto/api'
|
||||
import {type AppBskyFeedDefs, AtUri, moderatePost} from '@atproto/api'
|
||||
import {
|
||||
type InfiniteData,
|
||||
type QueryClient,
|
||||
@@ -13,8 +8,9 @@ import {
|
||||
} from '@tanstack/react-query'
|
||||
|
||||
import {useModerationOpts} from '#/state/preferences/moderation-opts'
|
||||
import {useAgent} from '#/state/session'
|
||||
import {useAppviewClient} from '#/state/session'
|
||||
import {type SearchFilters} from '#/screens/Search/searchParams'
|
||||
import {app} from '#/lexicons'
|
||||
import {
|
||||
appendFromMe,
|
||||
buildSearchPostsV2Filters,
|
||||
@@ -48,7 +44,7 @@ export function useSearchPostsV2Query({
|
||||
enabled?: boolean
|
||||
filters?: SearchFilters
|
||||
}) {
|
||||
const agent = useAgent()
|
||||
const client = useAppviewClient()
|
||||
const moderationOpts = useModerationOpts()
|
||||
const selectArgs = useMemo(
|
||||
() => ({
|
||||
@@ -59,15 +55,15 @@ export function useSearchPostsV2Query({
|
||||
[query, filters?.author, filters?.from, moderationOpts],
|
||||
)
|
||||
const lastRun = useRef<{
|
||||
data: InfiniteData<AppBskyFeedSearchPostsV2.OutputSchema>
|
||||
data: InfiniteData<app.bsky.feed.searchPostsV2.$OutputBody>
|
||||
args: typeof selectArgs
|
||||
result: InfiniteData<AppBskyFeedSearchPostsV2.OutputSchema>
|
||||
result: InfiniteData<app.bsky.feed.searchPostsV2.$OutputBody>
|
||||
} | null>(null)
|
||||
|
||||
return useInfiniteQuery<
|
||||
AppBskyFeedSearchPostsV2.OutputSchema,
|
||||
app.bsky.feed.searchPostsV2.$OutputBody,
|
||||
Error,
|
||||
InfiniteData<AppBskyFeedSearchPostsV2.OutputSchema>,
|
||||
InfiniteData<app.bsky.feed.searchPostsV2.$OutputBody>,
|
||||
QueryKey,
|
||||
string | undefined
|
||||
>({
|
||||
@@ -79,9 +75,18 @@ export function useSearchPostsV2Query({
|
||||
* dialog; see buildSearchPostsV2Filters for how the two sources combine.
|
||||
*/
|
||||
const {q, ...embedded} = extractSearchPostsParams(query)
|
||||
const builtFilters = buildSearchPostsV2Filters(embedded, filters)
|
||||
/*
|
||||
* The generated params brand format-constrained strings (uri,
|
||||
* at-identifier, language) as template-literal types. The builder emits
|
||||
* plain strings parsed from user input, which are runtime-identical, so
|
||||
* the brand is asserted rather than re-validated here.
|
||||
*/
|
||||
const builtFilters = buildSearchPostsV2Filters(
|
||||
embedded,
|
||||
filters,
|
||||
) as app.bsky.feed.searchPostsV2.$Params
|
||||
const finalQuery = appendFromMe(q, filters?.from === 'me')
|
||||
const res = await agent.app.bsky.feed.searchPostsV2({
|
||||
return await client.call(app.bsky.feed.searchPostsV2, {
|
||||
...builtFilters,
|
||||
query: finalQuery,
|
||||
limit: 25,
|
||||
@@ -93,13 +98,12 @@ export function useSearchPostsV2Query({
|
||||
sort: sort === 'latest' ? 'recent' : sort,
|
||||
allTime: true,
|
||||
})
|
||||
return res.data
|
||||
},
|
||||
initialPageParam: undefined,
|
||||
getNextPageParam: lastPage => lastPage.cursor,
|
||||
enabled: enabled ?? !!moderationOpts,
|
||||
select: useCallback(
|
||||
(data: InfiniteData<AppBskyFeedSearchPostsV2.OutputSchema>) => {
|
||||
(data: InfiniteData<app.bsky.feed.searchPostsV2.$OutputBody>) => {
|
||||
const {moderationOpts, isSearchingSpecificUser} = selectArgs
|
||||
|
||||
/*
|
||||
@@ -177,7 +181,7 @@ export function* findAllPostsInQueryData(
|
||||
uri: string,
|
||||
): Generator<AppBskyFeedDefs.PostView, undefined> {
|
||||
const queryDatas = queryClient.getQueriesData<
|
||||
InfiniteData<AppBskyFeedSearchPostsV2.OutputSchema>
|
||||
InfiniteData<app.bsky.feed.searchPostsV2.$OutputBody>
|
||||
>({
|
||||
queryKey: [searchPostsQueryKeyRoot],
|
||||
})
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import {type AppBskyGraphSearchStarterPacksV2} from '@atproto/api'
|
||||
import {
|
||||
type InfiniteData,
|
||||
keepPreviousData,
|
||||
@@ -7,7 +6,8 @@ import {
|
||||
} from '@tanstack/react-query'
|
||||
|
||||
import {STALE} from '#/state/queries'
|
||||
import {useAgent} from '#/state/session'
|
||||
import {useAppviewClient} from '#/state/session'
|
||||
import {app} from '#/lexicons'
|
||||
|
||||
export const RQKEY_ROOT = 'starter-pack-search'
|
||||
export const RQKEY = (query: string, limit?: number) => [
|
||||
@@ -27,23 +27,22 @@ export function useStarterPackSearch({
|
||||
maintainData?: boolean
|
||||
limit?: number
|
||||
}) {
|
||||
const agent = useAgent()
|
||||
const client = useAppviewClient()
|
||||
return useInfiniteQuery<
|
||||
AppBskyGraphSearchStarterPacksV2.OutputSchema,
|
||||
app.bsky.graph.searchStarterPacksV2.$OutputBody,
|
||||
Error,
|
||||
InfiniteData<AppBskyGraphSearchStarterPacksV2.OutputSchema>,
|
||||
InfiniteData<app.bsky.graph.searchStarterPacksV2.$OutputBody>,
|
||||
QueryKey,
|
||||
string | undefined
|
||||
>({
|
||||
staleTime: STALE.MINUTES.FIVE,
|
||||
queryKey: RQKEY(query, limit),
|
||||
queryFn: async ({pageParam}) => {
|
||||
const res = await agent.app.bsky.graph.searchStarterPacksV2({
|
||||
return await client.call(app.bsky.graph.searchStarterPacksV2, {
|
||||
q: query,
|
||||
limit,
|
||||
cursor: pageParam,
|
||||
})
|
||||
return res.data
|
||||
},
|
||||
enabled: enabled && !!query,
|
||||
initialPageParam: undefined,
|
||||
@@ -54,7 +53,7 @@ export function useStarterPackSearch({
|
||||
}
|
||||
|
||||
function select(
|
||||
data: InfiniteData<AppBskyGraphSearchStarterPacksV2.OutputSchema>,
|
||||
data: InfiniteData<app.bsky.graph.searchStarterPacksV2.$OutputBody>,
|
||||
) {
|
||||
// enforce uniqueness
|
||||
const uris = new Set()
|
||||
|
||||
@@ -7,7 +7,8 @@ import {
|
||||
import {getContentLanguages} from '#/state/preferences/languages'
|
||||
import {STALE} from '#/state/queries'
|
||||
import {usePreferencesQuery} from '#/state/queries/preferences'
|
||||
import {useAgent} from '#/state/session'
|
||||
import {useAppviewClient} from '#/state/session'
|
||||
import {app} from '#/lexicons'
|
||||
|
||||
export const createOnboardingSuggestedStarterPacksQueryKey = (
|
||||
interests?: string[],
|
||||
@@ -20,7 +21,7 @@ export function useOnboardingSuggestedStarterPacksQuery({
|
||||
enabled?: boolean
|
||||
overrideInterests?: string[]
|
||||
}) {
|
||||
const agent = useAgent()
|
||||
const client = useAppviewClient()
|
||||
const {data: preferences} = usePreferencesQuery()
|
||||
const contentLangs = getContentLanguages().join(',')
|
||||
|
||||
@@ -29,21 +30,20 @@ export function useOnboardingSuggestedStarterPacksQuery({
|
||||
staleTime: STALE.MINUTES.THREE,
|
||||
queryKey: createOnboardingSuggestedStarterPacksQueryKey(overrideInterests),
|
||||
queryFn: async () => {
|
||||
const {data} =
|
||||
await agent.app.bsky.unspecced.getOnboardingSuggestedStarterPacks(
|
||||
{limit: 6},
|
||||
{
|
||||
headers: {
|
||||
...createBskyTopicsHeader(
|
||||
overrideInterests
|
||||
? overrideInterests.join(',')
|
||||
: aggregateUserInterests(preferences),
|
||||
),
|
||||
'Accept-Language': contentLangs,
|
||||
},
|
||||
return await client.call(
|
||||
app.bsky.unspecced.getOnboardingSuggestedStarterPacks,
|
||||
{limit: 6},
|
||||
{
|
||||
headers: {
|
||||
...createBskyTopicsHeader(
|
||||
overrideInterests
|
||||
? overrideInterests.join(',')
|
||||
: aggregateUserInterests(preferences),
|
||||
),
|
||||
'Accept-Language': contentLangs,
|
||||
},
|
||||
)
|
||||
return data
|
||||
},
|
||||
)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -7,7 +7,8 @@ import {
|
||||
import {getContentLanguages} from '#/state/preferences/languages'
|
||||
import {STALE} from '#/state/queries'
|
||||
import {usePreferencesQuery} from '#/state/queries/preferences'
|
||||
import {useAgent} from '#/state/session'
|
||||
import {useAppviewClient} from '#/state/session'
|
||||
import {app} from '#/lexicons'
|
||||
|
||||
export const createSuggestedStarterPacksQueryKey = (interests?: string[]) => [
|
||||
'suggested-starter-packs',
|
||||
@@ -21,7 +22,7 @@ export function useSuggestedStarterPacksQuery({
|
||||
enabled?: boolean
|
||||
overrideInterests?: string[]
|
||||
}) {
|
||||
const agent = useAgent()
|
||||
const client = useAppviewClient()
|
||||
const {data: preferences} = usePreferencesQuery()
|
||||
const contentLangs = getContentLanguages().join(',')
|
||||
|
||||
@@ -30,8 +31,9 @@ export function useSuggestedStarterPacksQuery({
|
||||
staleTime: STALE.MINUTES.THREE,
|
||||
queryKey: createSuggestedStarterPacksQueryKey(overrideInterests),
|
||||
queryFn: async () => {
|
||||
const {data} = await agent.app.bsky.unspecced.getSuggestedStarterPacks(
|
||||
undefined,
|
||||
return await client.call(
|
||||
app.bsky.unspecced.getSuggestedStarterPacks,
|
||||
{},
|
||||
{
|
||||
headers: {
|
||||
...createBskyTopicsHeader(
|
||||
@@ -43,7 +45,6 @@ export function useSuggestedStarterPacksQuery({
|
||||
},
|
||||
},
|
||||
)
|
||||
return data
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user