element-call-Github/src/UserMenuContainer.tsx

55 lines
1.5 KiB
TypeScript
Raw Normal View History

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