Refactor invites modal (#1930)
* Refactor invites modal * Replace in drawer * Delete stuff from me model
This commit is contained in:
parent
8a1fd160e6
commit
e6efeea7c0
6 changed files with 103 additions and 76 deletions
36
src/state/queries/invites.ts
Normal file
36
src/state/queries/invites.ts
Normal file
|
@ -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,
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue