ghost-portal/packages/react-components/src/ChainImg.tsx
Jaco Greeff 2b534fc1fb
Additional wrappers via React.memo (#2377)
* More React.memo

* Last non-memo functional components in staking
2020-03-12 15:45:59 +01:00

40 lines
1.1 KiB
TypeScript

// Copyright 2017-2020 @polkadot/apps 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 React, { useMemo } from 'react';
import styled from 'styled-components';
import { chainLogos, emptyLogo, namedLogos, nodeLogos } from '@polkadot/apps-config/ui/logos';
import { useApi } from '@polkadot/react-hooks';
interface Props {
className?: string;
logo?: keyof typeof namedLogos;
onClick?: () => any;
}
function sanitize (value?: string): string {
return value?.toLowerCase().replace('-', ' ') || '';
}
function ChainImg ({ className, logo, onClick }: Props): React.ReactElement<Props> {
const { systemChain, systemName } = useApi();
const img = useMemo((): any => {
return namedLogos[logo || ''] || chainLogos[sanitize(systemChain)] || nodeLogos[sanitize(systemName)] || emptyLogo;
}, [logo, systemChain, systemName]);
return (
<img
alt='chain logo'
className={className}
onClick={onClick}
src={img}
/>
);
}
export default React.memo(styled(ChainImg)`
border-radius: 50%;
box-sizing: border-box;
`);