ghost-portal/packages/react-params/src/Param/Cid.tsx
Jaco 2c69be6ebc
Introduce alliance motions (#7925)
* Introduce alliance motions

* Quick-and-dirty motion import

* Adjust motion submission with filtering

* Adjust hooks

* Thresholds

* Align tech comm threshold

* Adjust threshold calcs

* Display announcements

* Adjust incomplete

* url calc

* Display encoded ipfsCid

* useIpfsLink with rule link

* Adjust tags

* Cleanups

* Adjust for prime type
2022-07-29 12:08:00 +03:00

75 lines
1.9 KiB
TypeScript

// 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<Props> {
const { className = '', defaultValue, isDisabled, isError, label, onChange, withLabel } = props;
const [isValid, setIsValid] = useState(false);
const [ipfsCid] = useState<string | null>(() =>
isDisabled && defaultValue && isCodec(defaultValue.value)
? toIpfsCid(defaultValue.value as PalletAllianceCid)
: null
);
const [isStruct] = useState<boolean>(() => 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 (
<Static {...props}>
<Input
className='full'
isDisabled
label='ipfs'
type='text'
value={ipfsCid}
withLabel={withLabel}
/>
</Static>
);
} else if (isStruct) {
return <Struct {...props} />;
}
return (
<Bare className={className}>
<Input
className='full'
isDisabled={isDisabled}
isError={isError || !isValid}
label={label}
onChange={_onChange}
placeholder='IPFS compatible CID'
type='text'
withLabel={withLabel}
/>
</Bare>
);
}
export default React.memo(Cid);