Skip to content

Commit 1c5a042

Browse files
authored
fix: include origin in support link (#16572)
Fixes: #15542
1 parent 0149222 commit 1c5a042

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { includeOrigin } from "./MobileMenu";
2+
3+
const mockOrigin = "https://example.com";
4+
5+
describe("support link", () => {
6+
it("should include origin if target starts with '/'", () => {
7+
(window as unknown as { location: Partial<Location> }).location = {
8+
origin: mockOrigin,
9+
}; // Mock the location origin
10+
11+
expect(includeOrigin("/test")).toBe(`${mockOrigin}/test`);
12+
expect(includeOrigin("/path/to/resource")).toBe(
13+
`${mockOrigin}/path/to/resource`,
14+
);
15+
});
16+
17+
it("should return the target unchanged if it does not start with '/'", () => {
18+
expect(includeOrigin(`${mockOrigin}/page`)).toBe(`${mockOrigin}/page`);
19+
expect(includeOrigin("../relative/path")).toBe("../relative/path");
20+
expect(includeOrigin("relative/path")).toBe("relative/path");
21+
});
22+
});

site/src/modules/dashboard/Navbar/MobileMenu.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,11 @@ const UserSettingsSub: FC<UserSettingsSubProps> = ({
307307
asChild
308308
className={cn(itemStyles.default, itemStyles.sub)}
309309
>
310-
<a href={l.target} target="_blank" rel="noreferrer">
310+
<a
311+
href={includeOrigin(l.target)}
312+
target="_blank"
313+
rel="noreferrer"
314+
>
311315
{l.name}
312316
</a>
313317
</DropdownMenuItem>
@@ -318,3 +322,11 @@ const UserSettingsSub: FC<UserSettingsSubProps> = ({
318322
</Collapsible>
319323
);
320324
};
325+
326+
export const includeOrigin = (target: string): string => {
327+
if (target.startsWith("/")) {
328+
const baseUrl = window.location.origin;
329+
return `${baseUrl}${target}`;
330+
}
331+
return target;
332+
};

site/src/testHelpers/entities.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,11 @@ export const MockSupportLinks: TypesGen.LinkConfig[] = [
246246
"https://github.com/coder/coder/issues/new?labels=needs+grooming&body={CODER_BUILD_INFO}",
247247
icon: "",
248248
},
249+
{
250+
name: "Fourth link",
251+
target: "/icons",
252+
icon: "",
253+
},
249254
];
250255

251256
export const MockUpdateCheck: TypesGen.UpdateCheckResponse = {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy