From dd9c63c6f43ae7ae5612d79b66bbcf5a5b4b2131 Mon Sep 17 00:00:00 2001 From: teodorus-nathaniel Date: Fri, 5 Apr 2024 17:00:56 +0700 Subject: [PATCH] Change identicon to use polkadot identicon --- package.json | 1 + src/components/utils/IdentityIcon.tsx | 65 ++++++++++++++++++++++----- yarn.lock | 13 ++++++ 3 files changed, 67 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 49220e27e..9d94f481c 100644 --- a/package.json +++ b/package.json @@ -88,6 +88,7 @@ "@opd/g2plot-react": "^2.6.0", "@polkadot/extension-dapp": "^0.44.6", "@polkadot/ui-keyring": "^2.9.8", + "@polkadot/ui-shared": "3.1.4", "@reduxjs/toolkit": "^1.3.4", "@subsocial/api": "0.8.10", "@subsocial/data-hub-sdk": "dappforce/subsocial-data-hub-sdk#staging", diff --git a/src/components/utils/IdentityIcon.tsx b/src/components/utils/IdentityIcon.tsx index 4ac7ff1fb..d3bbf5353 100644 --- a/src/components/utils/IdentityIcon.tsx +++ b/src/components/utils/IdentityIcon.tsx @@ -1,38 +1,79 @@ +import { Circle } from '@polkadot/ui-shared/icons/types' import Avatar from 'antd/lib/avatar/avatar' -import React, { useEffect, useRef } from 'react' +import React, { useMemo } from 'react' import { DEFAULT_AVATAR_SIZE } from 'src/config/Size.config' +import { polkadotIcon } from '@polkadot/ui-shared' import clsx from 'clsx' -import * as jdenticon from 'jdenticon' -type JdenticonProps = { +type PolkadotIdenticonProps = { value: string size: number className?: string style?: React.CSSProperties } -const Jdenticon = ({ value, size, ...props }: JdenticonProps) => { - const icon = useRef(null) - useEffect(() => { - jdenticon.update(icon?.current, value) - }, [value]) - +const PolkadotIdenticon = ({ value, size, ...props }: PolkadotIdenticonProps) => { return (
- +
) } -export const IdentityIcon = React.memo((allProps: JdenticonProps) => { +function renderCircle({ cx, cy, fill, r }: Circle, key: number): React.ReactNode { + return +} + +interface Props { + address: string + className?: string + isAlternative?: boolean + size: number + style?: React.CSSProperties +} +/** + * Code taken from https://github.com/polkadot-js/ui/blob/master/packages/react-identicon/src/icons/Polkadot.tsx + * @polkadot/ui-shared newest version needs node >=v18 so we can't use it + * instead, we installed @polkadot/ui-shared v3.1 which is compatible with node v16 + */ +function PolkadotIcon({ + address, + className = '', + isAlternative = false, + size, + style = {}, +}: Props): React.ReactElement { + const circles = useMemo(() => polkadotIcon(address, { isAlternative }), [address, isAlternative]) + + return ( + + {circles.map(renderCircle)} + + ) +} + +export const IdentityIcon = React.memo((allProps: PolkadotIdenticonProps) => { const { value, size = DEFAULT_AVATAR_SIZE, ...props } = allProps const address = value?.toString() || '' return (