Skip to content

Commit

Permalink
Load profile image from Stargaze Names if nothing set. (#1488)
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahSaso authored Nov 28, 2023
1 parent 5cec27f commit 43a7ef1
Showing 1 changed file with 75 additions and 12 deletions.
87 changes: 75 additions & 12 deletions packages/stateful/recoil/selectors/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
STARGAZE_NAMES_CONTRACT,
getChainForChainId,
getFallbackImage,
objectMatchesStructure,
transformBech32Address,
} from '@dao-dao/utils'

Expand Down Expand Up @@ -92,6 +93,56 @@ export const stargazeNameSelector = selectorFamily<string | undefined, string>({
},
})

// Get image for address from Stargaze Names.
export const stargazeNameImageForAddressSelector = selectorFamily<
string | undefined,
string
>({
key: 'stargazeNameImageForAddress',
get:
(walletAddress) =>
async ({ get }) => {
// Get name associated with address.
const name = get(stargazeNameSelector(walletAddress))
if (!name) {
return
}

const chainId = MAINNET
? ChainId.StargazeMainnet
: ChainId.StargazeTestnet
const client = get(cosmWasmClientForChainSelector(chainId))

// Get NFT associated with name.
let response
try {
response = await client.queryContractSmart(STARGAZE_NAMES_CONTRACT, {
image_n_f_t: { name },
})
} catch {
return
}

// If NFT exists, get image associated with NFT.
if (
objectMatchesStructure(response, {
collection: {},
token_id: {},
})
) {
const { imageUrl } = get(
nftCardInfoSelector({
chainId,
collection: response.collection,
tokenId: response.token_id,
})
)

return imageUrl
}
},
})

export const makeDefaultWalletProfileData = (
address: string,
loading = false
Expand Down Expand Up @@ -134,12 +185,27 @@ export const walletProfileDataSelector = selectorFamily<
profile.nft = pfpkProfile.nft
}

// Load Stargaze name as backup if no PFPK set.
if (!profile.name) {
const stargazeNameLoadable = get(noWait(stargazeNameSelector(address)))
if (
stargazeNameLoadable.state === 'hasValue' &&
stargazeNameLoadable.contents
) {
profile.name =
stargazeNameLoadable.contents +
'.' +
getChainForChainId(chainId).bech32_prefix
profile.nameSource = 'stargaze'
}
}

const backupImageUrl = getFallbackImage(address)

// Set `imageUrl` to PFPK image, defaulting to fallback image.
profile.imageUrl = pfpkProfile?.nft?.imageUrl || backupImageUrl

// If NFT present from PFPK, get image from token.
// If NFT present from PFPK, get image from token once loaded.
if (pfpkProfile?.nft) {
// Don't wait for NFT info to load. When it loads, it will update.
const nftInfoLoadable = get(
Expand All @@ -159,20 +225,17 @@ export const walletProfileDataSelector = selectorFamily<
) {
profile.imageUrl = nftInfoLoadable.contents.imageUrl
}
}

// Load Stargaze name as backup if no PFPK set.
if (!profile.name) {
const stargazeNameLoadable = get(noWait(stargazeNameSelector(address)))
// Load Stargaze name image if no PFPK.
} else if (profile.nameSource === 'stargaze') {
const stargazeNameImageLoadable = get(
noWait(stargazeNameImageForAddressSelector(address))
)
if (
stargazeNameLoadable.state === 'hasValue' &&
stargazeNameLoadable.contents
stargazeNameImageLoadable.state === 'hasValue' &&
stargazeNameImageLoadable.contents
) {
profile.name =
stargazeNameLoadable.contents +
'.' +
getChainForChainId(chainId).bech32_prefix
profile.nameSource = 'stargaze'
profile.imageUrl = stargazeNameImageLoadable.contents
}
}

Expand Down

2 comments on commit 43a7ef1

@vercel
Copy link

@vercel vercel bot commented on 43a7ef1 Nov 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 43a7ef1 Nov 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.