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); 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 { getForGroupKind<T extends CatalogCategory>(group: string, kind: string): T | undefined {
return this.groupKinds.get(group)?.get(kind) as T; return this.groupKinds.get(group)?.get(kind) as T;
} }

View File

@ -56,13 +56,14 @@ export abstract class CatalogCategory extends EventEmitter {
}; };
abstract spec: CatalogCategorySpec; abstract spec: CatalogCategorySpec;
// Should be URL-compatible, otherwise "Uncaught TypeError: Expected "categoryId" to match "[^\/#\?]+?"" static parseId(id = ""): { group?: string, kind?: string } {
static getId(group: string, kind: string): string { const [group, kind] = id.split("/") ?? [];
return `${group}--${kind}`;
return { group, kind };
} }
public getId(): string { 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"; import { buildURL } from "../../../common/utils/buildUrl";
export interface ICatalogViewRouteParam { export interface ICatalogViewRouteParam {
categoryId?: string; group?: string;
kind?: string;
} }
export const catalogRoute: RouteProps = { export const catalogRoute: RouteProps = {
path: "/catalog/:categoryId?" // `categoryId` == `${group}--${kind}` path: "/catalog/:group?/:kind?"
}; };
export const catalogURL = buildURL<ICatalogViewRouteParam>(catalogRoute.path); export const catalogURL = buildURL<ICatalogViewRouteParam>(catalogRoute.path);

View File

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

View File

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