mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
fix: include origin in support link (#16572)
Fixes: https://github.com/coder/coder/issues/15542
This commit is contained in:
@@ -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");
|
||||
});
|
||||
});
|
||||
@@ -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>
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
@@ -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 = {
|
||||
|
||||
Reference in New Issue
Block a user