|
| 1 | +import type { RefObject } from 'react'; |
| 2 | +import { useRef, useState } from 'react'; |
| 3 | + |
| 4 | +import { Button } from '../components/button'; |
| 5 | +import { Icon } from '../components/icon'; |
| 6 | +import { Section } from '../components/section'; |
| 7 | +import type { UserProfileMenuAction } from './user-profile-action-menu'; |
| 8 | +import { UserProfileActionMenu } from './user-profile-action-menu'; |
| 9 | +import { userProfileConnectedAccountsMessages as m } from './user-profile-connected-accounts.messages'; |
| 10 | +import type { |
| 11 | + UserProfileConnectedAccount, |
| 12 | + UserProfileConnectedAccountsSectionViewProps, |
| 13 | +} from './user-profile-connected-accounts-section.view'; |
| 14 | +import { UserProfileProviderIcon } from './user-profile-provider-icon'; |
| 15 | +import { UserProfileRemoveConnectedAccountDialog } from './user-profile-remove-connected-account.dialog'; |
| 16 | + |
| 17 | +interface UserProfileConnectedAccountRowViewProps extends Pick< |
| 18 | + UserProfileConnectedAccountsSectionViewProps, |
| 19 | + 'onConnect' | 'onManage' | 'onRemove' |
| 20 | +> { |
| 21 | + account: UserProfileConnectedAccount; |
| 22 | + removalFocusRef: RefObject<HTMLElement | null>; |
| 23 | +} |
| 24 | + |
| 25 | +export function UserProfileConnectedAccountRowView({ |
| 26 | + account, |
| 27 | + onConnect, |
| 28 | + onManage, |
| 29 | + onRemove, |
| 30 | + removalFocusRef, |
| 31 | +}: UserProfileConnectedAccountRowViewProps) { |
| 32 | + const [open, setOpen] = useState(false); |
| 33 | + const [removeError, setRemoveError] = useState<string>(); |
| 34 | + const removing = useRef(false); |
| 35 | + const confirmedRemoval = useRef(false); |
| 36 | + const connectRef = useRef<HTMLButtonElement>(null); |
| 37 | + const connected = account.connected ?? Boolean(account.identifier); |
| 38 | + const actions: UserProfileMenuAction[] = []; |
| 39 | + |
| 40 | + const confirmRemove = async () => { |
| 41 | + if (!onRemove || account.canRemove === false || removing.current) { |
| 42 | + return; |
| 43 | + } |
| 44 | + removing.current = true; |
| 45 | + confirmedRemoval.current = true; |
| 46 | + setOpen(false); |
| 47 | + try { |
| 48 | + await onRemove(account.id); |
| 49 | + } catch (error) { |
| 50 | + setRemoveError(error instanceof Error ? error.message : m.removeError); |
| 51 | + } finally { |
| 52 | + removing.current = false; |
| 53 | + } |
| 54 | + }; |
| 55 | + |
| 56 | + if (onRemove && account.canRemove !== false) { |
| 57 | + actions.push({ |
| 58 | + label: m.remove, |
| 59 | + color: 'negative', |
| 60 | + onClick: () => { |
| 61 | + if (removing.current) { |
| 62 | + return; |
| 63 | + } |
| 64 | + confirmedRemoval.current = false; |
| 65 | + setRemoveError(undefined); |
| 66 | + setOpen(true); |
| 67 | + }, |
| 68 | + }); |
| 69 | + } else if (!onRemove && onManage) { |
| 70 | + actions.push({ label: m.manage, onClick: () => onManage(account.id) }); |
| 71 | + } |
| 72 | + |
| 73 | + return ( |
| 74 | + <Section.Row> |
| 75 | + <Section.Item> |
| 76 | + {account.iconUrl ? <UserProfileProviderIcon iconUrl={account.iconUrl} /> : null} |
| 77 | + <Section.Content> |
| 78 | + <Section.Label>{account.provider}</Section.Label> |
| 79 | + {account.identifier ? <Section.Description>{account.identifier}</Section.Description> : null} |
| 80 | + {removeError ? <Section.Error role='alert'>{removeError}</Section.Error> : null} |
| 81 | + </Section.Content> |
| 82 | + <Section.Actions> |
| 83 | + {connected ? ( |
| 84 | + <UserProfileActionMenu |
| 85 | + actions={actions} |
| 86 | + label={`${m.manage} ${account.provider}`} |
| 87 | + > |
| 88 | + {onRemove && account.canRemove !== false ? ( |
| 89 | + <UserProfileRemoveConnectedAccountDialog |
| 90 | + provider={account.provider} |
| 91 | + open={open} |
| 92 | + onOpenChange={setOpen} |
| 93 | + onConfirm={() => void confirmRemove()} |
| 94 | + finalFocus={() => |
| 95 | + confirmedRemoval.current ? (connectRef.current ?? removalFocusRef.current) : undefined |
| 96 | + } |
| 97 | + /> |
| 98 | + ) : null} |
| 99 | + </UserProfileActionMenu> |
| 100 | + ) : null} |
| 101 | + {!connected && onConnect ? ( |
| 102 | + <Button |
| 103 | + ref={connectRef} |
| 104 | + color='neutral' |
| 105 | + size='sm' |
| 106 | + variant='outline' |
| 107 | + onClick={() => onConnect(account.id)} |
| 108 | + > |
| 109 | + {m.connect} |
| 110 | + <Icon |
| 111 | + name='arrow-right-top' |
| 112 | + placement='inline-end' |
| 113 | + size='sm' |
| 114 | + /> |
| 115 | + </Button> |
| 116 | + ) : null} |
| 117 | + </Section.Actions> |
| 118 | + </Section.Item> |
| 119 | + </Section.Row> |
| 120 | + ); |
| 121 | +} |
0 commit comments