From 0a5b8b37dbbf8ae3cd284dd349f751c9c34c95ae Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Tue, 15 Dec 2020 14:41:24 -0500 Subject: [PATCH] add more typing to sorting of tables Signed-off-by: Sebastian Malton --- src/common/utils/has-key.ts | 3 ++ src/common/utils/index.ts | 1 + src/renderer/components/table/table.tsx | 68 ++++++++++++++++++------- 3 files changed, 54 insertions(+), 18 deletions(-) create mode 100644 src/common/utils/has-key.ts diff --git a/src/common/utils/has-key.ts b/src/common/utils/has-key.ts new file mode 100644 index 0000000000..504afb3303 --- /dev/null +++ b/src/common/utils/has-key.ts @@ -0,0 +1,3 @@ +export function hasKey(obj: Record, testKey: string): testKey is K { + return obj.hasOwnProperty(testKey); +} diff --git a/src/common/utils/index.ts b/src/common/utils/index.ts index b1006b5f58..61e43af347 100644 --- a/src/common/utils/index.ts +++ b/src/common/utils/index.ts @@ -17,4 +17,5 @@ export * from "./openExternal"; export * from "./rectify-array"; export * from "./downloadFile"; export * from "./escapeRegExp"; +export * from "./has-key"; export * from "./tar"; diff --git a/src/renderer/components/table/table.tsx b/src/renderer/components/table/table.tsx index 2a4b191e19..f4ce66dd31 100644 --- a/src/renderer/components/table/table.tsx +++ b/src/renderer/components/table/table.tsx @@ -3,7 +3,7 @@ import "./table.scss"; import React from "react"; import { observer } from "mobx-react"; import { computed, observable } from "mobx"; -import { autobind, cssNames, noop } from "../../utils"; +import { autobind, cssNames, hasKey, noop } from "../../utils"; import { TableRow, TableRowElem, TableRowProps } from "./table-row"; import { TableHead, TableHeadElem, TableHeadProps } from "./table-head"; import { TableCellElem } from "./table-cell"; @@ -14,7 +14,10 @@ import { ItemObject } from "../../item.store"; // todo: refactor + decouple search from location -export type TableOrderBy = "asc" | "desc" | string; +export enum TableOrderBy { + ASCENDING = "asc", + DECENDING = "desc", +} export type TableSortParams = { sortBy: SortingOption; orderBy: TableOrderBy }; export type TableSortCallback = (data: D) => string | number | (string | number)[]; @@ -85,12 +88,42 @@ export interface TableProps number; + + /** + * A function for generating a special number for a specific item + */ + customRowHeights?: (item: any, lineHeight: number, paddings: number) => number; + + /** + * + */ getTableRow?: (uid: string) => React.ReactElement; } +function validateOrderBy(source: string | null, defaultDirection = TableOrderBy.DECENDING): TableOrderBy { + const transformed = source?.toLowerCase(); + + if (transformed === TableOrderBy.ASCENDING || transformed === TableOrderBy.DECENDING) { + return transformed; + } + + return defaultDirection; +} + +const swapOrderBy: Record = { + [TableOrderBy.ASCENDING]: TableOrderBy.DECENDING, + [TableOrderBy.DECENDING]: TableOrderBy.ASCENDING, +}; + @observer export class Table extends React.Component> { static defaultProps: TableProps = { @@ -104,11 +137,15 @@ export class Table> { - if (this.props.sortSyncWithUrl) { - const sortBy = navigation.searchParams.get("sortBy") as SortingOption; - const orderBy = navigation.searchParams.get("orderBy"); + const { sortSyncWithUrl, sortable } = this.props; - return { sortBy, orderBy }; + if (sortSyncWithUrl) { + const sortBy = navigation.searchParams.get("sortBy"); + const orderBy = validateOrderBy(navigation.searchParams.get("orderBy")); + + if (sortable && hasKey(sortable, sortBy)) { + return { sortBy, orderBy }; + } } return this.sortParamsLocal || {}; @@ -155,7 +192,7 @@ export class Table