1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

reverting splitted params for catalog's page route to "/catalog/:group?/:kind?"

Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman 2021-05-20 19:21:41 +03:00
parent 01fd4fd3c6
commit fd9a24fcd9
5 changed files with 29 additions and 31 deletions

View File

@ -51,10 +51,6 @@ export class CatalogCategoryRegistry {
return Array.from(this.categories);
}
getById(id: string): CatalogCategory{
return this.items.find(category => category.getId() === id);
}
getForGroupKind<T extends CatalogCategory>(group: string, kind: string): T | undefined {
return this.groupKinds.get(group)?.get(kind) as T;
}

View File

@ -56,13 +56,14 @@ export abstract class CatalogCategory extends EventEmitter {
};
abstract spec: CatalogCategorySpec;
// Should be URL-compatible, otherwise "Uncaught TypeError: Expected "categoryId" to match "[^\/#\?]+?""
static getId(group: string, kind: string): string {
return `${group}--${kind}`;
static parseId(id = ""): { group?: string, kind?: string } {
const [group, kind] = id.split("/") ?? [];
return { group, kind };
}
public getId(): string {
return `${this.spec.group}--${this.spec.names.kind}`;
return `${this.spec.group}/${this.spec.names.kind}`;
}
}

View File

@ -23,10 +23,11 @@ import type { RouteProps } from "react-router";
import { buildURL } from "../../../common/utils/buildUrl";
export interface ICatalogViewRouteParam {
categoryId?: string;
group?: string;
kind?: string;
}
export const catalogRoute: RouteProps = {
path: "/catalog/:categoryId?" // `categoryId` == `${group}--${kind}`
path: "/catalog/:group?/:kind?"
};
export const catalogURL = buildURL<ICatalogViewRouteParam>(catalogRoute.path);

View File

@ -23,9 +23,9 @@ import "./catalog.scss";
import React from "react";
import { disposeOnUnmount, observer } from "mobx-react";
import { ItemListLayout } from "../item-object-list";
import { computed, makeObservable, reaction, when } from "mobx";
import { computed, makeObservable, reaction } from "mobx";
import { CatalogEntityItem, CatalogEntityStore } from "./catalog-entity.store";
import { navigate, navigation } from "../../navigation";
import { navigate } from "../../navigation";
import { kebabCase } from "lodash";
import { PageLayout } from "../layout/page-layout";
import { MenuActions, MenuItem } from "../menu";
@ -64,12 +64,14 @@ export class Catalog extends React.Component<Props> {
navigate: (url: string) => navigate(url),
};
get categoryId(): string | undefined {
return this.props.match.params?.categoryId;
get routeParams(): ICatalogViewRouteParam {
return this.props.match.params ?? {};
}
get activeCategory(): CatalogCategory {
return catalogCategoryRegistry.getById(this.props.match.params?.categoryId);
get activeCategory(): CatalogCategory | undefined {
const { group, kind } = this.routeParams;
return catalogCategoryRegistry.getForGroupKind(group, kind);
}
@computed get categories(): CatalogCategory[] {
@ -86,17 +88,6 @@ export class Catalog extends React.Component<Props> {
reaction(() => this.entities, items => this.catalogEntityStore.loadItems(items), {
fireImmediately: true,
}),
// select initial category if nothing yet selected (via url-params)
when(() => this.categories.length > 0, () => {
const { categoryId } = this;
if (!categoryId) {
navigation.replace(
catalogURL({ params: { categoryId } })
);
}
}),
]);
}
@ -125,13 +116,23 @@ export class Catalog extends React.Component<Props> {
}
}
onTabChange = (categoryId: string) => {
const { group, kind } = CatalogCategory.parseId(categoryId);
if (group && kind) {
navigate(catalogURL({ params: { group, kind } }));
} else {
navigate(catalogURL());
}
};
renderNavigation() {
return (
<Tabs
className="flex column"
scrollable={false}
value={this.categoryId}
onChange={(categoryId: string) => navigate(catalogURL({ params: { categoryId } }))}
value={this.activeCategory?.getId()}
onChange={this.onTabChange}
>
<div className="sidebarHeader">Catalog</div>
<div className="sidebarTabs">

View File

@ -30,7 +30,6 @@ import { entitySettingsURL } from "../components/+entity-settings";
import { catalogEntityRegistry } from "../api/catalog-entity-registry";
import { ClusterStore } from "../../common/cluster-store";
import { EXTENSION_NAME_MATCH, EXTENSION_PUBLISHER_MATCH, LensProtocolRouter } from "../../common/protocol-handler";
import { CatalogCategory } from "../../common/catalog";
export function bindProtocolAddRouteHandlers() {
LensProtocolRouterRenderer
@ -47,7 +46,7 @@ export function bindProtocolAddRouteHandlers() {
.addInternalHandler("/landing/view/:group/:kind", ({ pathname: { group, kind } }) => {
navigate(catalogURL({
params: {
categoryId: CatalogCategory.getId(group, kind),
group, kind
}
}));
})