From 7e8895c2f668651b44f82dd795d880c2ae1bef97 Mon Sep 17 00:00:00 2001 From: Squirrel Date: Tue, 3 May 2022 09:29:20 +0100 Subject: [PATCH] calldata hyperlink (#7507) * try to autofil calldata from url * Kind help from a friend. * fixing warnings * space rearranging. * Jarco awesomeness --- packages/page-extrinsics/src/Decoder.tsx | 10 ++++++++-- packages/page-extrinsics/src/index.tsx | 8 ++++++-- packages/page-extrinsics/src/types.ts | 2 ++ packages/react-components/src/Input.tsx | 7 ++++++- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/packages/page-extrinsics/src/Decoder.tsx b/packages/page-extrinsics/src/Decoder.tsx index ff9e0b59ce..38812606bb 100644 --- a/packages/page-extrinsics/src/Decoder.tsx +++ b/packages/page-extrinsics/src/Decoder.tsx @@ -3,9 +3,11 @@ import type { SubmittableExtrinsic, SubmittableExtrinsicFunction } from '@polkadot/api/types'; import type { Call } from '@polkadot/types/interfaces'; +import type { HexString } from '@polkadot/util/types'; import type { DecodedExtrinsic } from './types'; import React, { useCallback, useState } from 'react'; +import { useParams } from 'react-router-dom'; import styled from 'styled-components'; import { Button, Call as CallDisplay, Input, InputAddress, InputExtrinsic, MarkError, TxButton } from '@polkadot/react-components'; @@ -18,6 +20,7 @@ import { useTranslation } from './translate'; interface Props { className?: string; + defaultValue?: HexString | null; setLast: (value: DecodedExtrinsic | null) => void; } @@ -39,7 +42,9 @@ const DEFAULT_INFO: ExtrinsicInfo = { extrinsicHex: null }; -function Decoder ({ className, setLast }: Props): React.ReactElement { +function Decoder ({ className, defaultValue, setLast }: Props): React.ReactElement { + const { value: encoded } = useParams<{ value: string }>(); + const [initialValue] = useState(() => defaultValue || encoded); const { t } = useTranslation(); const { api } = useApi(); const [{ decoded, extrinsic, extrinsicCall, extrinsicError, extrinsicFn }, setExtrinsicInfo] = useState(DEFAULT_INFO); @@ -70,7 +75,7 @@ function Decoder ({ className, setLast }: Props): React.ReactElement { } setExtrinsicInfo({ ...DEFAULT_INFO, decoded, extrinsic, extrinsicCall, extrinsicFn, extrinsicHex }); - setLast({ call: extrinsicCall, fn: extrinsicFn }); + setLast({ call: extrinsicCall, fn: extrinsicFn, hex: extrinsicHex }); } catch (e) { setExtrinsicInfo({ ...DEFAULT_INFO, extrinsicError: (e as Error).message }); setLast(null); @@ -82,6 +87,7 @@ function Decoder ({ className, setLast }: Props): React.ReactElement { return (
('hex-encoded call')} onChange={_setExtrinsicHex} diff --git a/packages/page-extrinsics/src/index.tsx b/packages/page-extrinsics/src/index.tsx index 2eefc1c441..87d7937501 100644 --- a/packages/page-extrinsics/src/index.tsx +++ b/packages/page-extrinsics/src/index.tsx @@ -24,6 +24,7 @@ function ExtrinsicsApp ({ basePath }: Props): React.ReactElement { text: t('Submission') }, { + hasParams: true, name: 'decode', text: t('Decode') } @@ -36,8 +37,11 @@ function ExtrinsicsApp ({ basePath }: Props): React.ReactElement { items={itemsRef.current} /> - - + + diff --git a/packages/page-extrinsics/src/types.ts b/packages/page-extrinsics/src/types.ts index b7a27234d9..f791d733b4 100644 --- a/packages/page-extrinsics/src/types.ts +++ b/packages/page-extrinsics/src/types.ts @@ -3,8 +3,10 @@ import type { SubmittableExtrinsicFunction } from '@polkadot/api/types'; import type { Call } from '@polkadot/types/interfaces'; +import type { HexString } from '@polkadot/util/types'; export interface DecodedExtrinsic { call: Call; + hex: HexString; fn: SubmittableExtrinsicFunction<'promise'>; } diff --git a/packages/react-components/src/Input.tsx b/packages/react-components/src/Input.tsx index d6b081a904..6f10f6954c 100644 --- a/packages/react-components/src/Input.tsx +++ b/packages/react-components/src/Input.tsx @@ -1,7 +1,7 @@ // Copyright 2017-2022 @polkadot/react-components authors & contributors // SPDX-License-Identifier: Apache-2.0 -import React, { useCallback, useState } from 'react'; +import React, { useCallback, useEffect, useState } from 'react'; import { Input as SUIInput } from 'semantic-ui-react'; import { isFunction, isUndefined } from '@polkadot/util'; @@ -94,6 +94,11 @@ let counter = 0; function Input ({ autoFocus = false, children, className, defaultValue, help, icon, inputClassName, isAction = false, isDisabled = false, isDisabledError = false, isEditable = false, isError = false, isFull = false, isHidden = false, isInPlaceEditor = false, isReadOnly = false, isWarning = false, label, labelExtra, max, maxLength, min, name, onBlur, onChange, onEnter, onEscape, onKeyDown, onKeyUp, onPaste, placeholder, tabIndex, type = 'text', value, withEllipsis, withLabel }: Props): React.ReactElement { const [stateName] = useState(() => `in_${counter++}_at_${Date.now()}`); + const [initialValue] = useState(() => defaultValue); + + useEffect((): void => { + initialValue && onChange && onChange(initialValue); + }, [initialValue, onChange]); const _onBlur = useCallback( () => onBlur && onBlur(),