|
| 1 | +// TODO fix type issues |
| 2 | +// @ts-nocheck |
| 3 | + |
| 4 | +import { Dispatch, SetStateAction, useState } from 'react'; |
| 5 | +import { Box, NumberField, SelectButton, Text } from '@interchain-ui/react'; |
| 6 | + |
| 7 | +import { useValidators } from '@/hooks'; |
| 8 | +import { Permission, PermissionId } from '@/configs'; |
| 9 | +import { SelectValidatorsModal } from './SelectValidatorsModal'; |
| 10 | +import { AccessList } from './GrantModal'; |
| 11 | + |
| 12 | +// ============================================== |
| 13 | + |
| 14 | +type SendCustomizationProps = { |
| 15 | + value: number | undefined; |
| 16 | + onChange: (value: string) => void; |
| 17 | +}; |
| 18 | + |
| 19 | +const SendCustomization = ({ value, onChange }: SendCustomizationProps) => { |
| 20 | + return ( |
| 21 | + <NumberField |
| 22 | + placeholder="Spend Limit (Optional)" |
| 23 | + value={value} |
| 24 | + onInput={(e) => { |
| 25 | + // @ts-ignore |
| 26 | + onChange(e.target.value); |
| 27 | + }} |
| 28 | + formatOptions={{ |
| 29 | + maximumFractionDigits: 6, |
| 30 | + }} |
| 31 | + /> |
| 32 | + ); |
| 33 | +}; |
| 34 | + |
| 35 | +// ============================================== |
| 36 | + |
| 37 | +type DelegateCustomizationProps = { |
| 38 | + value: number | undefined; |
| 39 | + onChange: (value: string) => void; |
| 40 | + chainName: string; |
| 41 | + accessList: AccessList; |
| 42 | + setAccessList: Dispatch<SetStateAction<AccessList>>; |
| 43 | +}; |
| 44 | + |
| 45 | +const DelegateCustomization = ({ |
| 46 | + value, |
| 47 | + onChange, |
| 48 | + chainName, |
| 49 | + accessList, |
| 50 | + setAccessList, |
| 51 | +}: DelegateCustomizationProps) => { |
| 52 | + const [isOpen, setIsOpen] = useState(false); |
| 53 | + |
| 54 | + const { data } = useValidators(chainName); |
| 55 | + |
| 56 | + const validatorNames = data |
| 57 | + ? accessList.addresses.map( |
| 58 | + (address) => data.find((v) => v.address === address)!.name |
| 59 | + ) |
| 60 | + : []; |
| 61 | + |
| 62 | + return ( |
| 63 | + <> |
| 64 | + <NumberField |
| 65 | + placeholder="Max Tokens (Optional)" |
| 66 | + value={value} |
| 67 | + onInput={(e) => { |
| 68 | + // @ts-ignore |
| 69 | + onChange(e.target.value); |
| 70 | + }} |
| 71 | + formatOptions={{ |
| 72 | + maximumFractionDigits: 6, |
| 73 | + }} |
| 74 | + /> |
| 75 | + <SelectButton |
| 76 | + placeholder="Select Validators (Optional)" |
| 77 | + onClick={() => setIsOpen(true)} |
| 78 | + /> |
| 79 | + <Box |
| 80 | + display={validatorNames.length > 0 ? 'block' : 'none'} |
| 81 | + mt="$2" |
| 82 | + px="$2" |
| 83 | + > |
| 84 | + <Text> |
| 85 | + <Text |
| 86 | + as="span" |
| 87 | + fontWeight="$semibold" |
| 88 | + color={ |
| 89 | + accessList.type === 'allowList' ? '$textSuccess' : '$textDanger' |
| 90 | + } |
| 91 | + > |
| 92 | + {accessList.type === 'allowList' ? 'Allow List' : 'Deny List'} |
| 93 | + : |
| 94 | + </Text> |
| 95 | + {validatorNames.join(', ')} |
| 96 | + </Text> |
| 97 | + </Box> |
| 98 | + <SelectValidatorsModal |
| 99 | + chainName={chainName} |
| 100 | + accessList={accessList} |
| 101 | + setAccessList={setAccessList} |
| 102 | + isOpen={isOpen} |
| 103 | + onClose={() => setIsOpen(false)} |
| 104 | + /> |
| 105 | + </> |
| 106 | + ); |
| 107 | +}; |
| 108 | + |
| 109 | +// ============================================== |
| 110 | + |
| 111 | +type CustomizationFieldProps = |
| 112 | + | ({ |
| 113 | + permissionType: typeof Permission['Send']; |
| 114 | + } & SendCustomizationProps) |
| 115 | + | ({ |
| 116 | + permissionType: typeof Permission['Delegate']; |
| 117 | + } & DelegateCustomizationProps); |
| 118 | + |
| 119 | +export const CustomizationField = ({ |
| 120 | + permissionType, |
| 121 | + ...rest |
| 122 | +}: CustomizationFieldProps): JSX.Element | null => { |
| 123 | + const fields: Partial<Record<PermissionId, JSX.Element | null>> = { |
| 124 | + send: |
| 125 | + permissionType === 'send' ? ( |
| 126 | + <SendCustomization {...(rest as SendCustomizationProps)} /> |
| 127 | + ) : null, |
| 128 | + delegate: |
| 129 | + permissionType === 'delegate' ? ( |
| 130 | + <DelegateCustomization {...(rest as DelegateCustomizationProps)} /> |
| 131 | + ) : null, |
| 132 | + }; |
| 133 | + |
| 134 | + return fields[permissionType] ?? null; |
| 135 | +}; |
0 commit comments