delete useMyFollowsQuery (#3529)

zio/stable
Samuel Newman 2024-04-17 19:39:37 +01:00 committed by GitHub
parent d5982891fa
commit a2df81416b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 3 additions and 80 deletions

View File

@ -3,10 +3,8 @@ import {AppBskyActorDefs, moderateProfile, ModerationOpts} from '@atproto/api'
import {useQuery, useQueryClient} from '@tanstack/react-query'
import {isJustAMute} from '#/lib/moderation'
import {isInvalidHandle} from '#/lib/strings/handles'
import {logger} from '#/logger'
import {STALE} from '#/state/queries'
import {useMyFollowsQuery} from '#/state/queries/my-follows'
import {getAgent} from '#/state/session'
import {DEFAULT_LOGGED_OUT_PREFERENCES, useModerationOpts} from './preferences'
@ -19,7 +17,6 @@ const RQKEY_ROOT = 'actor-autocomplete'
export const RQKEY = (prefix: string) => [RQKEY_ROOT, prefix]
export function useActorAutocompleteQuery(prefix: string) {
const {data: follows, isFetching} = useMyFollowsQuery()
const moderationOpts = useModerationOpts()
prefix = prefix.toLowerCase()
@ -36,17 +33,11 @@ export function useActorAutocompleteQuery(prefix: string) {
: undefined
return res?.data.actors || []
},
enabled: !isFetching,
select: React.useCallback(
(data: AppBskyActorDefs.ProfileViewBasic[]) => {
return computeSuggestions(
prefix,
follows,
data,
moderationOpts || DEFAULT_MOD_OPTS,
)
return computeSuggestions(data, moderationOpts || DEFAULT_MOD_OPTS)
},
[prefix, follows, moderationOpts],
[moderationOpts],
),
})
}
@ -54,7 +45,6 @@ export function useActorAutocompleteQuery(prefix: string) {
export type ActorAutocompleteFn = ReturnType<typeof useActorAutocompleteFn>
export function useActorAutocompleteFn() {
const queryClient = useQueryClient()
const {data: follows} = useMyFollowsQuery()
const moderationOpts = useModerationOpts()
return React.useCallback(
@ -80,26 +70,19 @@ export function useActorAutocompleteFn() {
}
return computeSuggestions(
query,
follows,
res?.data.actors,
moderationOpts || DEFAULT_MOD_OPTS,
)
},
[follows, queryClient, moderationOpts],
[queryClient, moderationOpts],
)
}
function computeSuggestions(
prefix: string,
follows: AppBskyActorDefs.ProfileViewBasic[] | undefined,
searched: AppBskyActorDefs.ProfileViewBasic[] = [],
moderationOpts: ModerationOpts,
) {
let items: AppBskyActorDefs.ProfileViewBasic[] = []
if (follows) {
items = follows.filter(follow => prefixMatch(prefix, follow)).slice(0, 8)
}
for (const item of searched) {
if (!items.find(item2 => item2.handle === item.handle)) {
items.push(item)
@ -110,16 +93,3 @@ function computeSuggestions(
return !modui.filter || isJustAMute(modui)
})
}
function prefixMatch(
prefix: string,
info: AppBskyActorDefs.ProfileViewBasic,
): boolean {
if (!isInvalidHandle(info.handle) && info.handle.includes(prefix)) {
return true
}
if (info.displayName?.toLocaleLowerCase().includes(prefix)) {
return true
}
return false
}

View File

@ -1,47 +0,0 @@
import {AppBskyActorDefs} from '@atproto/api'
import {useQuery} from '@tanstack/react-query'
import {STALE} from '#/state/queries'
import {getAgent, useSession} from '../session'
// sanity limit is SANITY_PAGE_LIMIT*PAGE_SIZE total records
const SANITY_PAGE_LIMIT = 1000
const PAGE_SIZE = 100
// ...which comes 10,000k follows
const RQKEY_ROOT = 'my-follows'
export const RQKEY = () => [RQKEY_ROOT]
export function useMyFollowsQuery() {
const {currentAccount} = useSession()
return useQuery<AppBskyActorDefs.ProfileViewBasic[]>({
staleTime: STALE.MINUTES.ONE,
queryKey: RQKEY(),
async queryFn() {
if (!currentAccount) {
return []
}
let cursor
let arr: AppBskyActorDefs.ProfileViewBasic[] = []
for (let i = 0; i < SANITY_PAGE_LIMIT; i++) {
const res = await getAgent().getFollows({
actor: currentAccount.did,
cursor,
limit: PAGE_SIZE,
})
// TODO
// res.data.follows = res.data.follows.filter(
// profile =>
// !moderateProfile(profile, this.rootStore.preferences.moderationOpts)
// .account.filter,
// )
arr = arr.concat(res.data.follows)
if (!res.data.cursor) {
break
}
cursor = res.data.cursor
}
return arr
},
})
}