element-call/src/UserMenuContainer.tsx

55 lines
1.5 KiB
TypeScript
Raw Normal View History

2021-12-23 14:40:23 -08:00
import React, { useCallback } from "react";
import { useHistory, useLocation } from "react-router-dom";
2022-01-05 17:19:03 -08:00
import { useClient } from "./ClientContext";
2022-01-05 17:27:01 -08:00
import { useProfile } from "./profile/useProfile";
2021-12-23 14:40:23 -08:00
import { useModalTriggerState } from "./Modal";
2022-01-05 17:27:01 -08:00
import { ProfileModal } from "./profile/ProfileModal";
2021-12-23 14:40:23 -08:00
import { UserMenu } from "./UserMenu";
interface Props {
preventNavigation?: boolean;
}
2022-08-02 00:46:16 +02:00
export function UserMenuContainer({ preventNavigation = false }: Props) {
2021-12-23 14:40:23 -08:00
const location = useLocation();
const history = useHistory();
2022-01-04 16:00:13 -08:00
const { isAuthenticated, isPasswordlessUser, logout, userName, client } =
useClient();
2021-12-23 14:40:23 -08:00
const { displayName, avatarUrl } = useProfile(client);
const { modalState, modalProps } = useModalTriggerState();
const onAction = useCallback(
(value: string) => {
2021-12-23 14:40:23 -08:00
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 14:40:23 -08:00
isAuthenticated={isAuthenticated}
isPasswordlessUser={isPasswordlessUser}
avatarUrl={avatarUrl}
onAction={onAction}
displayName={
displayName || (userName ? userName.replace("@", "") : undefined)
}
/>
2022-02-18 16:02:27 -08:00
{modalState.isOpen && <ProfileModal client={client} {...modalProps} />}
2021-12-23 14:40:23 -08:00
</>
);
}