ABI.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2017-2020 @polkadot/app-contracts 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 React from 'react';
  5. import styled from 'styled-components';
  6. import { Abi } from '@polkadot/api-contract';
  7. import { InputFile, Labelled } from '@polkadot/react-components';
  8. import Messages from './Messages';
  9. import { useTranslation } from '../translate';
  10. interface Props {
  11. className?: string;
  12. contractAbi?: Abi | null;
  13. errorText?: string | null;
  14. isContract?: boolean;
  15. isError?: boolean;
  16. isDisabled?: boolean;
  17. isRequired?: boolean;
  18. isValid?: boolean;
  19. isSupplied?: boolean;
  20. label?: React.ReactNode;
  21. onChange: (u8a: Uint8Array) => void;
  22. onRemove?: () => void;
  23. onRemoved?: () => void;
  24. onSelect?: () => void;
  25. onSelectConstructor?: (constructorIndex?: number) => void;
  26. withLabel?: boolean;
  27. }
  28. function renderMessages ({ contractAbi, isDisabled, onRemove, onSelectConstructor, withLabel }: Props): React.ReactNode {
  29. return (
  30. <Messages
  31. contractAbi={contractAbi}
  32. isLabelled={withLabel}
  33. isRemovable={!isDisabled}
  34. onRemove={onRemove}
  35. onSelectConstructor={onSelectConstructor}
  36. withConstructors
  37. />
  38. );
  39. }
  40. function ABI (props: Props): React.ReactElement<Props> {
  41. const { className, contractAbi, errorText, isContract = false, isDisabled, isError, isSupplied, isValid, onChange, withLabel } = props;
  42. const { t } = useTranslation();
  43. const help = isContract
  44. ? t('The ABI for the WASM code. Since we will be making a call into the code, the ABI is required and stored for future operations such as sending messages.')
  45. : t('The ABI for the WASM code. In this step it is optional, but setting it here simplifies the setup of contract instances.');
  46. const label = isContract
  47. ? 'contract ABI'
  48. : 'contract ABI (optional)';
  49. return (
  50. <div className={className}>
  51. {
  52. (contractAbi && isValid)
  53. ? (
  54. withLabel
  55. ? (
  56. <Labelled
  57. help={help}
  58. label={label}
  59. >
  60. {renderMessages(props)}
  61. </Labelled>
  62. )
  63. : renderMessages(props)
  64. )
  65. : (
  66. <InputFile
  67. help={help}
  68. isDisabled={isDisabled}
  69. isError={isError}
  70. label={label}
  71. onChange={onChange}
  72. placeholder={
  73. isSupplied && !isValid
  74. ? (
  75. <>
  76. {t<string>('invalid ABI file selected')}
  77. {!!errorText && (
  78. <>
  79. {' — '}
  80. {t<string>(errorText)}
  81. </>
  82. )}
  83. </>
  84. )
  85. : t<string>('click to select or drag and drop a JSON ABI file')
  86. }
  87. />
  88. )
  89. }
  90. </div>
  91. );
  92. }
  93. export default React.memo(
  94. styled(ABI)`
  95. min-height: 4rem;
  96. `
  97. );