Files
HiveGrid-Wireframes/hivegrid-cloud/apps/web/app/components/layouts/selectors/tenant-switch-url.ts
T

30 lines
877 B
TypeScript

import { appPath } from "~/utils/app/CanonicalUrlUtils";
type TenantSwitchLocation = {
pathname: string;
search?: string;
hash?: string;
};
type TenantSwitchOptions = {
currentTenant: {
id: string;
slug: string;
};
location: TenantSwitchLocation;
tenantSlug: string;
};
export function getTenantSwitchPath({ currentTenant, location, tenantSlug }: TenantSwitchOptions) {
const pathSegments = location.pathname.split("/").filter(Boolean);
let childPath = "";
if (pathSegments[0] === "app" && (pathSegments[1] === currentTenant.slug || pathSegments[1] === currentTenant.id)) {
childPath = pathSegments.slice(2).join("/");
} else if (pathSegments[0] === currentTenant.slug || pathSegments[0] === currentTenant.id) {
childPath = pathSegments.slice(1).join("/");
}
return appPath({ tenantSlug, path: childPath, mode: "host-aware" });
}