diff --git a/catalog/app/containers/NavBar/NavMenu.tsx b/catalog/app/containers/NavBar/NavMenu.tsx index a2f48ff77dc..95df1e53159 100644 --- a/catalog/app/containers/NavBar/NavMenu.tsx +++ b/catalog/app/containers/NavBar/NavMenu.tsx @@ -168,19 +168,22 @@ const useListItemTextStyles = M.makeStyles((t) => ({ }, })) +const LOADING = Symbol('loading') + interface RoleSwitcherProps { user: Me + close: Dialogs.Close } -function RoleSwitcher({ user }: RoleSwitcherProps) { +function RoleSwitcher({ user, close }: RoleSwitcherProps) { const switchRole = GQL.useMutation(SWITCH_ROLE_MUTATION) const classes = useRoleSwitcherStyles() const textClasses = useListItemTextStyles() - const loading = true - const [state, setState] = React.useState(null) + const [state, setState] = React.useState(null) const handleClick = React.useCallback( async (roleName: string) => { - setState(loading) + if (roleName === user.role.name) return close() + setState(LOADING) try { const { switchRole: r } = await switchRole({ roleName }) switch (r.__typename) { @@ -203,12 +206,12 @@ function RoleSwitcher({ user }: RoleSwitcherProps) { } } }, - [loading, switchRole], + [close, switchRole, user.role.name], ) return ( <> Switch role - {state !== true ? ( + {state !== LOADING ? ( <> {state instanceof Error && ( {state.message} @@ -217,11 +220,17 @@ function RoleSwitcher({ user }: RoleSwitcherProps) { {user.roles.map((role) => ( handleClick(role.name)} selected={role.name === user.role.name} > + + + {role.name} ))} @@ -244,7 +253,10 @@ const SWITCH_ROLES_DIALOG_PROPS = { function useRoleSwitcher() { const openDialog = Dialogs.use() return (user: Me) => - openDialog(() => , SWITCH_ROLES_DIALOG_PROPS) + openDialog( + ({ close }) => , + SWITCH_ROLES_DIALOG_PROPS, + ) } function useLinks(): ItemDescriptor[] {