2021-12-24 06:40:23 +08:00
|
|
|
import React, { useCallback } from "react";
|
|
|
|
import { useHistory, useLocation } from "react-router-dom";
|
2022-07-30 16:02:07 +08:00
|
|
|
|
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";
|
|
|
|
|
2022-07-30 16:02:07 +08:00
|
|
|
interface Props {
|
2022-08-02 01:04:43 +08:00
|
|
|
preventNavigation?: boolean;
|
2022-07-30 16:02:07 +08:00
|
|
|
}
|
|
|
|
|
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(
|
2022-07-30 16:02:07 +08:00
|
|
|
(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
|
2022-02-03 07:09:16 +08:00
|
|
|
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
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|