// Copyright 2017-2026 @pezkuwi/app-teyrchains authors & contributors // SPDX-License-Identifier: Apache-2.0 import React, { useCallback, useState } from 'react'; import { Button, Input, InputAddress, InputBalance, InputFile, InputNumber, InputWasm, MarkWarning, Modal, TxButton } from '@pezkuwi/react-components'; import { useApi } from '@pezkuwi/react-hooks'; import { BN, BN_TEN, BN_THOUSAND, BN_ZERO, compactAddLength } from '@pezkuwi/util'; import { useTranslation } from '../translate.js'; interface Props { className?: string; onClose: () => void; } interface CodeState { isWasmValid: boolean; wasm: Uint8Array | null; } interface ValidatorProps { address: string; index: number; setAddress: (index: number, value: string) => void; t: (key: string, options?: { replace: Record }) => string; } function Validator ({ address, index, setAddress, t }: ValidatorProps): React.ReactElement { const _setAddress = useCallback( (value: string | null) => value && setAddress(index, value), [index, setAddress] ); return ( ); } function Propose ({ className, onClose }: Props): React.ReactElement { const { t } = useTranslation(); const { api } = useApi(); const [accountId, setAccountId] = useState(null); const [name, setName] = useState(''); const [paraId, setParaId] = useState(); const [balance, setBalance] = useState(() => BN_THOUSAND.mul(BN_TEN.pow(new BN(api.registry.chainDecimals[0])))); const [validators, setValidators] = useState(['']); const [{ isWasmValid, wasm }, setWasm] = useState({ isWasmValid: false, wasm: null }); const [genesisState, setGenesisState] = useState(null); const _setGenesisState = useCallback( (data: Uint8Array) => setGenesisState(compactAddLength(data)), [] ); const _setWasm = useCallback( (wasm: Uint8Array, isWasmValid: boolean) => setWasm({ isWasmValid, wasm }), [] ); const _setAddress = useCallback( (index: number, address: string) => setValidators((v) => v.map((v, i) => i === index ? address : v)), [] ); const _addValidator = useCallback( () => setValidators((v) => [...v, '']), [] ); const _delValidator = useCallback( () => setValidators((v) => [...v.slice(0, v.length - 1)]), [] ); const isNameValid = name.length >= 3; const isValDuplicate = validators.some((a, ai) => validators.some((b, bi) => ai !== bi && a === b)); return ( {validators.map((address, index) => ( ))} {!validators.length && ( )} {isValDuplicate && ( )}