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

Moving slow parts to <RenderDelay/>

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
Alex Andreev 2021-07-19 10:22:23 +03:00
parent 2c86ac01ed
commit 1814b6ec86
5 changed files with 153 additions and 107 deletions

View File

@ -46,6 +46,11 @@
border-radius: 2px; border-radius: 2px;
} }
.content:active {
color: white;
background-color: var(--blue);
}
.group { .group {
margin-left: 0px; margin-left: 0px;
} }

View File

@ -42,6 +42,8 @@ import { CatalogEntityDetails } from "./catalog-entity-details";
import { catalogURL, CatalogViewRouteParam } from "../../../common/routes"; import { catalogURL, CatalogViewRouteParam } from "../../../common/routes";
import { CatalogMenu } from "./catalog-menu"; import { CatalogMenu } from "./catalog-menu";
import { HotbarIcon } from "../hotbar/hotbar-icon"; import { HotbarIcon } from "../hotbar/hotbar-icon";
import { RenderDelay } from "../render-delay/render-delay";
import { CatalogTopbar } from "../cluster-manager/catalog-topbar";
export const previousActiveTab = createAppStorage("catalog-previous-active-tab", ""); export const previousActiveTab = createAppStorage("catalog-previous-active-tab", "");
@ -144,7 +146,11 @@ export class Catalog extends React.Component<Props> {
}; };
renderNavigation() { renderNavigation() {
return <CatalogMenu activeItem={this.activeTab} onItemClick={this.onTabChange}/>; return (
<RenderDelay>
<CatalogMenu activeItem={this.activeTab} onItemClick={this.onTabChange}/>
</RenderDelay>
);
} }
renderItemMenu = (item: CatalogEntityItem<CatalogEntity>) => { renderItemMenu = (item: CatalogEntityItem<CatalogEntity>) => {
@ -172,6 +178,7 @@ export class Catalog extends React.Component<Props> {
renderIcon(item: CatalogEntityItem<CatalogEntity>) { renderIcon(item: CatalogEntityItem<CatalogEntity>) {
return ( return (
<RenderDelay>
<HotbarIcon <HotbarIcon
uid={`catalog-icon-${item.getId()}`} uid={`catalog-icon-${item.getId()}`}
title={item.getName()} title={item.getName()}
@ -181,60 +188,27 @@ export class Catalog extends React.Component<Props> {
background={item.entity.spec.icon?.background} background={item.entity.spec.icon?.background}
size={24} size={24}
/> />
</RenderDelay>
); );
} }
renderSingleCategoryList() { renderList() {
const { activeCategory } = this.catalogEntityStore;
const tableId = activeCategory ? `catalog-items-${activeCategory?.metadata.name.replace(" ", "")}` : "catalog-items";
if (this.activeTab === undefined) {
return null;
}
return ( return (
<ItemListLayout <ItemListLayout
key={this.catalogEntityStore.activeCategory.getId()} tableId={tableId}
tableId={`catalog-items-${this.catalogEntityStore.activeCategory?.metadata.name.replace(" ", "")}`} renderHeaderTitle={activeCategory?.metadata.name || "Browse All"}
renderHeaderTitle={this.catalogEntityStore.activeCategory?.metadata.name}
isSelectable={false} isSelectable={false}
isConfigurable={true} isConfigurable={true}
className="CatalogItemList" className="CatalogItemList"
store={this.catalogEntityStore} store={this.catalogEntityStore}
sortingCallbacks={{
[sortBy.name]: (item: CatalogEntityItem<CatalogEntity>) => item.name,
[sortBy.source]: (item: CatalogEntityItem<CatalogEntity>) => item.source,
[sortBy.status]: (item: CatalogEntityItem<CatalogEntity>) => item.phase,
}}
searchFilters={[
(entity: CatalogEntityItem<CatalogEntity>) => entity.searchFields,
]}
renderTableHeader={[
{ title: "", className: css.iconCell, id: "icon" },
{ title: "Name", className: css.nameCell, sortBy: sortBy.name, id: "name" },
{ title: "Source", className: css.sourceCell, sortBy: sortBy.source, id: "source" },
{ title: "Labels", className: css.labelsCell, id: "labels" },
{ title: "Status", className: css.statusCell, sortBy: sortBy.status, id: "status" },
]}
customizeTableRowProps={(item: CatalogEntityItem<CatalogEntity>) => ({
disabled: !item.enabled,
})}
renderTableContents={(item: CatalogEntityItem<CatalogEntity>) => [
this.renderIcon(item),
item.name,
item.source,
item.getLabelBadges(),
{ title: item.phase, className: cssNames(css[item.phase]) }
]}
onDetails={this.onDetails}
renderItemMenu={this.renderItemMenu}
/>
);
}
renderAllCategoriesList() {
return (
<ItemListLayout
key="all"
renderHeaderTitle={"Browse All"}
isSelectable={false}
isConfigurable={true}
className="CatalogItemList"
store={this.catalogEntityStore}
tableId="catalog-items"
sortingCallbacks={{ sortingCallbacks={{
[sortBy.name]: (item: CatalogEntityItem<CatalogEntity>) => item.name, [sortBy.name]: (item: CatalogEntityItem<CatalogEntity>) => item.name,
[sortBy.kind]: (item: CatalogEntityItem<CatalogEntity>) => item.kind, [sortBy.kind]: (item: CatalogEntityItem<CatalogEntity>) => item.kind,
@ -247,46 +221,39 @@ export class Catalog extends React.Component<Props> {
renderTableHeader={[ renderTableHeader={[
{ title: "", className: css.iconCell, id: "icon" }, { title: "", className: css.iconCell, id: "icon" },
{ title: "Name", className: css.nameCell, sortBy: sortBy.name, id: "name" }, { title: "Name", className: css.nameCell, sortBy: sortBy.name, id: "name" },
{ title: "Kind", className: css.kindCell, sortBy: sortBy.kind, id: "kind" }, !activeCategory && { title: "Kind", className: css.kindCell, sortBy: sortBy.kind, id: "kind" },
{ title: "Source", className: css.sourceCell, sortBy: sortBy.source, id: "source" }, { title: "Source", className: css.sourceCell, sortBy: sortBy.source, id: "source" },
{ title: "Labels", className: css.labelsCell, id: "labels" }, { title: "Labels", className: css.labelsCell, id: "labels" },
{ title: "Status", className: css.statusCell, sortBy: sortBy.status, id: "status" }, { title: "Status", className: css.statusCell, sortBy: sortBy.status, id: "status" },
]} ].filter(Boolean)}
customizeTableRowProps={(item: CatalogEntityItem<CatalogEntity>) => ({ customizeTableRowProps={(item: CatalogEntityItem<CatalogEntity>) => ({
disabled: !item.enabled, disabled: !item.enabled,
})} })}
renderTableContents={(item: CatalogEntityItem<CatalogEntity>) => [ renderTableContents={(item: CatalogEntityItem<CatalogEntity>) => [
this.renderIcon(item), this.renderIcon(item),
item.name, item.name,
item.kind, !activeCategory && item.kind,
item.source, item.source,
item.getLabelBadges(), item.getLabelBadges(),
{ title: item.phase, className: cssNames(css[item.phase]) } { title: item.phase, className: cssNames(css[item.phase]) }
]} ].filter(Boolean)}
detailsItem={this.catalogEntityStore.selectedItem}
onDetails={this.onDetails} onDetails={this.onDetails}
renderItemMenu={this.renderItemMenu} renderItemMenu={this.renderItemMenu}
/> />
); );
} }
renderCategoryList() {
if (this.activeTab === undefined) {
return null;
}
return this.catalogEntityStore.activeCategory ? this.renderSingleCategoryList() : this.renderAllCategoriesList();
}
render() { render() {
if (!this.catalogEntityStore) { if (!this.catalogEntityStore) {
return null; return null;
} }
return ( return (
<>
<CatalogTopbar/>
<MainLayout sidebar={this.renderNavigation()}> <MainLayout sidebar={this.renderNavigation()}>
<div className="p-6 h-full"> <div className="p-6 h-full">
{ this.renderCategoryList() } { this.renderList() }
</div> </div>
{ {
this.catalogEntityStore.selectedItem this.catalogEntityStore.selectedItem
@ -294,11 +261,16 @@ export class Catalog extends React.Component<Props> {
item={this.catalogEntityStore.selectedItem} item={this.catalogEntityStore.selectedItem}
hideDetails={() => this.catalogEntityStore.selectedItemId = null} hideDetails={() => this.catalogEntityStore.selectedItemId = null}
/> />
: <CatalogAddButton : (
<RenderDelay>
<CatalogAddButton
category={this.catalogEntityStore.activeCategory} category={this.catalogEntityStore.activeCategory}
/> />
</RenderDelay>
)
} }
</MainLayout> </MainLayout>
</>
); );
} }
} }

View File

@ -30,6 +30,7 @@ import { Icon, IconProps } from "../icon";
import { Menu, MenuItem, MenuProps } from "../menu"; import { Menu, MenuItem, MenuProps } from "../menu";
import uniqueId from "lodash/uniqueId"; import uniqueId from "lodash/uniqueId";
import isString from "lodash/isString"; import isString from "lodash/isString";
import { RenderDelay } from "../render-delay/render-delay";
export interface MenuActionsProps extends Partial<MenuProps> { export interface MenuActionsProps extends Partial<MenuProps> {
className?: string; className?: string;
@ -124,6 +125,7 @@ export class MenuActions extends React.Component<MenuActionsProps> {
return ( return (
<> <>
{this.renderTriggerIcon()} {this.renderTriggerIcon()}
<RenderDelay>
<Menu <Menu
htmlFor={this.id} htmlFor={this.id}
isOpen={this.isOpen} open={this.toggle} close={this.toggle} isOpen={this.isOpen} open={this.toggle} close={this.toggle}
@ -148,6 +150,7 @@ export class MenuActions extends React.Component<MenuActionsProps> {
</MenuItem> </MenuItem>
)} )}
</Menu> </Menu>
</RenderDelay>
</> </>
); );
} }

View File

@ -0,0 +1,61 @@
/**
* Copyright (c) 2021 OpenLens Authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import React from "react";
import { makeObservable, observable } from "mobx";
import { observer } from "mobx-react";
import { boundMethod } from "../../utils";
interface Props {
placeholder?: React.ReactNode;
children: unknown;
}
@observer
export class RenderDelay extends React.Component<Props> {
@observable isVisible = false;
constructor(props: Props) {
super(props);
makeObservable(this);
}
componentDidMount() {
window.requestIdleCallback(this.showContents);
}
componentWillUnmount() {
window.cancelIdleCallback(this.showContents);
}
@boundMethod
showContents() {
this.isVisible = true;
}
render() {
if (!this.isVisible) {
return this.props.placeholder || null;
}
return this.props.children;
}
}

5
types/dom.d.ts vendored
View File

@ -24,4 +24,9 @@ declare global {
interface Element { interface Element {
scrollIntoViewIfNeeded(opt_center?: boolean): void; scrollIntoViewIfNeeded(opt_center?: boolean): void;
} }
interface Window {
requestIdleCallback(callback: () => void);
cancelIdleCallback(callback: () => void);
}
} }