Files
pwap/pezkuwi-sdk-ui/packages/react-components/src/InputStorage/index.tsx
T
pezkuwichain 1295c36241 Rebrand: polkadot → pezkuwi build fixes
- Fixed TypeScript type assertion issues
- Updated imports from api-augment/substrate to api-augment/bizinikiwi
- Fixed imgConvert.mjs header and imports
- Added @ts-expect-error for runtime-converted types
- Fixed all @polkadot copyright headers to @pezkuwi
2026-01-07 02:32:54 +03:00

80 lines
2.3 KiB
TypeScript

// Copyright 2017-2025 @pezkuwi/react-components authors & contributors
// SPDX-License-Identifier: Apache-2.0
// TODO: We have a lot shared between this and InputExtrinsic
import type { QueryableStorageEntry } from '@pezkuwi/api/types';
import type { DropdownOptions } from '../util/types.js';
import React, { useCallback, useState } from 'react';
import { useApi } from '@pezkuwi/react-hooks';
import LinkedWrapper from '../InputExtrinsic/LinkedWrapper.js';
import { keyOptions, sectionOptions } from './options/index.js';
import SelectKey from './SelectKey.js';
import SelectSection from './SelectSection.js';
interface Props {
className?: string;
defaultValue: QueryableStorageEntry<'promise'>;
isError?: boolean;
label: React.ReactNode;
onChange?: (value: QueryableStorageEntry<'promise'>) => void;
withLabel?: boolean;
}
function InputStorage ({ className = '', defaultValue, label, onChange, withLabel }: Props): React.ReactElement<Props> {
const { api } = useApi();
const [optionsMethod, setOptionsMethod] = useState<DropdownOptions>(() => keyOptions(api, defaultValue.creator.section));
const [optionsSection] = useState<DropdownOptions>(() => sectionOptions(api));
const [value, setValue] = useState<QueryableStorageEntry<'promise'>>(() => defaultValue);
const _onKeyChange = useCallback(
(newValue: QueryableStorageEntry<'promise'>): void => {
if (value !== newValue) {
// set via callback
setValue(() => newValue);
onChange && onChange(newValue);
}
},
[onChange, value]
);
const _onSectionChange = useCallback(
(section: string): void => {
if (section !== value.creator.section) {
const optionsMethod = keyOptions(api, section);
setOptionsMethod(optionsMethod);
_onKeyChange(api.query[section][optionsMethod[0].value]);
}
},
[_onKeyChange, api, value]
);
return (
<LinkedWrapper
className={className}
label={label}
withLabel={withLabel}
>
<SelectSection
className='small'
onChange={_onSectionChange}
options={optionsSection}
value={value}
/>
<SelectKey
className='large'
key={value.creator.section}
onChange={_onKeyChange}
options={optionsMethod}
value={value}
/>
</LinkedWrapper>
);
}
export default React.memo(InputStorage);