Skip to content

Commit

Permalink
fix: add default profile to profile fetch (#1653)
Browse files Browse the repository at this point in the history
  • Loading branch information
andyesp authored Feb 15, 2024
1 parent b6bfb7f commit 19aaa32
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/components/Layout/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export default function Layout({ children }: LayoutProps) {
address={user || undefined}
avatar={hasProfile ? (profile as unknown as Avatar) : undefined}
activePage="governance"
isSignedIn={!isEmpty(profile)}
isSignedIn={hasProfile}
isSigningIn={userState.loading || isLoadingDclProfile}
onClickBalance={handleClickBalance}
onClickNavbarItem={handleClickNavbarOption}
Expand Down
1 change: 0 additions & 1 deletion src/hooks/useDclProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export default function useDclProfile(address?: string | null) {
queryKey: [`userProfile#${address?.toLowerCase()}`],
queryFn: () => fetchProfile(),
staleTime: DEFAULT_QUERY_STALE_TIME,
enabled: !!address,
})

return {
Expand Down
66 changes: 65 additions & 1 deletion src/utils/Catalyst/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,84 @@ function getUsername(profile: CatalystProfile | null, address: string) {
return hasClaimedName ? name : `${name.split('#')[0]}#${address.slice(-4)}`
}

const createDefaultProfile = (address: string): CatalystProfile => ({
userId: address,
ethAddress: address,
hasClaimedName: false,
avatar: {
snapshots: {
face: DEFAULT_AVATAR_IMAGE,
face128: DEFAULT_AVATAR_IMAGE,
face256: DEFAULT_AVATAR_IMAGE,
body: '',
},
bodyShape: 'dcl://base-avatars/BaseMale',
eyes: {
color: {
r: 0.125,
g: 0.703125,
b: 0.96484375,
},
},
hair: {
color: {
r: 0.234375,
g: 0.12890625,
b: 0.04296875,
},
},
skin: {
color: {
r: 0.94921875,
g: 0.76171875,
b: 0.6484375,
},
},
wearables: [
'dcl://base-avatars/green_hoodie',
'dcl://base-avatars/brown_pants',
'dcl://base-avatars/sneakers',
'dcl://base-avatars/casual_hair_01',
'dcl://base-avatars/beard',
],
version: 0,
},
name: '',
email: '',
description: '',
blocked: [],
inventory: [],
version: 0,
tutorialStep: 0,
isDefaultProfile: true,
})

function getDclProfile(profile: CatalystProfile | null, address: string): DclProfile {
const username = getUsername(profile, address)
const hasAvatar = !!profile && !!profile.avatar
const avatarUrl = hasAvatar ? profile.avatar.snapshots.face256 : DEFAULT_AVATAR_IMAGE

if (!profile) {
return {
...createDefaultProfile(address),
username,
avatarUrl,
hasCustomAvatar: hasAvatar,
address: address.toLowerCase(),
}
}

return { ...profile, username, avatarUrl, hasCustomAvatar: hasAvatar, address: address.toLowerCase() }
}

export async function getProfile(address: string): Promise<DclProfile> {
if (!isEthereumAddress(address)) {
if (!address || !isEthereumAddress(address)) {
throw new Error(`Invalid address provided. Value: ${address}`)
}

const response: ProfileResponse = await (await fetch(`${CATALYST_URL}/lambdas/profile/${address}`)).json()
const profile = response.avatars.length > 0 ? response.avatars[0] : null

return getDclProfile(profile, address)
}

Expand Down
1 change: 1 addition & 0 deletions src/utils/Catalyst/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type CatalystProfile = {
}
inventory?: string[]
hasConnectedWeb3?: boolean
isDefaultProfile: boolean
}

type Color = {
Expand Down

0 comments on commit 19aaa32

Please sign in to comment.