From fec831801f8a77a014ecf9b1b75458cdf91893e8 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Fri, 14 Feb 2025 12:32:15 +0100 Subject: [PATCH 1/9] fix: include origin in support link --- site/src/modules/dashboard/Navbar/MobileMenu.tsx | 10 +++++++++- site/src/testHelpers/entities.ts | 5 +++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/site/src/modules/dashboard/Navbar/MobileMenu.tsx b/site/src/modules/dashboard/Navbar/MobileMenu.tsx index f24755a5c4c17..22c074ca71ae4 100644 --- a/site/src/modules/dashboard/Navbar/MobileMenu.tsx +++ b/site/src/modules/dashboard/Navbar/MobileMenu.tsx @@ -307,7 +307,7 @@ const UserSettingsSub: FC = ({ asChild className={cn(itemStyles.default, itemStyles.sub)} > - + {l.name} @@ -318,3 +318,11 @@ const UserSettingsSub: FC = ({ ); }; + +const includeOrigin = (target: string): string => { + if (target.startsWith("/")) { + const baseUrl = window.location.origin; + return `${baseUrl}${target}`; + } + return target; +}; diff --git a/site/src/testHelpers/entities.ts b/site/src/testHelpers/entities.ts index a607df6bb87c9..c866c64f15b4e 100644 --- a/site/src/testHelpers/entities.ts +++ b/site/src/testHelpers/entities.ts @@ -246,6 +246,11 @@ export const MockSupportLinks: TypesGen.LinkConfig[] = [ "https://github.com/coder/coder/issues/new?labels=needs+grooming&body={CODER_BUILD_INFO}", icon: "", }, + { + name: "Fourth link", + target: "/icons", + icon: "", + }, ]; export const MockUpdateCheck: TypesGen.UpdateCheckResponse = { From 4d1274237b1e0780d80ada366f011560bfacb7c3 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Fri, 14 Feb 2025 12:36:37 +0100 Subject: [PATCH 2/9] fmt/biome --- site/src/modules/dashboard/Navbar/MobileMenu.tsx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/site/src/modules/dashboard/Navbar/MobileMenu.tsx b/site/src/modules/dashboard/Navbar/MobileMenu.tsx index 22c074ca71ae4..65633af67822a 100644 --- a/site/src/modules/dashboard/Navbar/MobileMenu.tsx +++ b/site/src/modules/dashboard/Navbar/MobileMenu.tsx @@ -307,7 +307,11 @@ const UserSettingsSub: FC = ({ asChild className={cn(itemStyles.default, itemStyles.sub)} > - + {l.name} From ff8c1018273a426d0f4d023429b4223303596aa3 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Fri, 14 Feb 2025 13:05:58 +0100 Subject: [PATCH 3/9] unit test --- .../dashboard/Navbar/MobileMenu.test.tsx | 22 +++++++++++++++++++ .../modules/dashboard/Navbar/MobileMenu.tsx | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 site/src/modules/dashboard/Navbar/MobileMenu.test.tsx diff --git a/site/src/modules/dashboard/Navbar/MobileMenu.test.tsx b/site/src/modules/dashboard/Navbar/MobileMenu.test.tsx new file mode 100644 index 0000000000000..91eaa39ddddd1 --- /dev/null +++ b/site/src/modules/dashboard/Navbar/MobileMenu.test.tsx @@ -0,0 +1,22 @@ +import { includeOrigin } from "./MobileMenu"; + +describe("support link", () => { + it("should include origin if target starts with '/'", () => { + const mockOrigin = "https://example.com"; + delete (window as any).location; // Remove the existing location object + (window as any).location = { origin: mockOrigin }; // Mock the location origin + + expect(includeOrigin("/test")).toBe("https://example.com/test"); + expect(includeOrigin("/path/to/resource")).toBe( + "https://example.com/path/to/resource", + ); + }); + + it("should return the target unchanged if it does not start with '/'", () => { + expect(includeOrigin("https://example.com/page")).toBe( + "https://example.com/page", + ); + expect(includeOrigin("../relative/path")).toBe("../relative/path"); + expect(includeOrigin("relative/path")).toBe("relative/path"); + }); +}); diff --git a/site/src/modules/dashboard/Navbar/MobileMenu.tsx b/site/src/modules/dashboard/Navbar/MobileMenu.tsx index 65633af67822a..20058335eb8e5 100644 --- a/site/src/modules/dashboard/Navbar/MobileMenu.tsx +++ b/site/src/modules/dashboard/Navbar/MobileMenu.tsx @@ -323,7 +323,7 @@ const UserSettingsSub: FC = ({ ); }; -const includeOrigin = (target: string): string => { +export const includeOrigin = (target: string): string => { if (target.startsWith("/")) { const baseUrl = window.location.origin; return `${baseUrl}${target}`; From ddd6a884d2cf5bfe5d5fed32267a68a87f150c69 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Fri, 14 Feb 2025 13:16:49 +0100 Subject: [PATCH 4/9] lint fix --- site/src/modules/dashboard/Navbar/MobileMenu.test.tsx | 3 +++ 1 file changed, 3 insertions(+) diff --git a/site/src/modules/dashboard/Navbar/MobileMenu.test.tsx b/site/src/modules/dashboard/Navbar/MobileMenu.test.tsx index 91eaa39ddddd1..8a762dc78f739 100644 --- a/site/src/modules/dashboard/Navbar/MobileMenu.test.tsx +++ b/site/src/modules/dashboard/Navbar/MobileMenu.test.tsx @@ -3,7 +3,10 @@ import { includeOrigin } from "./MobileMenu"; describe("support link", () => { it("should include origin if target starts with '/'", () => { const mockOrigin = "https://example.com"; + + // eslint-disable-next-line window object delete (window as any).location; // Remove the existing location object + // eslint-disable-next-line window object (window as any).location = { origin: mockOrigin }; // Mock the location origin expect(includeOrigin("/test")).toBe("https://example.com/test"); From 5dd40b7e8e71f21fd8d4ed17d69393cda13d5933 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Fri, 14 Feb 2025 13:29:41 +0100 Subject: [PATCH 5/9] fix --- site/src/modules/dashboard/Navbar/MobileMenu.test.tsx | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/site/src/modules/dashboard/Navbar/MobileMenu.test.tsx b/site/src/modules/dashboard/Navbar/MobileMenu.test.tsx index 8a762dc78f739..f5fd881d75cef 100644 --- a/site/src/modules/dashboard/Navbar/MobileMenu.test.tsx +++ b/site/src/modules/dashboard/Navbar/MobileMenu.test.tsx @@ -4,9 +4,7 @@ describe("support link", () => { it("should include origin if target starts with '/'", () => { const mockOrigin = "https://example.com"; - // eslint-disable-next-line window object - delete (window as any).location; // Remove the existing location object - // eslint-disable-next-line window object + // eslint-disable-next-line (window as any).location = { origin: mockOrigin }; // Mock the location origin expect(includeOrigin("/test")).toBe("https://example.com/test"); From 3c9ed32d04cd4a381b3970c81796934305957959 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Fri, 14 Feb 2025 13:42:53 +0100 Subject: [PATCH 6/9] fix --- site/src/modules/dashboard/Navbar/MobileMenu.test.tsx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/site/src/modules/dashboard/Navbar/MobileMenu.test.tsx b/site/src/modules/dashboard/Navbar/MobileMenu.test.tsx index f5fd881d75cef..8e8326ee12b5b 100644 --- a/site/src/modules/dashboard/Navbar/MobileMenu.test.tsx +++ b/site/src/modules/dashboard/Navbar/MobileMenu.test.tsx @@ -4,8 +4,7 @@ describe("support link", () => { it("should include origin if target starts with '/'", () => { const mockOrigin = "https://example.com"; - // eslint-disable-next-line - (window as any).location = { origin: mockOrigin }; // Mock the location origin + window.location = { origin: mockOrigin }; // Mock the location origin expect(includeOrigin("/test")).toBe("https://example.com/test"); expect(includeOrigin("/path/to/resource")).toBe( From 7b3cdce034f96d45e3ca7586e37bb951f4e44c87 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Fri, 14 Feb 2025 13:47:46 +0100 Subject: [PATCH 7/9] fix --- .../modules/dashboard/Navbar/MobileMenu.test.tsx | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/site/src/modules/dashboard/Navbar/MobileMenu.test.tsx b/site/src/modules/dashboard/Navbar/MobileMenu.test.tsx index 8e8326ee12b5b..11700acca86b4 100644 --- a/site/src/modules/dashboard/Navbar/MobileMenu.test.tsx +++ b/site/src/modules/dashboard/Navbar/MobileMenu.test.tsx @@ -1,21 +1,19 @@ import { includeOrigin } from "./MobileMenu"; +const mockOrigin = "https://example.com"; + describe("support link", () => { it("should include origin if target starts with '/'", () => { - const mockOrigin = "https://example.com"; - - window.location = { origin: mockOrigin }; // Mock the location origin + (window as unknown as { location: Partial }).location = { origin: mockOrigin }; // Mock the location origin - expect(includeOrigin("/test")).toBe("https://example.com/test"); + expect(includeOrigin("/test")).toBe(mockOrigin + "/test"); expect(includeOrigin("/path/to/resource")).toBe( - "https://example.com/path/to/resource", + mockOrigin + "/path/to/resource", ); }); it("should return the target unchanged if it does not start with '/'", () => { - expect(includeOrigin("https://example.com/page")).toBe( - "https://example.com/page", - ); + expect(includeOrigin(mockOrigin + "/page")).toBe(mockOrigin + "/page"); expect(includeOrigin("../relative/path")).toBe("../relative/path"); expect(includeOrigin("relative/path")).toBe("relative/path"); }); From 45df0449beeed864d76a8a6c4f23053004e89b85 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Fri, 14 Feb 2025 13:49:12 +0100 Subject: [PATCH 8/9] fmt/biome --- site/src/modules/dashboard/Navbar/MobileMenu.test.tsx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/site/src/modules/dashboard/Navbar/MobileMenu.test.tsx b/site/src/modules/dashboard/Navbar/MobileMenu.test.tsx index 11700acca86b4..86ba656128e8f 100644 --- a/site/src/modules/dashboard/Navbar/MobileMenu.test.tsx +++ b/site/src/modules/dashboard/Navbar/MobileMenu.test.tsx @@ -4,7 +4,9 @@ const mockOrigin = "https://example.com"; describe("support link", () => { it("should include origin if target starts with '/'", () => { - (window as unknown as { location: Partial }).location = { origin: mockOrigin }; // Mock the location origin + (window as unknown as { location: Partial }).location = { + origin: mockOrigin, + }; // Mock the location origin expect(includeOrigin("/test")).toBe(mockOrigin + "/test"); expect(includeOrigin("/path/to/resource")).toBe( From 7800e9e34db0d8a2fc24c36af95dd1d79de7d2a0 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Fri, 14 Feb 2025 13:59:26 +0100 Subject: [PATCH 9/9] fix --- site/src/modules/dashboard/Navbar/MobileMenu.test.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/site/src/modules/dashboard/Navbar/MobileMenu.test.tsx b/site/src/modules/dashboard/Navbar/MobileMenu.test.tsx index 86ba656128e8f..ce8a29df78fc4 100644 --- a/site/src/modules/dashboard/Navbar/MobileMenu.test.tsx +++ b/site/src/modules/dashboard/Navbar/MobileMenu.test.tsx @@ -8,14 +8,14 @@ describe("support link", () => { origin: mockOrigin, }; // Mock the location origin - expect(includeOrigin("/test")).toBe(mockOrigin + "/test"); + expect(includeOrigin("/test")).toBe(`${mockOrigin}/test`); expect(includeOrigin("/path/to/resource")).toBe( - mockOrigin + "/path/to/resource", + `${mockOrigin}/path/to/resource`, ); }); it("should return the target unchanged if it does not start with '/'", () => { - expect(includeOrigin(mockOrigin + "/page")).toBe(mockOrigin + "/page"); + expect(includeOrigin(`${mockOrigin}/page`)).toBe(`${mockOrigin}/page`); expect(includeOrigin("../relative/path")).toBe("../relative/path"); expect(includeOrigin("relative/path")).toBe("relative/path"); });