CodeRow.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2017-2020 @polkadot/react-components 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 { CodeStored } from '@polkadot/app-contracts/types';
  5. import React, { useCallback, useEffect, useState } from 'react';
  6. import styled from 'styled-components';
  7. import { createType } from '@polkadot/types';
  8. import { registry } from '@polkadot/react-api';
  9. import { toShortAddress } from '@polkadot/react-components/util';
  10. import Row from '@polkadot/react-components/Row';
  11. import { CopyButton, Icon } from '@polkadot/react-components';
  12. import contracts from '../store';
  13. interface Props {
  14. buttons?: React.ReactNode;
  15. children?: React.ReactNode;
  16. className?: string;
  17. code: CodeStored;
  18. isInline?: boolean;
  19. withTags?: boolean;
  20. }
  21. const DEFAULT_HASH = '0x';
  22. const DEFAULT_NAME = '<unknown>';
  23. function CodeRow ({ buttons, children, className, code: { json }, isInline, withTags }: Props): React.ReactElement<Props> {
  24. const [name, setName] = useState(json.name || DEFAULT_NAME);
  25. const [tags, setTags] = useState(json.tags || []);
  26. const [codeHash, setCodeHash] = useState(json.codeHash || DEFAULT_HASH);
  27. useEffect((): void => {
  28. setName(json.name || DEFAULT_NAME);
  29. setTags(json.tags || []);
  30. setCodeHash(json.codeHash || DEFAULT_HASH);
  31. }, [json]);
  32. const _onSaveName = useCallback(
  33. (): void => {
  34. const trimmedName = name.trim();
  35. if (trimmedName && codeHash) {
  36. contracts.saveCode(createType(registry, 'Hash', codeHash), { name })
  37. .catch((e): void => console.error(e));
  38. }
  39. },
  40. [codeHash, name]
  41. );
  42. const _onSaveTags = useCallback(
  43. (): void => {
  44. codeHash && contracts
  45. .saveCode(createType(registry, 'Hash', codeHash), { tags })
  46. .catch((e): void => console.error(e));
  47. },
  48. [codeHash, tags]
  49. );
  50. return (
  51. <Row
  52. address={
  53. <CopyButton
  54. isAddress
  55. value={codeHash}
  56. >
  57. <span>{toShortAddress(codeHash)}</span>
  58. </CopyButton>
  59. }
  60. buttons={buttons}
  61. className={className}
  62. icon={
  63. <div className='ui--CodeRow-icon'>
  64. <Icon
  65. icon='code'
  66. size='large'
  67. />
  68. </div>
  69. }
  70. isEditableName
  71. isEditableTags
  72. isInline={isInline}
  73. name={name}
  74. onChangeName={setName}
  75. onChangeTags={setTags}
  76. onSaveName={_onSaveName}
  77. onSaveTags={_onSaveTags}
  78. tags={withTags && tags}
  79. >
  80. {children}
  81. </Row>
  82. );
  83. }
  84. export default React.memo(
  85. styled(CodeRow)`
  86. .ui--CodeRow-icon {
  87. margin-right: 1em;
  88. background: #eee;
  89. color: #666;
  90. width: 4rem;
  91. height: 5rem;
  92. padding: 0.5rem;
  93. display: flex;
  94. justify-content: flex-end;
  95. align-items: flex-end;
  96. }
  97. `
  98. );