* refactor: ♻️ refactor isHosted && type clean up Change-Id: I4dfbad8a07607432801de78920ce42bf81e46498 * refactor: ♻️ code clean up Change-Id: I8f487a33d332a2acaff84397a97371b56ace28a1 * feat: 💄 lint Change-Id: I776b1a5e249bdb542f8e6da7cb2277821cf91094 * feat: ✨ ci type Change-Id: I486dde7bf60321238e9a394c40ad8cdb8bfc54c8 * feat: some code sugession Change-Id: I4761d057344b95a98e99068d312a42292977875b
79 lines
2.3 KiB
TypeScript
79 lines
2.3 KiB
TypeScript
import { observer } from "mobx-react";
|
|
import * as React from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
import { MenuButton, useMenuState } from "reakit/Menu";
|
|
import styled from "styled-components";
|
|
import ContextMenu from "~/components/ContextMenu";
|
|
import Template from "~/components/ContextMenu/Template";
|
|
import { createAction } from "~/actions";
|
|
import { navigateToSettings, logout } from "~/actions/definitions/navigation";
|
|
import useCurrentTeam from "~/hooks/useCurrentTeam";
|
|
import usePrevious from "~/hooks/usePrevious";
|
|
import useSessions from "~/hooks/useSessions";
|
|
import useStores from "~/hooks/useStores";
|
|
import separator from "~/menus/separator";
|
|
|
|
const OrganizationMenu: React.FC = ({ children }) => {
|
|
const [sessions] = useSessions();
|
|
const menu = useMenuState({
|
|
unstable_offset: [4, -4],
|
|
placement: "bottom-start",
|
|
modal: true,
|
|
});
|
|
const { ui } = useStores();
|
|
const { theme } = ui;
|
|
const team = useCurrentTeam();
|
|
const previousTheme = usePrevious(theme);
|
|
const { t } = useTranslation();
|
|
|
|
React.useEffect(() => {
|
|
if (theme !== previousTheme) {
|
|
menu.hide();
|
|
}
|
|
}, [menu, theme, previousTheme]);
|
|
|
|
const actions = React.useMemo(() => {
|
|
const otherSessions = sessions.filter(
|
|
(session) => session.teamId !== team.id && session.url !== team.url
|
|
);
|
|
|
|
return [
|
|
navigateToSettings,
|
|
separator(),
|
|
...(otherSessions.length
|
|
? [
|
|
createAction({
|
|
name: t("Switch team"),
|
|
section: "account",
|
|
children: otherSessions.map((session) => ({
|
|
id: session.url,
|
|
name: session.name,
|
|
section: "account",
|
|
icon: <Logo alt={session.name} src={session.logoUrl} />,
|
|
perform: () => (window.location.href = session.url),
|
|
})),
|
|
}),
|
|
]
|
|
: []),
|
|
logout,
|
|
];
|
|
}, [team.id, team.url, sessions, t]);
|
|
|
|
return (
|
|
<>
|
|
<MenuButton {...menu}>{children}</MenuButton>
|
|
<ContextMenu {...menu} aria-label={t("Account")}>
|
|
<Template {...menu} items={undefined} actions={actions} />
|
|
</ContextMenu>
|
|
</>
|
|
);
|
|
};
|
|
|
|
const Logo = styled("img")`
|
|
border-radius: 2px;
|
|
width: 24px;
|
|
height: 24px;
|
|
`;
|
|
|
|
export default observer(OrganizationMenu);
|