diff options
author | Eric Bailey <git@esb.lol> | 2023-11-16 10:40:31 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-16 08:40:31 -0800 |
commit | e6efeea7c07682c981998483bd49d7c01822911e (patch) | |
tree | 0da4d0b8ba03648fe8ceaef92d8d53ef4cc9fd9d /src/state/queries | |
parent | 8a1fd160e6a1f9beeb735bb2320c12e5e71963d6 (diff) | |
download | voidsky-e6efeea7c07682c981998483bd49d7c01822911e.tar.zst |
Refactor invites modal (#1930)
* Refactor invites modal * Replace in drawer * Delete stuff from me model
Diffstat (limited to 'src/state/queries')
-rw-r--r-- | src/state/queries/invites.ts | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/state/queries/invites.ts b/src/state/queries/invites.ts new file mode 100644 index 000000000..77494d273 --- /dev/null +++ b/src/state/queries/invites.ts @@ -0,0 +1,36 @@ +import {ComAtprotoServerDefs} from '@atproto/api' +import {useQuery} from '@tanstack/react-query' + +import {useSession} from '#/state/session' + +function isInviteAvailable(invite: ComAtprotoServerDefs.InviteCode): boolean { + return invite.available - invite.uses.length > 0 && !invite.disabled +} + +export type InviteCodesQueryResponse = Exclude< + ReturnType<typeof useInviteCodesQuery>['data'], + undefined +> +export function useInviteCodesQuery() { + const {agent} = useSession() + + return useQuery({ + queryKey: ['inviteCodes'], + queryFn: async () => { + const res = await agent.com.atproto.server.getAccountInviteCodes({}) + + if (!res.data?.codes) { + throw new Error(`useInviteCodesQuery: no codes returned`) + } + + const available = res.data.codes.filter(isInviteAvailable) + const used = res.data.codes.filter(code => !isInviteAvailable(code)) + + return { + all: [...available, ...used], + available, + used, + } + }, + }) +} |