element-call/src/UserMenuContainer.jsx

57 lines
1.6 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({ disableLogout }) {
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
disableLogout={disableLogout}
isAuthenticated={isAuthenticated}
isPasswordlessUser={isPasswordlessUser}
avatarUrl={avatarUrl}
onAction={onAction}
displayName={
displayName || (userName ? userName.replace("@", "") : undefined)
}
/>
{modalState.isOpen && (
<ProfileModal
client={client}
isAuthenticated={isAuthenticated}
isPasswordlessUser={isPasswordlessUser}
{...modalProps}
/>
)}
</>
);
}