import { chakra, Checkbox, Code } from '@chakra-ui/react'; import { useQueryClient } from '@tanstack/react-query'; import React from 'react'; import type { ControllerRenderProps } from 'react-hook-form'; import { useFormContext, Controller } from 'react-hook-form'; import type { FormFields } from '../types'; import type { SmartContractVerificationConfig } from 'types/api/contract'; import { getResourceKey } from 'lib/api/useApiQuery'; import useIsMobile from 'lib/hooks/useIsMobile'; import FancySelect from 'ui/shared/FancySelect/FancySelect'; import IconSvg from 'ui/shared/IconSvg'; import ContractVerificationFormRow from '../ContractVerificationFormRow'; const OPTIONS_LIMIT = 50; interface Props { isVyper?: boolean; } const ContractVerificationFieldCompiler = ({ isVyper }: Props) => { const [ isNightly, setIsNightly ] = React.useState(false); const { formState, control, getValues, resetField } = useFormContext(); const isMobile = useIsMobile(); const queryClient = useQueryClient(); const config = queryClient.getQueryData(getResourceKey('contract_verification_config')); const handleCheckboxChange = React.useCallback(() => { if (isNightly) { const field = getValues('compiler'); field?.value.includes('nightly') && resetField('compiler', { defaultValue: null }); } setIsNightly(prev => !prev); }, [ getValues, isNightly, resetField ]); const options = React.useMemo(() => ( (isVyper ? config?.vyper_compiler_versions : config?.solidity_compiler_versions)?.map((option) => ({ label: option, value: option })) || [] ), [ config?.solidity_compiler_versions, config?.vyper_compiler_versions, isVyper ]); const loadOptions = React.useCallback(async(inputValue: string) => { return options .filter(({ label }) => !inputValue || label.toLowerCase().includes(inputValue.toLowerCase())) .filter(({ label }) => isNightly ? true : !label.includes('nightly')) .slice(0, OPTIONS_LIMIT); }, [ isNightly, options ]); const renderControl = React.useCallback(({ field }: {field: ControllerRenderProps}) => { const error = 'compiler' in formState.errors ? formState.errors.compiler : undefined; return ( } isDisabled={ formState.isSubmitting } error={ error } isRequired isAsync /> ); }, [ formState.errors, formState.isSubmitting, isMobile, loadOptions ]); return ( <> { !isVyper && ( Include nightly builds ) } { isVyper ? null : ( The compiler version is specified in pragma solidity X.X.X . Use the compiler version rather than the nightly build. If using the Solidity compiler, run solc —version to check. ) } ); }; export default React.memo(ContractVerificationFieldCompiler);