// Copyright 2017-2026 @pezkuwi/react-params authors & contributors // SPDX-License-Identifier: Apache-2.0 import type { PezpalletAllianceCid } from '@pezkuwi/types/lookup'; import type { Props } from '../types.js'; import React, { useCallback, useState } from 'react'; import { Input } from '@pezkuwi/react-components'; import { isCodec } from '@pezkuwi/util'; import { fromIpfsCid, toIpfsCid } from '../util.js'; import Bare from './Bare.js'; import Static from './Static.js'; import Struct from './Struct.js'; 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 PezpalletAllianceCid) : 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);