Skip to content
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

fix(dashboard): Tab menu visible for urls trailing '/' #10698

Merged
merged 2 commits into from
Jul 6, 2022
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
8 changes: 4 additions & 4 deletions components/dashboard/src/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { getProjectSettingsMenu } from "./projects/ProjectSettings";
import { ProjectContext } from "./projects/project-context";
import { PaymentContext } from "./payment-context";
import FeedbackFormModal from "./feedback-form/FeedbackModal";
import { isGitpodIo } from "./utils";
import { inResource, isGitpodIo } from "./utils";
import { getExperimentsClient } from "./experiments/client";

interface Entry {
Expand Down Expand Up @@ -105,9 +105,9 @@ export default function Menu() {
}

// Hide most of the top menu when in a full-page form.
const isMinimalUI = ["/new", "/teams/new", "/open"].includes(location.pathname);
const isWorkspacesUI = ["/workspaces"].includes(location.pathname);
const isAdminUI = window.location.pathname.startsWith("/admin");
const isMinimalUI = inResource(location.pathname, ["new", "teams/new", "open"]);
const isWorkspacesUI = inResource(location.pathname, ["workspaces"]);
const isAdminUI = inResource(window.location.pathname, ["admin"]);

const [teamMembers, setTeamMembers] = useState<Record<string, TeamMemberInfo[]>>({});
useEffect(() => {
Expand Down
29 changes: 29 additions & 0 deletions components/dashboard/src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright (c) 2022 Gitpod GmbH. All rights reserved.
* Licensed under the GNU Affero General Public License (AGPL).
* See License-AGPL.txt in the project root for license information.
*/

import { inResource } from "./utils";

test("inResource", () => {

// Given root path is a part of resources specified
expect(inResource("/app", ["new", "app", "teams"])).toBe(true);

// Given path is a part of resources specified
expect(inResource("/app/testing", ["new", "app", "teams"])).toBe(true);

// Empty resources
expect(inResource("/just/a/path", [])).toBe(false);

// Both resources starting with '/'
expect(inResource("/app", ["/app"])).toBe(true);

// Both resources ending with '/'
expect(inResource("app/", ["app/"])).toBe(true);

// Both resources containing path with subdirectories
expect(inResource("/admin/teams/someTeam/somePerson", ["/admin/teams"])).toBe(true);

});
18 changes: 18 additions & 0 deletions components/dashboard/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,21 @@ export function isGitpodIo() {
window.location.hostname.endsWith("gitpod-io-dev.com")
);
}

function trimResource(resource: string): string {
return resource.split('/').filter(Boolean).join('/');
}

// Returns 'true' if a 'pathname' is a part of 'resources' provided.
// `inResource("/app/testing/", ["new", "app", "teams"])` will return true
// because '/app/testing' is a part of root 'app'
//
// 'pathname' arg can be provided via `location.pathname`.
export function inResource(pathname: string, resources: string[]): boolean {
// Removes leading and trailing '/'
const trimmedResource = trimResource(pathname)

// Checks if a path is part of a resource.
// E.g. "api/userspace/resource" path is a part of resource "api/userspace"
return resources.map(res => trimmedResource.startsWith(trimResource(res))).some(Boolean)
}