polkadot-js/apps

View on GitHub
packages/react-params/src/Param/DispatchResult.tsx

Summary

Maintainability
A
1 hr
Test Coverage
// Copyright 2017-2024 @polkadot/react-params authors & contributors
// SPDX-License-Identifier: Apache-2.0

import type { DispatchResult } from '@polkadot/types/interfaces';
import type { Props } from '../types.js';

import React, { useMemo } from 'react';

import { Input } from '@polkadot/react-components';

import DispatchError from './DispatchError.js';
import Static from './Static.js';
import Unknown from './Unknown.js';

function isDispatchResultErr (value?: unknown): value is DispatchResult {
  return !!(value && (value as DispatchResult).isErr);
}

function DispatchResultDisplay (props: Props): React.ReactElement<Props> {
  const { defaultValue, isDisabled, label } = props;
  const dispatchError = useMemo(
    () => defaultValue && isDispatchResultErr(defaultValue.value)
      ? { isValid: true, value: defaultValue.value.asErr }
      : null,
    [defaultValue]
  );

  if (!isDisabled) {
    return <Unknown {...props} />;
  } else if (!dispatchError) {
    return (
      <Static
        {...props}
        defaultValue={{ isValid: true, value: 'Ok' }}
      />
    );
  }

  return (
    <DispatchError
      {...props}
      childrenPre={
        <Input
          className='full'
          isDisabled
          label={label}
          value='Err'
        />
      }
      defaultValue={dispatchError}
      label='DispatchError'
    />
  );
}

export default React.memo(DispatchResultDisplay);