Add profile link to switcher menu (#8867)
* feat(leftnav): add profile link to switcher menu * fix: close menu on navigate
This commit is contained in:
@@ -236,6 +236,7 @@ function SwitchMenuItems({
|
|||||||
setShowLoggedOut(true)
|
setShowLoggedOut(true)
|
||||||
closeEverything()
|
closeEverything()
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Menu.Outer>
|
<Menu.Outer>
|
||||||
{accounts && accounts.length > 0 && (
|
{accounts && accounts.length > 0 && (
|
||||||
@@ -255,6 +256,7 @@ function SwitchMenuItems({
|
|||||||
<Menu.Divider />
|
<Menu.Divider />
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
|
<SwitcherMenuProfileLink />
|
||||||
<Menu.Item
|
<Menu.Item
|
||||||
label={_(msg`Add another account`)}
|
label={_(msg`Add another account`)}
|
||||||
onPress={onAddAnotherAccount}>
|
onPress={onAddAnotherAccount}>
|
||||||
@@ -273,6 +275,56 @@ function SwitchMenuItems({
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function SwitcherMenuProfileLink() {
|
||||||
|
const {_} = useLingui()
|
||||||
|
const {currentAccount} = useSession()
|
||||||
|
const navigation = useNavigation()
|
||||||
|
const context = Menu.useMenuContext()
|
||||||
|
const profileLink = currentAccount ? makeProfileLink(currentAccount) : '/'
|
||||||
|
const [pathName] = useMemo(() => router.matchPath(profileLink), [profileLink])
|
||||||
|
const currentRouteInfo = useNavigationState(state => {
|
||||||
|
if (!state) {
|
||||||
|
return {name: 'Home'}
|
||||||
|
}
|
||||||
|
return getCurrentRoute(state)
|
||||||
|
})
|
||||||
|
let isCurrent =
|
||||||
|
currentRouteInfo.name === 'Profile'
|
||||||
|
? isTab(currentRouteInfo.name, pathName) &&
|
||||||
|
(currentRouteInfo.params as CommonNavigatorParams['Profile']).name ===
|
||||||
|
currentAccount?.handle
|
||||||
|
: isTab(currentRouteInfo.name, pathName)
|
||||||
|
const onProfilePress = useCallback(
|
||||||
|
(e: React.MouseEvent<HTMLAnchorElement, MouseEvent>) => {
|
||||||
|
if (e.ctrlKey || e.metaKey || e.altKey) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
e.preventDefault()
|
||||||
|
context.control.close()
|
||||||
|
if (isCurrent) {
|
||||||
|
emitSoftReset()
|
||||||
|
} else {
|
||||||
|
const [screen, params] = router.matchPath(profileLink)
|
||||||
|
// @ts-expect-error TODO: type matchPath well enough that it can be plugged into navigation.navigate directly
|
||||||
|
navigation.navigate(screen, params, {pop: true})
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[navigation, profileLink, isCurrent, context],
|
||||||
|
)
|
||||||
|
return (
|
||||||
|
<Menu.Item
|
||||||
|
label={_(msg`Go to profile`)}
|
||||||
|
// @ts-expect-error The function signature differs on web -inb
|
||||||
|
onPress={onProfilePress}
|
||||||
|
href={profileLink}>
|
||||||
|
<Menu.ItemIcon icon={UserCircle} />
|
||||||
|
<Menu.ItemText>
|
||||||
|
<Trans>Go to profile</Trans>
|
||||||
|
</Menu.ItemText>
|
||||||
|
</Menu.Item>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
function SwitchMenuItem({
|
function SwitchMenuItem({
|
||||||
account,
|
account,
|
||||||
profile,
|
profile,
|
||||||
|
|||||||
Reference in New Issue
Block a user