Skip to content

Commit

Permalink
feat: use skin hair and body shape from avatar (decentraland#487)
Browse files Browse the repository at this point in the history
* feat: use skin hair and body shape from avatar

* fix: user without avatar
  • Loading branch information
cazala authored Dec 20, 2021
1 parent e502949 commit ae1d3ab
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 26 deletions.
6 changes: 3 additions & 3 deletions webapp/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion webapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"date-fns": "^2.23.0",
"decentraland-dapps": "^12.32.0",
"decentraland-transactions": "^1.23.1",
"decentraland-ui": "^3.14.0",
"decentraland-ui": "^3.15.0",
"dotenv": "^10.0.0",
"graphql": "^14.7.0",
"history": "^4.10.1",
Expand Down
28 changes: 28 additions & 0 deletions webapp/src/components/AssetImage/AssetImage.container.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { getData as getProfiles } from 'decentraland-dapps/dist/modules/profile/selectors'
import { Avatar } from 'decentraland-ui'
import { connect } from 'react-redux'
import { RootState } from '../../modules/reducer'
import { getWallet } from '../../modules/wallet/selectors'
import {
MapStateProps,
MapDispatchProps,
MapDispatch
} from './AssetImage.types'
import AssetImage from './AssetImage'

const mapState = (state: RootState): MapStateProps => {
const profiles = getProfiles(state)
const wallet = getWallet(state)
let avatar: Avatar | undefined = undefined
if (wallet && !!profiles[wallet.address]) {
const profile = profiles[wallet.address]
avatar = profile.avatars[0]
}
return {
avatar
}
}

const mapDispatch = (_dispatch: MapDispatch): MapDispatchProps => ({})

export default connect(mapState, mapDispatch)(AssetImage)
55 changes: 34 additions & 21 deletions webapp/src/components/AssetImage/AssetImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ import './AssetImage.css'
const PIXEL =
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR4nGNiYAAAAAkAAxkR2eQAAAAASUVORK5CYII='

const valueToHex = (value: number) =>
('00' + Math.min(255, (value * 255) | 0).toString(16)).slice(-2)

const colorToHex = (color: { r: number; g: number; b: number }) =>
valueToHex(color.r) + valueToHex(color.g) + valueToHex(color.b)

const AssetImage = (props: Props) => {
const {
asset,
Expand All @@ -21,7 +27,8 @@ const AssetImage = (props: Props) => {
hasPopup,
zoom,
isSmall,
showMonospace
showMonospace,
avatar
} = props
const { parcel, estate, wearable, ens } = asset.data

Expand Down Expand Up @@ -75,28 +82,24 @@ const AssetImage = (props: Props) => {
const isDev = process.env.REACT_APP_NETWORK !== 'mainnet'

if (isDraggable) {
let component: React.ReactNode
let itemId: string | undefined
let tokenId: string | undefined
let skin = 'bbbbbb'
let hair = 'bbbbbb'
let shape: 'male' | 'female' = 'male'
if ('itemId' in asset && asset.itemId) {
component = (
<WearablePreview
contractAddress={asset.contractAddress}
itemId={asset.itemId}
dev={isDev}
onLoad={handleLoad}
onError={handleError}
/>
)
itemId = asset.itemId
} else if ('tokenId' in asset && asset.tokenId) {
component = (
<WearablePreview
contractAddress={asset.contractAddress}
tokenId={asset.tokenId}
dev={isDev}
onLoad={handleLoad}
onError={handleError}
/>
)
tokenId = asset.tokenId
}
if (avatar) {
skin = colorToHex(avatar.avatar.skin.color)
hair = colorToHex(avatar.avatar.hair.color)
shape = avatar.avatar.bodyShape.toLowerCase().includes('female')
? 'female'
: 'male'
}

wearablePreview = (
<>
{isLoadingWearablePreview && (
Expand All @@ -108,7 +111,17 @@ const AssetImage = (props: Props) => {
/>
</Center>
)}
{component}
<WearablePreview
contractAddress={asset.contractAddress}
itemId={itemId}
tokenId={tokenId}
skin={skin}
hair={hair}
shape={shape}
dev={isDev}
onLoad={handleLoad}
onError={handleError}
/>
</>
)
}
Expand Down
7 changes: 7 additions & 0 deletions webapp/src/components/AssetImage/AssetImage.types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Dispatch } from 'redux'
import { Avatar } from 'decentraland-ui'
import { Item } from '@dcl/schemas'
import { NFT } from '../../modules/nft/types'

Expand All @@ -10,4 +12,9 @@ export type Props = {
zoom?: number
isSmall?: boolean
showMonospace?: boolean
avatar?: Avatar
}

export type MapStateProps = Pick<Props, 'avatar'>
export type MapDispatchProps = {}
export type MapDispatch = Dispatch
2 changes: 1 addition & 1 deletion webapp/src/components/AssetImage/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import AssetImage from './AssetImage'
import AssetImage from './AssetImage.container'
export { AssetImage }

0 comments on commit ae1d3ab

Please sign in to comment.