Address.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright 2017-2019 @polkadot/app-staking authors & contributors
  2. // This software may be modified and distributed under the terms
  3. // of the Apache-2.0 license. See the LICENSE file for details.
  4. import { KeyringAddress } from '@polkadot/ui-keyring/types';
  5. import { ActionStatus } from '@polkadot/react-components/Status/types';
  6. import { I18nProps } from '@polkadot/react-components/types';
  7. import React, { useEffect, useState } from 'react';
  8. import styled from 'styled-components';
  9. import { AddressCard, AddressInfo, Button, ChainLock, Forget } from '@polkadot/react-components';
  10. import keyring from '@polkadot/ui-keyring';
  11. import Transfer from '@polkadot/app-accounts/modals/Transfer';
  12. import translate from './translate';
  13. interface Props extends I18nProps {
  14. address: string;
  15. className?: string;
  16. }
  17. const WITH_BALANCE = { available: true, bonded: true, free: true, locked: true, reserved: true, total: true };
  18. const WITH_EXTENDED = { nonce: true };
  19. const isEditable = true;
  20. function Address ({ address, className, t }: Props): React.ReactElement<Props> {
  21. const [current, setCurrent] = useState<KeyringAddress | null>(null);
  22. const [genesisHash, setGenesisHash] = useState<string | null>(null);
  23. const [isForgetOpen, setIsForgetOpen] = useState(false);
  24. const [isTransferOpen, setIsTransferOpen] = useState(false);
  25. useEffect((): void => {
  26. const current = keyring.getAddress(address);
  27. setCurrent(current || null);
  28. setGenesisHash((current && current.meta.genesisHash) || null);
  29. }, []);
  30. const _toggleForget = (): void => setIsForgetOpen(!isForgetOpen);
  31. const _toggleTransfer = (): void => setIsTransferOpen(!isTransferOpen);
  32. const _onForget = (): void => {
  33. if (address) {
  34. const status: Partial<ActionStatus> = {
  35. account: address,
  36. action: 'forget'
  37. };
  38. try {
  39. keyring.forgetAddress(address);
  40. status.status = 'success';
  41. status.message = t('address forgotten');
  42. } catch (error) {
  43. status.status = 'error';
  44. status.message = error.message;
  45. }
  46. }
  47. };
  48. const _onGenesisChange = (genesisHash: string | null): void => {
  49. setGenesisHash(genesisHash);
  50. const account = keyring.getAddress(address);
  51. account && keyring.saveAddress(address, { ...account.meta, genesisHash });
  52. setGenesisHash(genesisHash);
  53. };
  54. return (
  55. <AddressCard
  56. buttons={
  57. <div className='addresses--Address-buttons buttons'>
  58. <div className='actions'>
  59. {isEditable && (
  60. <Button
  61. isNegative
  62. onClick={_toggleForget}
  63. icon='trash'
  64. key='forget'
  65. size='small'
  66. tooltip={t('Forget this address')}
  67. />
  68. )}
  69. <Button
  70. icon='paper plane'
  71. isPrimary
  72. key='deposit'
  73. label={t('deposit')}
  74. onClick={_toggleTransfer}
  75. size='small'
  76. tooltip={t('Send funds to this address')}
  77. />
  78. </div>
  79. {isEditable && (
  80. <div className='others'>
  81. <ChainLock
  82. genesisHash={genesisHash}
  83. onChange={_onGenesisChange}
  84. />
  85. </div>
  86. )}
  87. </div>
  88. }
  89. className={className}
  90. isEditable={isEditable}
  91. type='address'
  92. value={address}
  93. withExplorer
  94. withIndexOrAddress={false}
  95. withTags
  96. >
  97. {address && current && (
  98. <>
  99. {isForgetOpen && (
  100. <Forget
  101. address={current.address}
  102. onForget={_onForget}
  103. key='modal-forget-account'
  104. mode='address'
  105. onClose={_toggleForget}
  106. />
  107. )}
  108. {isTransferOpen && (
  109. <Transfer
  110. key='modal-transfer'
  111. onClose={_toggleTransfer}
  112. recipientId={address}
  113. />
  114. )}
  115. </>
  116. )}
  117. <AddressInfo
  118. address={address}
  119. withBalance={WITH_BALANCE}
  120. withExtended={WITH_EXTENDED}
  121. />
  122. </AddressCard>
  123. );
  124. }
  125. export default translate(
  126. styled(Address)`
  127. .addresses--Address-buttons {
  128. text-align: right;
  129. .others {
  130. margin-right: 0.125rem;
  131. margin-top: 0.25rem;
  132. }
  133. }
  134. `
  135. );