-
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.
* fix: add search local social account * fix: search loading * fix: add useMemo dep * chore: code review * chore: code review * refactor: code style (#10895) --------- Co-authored-by: guanbinrui <52657989+guanbinrui@users.noreply.github.com>
- Loading branch information
1 parent
738e262
commit 8fd1857
Showing
7 changed files
with
131 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
84 changes: 57 additions & 27 deletions
84
packages/mask/src/extension/popups/hooks/useFriendsFromSearch.tsx
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,43 +1,73 @@ | ||
import { ECKeyIdentifier, EMPTY_LIST, type NextIDPersonaBindings } from '@masknet/shared-base' | ||
import { uniqBy } from 'lodash-es' | ||
import { useMemo } from 'react' | ||
import type { Friend } from './useFriends.js' | ||
import { uniqBy } from 'lodash-es' | ||
import { ECKeyIdentifier, EMPTY_LIST, type NextIDPersonaBindings } from '@masknet/shared-base' | ||
import { useCurrentLinkedPersona } from '@masknet/shared' | ||
import type { Friend } from './useFriends.js' | ||
import { profilesFilter } from './useFriendProfiles.js' | ||
import { PlatformSort } from '../pages/Friends/common.js' | ||
import { PlatformSort, type FriendNetwork, type Profile } from '../pages/Friends/common.js' | ||
|
||
export type NextIDPersonaBindingsWithIdentifier = NextIDPersonaBindings & { linkedPersona: ECKeyIdentifier } & { | ||
export type NextIDPersonaBindingsWithIdentifier = Omit<NextIDPersonaBindings, 'proofs'> & { proofs: Profile[] } & { | ||
linkedPersona: ECKeyIdentifier | ||
} & { | ||
isLocal?: boolean | ||
} | ||
|
||
export function useFriendsFromSearch( | ||
localSearchedResult: Friend[], | ||
searchResult?: NextIDPersonaBindings[], | ||
localList?: Friend[], | ||
searchValue?: string, | ||
): NextIDPersonaBindingsWithIdentifier[] { | ||
const currentIdentifier = useCurrentLinkedPersona() | ||
return useMemo(() => { | ||
if (!searchResult?.length) return EMPTY_LIST | ||
if (!searchResult?.length && !localSearchedResult?.length) return EMPTY_LIST | ||
const localProfiles: NextIDPersonaBindingsWithIdentifier[] = | ||
localSearchedResult | ||
?.filter((x) => x.persona.publicKeyAsHex !== currentIdentifier?.identifier.publicKeyAsHex && x.profile) | ||
.map((item) => { | ||
const profile = item.profile! | ||
return { | ||
proofs: [ | ||
{ | ||
platform: profile.network as FriendNetwork, | ||
identity: profile.userId, | ||
is_valid: true, | ||
last_checked_at: '', | ||
name: profile.userId, | ||
created_at: '', | ||
}, | ||
], | ||
linkedPersona: item.persona, | ||
activated_at: '', | ||
persona: item.persona.publicKeyAsHex, | ||
isLocal: true, | ||
} | ||
}) ?? EMPTY_LIST | ||
const profiles: NextIDPersonaBindingsWithIdentifier[] = searchResult | ||
.filter((x) => x.persona !== currentIdentifier?.identifier.publicKeyAsHex) | ||
.map((item) => { | ||
const filtered = item.proofs.filter(profilesFilter) | ||
const identifier = ECKeyIdentifier.fromHexPublicKeyK256(item.persona).expect( | ||
`${item.persona} should be a valid hex public key in k256`, | ||
) | ||
filtered.sort((a, b) => PlatformSort[a.platform] - PlatformSort[b.platform]) | ||
const searchItem = filtered.findIndex((x) => x.identity === searchValue || x.name === searchValue) | ||
if (searchItem !== -1) filtered.unshift(filtered.splice(searchItem, 1)[0]) | ||
return { | ||
proofs: uniqBy(filtered, ({ identity }) => identity), | ||
linkedPersona: identifier, | ||
activated_at: item.activated_at, | ||
persona: item.persona, | ||
isLocal: localList | ||
? localList.some((x) => x.persona.publicKeyAsHex === identifier.publicKeyAsHex) | ||
: false, | ||
} | ||
}) | ||
return uniqBy(profiles, ({ linkedPersona }) => linkedPersona.publicKeyAsHex) | ||
}, [searchResult, localList, currentIdentifier]) | ||
? searchResult | ||
.filter((x) => x.persona !== currentIdentifier?.identifier.publicKeyAsHex) | ||
.map((item) => { | ||
const filtered = item.proofs.filter(profilesFilter) | ||
const identifier = ECKeyIdentifier.fromHexPublicKeyK256(item.persona).expect( | ||
`${item.persona} should be a valid hex public key in k256`, | ||
) | ||
filtered.sort((a, b) => PlatformSort[a.platform] - PlatformSort[b.platform]) | ||
const searchItem = filtered.findIndex((x) => x.identity === searchValue || x.name === searchValue) | ||
if (searchItem !== -1) filtered.unshift(filtered.splice(searchItem, 1)[0]) | ||
return { | ||
proofs: uniqBy(filtered, ({ identity }) => identity), | ||
linkedPersona: identifier, | ||
activated_at: item.activated_at, | ||
persona: item.persona, | ||
isLocal: localList | ||
? localList.some((x) => x.persona.publicKeyAsHex === identifier.publicKeyAsHex) | ||
: false, | ||
} | ||
}) | ||
: EMPTY_LIST | ||
return uniqBy( | ||
localProfiles ? localProfiles.concat(profiles) : profiles, | ||
({ linkedPersona }) => linkedPersona.publicKeyAsHex, | ||
) | ||
}, [searchResult, localList, currentIdentifier, localSearchedResult]) | ||
} |
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
6 changes: 3 additions & 3 deletions
6
packages/plugins/Calendar/src/SiteAdaptor/CalendarContent.tsx
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