diff --git a/frontend/src/components/core/SortIcon.tsx b/frontend/src/components/core/SortIcon.tsx new file mode 100644 index 0000000..dcb6378 --- /dev/null +++ b/frontend/src/components/core/SortIcon.tsx @@ -0,0 +1,41 @@ +import { ActionIcon } from "@mantine/core"; +import { Dispatch, SetStateAction } from "react"; +import { TbChevronDown, TbChevronUp, TbSelector } from "react-icons/tb"; + +export type TableSort = { + property?: string; + direction: "asc" | "desc"; +}; + +const TableSortIcon = ({ + sort, + setSort, + property, +}: { + sort: TableSort; + setSort: Dispatch>; + property: string; +}) => { + if (sort.property === property) { + return ( + + setSort({ + property, + direction: sort.direction === "asc" ? "desc" : "asc", + }) + } + > + {sort.direction === "asc" ? : } + + ); + } else { + return ( + setSort({ property, direction: "asc" })}> + + + ); + } +}; + +export default TableSortIcon; diff --git a/frontend/src/components/share/FileList.tsx b/frontend/src/components/share/FileList.tsx index 9918c63..6d7410a 100644 --- a/frontend/src/components/share/FileList.tsx +++ b/frontend/src/components/share/FileList.tsx @@ -9,6 +9,7 @@ import { } from "@mantine/core"; import { useClipboard } from "@mantine/hooks"; import { useModals } from "@mantine/modals"; +import { Dispatch, SetStateAction, useEffect, useState } from "react"; import { TbDownload, TbEye, TbLink } from "react-icons/tb"; import useConfig from "../../hooks/config.hook"; import shareService from "../../services/share.service"; @@ -16,14 +17,17 @@ import { FileMetaData } from "../../types/File.type"; import { Share } from "../../types/share.type"; import { byteToHumanSizeString } from "../../utils/fileSize.util"; import toast from "../../utils/toast.util"; +import TableSortIcon, { TableSort } from "../core/SortIcon"; import showFilePreviewModal from "./modals/showFilePreviewModal"; const FileList = ({ files, + setShare, share, isLoading, }: { files?: FileMetaData[]; + setShare: Dispatch>; share: Share; isLoading: boolean; }) => { @@ -31,6 +35,32 @@ const FileList = ({ const config = useConfig(); const modals = useModals(); + const [sort, setSort] = useState({ + property: undefined, + direction: "desc", + }); + + const sortFiles = () => { + if (files && sort.property) { + const sortedFiles = files.sort((a: any, b: any) => { + if (sort.direction === "asc") { + return b[sort.property!].localeCompare(a[sort.property!], undefined, { + numeric: true, + }); + } else { + return a[sort.property!].localeCompare(b[sort.property!], undefined, { + numeric: true, + }); + } + }); + + setShare({ + ...share, + files: sortedFiles, + }); + } + }; + const copyFileLink = (file: FileMetaData) => { const link = `${config.get("general.appUrl")}/api/shares/${ share.id @@ -51,13 +81,25 @@ const FileList = ({ } }; + useEffect(sortFiles, [sort]); + return ( - - + + diff --git a/frontend/src/pages/share/[shareId]/index.tsx b/frontend/src/pages/share/[shareId]/index.tsx index 4327fcd..6dfea4d 100644 --- a/frontend/src/pages/share/[shareId]/index.tsx +++ b/frontend/src/pages/share/[shareId]/index.tsx @@ -89,7 +89,12 @@ const Share = ({ shareId }: { shareId: string }) => { {share?.files.length > 1 && } - + ); };
NameSize + + Name + + + + + Size + + +