-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: next id query * refactor: next id query
- Loading branch information
1 parent
bec6e62
commit 1131d7e
Showing
13 changed files
with
75 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 11 additions & 9 deletions
20
packages/plugin-infra/src/site-adaptor/useSocialIdentity.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,27 @@ | ||
import { useEffect } from 'react' | ||
import { useAsyncRetry } from 'react-use' | ||
import { MaskMessages, type SocialIdentity } from '@masknet/shared-base' | ||
import type { IdentityResolved } from '../types.js' | ||
import { useSiteAdaptorContext } from '../dom/useSiteAdaptorContext.js' | ||
import { useQuery } from '@tanstack/react-query' | ||
|
||
/** | ||
* Get the social identity of the given identity | ||
*/ | ||
export function useSocialIdentity(identity: IdentityResolved | null | undefined) { | ||
const { getSocialIdentity, getNextIDPlatform } = useSiteAdaptorContext() | ||
|
||
const result = useAsyncRetry<SocialIdentity | undefined>(async () => { | ||
if (!identity) return | ||
const platform = getNextIDPlatform() | ||
|
||
const platform = getNextIDPlatform() | ||
if (!platform) return | ||
const result = useQuery<SocialIdentity | undefined, Error>({ | ||
queryKey: ['next-id', identity, platform], | ||
enabled: Boolean(identity && platform), | ||
queryFn: async () => { | ||
if (!platform || !identity) return | ||
return getSocialIdentity(platform, identity) | ||
}, | ||
}) | ||
|
||
return getSocialIdentity(platform, identity) | ||
}, [identity]) | ||
|
||
useEffect(() => MaskMessages.events.ownProofChanged.on(result.retry), [result.retry]) | ||
useEffect(() => MaskMessages.events.ownProofChanged.on(() => result.refetch()), [result.refetch]) | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,47 @@ | ||
import { useEffect } from 'react' | ||
import { useAsyncRetry } from 'react-use' | ||
import { EMPTY_LIST, MaskMessages, NextIDPlatform } from '@masknet/shared-base' | ||
import { | ||
EMPTY_LIST, | ||
MaskMessages, | ||
NextIDPlatform, | ||
type BindingProof, | ||
type PersonaInformation, | ||
} from '@masknet/shared-base' | ||
import { NextIDProof } from '@masknet/web3-providers' | ||
import { useAllPersonas, useSiteAdaptorContext } from '@masknet/plugin-infra/content-script' | ||
import { useQuery } from '@tanstack/react-query' | ||
|
||
export function useConnectedPersonas() { | ||
const personasInDB = useAllPersonas() | ||
const { getPersonaAvatars } = useSiteAdaptorContext() | ||
|
||
const result = useAsyncRetry(async () => { | ||
const allPersonaPublicKeys = personasInDB.map((x) => x.identifier.publicKeyAsHex) | ||
const allPersonaIdentifiers = personasInDB.map((x) => x.identifier) | ||
const result = useQuery< | ||
Array<{ persona: PersonaInformation; proof: BindingProof[]; avatar: string | undefined }>, | ||
Error | ||
>({ | ||
queryFn: async () => { | ||
const allPersonaPublicKeys = personasInDB.map((x) => x.identifier.publicKeyAsHex) | ||
const allPersonaIdentifiers = personasInDB.map((x) => x.identifier) | ||
|
||
const avatars = await getPersonaAvatars?.(allPersonaIdentifiers) | ||
const allNextIDBindings = await NextIDProof.queryExistedBindingByPlatform( | ||
NextIDPlatform.NextID, | ||
allPersonaPublicKeys.join(','), | ||
) | ||
const avatars = await getPersonaAvatars?.(allPersonaIdentifiers) | ||
const allNextIDBindings = await NextIDProof.queryAllExistedBindingsByPlatform( | ||
NextIDPlatform.NextID, | ||
allPersonaPublicKeys.join(','), | ||
) | ||
|
||
return personasInDB.map((x) => { | ||
return { | ||
persona: x, | ||
proof: | ||
allNextIDBindings | ||
.find((p) => p.persona.toLowerCase() === x.identifier.publicKeyAsHex.toLowerCase()) | ||
?.proofs.filter((x) => x.is_valid) ?? EMPTY_LIST, | ||
avatar: avatars?.get(x.identifier), | ||
} | ||
}) | ||
}, [personasInDB, getPersonaAvatars]) | ||
return personasInDB.map((x) => { | ||
return { | ||
persona: x, | ||
proof: | ||
allNextIDBindings | ||
.find((p) => p.persona.toLowerCase() === x.identifier.publicKeyAsHex.toLowerCase()) | ||
?.proofs.filter((x) => x.is_valid) ?? EMPTY_LIST, | ||
avatar: avatars?.get(x.identifier), | ||
} | ||
}) | ||
}, | ||
}) | ||
|
||
useEffect(() => MaskMessages.events.ownProofChanged.on(result.retry), [result.retry]) | ||
useEffect(() => MaskMessages.events.ownProofChanged.on(() => result.refetch()), [result.refetch]) | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,21 @@ | ||
import { useEffect } from 'react' | ||
import { useAsyncRetry } from 'react-use' | ||
import { EMPTY_LIST, MaskMessages, type NextIDPlatform } from '@masknet/shared-base' | ||
import { EMPTY_LIST, MaskMessages, type NextIDPersonaBindings, type NextIDPlatform } from '@masknet/shared-base' | ||
import { NextIDProof } from '@masknet/web3-providers' | ||
import { useQuery } from '@tanstack/react-query' | ||
|
||
/** | ||
* Get all personas bound with the given identity from NextID service | ||
*/ | ||
export function usePersonasFromNextID(userId: string, platform: NextIDPlatform, exact?: boolean) { | ||
const asyncRetry = useAsyncRetry(async () => { | ||
if (!platform || !userId) return EMPTY_LIST | ||
return NextIDProof.queryAllExistedBindingsByPlatform(platform, userId, exact) | ||
}, [platform, userId, exact]) | ||
useEffect(() => MaskMessages.events.ownProofChanged.on(asyncRetry.retry), [asyncRetry.retry]) | ||
return asyncRetry | ||
const result = useQuery<NextIDPersonaBindings[], Error>({ | ||
queryKey: ['next-id', 'personas', userId], | ||
enabled: Boolean(platform && userId), | ||
queryFn: async () => { | ||
if (!platform || !userId) return EMPTY_LIST | ||
return NextIDProof.queryAllExistedBindingsByPlatform(platform, userId, exact) | ||
}, | ||
}) | ||
|
||
useEffect(() => MaskMessages.events.ownProofChanged.on(() => result.refetch), [result.refetch]) | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters