Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,47 @@
|
||||
import { useAppData } from "~/utils/data/useAppData";
|
||||
|
||||
interface Props {
|
||||
onOpenCommandPalette: () => void;
|
||||
}
|
||||
|
||||
export default function NewTenantSelect({ onOpenCommandPalette }: Props) {
|
||||
const appData = useAppData();
|
||||
if (!appData.currentTenant) {
|
||||
return null;
|
||||
}
|
||||
return (
|
||||
<>
|
||||
<button type="button" onClick={onOpenCommandPalette} className="text-foreground group flex w-full shrink-0 focus:outline-hidden">
|
||||
<div className="group block w-full shrink-0">
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="flex items-center truncate text-left">
|
||||
{appData?.currentTenant?.icon ? (
|
||||
<img className="inline-block h-9 w-9 shrink-0 rounded-full shadow-2xs" src={appData?.currentTenant?.icon} alt={appData?.currentTenant?.name} />
|
||||
) : (
|
||||
<span className="bg-primary inline-flex h-9 w-9 shrink-0 items-center justify-center rounded-full">
|
||||
<span className="text-primary-foreground text-sm font-medium leading-none">{appData?.currentTenant?.name.substring(0, 1)}</span>
|
||||
</span>
|
||||
)}
|
||||
<div className="ml-3 truncate">
|
||||
<p className="text-sm font-medium">{appData?.currentTenant?.name}</p>
|
||||
<p className="text-muted-foreground truncate text-xs font-normal">{appData?.user.email}</p>
|
||||
</div>
|
||||
</div>
|
||||
{/* <svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
className="text-muted-foreground group-hover:text-foreground h-4 w-4"
|
||||
viewBox="0 0 20 20"
|
||||
fill="currentColor"
|
||||
>
|
||||
<path
|
||||
fillRule="evenodd"
|
||||
d="M10 3a1 1 0 01.707.293l3 3a1 1 0 01-1.414 1.414L10 5.414 7.707 7.707a1 1 0 01-1.414-1.414l3-3A1 1 0 0110 3zm-3.707 9.293a1 1 0 011.414 0L10 14.586l2.293-2.293a1 1 0 011.414 1.414l-3 3a1 1 0 01-1.414 0l-3-3a1 1 0 010-1.414z"
|
||||
clipRule="evenodd"
|
||||
/>
|
||||
</svg> */}
|
||||
</div>
|
||||
</div>
|
||||
</button>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
import { useLocation, useNavigate } from "react-router";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import PlusIcon from "~/components/ui/icons/PlusIcon";
|
||||
import InputSelector from "~/components/ui/input/InputSelector";
|
||||
import { useAppData } from "~/utils/data/useAppData";
|
||||
import { useRootData } from "~/utils/data/useRootData";
|
||||
import { absolutePublicAppPath, isAbsoluteHttpUrl } from "~/utils/app/CrossHostUrls";
|
||||
import { translateLabel } from "~/utils/i18n/translateLabel";
|
||||
import { getTenantSwitchPath } from "./tenant-switch-url";
|
||||
|
||||
interface Props {
|
||||
className?: string;
|
||||
}
|
||||
|
||||
export default function TenantSelector({ className }: Props) {
|
||||
const { t, i18n } = useTranslation();
|
||||
const appData = useAppData();
|
||||
const rootData = useRootData();
|
||||
const location = useLocation();
|
||||
const navigate = useNavigate();
|
||||
|
||||
const [selected, setSelected] = useState(appData?.currentTenant?.slug);
|
||||
|
||||
function switchTenantPath(tenantSlug: string) {
|
||||
return getTenantSwitchPath({ currentTenant: appData.currentTenant, location, tenantSlug });
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (selected) {
|
||||
const tenant = appData?.myTenants.find((f) => f.slug === selected);
|
||||
if (tenant && tenant.id !== appData.currentTenant.id) {
|
||||
navigate(switchTenantPath(tenant.slug));
|
||||
}
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [selected]);
|
||||
|
||||
function getAllTenants() {
|
||||
const items = appData?.myTenants
|
||||
.map((f) => {
|
||||
let group = t("models.tenant.plural");
|
||||
if (f.types.length > 0) {
|
||||
group = translateLabel(t, i18n, f.types[0].titlePlural) || t("models.tenant.plural");
|
||||
}
|
||||
return {
|
||||
group,
|
||||
value: f.slug,
|
||||
name: (
|
||||
<div className="flex items-center space-x-2">
|
||||
{f.icon ? (
|
||||
<img className="inline-block h-4 w-4 shrink-0 rounded-md shadow-xs" src={f.icon} alt={f.name} />
|
||||
) : (
|
||||
<span className="bg-primary inline-flex h-4 w-4 shrink-0 items-center justify-center rounded-md">
|
||||
<span className="text-primary-foreground text-xs font-medium leading-none">{f?.name.substring(0, 1)}</span>
|
||||
</span>
|
||||
)}
|
||||
<div>{f.name}</div>
|
||||
</div>
|
||||
),
|
||||
};
|
||||
})
|
||||
.sort((a, b) => a.group.localeCompare(b.group));
|
||||
// return unique slugs
|
||||
const unique = items.filter((item, index, self) => self.findIndex((t) => t.value === item.value) === index);
|
||||
return [
|
||||
...unique,
|
||||
{
|
||||
title: "hey",
|
||||
value: "{new}",
|
||||
className: "border-t border-border rounded-none",
|
||||
name: (
|
||||
<div className="flex items-center gap-2">
|
||||
<div className="w-4 shrink-0">
|
||||
<PlusIcon className="h-4 w-4 p-0.5" />
|
||||
</div>
|
||||
<div>{t("app.tenants.create.title")}</div>
|
||||
</div>
|
||||
),
|
||||
},
|
||||
];
|
||||
}
|
||||
return (
|
||||
<div className="text-foreground">
|
||||
{appData?.myTenants.length > 1 && (
|
||||
<InputSelector
|
||||
selectPlaceholder=""
|
||||
withSearch={false}
|
||||
value={selected}
|
||||
options={getAllTenants()}
|
||||
setValue={(e) => {
|
||||
if (e === "{new}") {
|
||||
const newAccountPath = absolutePublicAppPath(rootData.routing, "new-account");
|
||||
if (isAbsoluteHttpUrl(newAccountPath)) {
|
||||
window.location.assign(newAccountPath);
|
||||
} else {
|
||||
navigate(newAccountPath);
|
||||
}
|
||||
setSelected(e?.toString() ?? "");
|
||||
} else {
|
||||
setSelected(e?.toString() ?? "");
|
||||
}
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import { useAppData } from "~/utils/data/useAppData";
|
||||
import { ChevronsUpDown } from "~/components/ui/icons/vendor";
|
||||
|
||||
interface Props {
|
||||
onOpenCommandPalette: () => void;
|
||||
}
|
||||
|
||||
export default function TenantSelect({ onOpenCommandPalette }: Props) {
|
||||
const appData = useAppData();
|
||||
if (!appData.currentTenant) {
|
||||
return null;
|
||||
}
|
||||
return (
|
||||
<>
|
||||
<button type="button" onClick={onOpenCommandPalette} className="group flex shrink-0 bg-slate-800 p-4 focus:outline-hidden">
|
||||
<div className="group block w-full shrink-0">
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="flex items-center truncate text-left">
|
||||
{appData?.currentTenant?.icon ? (
|
||||
<img
|
||||
className="inline-block h-9 w-9 shrink-0 rounded-full bg-gray-500 shadow-2xs"
|
||||
src={appData?.currentTenant?.icon}
|
||||
alt={appData?.currentTenant?.name}
|
||||
/>
|
||||
) : (
|
||||
<span className="bg-primary inline-flex h-9 w-9 shrink-0 items-center justify-center rounded-full">
|
||||
<span className="text-primary-foreground text-sm font-medium leading-none">{appData?.currentTenant?.name.substring(0, 1)}</span>
|
||||
</span>
|
||||
)}
|
||||
<div className="ml-3 truncate">
|
||||
<p className="text-sm font-medium text-gray-200 group-hover:text-white">{appData?.currentTenant?.name}</p>
|
||||
<p className="truncate text-xs font-medium text-gray-300 group-hover:text-gray-200">{appData?.user.email}</p>
|
||||
</div>
|
||||
</div>
|
||||
<ChevronsUpDown className="text-muted-foreground h-5 w-5 group-hover:text-white" />
|
||||
</div>
|
||||
</div>
|
||||
</button>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
import { useLocation, useNavigate } from "react-router";
|
||||
import { Fragment, useEffect, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import InputSelectorDarkMode from "~/components/ui/input/dark/InputSelectorDarkMode";
|
||||
import { useAppData } from "~/utils/data/useAppData";
|
||||
import { translateLabel } from "~/utils/i18n/translateLabel";
|
||||
import { getTenantSwitchPath } from "./tenant-switch-url";
|
||||
|
||||
interface Props {
|
||||
className?: string;
|
||||
}
|
||||
|
||||
export default function TenantSelector({ className }: Props) {
|
||||
const { t, i18n } = useTranslation();
|
||||
const appData = useAppData();
|
||||
const location = useLocation();
|
||||
const navigate = useNavigate();
|
||||
|
||||
const [selected, setSelected] = useState(appData?.currentTenant?.slug);
|
||||
|
||||
function switchTenantPath(tenantSlug: string) {
|
||||
return getTenantSwitchPath({ currentTenant: appData.currentTenant, location, tenantSlug });
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (selected) {
|
||||
const tenant = appData.myTenants.find((f) => f.slug === selected);
|
||||
if (tenant && tenant.id !== appData.currentTenant.id) {
|
||||
navigate(switchTenantPath(tenant.slug));
|
||||
}
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [selected]);
|
||||
|
||||
function getAllTenants() {
|
||||
const items = appData.myTenants
|
||||
.map((f) => {
|
||||
let group = t("models.tenant.plural");
|
||||
if (f.types.length > 0) {
|
||||
group = translateLabel(t, i18n, f.types[0].titlePlural) || t("models.tenant.plural");
|
||||
}
|
||||
return {
|
||||
group,
|
||||
value: f.slug,
|
||||
name: f.name,
|
||||
img: f.icon ? (
|
||||
<img className="inline-block h-6 w-6 shrink-0 rounded-md bg-gray-700 shadow-xs" src={f.icon} alt={f.name} />
|
||||
) : (
|
||||
<span className="bg-primary inline-flex h-6 w-6 shrink-0 items-center justify-center rounded-md">
|
||||
<span className="text-theme-200 text-xs font-medium leading-none">{f?.name.substring(0, 1)}</span>
|
||||
</span>
|
||||
),
|
||||
};
|
||||
})
|
||||
.sort((a, b) => a.group.localeCompare(b.group));
|
||||
// return unique slugs
|
||||
const unique = items.filter((item, index, self) => self.findIndex((t) => t.value === item.value) === index);
|
||||
return unique;
|
||||
}
|
||||
return (
|
||||
<Fragment>
|
||||
{appData.myTenants.length > 1 && (
|
||||
<InputSelectorDarkMode
|
||||
withSearch={false}
|
||||
value={selected}
|
||||
renderSelected={(e) => {
|
||||
const tenant = appData.myTenants.find((f) => f.slug === e.value);
|
||||
return <div>{tenant?.name}</div>;
|
||||
}}
|
||||
options={getAllTenants()}
|
||||
setValue={(e) => setSelected(e?.toString() ?? "")}
|
||||
/>
|
||||
)}
|
||||
</Fragment>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { getTenantSwitchPath } from "./tenant-switch-url";
|
||||
|
||||
const currentTenant = {
|
||||
id: "tenant_123",
|
||||
slug: "acme",
|
||||
};
|
||||
|
||||
describe("getTenantSwitchPath", () => {
|
||||
it("keeps the current child path when switching tenants", () => {
|
||||
expect(
|
||||
getTenantSwitchPath({
|
||||
currentTenant,
|
||||
location: { pathname: "/acme/settings/members" },
|
||||
tenantSlug: "beta",
|
||||
}),
|
||||
).toBe("/beta/settings/members");
|
||||
});
|
||||
|
||||
it("supports legacy app-prefixed tenant paths without preserving the legacy prefix", () => {
|
||||
expect(
|
||||
getTenantSwitchPath({
|
||||
currentTenant,
|
||||
location: { pathname: "/app/acme/entities/rows" },
|
||||
tenantSlug: "beta",
|
||||
}),
|
||||
).toBe("/beta/entities/rows");
|
||||
});
|
||||
|
||||
it("does not carry scope-bearing query or hash state into the next tenant", () => {
|
||||
expect(
|
||||
getTenantSwitchPath({
|
||||
currentTenant,
|
||||
location: { pathname: "/acme/entities/rows", search: "?tenantId=tenant_123", hash: "#row_1" },
|
||||
tenantSlug: "beta",
|
||||
}),
|
||||
).toBe("/beta/entities/rows");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,29 @@
|
||||
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" });
|
||||
}
|
||||
Reference in new issue
Block a user