diff options
author | Paul Frazee <pfrazee@gmail.com> | 2023-11-14 10:41:55 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-14 10:41:55 -0800 |
commit | 0a26e78dcbbf48dad5daae73b210e236d706b22c (patch) | |
tree | c06c737ed49e8294bf5cbec1a75c36b591cb6669 /src/state/queries/actor-autocomplete.ts | |
parent | c687172de96bd6aa85d3aa025c2e0f024640f345 (diff) | |
download | voidsky-0a26e78dcbbf48dad5daae73b210e236d706b22c.tar.zst |
Composer update (react-query refactor) (#1899)
* Move composer state to a context * Rework composer to use RQ --------- Co-authored-by: Eric Bailey <git@esb.lol>
Diffstat (limited to 'src/state/queries/actor-autocomplete.ts')
-rw-r--r-- | src/state/queries/actor-autocomplete.ts | 54 |
1 files changed, 53 insertions, 1 deletions
diff --git a/src/state/queries/actor-autocomplete.ts b/src/state/queries/actor-autocomplete.ts index 18abb6314..62c4781c4 100644 --- a/src/state/queries/actor-autocomplete.ts +++ b/src/state/queries/actor-autocomplete.ts @@ -1,7 +1,8 @@ -import {AppBskyActorDefs} from '@atproto/api' +import {AppBskyActorDefs, BskyAgent} from '@atproto/api' import {useQuery} from '@tanstack/react-query' import {useSession} from '../session' import {useMyFollowsQuery} from './my-follows' +import AwaitLock from 'await-lock' export const RQKEY = (prefix: string) => ['actor-autocomplete', prefix] @@ -21,6 +22,57 @@ export function useActorAutocompleteQuery(prefix: string) { }) } +export class ActorAutocomplete { + // state + isLoading = false + isActive = false + prefix = '' + lock = new AwaitLock() + + // data + suggestions: AppBskyActorDefs.ProfileViewBasic[] = [] + + constructor( + public agent: BskyAgent, + public follows?: AppBskyActorDefs.ProfileViewBasic[] | undefined, + ) {} + + setFollows(follows: AppBskyActorDefs.ProfileViewBasic[]) { + this.follows = follows + } + + async query(prefix: string) { + const origPrefix = prefix.trim().toLocaleLowerCase() + this.prefix = origPrefix + await this.lock.acquireAsync() + try { + if (this.prefix) { + if (this.prefix !== origPrefix) { + return // another prefix was set before we got our chance + } + + // start with follow results + this.suggestions = computeSuggestions(this.prefix, this.follows) + + // ask backend + const res = await this.agent.searchActorsTypeahead({ + term: this.prefix, + limit: 8, + }) + this.suggestions = computeSuggestions( + this.prefix, + this.follows, + res.data.actors, + ) + } else { + this.suggestions = computeSuggestions(this.prefix, this.follows) + } + } finally { + this.lock.release() + } + } +} + function computeSuggestions( prefix: string, follows: AppBskyActorDefs.ProfileViewBasic[] = [], |