Skip to content

Commit

Permalink
Retain non-variable sub route on breadcrumbs (#5526)
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaHegde authored Aug 26, 2024
1 parent 00d9dc7 commit ce2874a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<script lang="ts">
import { page } from "$app/stores";
import * as DropdownMenu from "@rilldata/web-common/components/dropdown-menu";
import CaretDownIcon from "@rilldata/web-common/components/icons/CaretDownIcon.svelte";
import { getNonVariableSubRoute } from "@rilldata/web-common/components/navigation/breadcrumbs/utils";
import type { PathOption, PathOptions } from "./Breadcrumbs.svelte";
export let options: PathOptions;
Expand All @@ -18,6 +20,7 @@
depth: number,
id: string,
option: PathOption,
route: string,
) {
if (onSelect) return undefined;
if (option?.href) return option.href;
Expand All @@ -29,8 +32,10 @@
if (option?.section) newPath.push(option.section);
newPath.push(id);
const path = `/${newPath.join("/")}`;
return `/${newPath.join("/")}`;
// add the sub route if it has no variables
return path + getNonVariableSubRoute(path, route);
}
</script>

Expand All @@ -41,7 +46,7 @@
on:click={() => {
if (isCurrentPage && !isEmbedded) window.location.reload();
}}
href={linkMaker(currentPath, depth, current, selected)}
href={linkMaker(currentPath, depth, current, selected, "")}
class="text-gray-500 hover:text-gray-600"
class:current={isCurrentPage}
>
Expand All @@ -65,7 +70,13 @@
class="cursor-pointer"
checked={selected}
checkSize={"h-3 w-3"}
href={linkMaker(currentPath, depth, id, option)}
href={linkMaker(
currentPath,
depth,
id,
option,
$page.route.id ?? "",
)}
on:click={() => {
if (onSelect) {
onSelect(id);
Expand Down
21 changes: 21 additions & 0 deletions web-common/src/components/navigation/breadcrumbs/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const variableRouteRegex = /\[.*]/;

/**
* Gets sub route with prefix of path if it does not have any variables.
* This helps to easily switch to another org/project but keep the same sub route like settings/alerts
* @param path
* @param route
*/
export function getNonVariableSubRoute(path: string, route: string) {
const pathParts = path.split("/");
const routeParts = route.split("/").slice(pathParts.length);
if (
// if any sub route has a variable then do not return sub route
routeParts.some((p) => p.match(variableRouteRegex)) ||
routeParts.length === 0
) {
return "";
}

return "/" + routeParts.join("/");
}

1 comment on commit ce2874a

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.