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