Skip to content

Commit

Permalink
feat: improve seed for name and color
Browse files Browse the repository at this point in the history
  • Loading branch information
teodorus-nathaniel committed Dec 11, 2023
1 parent f784e73 commit a6e49f5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/components/Name.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import KusamaIcon from '@/assets/icons/kusama-dynamic-size.svg'
import PolkadotIcon from '@/assets/icons/polkadot-dynamic-size.svg'
import SubsocialIcon from '@/assets/icons/subsocial-dynamic-size.svg'
import XLogoIcon from '@/assets/icons/x-logo-dynamic-size.svg'
import useAddressRandomSeed from '@/hooks/useAddressRandomSeed'
import useRandomColor from '@/hooks/useRandomColor'
import { getIdentityQuery, getProfileQuery } from '@/services/api/query'
import { getLinkedIdentityQuery } from '@/services/datahub/identity/query'
Expand Down Expand Up @@ -168,7 +169,8 @@ export function useName(
const isLoadingIdentities = isFetchingIdentities && isIdentitiesNeeded

const { ensNames, evmAddress } = accountData || {}
let name = generateRandomName(address)
const randomSeed = useAddressRandomSeed(address)
let name = generateRandomName(randomSeed)

function getNameFromSource(profileSource?: ProfileSource, content?: string) {
switch (profileSource) {
Expand Down
18 changes: 18 additions & 0 deletions src/hooks/useAddressRandomSeed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { getLinkedIdentityQuery } from '@/services/datahub/identity/query'
import { getAccountDataQuery } from '@/services/subsocial/evmAddresses'

export default function useAddressRandomSeed(
address: string,
{ enabled } = { enabled: true }
) {
const { data: linkedIdentity } = getLinkedIdentityQuery.useQuery(
address || '',
{ enabled }
)
const { data: accountData } = getAccountDataQuery.useQuery(address || '', {
enabled,
})
const { evmAddress } = accountData || {}

return evmAddress || linkedIdentity?.externalId || address
}
10 changes: 4 additions & 6 deletions src/hooks/useRandomColor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Theme } from '@/@types/theme'
import { useConfigContext } from '@/providers/ConfigProvider'
import { getAccountDataQuery } from '@/services/subsocial/evmAddresses'
import { generateRandomColor } from '@/utils/random-colors'
import useAddressRandomSeed from './useAddressRandomSeed'
import useGetTheme from './useGetTheme'

export default function useRandomColor(
Expand All @@ -15,14 +15,12 @@ export default function useRandomColor(

const { theme: configTheme } = useConfigContext()
const currentTheme = useGetTheme()
const { data: accountData } = getAccountDataQuery.useQuery(seed || '', {
enabled: !!isAddress,
const addressSeed = useAddressRandomSeed(seed ?? '', {
enabled: !!isAddress && !!seed,
})

const { evmAddress } = accountData || {}

return generateRandomColor(
evmAddress || seed,
addressSeed || seed,
theme ?? configTheme ?? currentTheme
)
}

0 comments on commit a6e49f5

Please sign in to comment.