2023-01-16 09:45:12 +00:00
|
|
|
import { AppExtensionMountEnum } from "@dashboard/graphql";
|
2023-02-28 13:20:17 +00:00
|
|
|
import { Extension } from "@dashboard/new-apps/hooks/useExtensions";
|
2023-03-02 09:22:01 +00:00
|
|
|
import { AppUrls } from "@dashboard/new-apps/urls";
|
2023-01-16 09:45:12 +00:00
|
|
|
import { orderDraftListUrl, orderListUrl } from "@dashboard/orders/urls";
|
2020-11-18 15:11:15 +00:00
|
|
|
import { matchPath } from "react-router";
|
|
|
|
|
2023-02-20 15:21:28 +00:00
|
|
|
import { SidebarMenuItem } from "./types";
|
2022-02-02 15:30:34 +00:00
|
|
|
|
2023-02-20 15:21:28 +00:00
|
|
|
export const mapToExtensionsItems = (
|
|
|
|
extensions: Extension[],
|
|
|
|
header: SidebarMenuItem,
|
|
|
|
) => {
|
|
|
|
const items: SidebarMenuItem[] = extensions.map(
|
|
|
|
({ label, id, app, url, permissions, open }) => ({
|
|
|
|
id: `extension-${id}`,
|
|
|
|
label,
|
2023-03-02 09:22:01 +00:00
|
|
|
url: AppUrls.resolveDashboardUrlFromAppCompleteUrl(
|
|
|
|
url,
|
|
|
|
app.appUrl,
|
|
|
|
app.id,
|
|
|
|
),
|
2023-02-20 15:21:28 +00:00
|
|
|
permissions,
|
|
|
|
onClick: open,
|
|
|
|
type: "item",
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
if (items.length) {
|
|
|
|
items.unshift(header);
|
2020-11-18 15:11:15 +00:00
|
|
|
}
|
2023-02-20 15:21:28 +00:00
|
|
|
return items;
|
|
|
|
};
|
2020-11-18 15:11:15 +00:00
|
|
|
|
2023-02-20 15:21:28 +00:00
|
|
|
export function isMenuActive(location: string, menuItem: SidebarMenuItem) {
|
2022-02-02 15:30:34 +00:00
|
|
|
if (!menuItem.url) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-28 12:15:25 +00:00
|
|
|
const activeUrl = getPureUrl(location.split("?")[0]);
|
2020-11-18 15:11:15 +00:00
|
|
|
const menuItemUrl = menuItem.url.split("?")[0];
|
|
|
|
|
2022-02-02 15:30:34 +00:00
|
|
|
if (isMenuItemExtension(menuItem)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
activeUrl === orderDraftListUrl().split("?")[0] &&
|
2020-11-18 15:11:15 +00:00
|
|
|
menuItemUrl === orderListUrl().split("?")[0]
|
2022-02-02 15:30:34 +00:00
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !!matchPath(activeUrl, {
|
|
|
|
exact: menuItemUrl === "/",
|
2022-06-21 09:36:55 +00:00
|
|
|
path: menuItemUrl,
|
2022-02-02 15:30:34 +00:00
|
|
|
});
|
2020-11-18 15:11:15 +00:00
|
|
|
}
|
2022-02-02 15:30:34 +00:00
|
|
|
|
2023-02-28 12:15:25 +00:00
|
|
|
const getPureUrl = (url: string) => {
|
|
|
|
if (url.includes("/dashboard")) {
|
|
|
|
return url.split("/dashboard")[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
return url;
|
|
|
|
};
|
|
|
|
|
2022-02-02 15:30:34 +00:00
|
|
|
const isMenuItemExtension = (menuItem: SidebarMenuItem) =>
|
|
|
|
menuItem.id.startsWith("extension-");
|
|
|
|
|
|
|
|
export const getMenuItemExtension = (
|
|
|
|
extensions: Record<AppExtensionMountEnum, Extension[]>,
|
2023-02-20 15:21:28 +00:00
|
|
|
id: string,
|
2022-02-02 15:30:34 +00:00
|
|
|
) => {
|
|
|
|
const extensionsList = Object.values(extensions).reduce(
|
|
|
|
(list, extensions) => list.concat(extensions),
|
2022-06-21 09:36:55 +00:00
|
|
|
[],
|
2022-02-02 15:30:34 +00:00
|
|
|
);
|
|
|
|
const extension = extensionsList.find(
|
2023-02-20 15:21:28 +00:00
|
|
|
extension => id === `extension-${extension.id}`,
|
2022-02-02 15:30:34 +00:00
|
|
|
);
|
|
|
|
return extension;
|
|
|
|
};
|