Assets.tsx 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2019 @polkadot/app-generic-asset 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 { I18nProps } from '@polkadot/react-components/types';
  5. import BN from 'bn.js';
  6. import React, { useState } from 'react';
  7. import { Button, CardGrid } from '@polkadot/react-components';
  8. import { withMulti, withObservable } from '@polkadot/react-api/hoc';
  9. import assetRegistry, { AssetsSubjectInfo } from './assetsRegistry';
  10. import AssetCard from './AssetCard';
  11. import CreateModal from './modals/Create';
  12. import translate from './translate';
  13. type Props = I18nProps & {
  14. assets?: AssetsSubjectInfo;
  15. };
  16. function Assets ({ assets, t }: Props): React.ReactElement<Props> {
  17. const [isCreateOpen, setIsCreateOpen] = useState(false);
  18. const emptyScreen = !isCreateOpen && (!assets || Object.keys(assets).length === 0);
  19. const _onForget = (id: string): void => assetRegistry.remove(id);
  20. const _onRegister = (id: BN, name: string): void => assetRegistry.add(id.toString(), name);
  21. const _onSaveName = (id: string, name: string): void => assetRegistry.add(id, name);
  22. const _toggleCreate = (): void => setIsCreateOpen(!isCreateOpen);
  23. return (
  24. <CardGrid
  25. buttons={
  26. <Button.Group>
  27. <Button
  28. icon='registered'
  29. label={t<string>('Register Asset')}
  30. onClick={_toggleCreate}
  31. />
  32. </Button.Group>
  33. }
  34. emptyText={t<string>('No assets found.')}
  35. isEmpty={emptyScreen}
  36. >
  37. {isCreateOpen && (
  38. <CreateModal
  39. onClose={_toggleCreate}
  40. onRegister={_onRegister}
  41. />
  42. )}
  43. {assets && Object.entries(assets).map(([id, name]): React.ReactNode => (
  44. <AssetCard
  45. assetId={id}
  46. key={id}
  47. name={name}
  48. onForget={_onForget}
  49. onSaveName={_onSaveName}
  50. />
  51. ))}
  52. </CardGrid>
  53. );
  54. }
  55. export default withMulti(
  56. Assets,
  57. translate,
  58. withObservable(assetRegistry.subject, { propName: 'assets' })
  59. );