|
| 1 | +import React, { useCallback } from 'react'; |
| 2 | +import { |
| 3 | + Avatar, |
| 4 | + Menu, |
| 5 | + MenuTrigger, |
| 6 | + MenuPopover, |
| 7 | + MenuList, |
| 8 | + MenuItem, |
| 9 | + MenuButton, |
| 10 | + makeStyles, |
| 11 | + tokens, |
| 12 | +} from '@fluentui/react-components'; |
| 13 | +import { Person20Regular, SignOut24Regular } from '@fluentui/react-icons'; |
| 14 | +import { useAppSelector } from '../../store/hooks'; |
| 15 | + |
| 16 | +const useStyles = makeStyles({ |
| 17 | + menuButton: { |
| 18 | + minWidth: 'auto', |
| 19 | + paddingLeft: tokens.spacingHorizontalXS, |
| 20 | + paddingRight: tokens.spacingHorizontalS, |
| 21 | + border: 'none', |
| 22 | + background: 'transparent', |
| 23 | + }, |
| 24 | + menuItem: { |
| 25 | + paddingLeft: tokens.spacingHorizontalM, |
| 26 | + paddingRight: tokens.spacingHorizontalM, |
| 27 | + }, |
| 28 | + userInfo: { |
| 29 | + display: 'flex', |
| 30 | + flexDirection: 'column', |
| 31 | + alignItems: 'flex-start', |
| 32 | + gap: tokens.spacingVerticalXXS, |
| 33 | + }, |
| 34 | + userName: { |
| 35 | + fontWeight: tokens.fontWeightSemibold, |
| 36 | + fontSize: tokens.fontSizeBase200, |
| 37 | + }, |
| 38 | + userEmail: { |
| 39 | + fontSize: tokens.fontSizeBase100, |
| 40 | + color: tokens.colorNeutralForeground2, |
| 41 | + }, |
| 42 | + triggerContainer: { |
| 43 | + display: 'flex', |
| 44 | + alignItems: 'center', |
| 45 | + gap: tokens.spacingHorizontalS, |
| 46 | + }, |
| 47 | + displayName: { |
| 48 | + fontSize: tokens.fontSizeBase300, |
| 49 | + fontWeight: tokens.fontWeightSemibold, |
| 50 | + color: tokens.colorNeutralForeground1, |
| 51 | + }, |
| 52 | +}); |
| 53 | + |
| 54 | +const getUserInitials = (name: string | undefined): string => { |
| 55 | + if (!name) return 'U'; |
| 56 | + const cleanName = name.replace(/\s*\([^)]*\)/g, '').trim(); |
| 57 | + if (!cleanName) return 'U'; |
| 58 | + const parts = cleanName.split(/\s+/).filter(Boolean); |
| 59 | + if (parts.length >= 2) { |
| 60 | + return (parts[0][0] + parts[1][0]).toUpperCase(); |
| 61 | + } |
| 62 | + return cleanName.charAt(0).toUpperCase(); |
| 63 | +}; |
| 64 | + |
| 65 | +interface LoginButtonProps { |
| 66 | + showName?: boolean; |
| 67 | +} |
| 68 | + |
| 69 | +const LoginButton: React.FC<LoginButtonProps> = ({ showName = false }) => { |
| 70 | + const styles = useStyles(); |
| 71 | + const userName = useAppSelector(state => state.app.userName); |
| 72 | + const userId = useAppSelector(state => state.app.userId); |
| 73 | + const userEmail = useAppSelector(state => state.app.userEmail); |
| 74 | + const isAuthenticated = Boolean(userId && userId !== 'anonymous'); |
| 75 | + |
| 76 | + const login = useCallback(() => { |
| 77 | + window.location.href = '/.auth/login/aad'; |
| 78 | + }, []); |
| 79 | + |
| 80 | + const logout = useCallback(() => { |
| 81 | + const logoutUrl = '/.auth/logout?post_logout_redirect_uri=' + encodeURIComponent('/.auth/login/aad'); |
| 82 | + window.location.href = logoutUrl; |
| 83 | + }, []); |
| 84 | + |
| 85 | + const displayName = isAuthenticated ? userName || userId || 'User' : 'Guest'; |
| 86 | + |
| 87 | + if (!isAuthenticated) { |
| 88 | + return ( |
| 89 | + <div className={styles.triggerContainer} style={{ cursor: 'default' }}> |
| 90 | + <Avatar |
| 91 | + name="Guest" |
| 92 | + initials="G" |
| 93 | + size={28} |
| 94 | + color="colorful" |
| 95 | + style={{ fontWeight: 'bold' }} |
| 96 | + /> |
| 97 | + {showName && <span className={styles.displayName}>Guest</span>} |
| 98 | + </div> |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + return ( |
| 103 | + <Menu positioning="above-end"> |
| 104 | + <MenuTrigger disableButtonEnhancement> |
| 105 | + <MenuButton |
| 106 | + appearance="subtle" |
| 107 | + className={styles.menuButton} |
| 108 | + title={`Signed in as ${displayName}`} |
| 109 | + aria-label={`User menu for ${displayName}`} |
| 110 | + icon={ |
| 111 | + <Avatar |
| 112 | + name={displayName} |
| 113 | + initials={getUserInitials(displayName)} |
| 114 | + size={28} |
| 115 | + color="colorful" |
| 116 | + style={{ fontWeight: 'bold' }} |
| 117 | + /> |
| 118 | + } |
| 119 | + > |
| 120 | + {showName ? <span className={styles.displayName}>{displayName}</span> : undefined} |
| 121 | + </MenuButton> |
| 122 | + </MenuTrigger> |
| 123 | + |
| 124 | + <MenuPopover> |
| 125 | + <MenuList> |
| 126 | + <MenuItem className={styles.menuItem} icon={<Person20Regular />} disabled style={{ cursor: 'default' }}> |
| 127 | + <div className={styles.userInfo}> |
| 128 | + <div className={styles.userName}>{displayName}</div> |
| 129 | + {userEmail && <div className={styles.userEmail}>{userEmail}</div>} |
| 130 | + </div> |
| 131 | + </MenuItem> |
| 132 | + <MenuItem |
| 133 | + className={styles.menuItem} |
| 134 | + icon={<SignOut24Regular />} |
| 135 | + onClick={logout} |
| 136 | + > |
| 137 | + Sign out |
| 138 | + </MenuItem> |
| 139 | + </MenuList> |
| 140 | + </MenuPopover> |
| 141 | + </Menu> |
| 142 | + ); |
| 143 | +}; |
| 144 | + |
| 145 | +export default LoginButton; |
0 commit comments