mirror of
https://github.com/pezkuwichain/pezkuwi-ui.git
synced 2026-04-22 19:38:03 +00:00
85a8a3a0ee
* Add vue-identicon * Remove generated file * Add links to reactnative & vue identicons * Update package.json * Update Jdenticon.vue * Update Polkadot.vue * Move deps around * Empty on error * Build to build * ... typo * Fix vue-identicon doc generation * Ok, I give up... vuepress and vue packages, no luck * Swap to TypeScript components (aligning with polkadot-js) * Expand template with build * Adjust vue examples * debump gh-pages dep * Expand doc. desc. * Fix vuepress docs generation * Address CC complexity * eslint fix (babel config) * Add Bechball
82 lines
2.0 KiB
TypeScript
82 lines
2.0 KiB
TypeScript
// Copyright 2017-2019 @polkadot/reactnative-identicon authors & contributors
|
|
// This software may be modified and distributed under the terms
|
|
// of the Apache-2.0 license. See the LICENSE file for details.
|
|
|
|
import { Prefix } from '@polkadot/util-crypto/address/types';
|
|
import { Props as ComponentProps } from './types';
|
|
|
|
import React from 'react';
|
|
import { isHex, isU8a, u8aToHex } from '@polkadot/util';
|
|
import { decodeAddress, encodeAddress } from '@polkadot/util-crypto';
|
|
|
|
import { Empty, Polkadot } from './icons';
|
|
|
|
const Fallback = Polkadot;
|
|
|
|
interface Props {
|
|
prefix?: Prefix;
|
|
size?: number;
|
|
theme?: 'polkadot';
|
|
value?: string | Uint8Array | null;
|
|
}
|
|
|
|
interface State {
|
|
address: string;
|
|
publicKey: string;
|
|
}
|
|
|
|
const DEFAULT_SIZE = 64;
|
|
const DEFAULT_THEME = 'polkadot';
|
|
|
|
const Components: Record<string, React.ComponentType<ComponentProps>> = {
|
|
polkadot: Polkadot
|
|
};
|
|
|
|
export default class IdentityIcon extends React.PureComponent<Props, State> {
|
|
public state: State = {
|
|
address: '',
|
|
publicKey: '0x'
|
|
};
|
|
|
|
private static prefix?: Prefix = undefined;
|
|
|
|
public static setDefaultPrefix (prefix: Prefix): void {
|
|
IdentityIcon.prefix = prefix;
|
|
}
|
|
|
|
public static getDerivedStateFromProps ({ prefix = IdentityIcon.prefix, value }: Props, prevState: State): State | null {
|
|
try {
|
|
const address = isU8a(value) || isHex(value)
|
|
? encodeAddress(value, prefix)
|
|
: (value || '');
|
|
const publicKey = u8aToHex(decodeAddress(address, true, prefix));
|
|
|
|
return address === prevState.address
|
|
? null
|
|
: { address, publicKey };
|
|
} catch (error) {
|
|
return {
|
|
address: '',
|
|
publicKey: '0x'
|
|
};
|
|
}
|
|
}
|
|
|
|
public render (): React.ReactNode {
|
|
const { theme = DEFAULT_THEME, size = DEFAULT_SIZE } = this.props;
|
|
const { address, publicKey } = this.state;
|
|
|
|
const Component = !address
|
|
? Empty
|
|
: Components[theme] || Fallback;
|
|
|
|
return (
|
|
<Component
|
|
address={address}
|
|
publicKey={publicKey}
|
|
size={size}
|
|
/>
|
|
);
|
|
}
|
|
}
|