From 7473d5ce5dc1b05068a0b26059c89aeeeb0ee355 Mon Sep 17 00:00:00 2001 From: hexxa Date: Wed, 16 Feb 2022 17:58:11 +0800 Subject: [PATCH] fix(fe/sorting): move sorting function to common --- src/client/web/src/common/utils.ts | 30 ++++++++++++++++++- src/client/web/src/components/layout/rows.tsx | 27 ++--------------- .../web/src/components/state_updater.ts | 13 ++++++-- 3 files changed, 43 insertions(+), 27 deletions(-) diff --git a/src/client/web/src/common/utils.ts b/src/client/web/src/common/utils.ts index f1479f4..cb3a205 100644 --- a/src/client/web/src/common/utils.ts +++ b/src/client/web/src/common/utils.ts @@ -1,4 +1,6 @@ -import { Map } from "immutable"; +import { List, Map } from "immutable"; + +import { Row } from "../components/layout/rows"; export function getItemPath(dirPath: string, itemName: string): string { return dirPath.endsWith("/") @@ -13,3 +15,29 @@ export function getErrMsg( ): string { return `${msgPkg.get(msg)}: ${msgPkg.get(status)}`; } + +export function sortRows( + rows: List, + key: number, + order: boolean +): List { + return rows.sort((row1: Row, row2: Row) => { + const val1 = row1.sortVals.get(key); + const val2 = row2.sortVals.get(key); + + if (val1 == null || val2 == null) { + // elements without the sort key will be moved to the last + if (val1 == null && val2 != null) { + return 1; + } else if (val1 != null && val2 == null) { + return -1; + } + return 0; + } else if (val1 < val2) { + return order ? -1 : 1; + } else if (val1 === val2) { + return 0; + } + return order ? 1 : -1; + }); +} diff --git a/src/client/web/src/components/layout/rows.tsx b/src/client/web/src/components/layout/rows.tsx index 5a7bb44..151b1cc 100644 --- a/src/client/web/src/components/layout/rows.tsx +++ b/src/client/web/src/components/layout/rows.tsx @@ -3,8 +3,8 @@ import { List } from "immutable"; import { BiSortUp } from "@react-icons/all-files/bi/BiSortUp"; - import { Flexbox } from "./flexbox"; +import { sortRows } from "../../common/utils"; export interface Row { elem: React.ReactNode; // element to display @@ -49,25 +49,7 @@ export class Rows extends React.Component { } const expectedOrder = !currentOrder; - const sortedRows = this.props.rows.sort((row1: Row, row2: Row) => { - const val1 = row1.sortVals.get(key); - const val2 = row2.sortVals.get(key); - if (val1 == null || val2 == null) { - // elements without the sort key will be moved to the last - if (val1 == null && val2 != null) { - return 1; - } else if (val1 != null && val2 == null) { - return -1; - } - return 0; - } else if (val1 < val2) { - return expectedOrder ? -1 : 1; - } else if (val1 === val2) { - return 0; - } - return expectedOrder ? 1 : -1; - }); - + const sortedRows = sortRows(this.props.rows, key, expectedOrder); const sortedItems = sortedRows.map((row: Row): Object => { return row.val; }); @@ -104,10 +86,7 @@ export class Rows extends React.Component {
, + , {sortBtns}, ])} childrenStyles={List([{ flex: "0 0 auto" }, { flex: "0 0 auto" }])} diff --git a/src/client/web/src/components/state_updater.ts b/src/client/web/src/components/state_updater.ts index b85dc77..03d5b3e 100644 --- a/src/client/web/src/components/state_updater.ts +++ b/src/client/web/src/components/state_updater.ts @@ -1,7 +1,7 @@ import { List, Map, Set } from "immutable"; import { ICoreState } from "./core_state"; -import { getItemPath } from "../common/utils"; +import { getItemPath, sortRows } from "../common/utils"; import { User, ListUsersResp, @@ -238,7 +238,16 @@ export class Updater { }; refreshFiles = async (): Promise => { - return await this.setItems(this.props.filesInfo.dirPath); + const status = await this.setItems(this.props.filesInfo.dirPath); + if (status !== "") { + return status; + }; + + // TODO: this part is duplicated in the panel_files.tsx + const sortKeys = List([ + this.props.msg.pkg.get("item.type"), + this.props.msg.pkg.get("item.name"), + ]); }; setItems = async (dirParts: List): Promise => {