forked from lobehub/dumi-theme-lobehub
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add UserActionButton for header
- Loading branch information
Showing
9 changed files
with
120 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
{ | ||
"header.actions.user": "Login / Sign up", | ||
"header.nav.home": "Home" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
{ | ||
"header.actions.user": "登录/注册", | ||
"header.nav.home": "首页" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { Icon } from '@lobehub/ui'; | ||
import { Avatar, Button, Dropdown } from 'antd'; | ||
import { createStyles } from 'antd-style'; | ||
import { useIntl } from 'dumi'; | ||
import { LogOut, User2 } from 'lucide-react'; | ||
import React, { memo, useCallback, useEffect, useState } from 'react'; | ||
|
||
import { headerSel, useSiteStore } from '@/store'; | ||
|
||
export const useStyles = createStyles(({ css }) => { | ||
return { | ||
avatar: css` | ||
cursor: pointer; | ||
`, | ||
menu: css` | ||
width: 180px; | ||
`, | ||
}; | ||
}); | ||
|
||
const LOGIN_USER_KEY = '__LOGIN_USER'; | ||
|
||
export const HeaderUserActionButton: React.FC = memo(() => { | ||
const { styles } = useStyles(); | ||
const headerConfig = useSiteStore(headerSel); | ||
const loginUser = useSiteStore(s => s.loginUser); | ||
const intl = useIntl(); | ||
const [userInfo, setUserInfo] = useState(loginUser); | ||
useEffect(() => { | ||
const loginUserStr = window.localStorage.getItem(LOGIN_USER_KEY); | ||
if (loginUserStr) { | ||
try { | ||
const loginUser = JSON.parse(loginUserStr); | ||
setUserInfo(loginUser); | ||
useSiteStore.setState({ loginUser }); | ||
} catch (error) { | ||
console.warn(`parse login user info from ${LOGIN_USER_KEY} failed`, error); | ||
} | ||
} | ||
}, []); | ||
|
||
const clearLoginUser = useCallback(() => { | ||
window.localStorage.removeItem(LOGIN_USER_KEY); | ||
}, []); | ||
|
||
if (!headerConfig?.userActionButton?.button) { | ||
return; | ||
} | ||
if (userInfo?.user) { | ||
return ( | ||
<Dropdown | ||
menu={{ | ||
className: styles.menu, | ||
items: headerConfig.userActionButton.menuItems || [ | ||
{ | ||
icon: <Icon icon={User2} />, | ||
label: <a href="https://console.botnow.cn/oidc/management/account">账户中心</a>, | ||
key: 'account', | ||
}, | ||
{ type: 'divider' }, | ||
{ | ||
icon: <Icon icon={LogOut} />, | ||
label: ( | ||
<a href="https://console.botnow.cn/oidc/logout" onClick={clearLoginUser}> | ||
退出登录 | ||
</a> | ||
), | ||
key: 'logout', | ||
}, | ||
], | ||
}} | ||
> | ||
<Avatar className={styles.avatar} size="small" src={userInfo.avatar}> | ||
{userInfo.user.charAt(0).toUpperCase()} | ||
</Avatar> | ||
</Dropdown> | ||
); | ||
} | ||
return ( | ||
<Button {...headerConfig.userActionButton.button}> | ||
{intl.formatMessage({ id: 'header.actions.user' })} | ||
</Button> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters