fix(portal): Modul-Kachel-Links dynamisch statt hartkodiertem localhost auflösen #260
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { fetchVisibleModules } from "./ModuleGrid";
|
||||
import { fetchVisibleModules, resolveModuleHref } from "./ModuleGrid";
|
||||
|
||||
describe("fetchVisibleModules", () => {
|
||||
it("ruft die Portal-eigene Route mit tenant/role auf und gibt die Modulliste zurueck", async () => {
|
||||
@@ -22,3 +22,17 @@ describe("fetchVisibleModules", () => {
|
||||
expect(modules).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveModuleHref", () => {
|
||||
it("ersetzt localhost durch den echten Hostnamen", () => {
|
||||
expect(resolveModuleHref("http://localhost:3107/", "192.168.1.204")).toBe("http://192.168.1.204:3107/");
|
||||
});
|
||||
|
||||
it("laesst Nicht-localhost-Hrefs unveraendert", () => {
|
||||
expect(resolveModuleHref("http://dms.example.com:3107/", "192.168.1.204")).toBe("http://dms.example.com:3107/");
|
||||
});
|
||||
|
||||
it("laesst localhost unveraendert wenn das Portal selbst auf localhost laeuft", () => {
|
||||
expect(resolveModuleHref("http://localhost:3107/", "localhost")).toBe("http://localhost:3107/");
|
||||
});
|
||||
});
|
||||
@@ -23,6 +23,21 @@ export async function fetchVisibleModules(
|
||||
return body.modules ?? [];
|
||||
}
|
||||
|
||||
export function resolveModuleHref(
|
||||
href: string,
|
||||
currentHostname: string = typeof window !== "undefined" ? window.location.hostname : "localhost"
|
||||
): string {
|
||||
try {
|
||||
const url = new URL(href);
|
||||
if (url.hostname === "localhost" && currentHostname !== "localhost") {
|
||||
url.hostname = currentHostname;
|
||||
}
|
||||
return url.toString();
|
||||
} catch {
|
||||
return href;
|
||||
}
|
||||
}
|
||||
|
||||
type State =
|
||||
| { status: "loading" }
|
||||
| { status: "error"; message: string }
|
||||
@@ -76,7 +91,7 @@ export function ModuleGrid() {
|
||||
{state.modules.map((mod) => (
|
||||
<li key={mod.key}>
|
||||
<a
|
||||
href={mod.href}
|
||||
href={resolveModuleHref(mod.href)}
|
||||
style={{
|
||||
display: "block",
|
||||
minHeight: 44,
|
||||
|
||||
Reference in new issue
Block a user