// Copyright 2017-2022 @polkadot/react-params authors & contributors // SPDX-License-Identifier: Apache-2.0 import type { PalletAllianceCid } from '@polkadot/types/lookup'; import type { Props } from '../types'; import React, { useCallback, useState } from 'react'; import { Input } from '@polkadot/react-components'; import { isCodec } from '@polkadot/util'; import { fromIpfsCid, toIpfsCid } from '../util'; import Bare from './Bare'; import Static from './Static'; import Struct from './Struct'; function Cid (props: Props): React.ReactElement { const { className = '', defaultValue, isDisabled, isError, label, onChange, withLabel } = props; const [isValid, setIsValid] = useState(false); const [ipfsCid] = useState(() => isDisabled && defaultValue && isCodec(defaultValue.value) ? toIpfsCid(defaultValue.value as PalletAllianceCid) : null ); const [isStruct] = useState(() => isDisabled || !defaultValue || isCodec(defaultValue.value)); const _onChange = useCallback( (_value: string): void => { const value = fromIpfsCid(_value); const isValid = !!value; onChange && onChange({ isValid, value }); setIsValid(isValid); }, [onChange] ); if (ipfsCid) { return ( ); } else if (isStruct) { return ; } return ( ); } export default React.memo(Cid);