|
| 1 | +import { |
| 2 | + AlertDialog, |
| 3 | + AlertDialogBody, |
| 4 | + AlertDialogContent, |
| 5 | + AlertDialogFooter, |
| 6 | + AlertDialogHeader, |
| 7 | + AlertDialogOverlay, |
| 8 | + Badge, |
| 9 | + Box, |
| 10 | + Button, |
| 11 | + CloseButton, |
| 12 | + Flex, |
| 13 | + HStack, |
| 14 | + Input, |
| 15 | + Stack, |
| 16 | + Text, |
| 17 | +} from '@chakra-ui/react' |
| 18 | +import { useRef, useState } from 'react' |
| 19 | + |
| 20 | +import type { ActionMessage } from '../../hooks/useAffiliateActions' |
| 21 | +import { useAffiliateMembers } from '../../hooks/useAffiliateMembers' |
| 22 | +import { EVM_ADDRESS_REGEX, formatDate } from '../../lib/format' |
| 23 | +import { SettingsCard } from './SettingsCard' |
| 24 | + |
| 25 | +interface AccessManagementCardProps { |
| 26 | + affiliateAddress: string |
| 27 | + authHeaders: Record<string, string> |
| 28 | + setMessage: (message: ActionMessage | null) => void |
| 29 | +} |
| 30 | + |
| 31 | +export const AccessManagementCard = ({ |
| 32 | + affiliateAddress, |
| 33 | + authHeaders, |
| 34 | + setMessage, |
| 35 | +}: AccessManagementCardProps): React.JSX.Element => { |
| 36 | + const [address, setAddress] = useState('') |
| 37 | + const [pendingRemoval, setPendingRemoval] = useState<string | null>(null) |
| 38 | + const cancelRef = useRef<HTMLButtonElement>(null) |
| 39 | + const { query, isAdding, isRemoving, addMember, removeMember } = useAffiliateMembers({ |
| 40 | + affiliateAddress, |
| 41 | + authHeaders, |
| 42 | + setMessage, |
| 43 | + }) |
| 44 | + |
| 45 | + const handleAdd = async (): Promise<void> => { |
| 46 | + const trimmed = address.trim() |
| 47 | + if (!EVM_ADDRESS_REGEX.test(trimmed)) { |
| 48 | + setMessage({ type: 'error', text: 'Invalid EVM address' }) |
| 49 | + return |
| 50 | + } |
| 51 | + try { |
| 52 | + await addMember(trimmed) |
| 53 | + setAddress('') |
| 54 | + } catch { |
| 55 | + // error surfaced via useAffiliateMembers.onError; preserve input for retry |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + const handleRemove = async (): Promise<void> => { |
| 60 | + if (!pendingRemoval) return |
| 61 | + try { |
| 62 | + await removeMember(pendingRemoval) |
| 63 | + } catch { |
| 64 | + // error surfaced via useAffiliateMembers.onError |
| 65 | + } finally { |
| 66 | + setPendingRemoval(null) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return ( |
| 71 | + <SettingsCard |
| 72 | + title='Authorized Wallets' |
| 73 | + description='Wallets that can sign in and manage this affiliate.' |
| 74 | + > |
| 75 | + <Stack spacing={4}> |
| 76 | + {query.isLoading && ( |
| 77 | + <Text fontSize='sm' color='fg.muted'> |
| 78 | + Loading... |
| 79 | + </Text> |
| 80 | + )} |
| 81 | + {query.error && ( |
| 82 | + <Text fontSize='sm' color='danger'> |
| 83 | + {query.error.message} |
| 84 | + </Text> |
| 85 | + )} |
| 86 | + <Stack spacing={0}> |
| 87 | + {(query.data ?? []).map(member => { |
| 88 | + const isSelf = member.walletAddress.toLowerCase() === affiliateAddress.toLowerCase() |
| 89 | + const canRemove = member.role === 'admin' && !isSelf |
| 90 | + return ( |
| 91 | + <Flex |
| 92 | + key={member.walletAddress} |
| 93 | + justify='space-between' |
| 94 | + align='center' |
| 95 | + gap={4} |
| 96 | + py={2} |
| 97 | + borderBottom='1px solid' |
| 98 | + borderColor='border.muted' |
| 99 | + _last={{ borderBottom: 'none' }} |
| 100 | + > |
| 101 | + <HStack spacing={3} minW={0}> |
| 102 | + <Text |
| 103 | + fontSize='sm' |
| 104 | + fontWeight={500} |
| 105 | + color='fg.bright' |
| 106 | + fontFamily='mono' |
| 107 | + wordBreak='break-all' |
| 108 | + > |
| 109 | + {member.walletAddress} |
| 110 | + </Text> |
| 111 | + <Badge |
| 112 | + variant='subtle' |
| 113 | + colorScheme={member.role === 'owner' ? 'blue' : 'gray'} |
| 114 | + textTransform='capitalize' |
| 115 | + flexShrink={0} |
| 116 | + > |
| 117 | + {member.role} |
| 118 | + </Badge> |
| 119 | + </HStack> |
| 120 | + <HStack spacing={3} flexShrink={0}> |
| 121 | + <Text fontSize='xs' color='fg.muted' display={{ base: 'none', md: 'block' }}> |
| 122 | + {member.role === 'owner' ? 'Registered' : 'Added'}{' '} |
| 123 | + {formatDate(member.createdAt)} |
| 124 | + </Text> |
| 125 | + <Box w={6} h={6} flexShrink={0}> |
| 126 | + {canRemove && ( |
| 127 | + <CloseButton |
| 128 | + size='sm' |
| 129 | + aria-label={`Remove ${member.walletAddress}`} |
| 130 | + isDisabled={isRemoving} |
| 131 | + onClick={() => setPendingRemoval(member.walletAddress)} |
| 132 | + /> |
| 133 | + )} |
| 134 | + </Box> |
| 135 | + </HStack> |
| 136 | + </Flex> |
| 137 | + ) |
| 138 | + })} |
| 139 | + </Stack> |
| 140 | + <HStack spacing={3} wrap='wrap'> |
| 141 | + <Input |
| 142 | + value={address} |
| 143 | + onChange={e => setAddress(e.target.value)} |
| 144 | + placeholder='Wallet address' |
| 145 | + spellCheck={false} |
| 146 | + w='46ch' |
| 147 | + maxW='100%' |
| 148 | + /> |
| 149 | + <Button |
| 150 | + onClick={() => void handleAdd()} |
| 151 | + isDisabled={!address.trim()} |
| 152 | + isLoading={isAdding} |
| 153 | + loadingText='Adding...' |
| 154 | + > |
| 155 | + Add admin |
| 156 | + </Button> |
| 157 | + </HStack> |
| 158 | + </Stack> |
| 159 | + <AlertDialog |
| 160 | + isOpen={pendingRemoval !== null} |
| 161 | + leastDestructiveRef={cancelRef} |
| 162 | + onClose={() => setPendingRemoval(null)} |
| 163 | + isCentered |
| 164 | + > |
| 165 | + <AlertDialogOverlay> |
| 166 | + <AlertDialogContent bg='bg.surface' border='1px solid' borderColor='border.subtle'> |
| 167 | + <AlertDialogHeader fontSize='md' fontWeight={600} color='fg.bright'> |
| 168 | + Remove admin |
| 169 | + </AlertDialogHeader> |
| 170 | + <AlertDialogBody fontSize='sm' color='fg.muted'> |
| 171 | + This wallet will no longer be able to sign in and manage this affiliate. |
| 172 | + <Text mt={3} fontFamily='mono' color='fg.bright' wordBreak='break-all'> |
| 173 | + {pendingRemoval} |
| 174 | + </Text> |
| 175 | + </AlertDialogBody> |
| 176 | + <AlertDialogFooter gap={3}> |
| 177 | + <Button |
| 178 | + ref={cancelRef} |
| 179 | + variant='outline' |
| 180 | + colorScheme='gray' |
| 181 | + borderColor='border.input' |
| 182 | + color='fg.default' |
| 183 | + onClick={() => setPendingRemoval(null)} |
| 184 | + > |
| 185 | + Cancel |
| 186 | + </Button> |
| 187 | + <Button |
| 188 | + bg='red.700' |
| 189 | + color='white' |
| 190 | + _hover={{ bg: 'red.800' }} |
| 191 | + _active={{ bg: 'red.900' }} |
| 192 | + isLoading={isRemoving} |
| 193 | + onClick={() => void handleRemove()} |
| 194 | + > |
| 195 | + Remove |
| 196 | + </Button> |
| 197 | + </AlertDialogFooter> |
| 198 | + </AlertDialogContent> |
| 199 | + </AlertDialogOverlay> |
| 200 | + </AlertDialog> |
| 201 | + </SettingsCard> |
| 202 | + ) |
| 203 | +} |
0 commit comments