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

master-merge conflict fixes:

- proper handling and navigating into catalog's active category via URL-builder

Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman 2021-05-20 18:24:43 +03:00
parent f648597266
commit 01fd4fd3c6
5 changed files with 35 additions and 37 deletions

View File

@ -56,8 +56,13 @@ export abstract class CatalogCategory extends EventEmitter {
}; };
abstract spec: CatalogCategorySpec; abstract spec: CatalogCategorySpec;
// Should be URL-compatible, otherwise "Uncaught TypeError: Expected "categoryId" to match "[^\/#\?]+?""
static getId(group: string, kind: string): string {
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

@ -19,13 +19,14 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
import { reaction, toJS } from "mobx"; import { reaction } from "mobx";
import { broadcastMessage } from "../common/ipc"; import { broadcastMessage } from "../common/ipc";
import type { CatalogEntityRegistry} from "./catalog"; import type { CatalogEntityRegistry } from "./catalog";
import "../common/catalog-entities/kubernetes-cluster"; import "../common/catalog-entities/kubernetes-cluster";
import { toJS } from "../common/utils";
export function pushCatalogToRenderer(catalog: CatalogEntityRegistry) { export function pushCatalogToRenderer(catalog: CatalogEntityRegistry) {
return reaction(() => toJS(catalog.items, { recurseEverything: true }), (items) => { return reaction(() => toJS(catalog.items), (items) => {
broadcastMessage("catalog:items", items); broadcastMessage("catalog:items", items);
}, { }, {
fireImmediately: true, fireImmediately: true,

View File

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

View File

@ -23,12 +23,12 @@ 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, observable, reaction, when } from "mobx"; import { computed, makeObservable, reaction, when } from "mobx";
import { CatalogEntityItem, CatalogEntityStore } from "./catalog-entity.store"; import { CatalogEntityItem, CatalogEntityStore } from "./catalog-entity.store";
import { navigate } from "../../navigation"; import { navigate, navigation } from "../../navigation";
import { kebabCase } from "lodash"; import { kebabCase } from "lodash";
import { PageLayout } from "../layout/page-layout"; import { PageLayout } from "../layout/page-layout";
import { MenuItem, MenuActions } from "../menu"; import { MenuActions, MenuItem } from "../menu";
import { CatalogCategory, CatalogEntityContextMenu, CatalogEntityContextMenuContext, catalogEntityRunContext } from "../../api/catalog-entity"; import { CatalogCategory, CatalogEntityContextMenu, CatalogEntityContextMenuContext, catalogEntityRunContext } from "../../api/catalog-entity";
import { Badge } from "../badge"; import { Badge } from "../badge";
import { HotbarStore } from "../../../common/hotbar-store"; import { HotbarStore } from "../../../common/hotbar-store";
@ -39,7 +39,7 @@ import { catalogCategoryRegistry } from "../../../common/catalog";
import { CatalogAddButton } from "./catalog-add-button"; import { CatalogAddButton } from "./catalog-add-button";
import type { RouteComponentProps } from "react-router"; import type { RouteComponentProps } from "react-router";
import type { ICatalogViewRouteParam } from "./catalog.route"; import type { ICatalogViewRouteParam } from "./catalog.route";
import { Notifications } from "../notifications"; import { catalogURL } from "./catalog.route";
enum sortBy { enum sortBy {
name = "name", name = "name",
@ -64,45 +64,37 @@ export class Catalog extends React.Component<Props> {
navigate: (url: string) => navigate(url), navigate: (url: string) => navigate(url),
}; };
@observable activeCategoryId = this.categories[0]?.getId(); get categoryId(): string | undefined {
return this.props.match.params?.categoryId;
}
@computed get activeCategory(): CatalogCategory { get activeCategory(): CatalogCategory {
return catalogCategoryRegistry.getById(this.activeCategoryId); return catalogCategoryRegistry.getById(this.props.match.params?.categoryId);
} }
@computed get categories(): CatalogCategory[] { @computed get categories(): CatalogCategory[] {
return catalogCategoryRegistry.items; return catalogCategoryRegistry.items;
} }
@computed get items(): CatalogEntityItem[] { @computed get entities(): CatalogEntityItem[] {
return this.catalogEntityStore.getEntities(this.activeCategory); return this.catalogEntityStore.getEntities(this.activeCategory);
} }
get routeActiveTab(): string | undefined {
const { group, kind } = this.props.match.params ?? {};
if (group && kind) {
return `${group}/${kind}`;
}
return undefined;
}
componentDidMount() { componentDidMount() {
disposeOnUnmount(this, [ disposeOnUnmount(this, [
// autofill catalog entities into store // autofill catalog entities into store
reaction(() => this.items, items => this.catalogEntityStore.loadItems(items), { reaction(() => this.entities, items => this.catalogEntityStore.loadItems(items), {
fireImmediately: true, fireImmediately: true,
}), }),
when(() => catalogCategoryRegistry.items.length > 0, () => { // select initial category if nothing yet selected (via url-params)
const item = catalogCategoryRegistry.items.find(i => i.getId() === this.routeActiveTab); when(() => this.categories.length > 0, () => {
const { categoryId } = this;
if (item || this.routeActiveTab === undefined) { if (!categoryId) {
this.activeTab = this.routeActiveTab; navigation.replace(
this.catalogEntityStore.activeCategory = item; catalogURL({ params: { categoryId } })
} else { );
Notifications.error(<p>Unknown category: {this.routeActiveTab}</p>);
} }
}), }),
]); ]);
@ -138,8 +130,8 @@ export class Catalog extends React.Component<Props> {
<Tabs <Tabs
className="flex column" className="flex column"
scrollable={false} scrollable={false}
value={this.activeCategoryId} value={this.categoryId}
onChange={(categoryId: string) => this.activeCategoryId = categoryId} onChange={(categoryId: string) => navigate(catalogURL({ params: { categoryId } }))}
> >
<div className="sidebarHeader">Catalog</div> <div className="sidebarHeader">Catalog</div>
<div className="sidebarTabs"> <div className="sidebarTabs">
@ -177,7 +169,7 @@ export class Catalog extends React.Component<Props> {
</MenuItem> </MenuItem>
)) ))
} }
<MenuItem key="add-to-hotbar" onClick={() => this.addToHotbar(item) }> <MenuItem key="add-to-hotbar" onClick={() => this.addToHotbar(item)}>
Pin to Hotbar Pin to Hotbar
</MenuItem> </MenuItem>
</MenuActions> </MenuActions>

View File

@ -30,6 +30,7 @@ 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
@ -46,7 +47,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: {
group, kind categoryId: CatalogCategory.getId(group, kind),
} }
})); }));
}) })