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

Save sorting order when changing view (#2353)

Signed-off-by: vshakirova <vshakirova@mirantis.com>
This commit is contained in:
Violetta 2021-04-01 18:29:17 +04:00 committed by GitHub
parent 48ea877cd5
commit 33c405bdcf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 44 additions and 11 deletions

View File

@ -134,6 +134,7 @@ export class ClusterIssues extends React.Component<Props> {
<>Warnings: {warnings.length}</> <>Warnings: {warnings.length}</>
</SubHeader> </SubHeader>
<Table <Table
tableId="cluster_issues"
items={warnings} items={warnings}
virtual virtual
selectable selectable

View File

@ -20,7 +20,7 @@ export class PodSecurityPolicies extends React.Component {
return ( return (
<KubeObjectListLayout <KubeObjectListLayout
isConfigurable isConfigurable
tableId="access_roles" tableId="access_pod_security_policies"
className="PodSecurityPolicies" className="PodSecurityPolicies"
isClusterScoped={true} isClusterScoped={true}
store={podSecurityPoliciesStore} store={podSecurityPoliciesStore}

View File

@ -64,6 +64,7 @@ export class VolumeDetailsList extends React.Component<Props> {
<div className="VolumeDetailsList flex column"> <div className="VolumeDetailsList flex column">
<DrawerTitle title="Persistent Volumes"/> <DrawerTitle title="Persistent Volumes"/>
<Table <Table
tableId="storage_volume_details_list"
items={persistentVolumes} items={persistentVolumes}
selectable selectable
virtual={virtual} virtual={virtual}

View File

@ -135,6 +135,7 @@ export class PodDetailsList extends React.Component<Props> {
<div className="PodDetailsList flex column"> <div className="PodDetailsList flex column">
{showTitle && <DrawerTitle title="Pods"/>} {showTitle && <DrawerTitle title="Pods"/>}
<Table <Table
tableId="workloads_pod_details_list"
items={pods} items={pods}
selectable selectable
virtual={virtual} virtual={virtual}

View File

@ -43,6 +43,7 @@ const getTableRow = (toleration: IToleration) => {
export function PodTolerations({ tolerations }: Props) { export function PodTolerations({ tolerations }: Props) {
return ( return (
<Table <Table
tableId="workloads_pod_tolerations"
selectable selectable
scrollable={false} scrollable={false}
sortable={sortingCallbacks} sortable={sortingCallbacks}

View File

@ -413,7 +413,7 @@ export class ItemListLayout extends React.Component<ItemListLayoutProps> {
renderList() { renderList() {
const { const {
store, hasDetailsView, addRemoveButtons = {}, virtual, sortingCallbacks, detailsItem, store, hasDetailsView, addRemoveButtons = {}, virtual, sortingCallbacks, detailsItem,
tableProps = {}, tableProps = {}, tableId
} = this.props; } = this.props;
const { isReady, removeItemsDialog, items } = this; const { isReady, removeItemsDialog, items } = this;
const { selectedItems } = store; const { selectedItems } = store;
@ -426,6 +426,7 @@ export class ItemListLayout extends React.Component<ItemListLayoutProps> {
)} )}
{isReady && ( {isReady && (
<Table <Table
tableId={tableId}
virtual={virtual} virtual={virtual}
selectable={hasDetailsView} selectable={hasDetailsView}
sortable={sortingCallbacks} sortable={sortingCallbacks}

View File

@ -0,0 +1,22 @@
import { createStorage } from "../../utils";
import { TableSortParams } from "./table";
export interface TableStorageModel {
sortParams: {
[tableId: string]: Partial<TableSortParams>;
}
}
export const tableStorage = createStorage<TableStorageModel>("table_settings", {
sortParams: {}
});
export function getSortParams(tableId: string): Partial<TableSortParams> {
return tableStorage.get().sortParams[tableId];
}
export function setSortParams(tableId: string, sortParams: Partial<TableSortParams>) {
tableStorage.merge(draft => {
draft.sortParams[tableId] = sortParams;
});
}

View File

@ -3,7 +3,6 @@ import "./table.scss";
import React from "react"; import React from "react";
import { orderBy } from "lodash"; import { orderBy } from "lodash";
import { observer } from "mobx-react"; import { observer } from "mobx-react";
import { observable } from "mobx";
import { autobind, cssNames, noop } from "../../utils"; import { autobind, cssNames, noop } from "../../utils";
import { TableRow, TableRowElem, TableRowProps } from "./table-row"; import { TableRow, TableRowElem, TableRowProps } from "./table-row";
import { TableHead, TableHeadElem, TableHeadProps } from "./table-head"; import { TableHead, TableHeadElem, TableHeadProps } from "./table-head";
@ -11,6 +10,8 @@ import { TableCellElem } from "./table-cell";
import { VirtualList } from "../virtual-list"; import { VirtualList } from "../virtual-list";
import { createPageParam } from "../../navigation"; import { createPageParam } from "../../navigation";
import { ItemObject } from "../../item.store"; import { ItemObject } from "../../item.store";
import { getSortParams, setSortParams } from "./table.storage";
import { computed } from "mobx";
export type TableSortBy = string; export type TableSortBy = string;
export type TableOrderBy = "asc" | "desc" | string; export type TableOrderBy = "asc" | "desc" | string;
@ -19,6 +20,7 @@ export type TableSortCallback<D = any> = (data: D) => string | number | (string
export type TableSortCallbacks = { [columnId: string]: TableSortCallback }; export type TableSortCallbacks = { [columnId: string]: TableSortCallback };
export interface TableProps extends React.DOMAttributes<HTMLDivElement> { export interface TableProps extends React.DOMAttributes<HTMLDivElement> {
tableId?: string;
items?: ItemObject[]; // Raw items data items?: ItemObject[]; // Raw items data
className?: string; className?: string;
autoSize?: boolean; // Setup auto-sizing for all columns (flex: 1 0) autoSize?: boolean; // Setup auto-sizing for all columns (flex: 1 0)
@ -62,13 +64,17 @@ export class Table extends React.Component<TableProps> {
sortSyncWithUrl: true, sortSyncWithUrl: true,
}; };
@observable sortParams: Partial<TableSortParams> = Object.assign( componentDidMount() {
this.props.sortSyncWithUrl ? { const { sortable, tableId } = this.props;
sortBy: sortByUrlParam.get(),
orderBy: orderByUrlParam.get(), if (sortable && !tableId) {
} : {}, console.error("[Table]: sorted table requires props.tableId to be specified");
this.props.sortByDefault, }
); }
@computed get sortParams() {
return Object.assign({}, this.props.sortByDefault, getSortParams(this.props.tableId));
}
renderHead() { renderHead() {
const { sortable, children } = this.props; const { sortable, children } = this.props;
@ -113,7 +119,7 @@ export class Table extends React.Component<TableProps> {
@autobind() @autobind()
protected onSort({ sortBy, orderBy }: TableSortParams) { protected onSort({ sortBy, orderBy }: TableSortParams) {
this.sortParams = { sortBy, orderBy }; setSortParams(this.props.tableId, { sortBy, orderBy });
const { sortSyncWithUrl, onSort } = this.props; const { sortSyncWithUrl, onSort } = this.props;
if (sortSyncWithUrl) { if (sortSyncWithUrl) {