Skip to content

Instantly share code, notes, and snippets.

@olehmell
Created February 22, 2023 11:30
Show Gist options
  • Save olehmell/d3790419b4c962613387a3eb8f292474 to your computer and use it in GitHub Desktop.
Save olehmell/d3790419b4c962613387a3eb8f292474 to your computer and use it in GitHub Desktop.
Integrate Jdenticon to Antd Avatar component
import { IdentityProps } from '@polkadot/react-identicon/types'
import Avatar from 'antd/lib/avatar/avatar'
import React, {useEffect, useRef} from 'react'
import { DEFAULT_AVATAR_SIZE } from 'src/config/Size.config'
import * as jdenticon from 'jdenticon';
type JdenticonProps = {
value: string;
size: number;
className?: string;
}
const Jdenticon = ({ value, size, className }: JdenticonProps) => {
const icon = useRef<any>(null);
useEffect(() => {
jdenticon.update(icon?.current, value);
}, [value]);
return (
<div>
<svg className={className} data-jdenticon-value={value} height={size} ref={icon} width={size} />
</div>
);
};
export const IdentityIcon = React.memo((allProps: IdentityProps) => {
const { value, size = DEFAULT_AVATAR_SIZE } = allProps
const address = value?.toString() || ''
return (
<Avatar
icon={
<Jdenticon
className='DfIdentityIconContent'
value={address}
size={size - 2}
/>
}
size={size}
className='DfIdentityIcon'
/>
)
})
export default IdentityIcon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment