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