element-call/src/UserMenuContainer.jsx

50 lines
1.5 KiB
React
Raw Normal View History

2021-12-23 22:40:23 +00:00
import React, { useCallback } from "react";
import { useHistory, useLocation } from "react-router-dom";
2022-01-06 01:19:03 +00:00
import { useClient } from "./ClientContext";
2022-01-06 01:27:01 +00:00
import { useProfile } from "./profile/useProfile";
2021-12-23 22:40:23 +00:00
import { useModalTriggerState } from "./Modal";
2022-01-06 01:27:01 +00:00
import { ProfileModal } from "./profile/ProfileModal";
2021-12-23 22:40:23 +00:00
import { UserMenu } from "./UserMenu";
export function UserMenuContainer({ preventNavigation }) {
2021-12-23 22:40:23 +00:00
const location = useLocation();
const history = useHistory();
2022-01-05 00:00:13 +00:00
const { isAuthenticated, isPasswordlessUser, logout, userName, client } =
useClient();
2021-12-23 22:40:23 +00:00
const { displayName, avatarUrl } = useProfile(client);
const { modalState, modalProps } = useModalTriggerState();
const onAction = useCallback(
(value) => {
switch (value) {
case "user":
modalState.open();
break;
case "logout":
logout();
break;
case "login":
history.push("/login", { state: { from: location } });
break;
}
},
[history, location, logout, modalState]
);
return (
<>
<UserMenu
preventNavigation={preventNavigation}
2021-12-23 22:40:23 +00:00
isAuthenticated={isAuthenticated}
isPasswordlessUser={isPasswordlessUser}
avatarUrl={avatarUrl}
onAction={onAction}
displayName={
displayName || (userName ? userName.replace("@", "") : undefined)
}
/>
2022-02-19 00:02:27 +00:00
{modalState.isOpen && <ProfileModal client={client} {...modalProps} />}
2021-12-23 22:40:23 +00:00
</>
);
}