Skip to content

fix: include origin in support link #16572

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions site/src/modules/dashboard/Navbar/MobileMenu.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { includeOrigin } from "./MobileMenu";

const mockOrigin = "https://example.com";

describe("support link", () => {
it("should include origin if target starts with '/'", () => {
(window as unknown as { location: Partial<Location> }).location = {
origin: mockOrigin,
}; // Mock the location origin

expect(includeOrigin("/test")).toBe(`${mockOrigin}/test`);
expect(includeOrigin("/path/to/resource")).toBe(
`${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("../relative/path")).toBe("../relative/path");
expect(includeOrigin("relative/path")).toBe("relative/path");
});
});
14 changes: 13 additions & 1 deletion site/src/modules/dashboard/Navbar/MobileMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,11 @@ const UserSettingsSub: FC<UserSettingsSubProps> = ({
asChild
className={cn(itemStyles.default, itemStyles.sub)}
>
<a href={l.target} target="_blank" rel="noreferrer">
<a
href={includeOrigin(l.target)}
target="_blank"
rel="noreferrer"
>
{l.name}
</a>
</DropdownMenuItem>
Expand All @@ -318,3 +322,11 @@ const UserSettingsSub: FC<UserSettingsSubProps> = ({
</Collapsible>
);
};

export const includeOrigin = (target: string): string => {
if (target.startsWith("/")) {
const baseUrl = window.location.origin;
return `${baseUrl}${target}`;
}
return target;
};
5 changes: 5 additions & 0 deletions site/src/testHelpers/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Loading