ghost-portal/packages/react-components/src/AccountIndex.tsx
Ewa Kowalska 541213f233
Ek extract type only imports into separate group (#4168)
* Plug new beta of eslint plugin simple import sort

* Apply new configuration to existing imports
2020-12-07 16:04:05 +01:00

52 lines
1.4 KiB
TypeScript

// Copyright 2017-2020 @polkadot/react-components authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { DeriveAccountInfo } from '@polkadot/api-derive/types';
import type { ThemeProps } from '@polkadot/react-components/types';
import type { AccountId, Address } from '@polkadot/types/interfaces';
import React, { useMemo } from 'react';
import styled from 'styled-components';
import { useApi, useCall } from '@polkadot/react-hooks';
interface Props {
children?: React.ReactNode;
className?: string;
defaultValue?: string;
label?: React.ReactNode;
value?: string | AccountId | Address | null | Uint8Array;
}
function extractIndex ({ accountIndex }: Partial<DeriveAccountInfo> = {}): string | null {
return accountIndex
? accountIndex.toString()
: null;
}
function AccountIndex ({ children, className = '', defaultValue, label, value }: Props): React.ReactElement<Props> | null {
const { api } = useApi();
const info = useCall<DeriveAccountInfo>(api.derive.accounts.info, [value]);
const accountIndex = useMemo(
() => extractIndex(info),
[info]
);
if (!api.query.indices) {
return null;
}
return (
<div className={`ui--AccountIndex ${className}`}>
{label || ''}<div className='account-index'>{accountIndex || defaultValue || '-'}</div>{children}
</div>
);
}
export default React.memo(styled(AccountIndex)(({ theme }: ThemeProps) => `
.account-index {
font: ${theme.fontMono};
}
`));