TagSpaces is an offline, open source, document manager with tagging support
fix sorting
sytolk10cafdab5be4
diff --git a/app/perspectives/grid-perspective/components/GridSettingsDialog.tsx b/app/perspectives/grid-perspective/components/GridSettingsDialog.tsxindex 5fa9164e0..e854f77f5 100644--- a/app/perspectives/grid-perspective/components/GridSettingsDialog.tsx+++ b/app/perspectives/grid-perspective/components/GridSettingsDialog.tsx@@ -45,6 +45,7 @@ import DialogCloseButton from '-/components/dialogs/DialogCloseButton';import { Pro } from '-/pro';import { useTheme } from '@mui/material/styles';import { useTranslation } from 'react-i18next';+import { useSortedDirContext } from '-/perspectives/grid-perspective/hooks/useSortedDirContext';/*const styles: any = {root: {@@ -85,8 +86,6 @@ interface Props {changeSingleClickAction: (actionType: string) => void;singleClickAction: string;openHelpWebPage: () => void;- sortBy: string;- orderBy: boolean;handleSortingMenu: (event) => void;isLocal: boolean;resetLocalSettings: () => void;@@ -95,7 +94,8 @@ interface Props {function GridSettingsDialog(props: Props) {const { t } = useTranslation();- const [ignored, forceUpdate] = useReducer(x => x + 1, 0);+ const { sortBy, orderBy } = useSortedDirContext();+ const [ignored, forceUpdate] = useReducer((x: number) => x + 1, 0, undefined);const thumbnailMode = useRef<string>(props.thumbnailMode);const entrySize = useRef<string>(props.entrySize);const singleClickAction = useRef<string>(props.singleClickAction);@@ -271,9 +271,9 @@ function GridSettingsDialog(props: Props) {primary={t('core:sort') +': ' +- t(props.sortBy) ++ t(sortBy) +' ' +- (props.orderBy ? 'ASC' : 'DESC')+ (orderBy ? 'ASC' : 'DESC')}/></MenuItem>diff --git a/app/perspectives/grid-perspective/components/MainContainer.tsx b/app/perspectives/grid-perspective/components/MainContainer.tsxindex 59e6376a2..753a2e90a 100644--- a/app/perspectives/grid-perspective/components/MainContainer.tsx+++ b/app/perspectives/grid-perspective/components/MainContainer.tsx@@ -81,7 +81,13 @@ function GridPerspective(props: Props) {const dispatch: AppDispatch = useDispatch();- const { sortedDirContent } = useSortedDirContext();+ const {+ sortedDirContent,+ sortBy,+ orderBy,+ setSortBy,+ setOrderBy+ } = useSortedDirContext();const directoryMeta: TS.FileSystemEntryMeta = useSelector(getDirectoryMeta);const readOnlyMode = useSelector(isReadOnlyMode);const lastSearchTimestamp = useSelector(getLastSearchTimestamp);@@ -156,14 +162,14 @@ function GridPerspective(props: Props) {const [isAddTagDialogOpened, setIsAddTagDialogOpened] = useState<TS.Tag>(undefined);- const sortBy = useRef<string>(+ /*const sortBy = useRef<string>(settings && settings.sortBy ? settings.sortBy : defaultSettings.sortBy);const orderBy = useRef<null | boolean>(settings && typeof settings.orderBy !== 'undefined'? settings.orderBy: defaultSettings.orderBy- );+ );*/const layoutType = useRef<string>(settings && settings.layoutType? settings.layoutType@@ -239,90 +245,6 @@ function GridPerspective(props: Props) {}}, [selectedEntries]);- /*useEffect(() => {- if (!firstRender) {- sortedDirContent.current = sortByCriteria(- searchFilter- ? directoryContent.filter(entry =>- entry.name.toLowerCase().includes(searchFilter.toLowerCase())- )- : directoryContent,- sortBy.current,- orderBy.current- );- forceUpdate();- }- }, [searchFilter]);*/-- /*useEffect(() => {- if (!firstRender) {- sortedDirContent.current = getSortedDirContent();- forceUpdate();- }- }, [- directoryContent, // open subdirs todo rethink this (replace with useEffect for currDirPath changes only)- // currentDirectoryPath,- sortBy.current,- orderBy.current- ]);*/-- /*useEffect(() => {- if (!firstRender) {- if (lastSearchTimestamp) {- sortBy.current = 'byRelevance';- // orderBy.current = false;- } else {- sortBy.current =- settings && settings.sortBy- ? settings.sortBy- : defaultSettings.sortBy;- orderBy.current =- settings && typeof settings.orderBy !== 'undefined'- ? settings.orderBy- : defaultSettings.orderBy;- }- sortedDirContent.current = getSortedDirContent();- forceUpdate();- }- }, [lastSearchTimestamp]);*/-- /*function getSortedDirContent() {- if (!lastSearchTimestamp) {- // not in search mode- return sortByCriteria(directoryContent, sortBy.current, orderBy.current);- } else {- if (sortBy.current === 'byRelevance') {- // initial search results is sorted by relevance- if (orderBy.current) {- return GlobalSearch.getInstance().getResults();- } else {- return [...GlobalSearch.getInstance().getResults()].reverse();- }- } else {- return sortByCriteria(- searchFilter- ? GlobalSearch.getInstance()- .getResults()- .filter(entry =>- entry.name.toLowerCase().includes(searchFilter.toLowerCase())- )- : GlobalSearch.getInstance().getResults(),- sortBy.current,- orderBy.current- );- }- }- //forceUpdate();- }*/-- /*useEffect(() => {- // HANDLE (ADD/REMOVE sidecar TAGS) IN SEARCH RESULTS- if (!firstRender && lastSearchTimestamp) {- sortedDirContent.current = GlobalSearch.getInstance().getResults();- forceUpdate();- }- }, [editedEntryPaths]);*/-useEffect(() => {if (!firstRender) {const perspectiveSettings = getSettings(directoryMeta);@@ -348,14 +270,16 @@ function GridPerspective(props: Props) {? perspectiveSettings.showTags: defaultSettings.showTags;layoutType.current = defaultSettings.layoutType;- orderBy.current =+ setOrderBy(perspectiveSettings && perspectiveSettings.orderBy !== undefined? perspectiveSettings.orderBy- : defaultSettings.orderBy;- sortBy.current =+ : defaultSettings.orderBy+ );+ setSortBy(perspectiveSettings && perspectiveSettings.sortBy !== undefined? perspectiveSettings.sortBy- : defaultSettings.sortBy;+ : defaultSettings.sortBy+ );singleClickAction.current =perspectiveSettings &&perspectiveSettings.singleClickAction !== undefined@@ -386,8 +310,8 @@ function GridPerspective(props: Props) {showDetails: showDetails.current,showTags: showTags.current,layoutType: layoutType.current,- orderBy: orderBy.current,- sortBy: sortBy.current,+ orderBy: orderBy,+ sortBy: sortBy,singleClickAction: singleClickAction.current,entrySize: entrySize.current,thumbnailMode: thumbnailMode.current,@@ -418,8 +342,8 @@ function GridPerspective(props: Props) {showDetails.current,showTags.current,layoutType.current,- orderBy.current,- sortBy.current,+ orderBy,+ sortBy,singleClickAction.current,entrySize.current,thumbnailMode.current,@@ -462,10 +386,10 @@ function GridPerspective(props: Props) {};const handleSortBy = handleSort => {- if (sortBy.current !== handleSort) {- sortBy.current = handleSort;+ if (sortBy !== handleSort) {+ setSortBy(handleSort);} else {- orderBy.current = !orderBy.current;+ setOrderBy(!orderBy);}// forceUpdate();setSortingContextMenuAnchorEl(null);@@ -829,8 +753,6 @@ function GridPerspective(props: Props) {changeSingleClickAction={changeSingleClickAction}singleClickAction={singleClickAction.current}openHelpWebPage={openHelpWebPage}- sortBy={sortBy.current}- orderBy={orderBy.current}handleSortingMenu={handleSortingMenu}isLocal={isLocal}resetLocalSettings={() => {@@ -904,8 +826,6 @@ function GridPerspective(props: Props) {open={Boolean(sortingContextMenuAnchorEl)}onClose={() => setSortingContextMenuAnchorEl(null)}anchorEl={sortingContextMenuAnchorEl}- sortBy={sortBy.current}- orderBy={orderBy.current}handleSortBy={handleSortBy}searchModeEnabled={lastSearchTimestamp !== undefined}/>diff --git a/app/perspectives/grid-perspective/components/SortingMenu.tsx b/app/perspectives/grid-perspective/components/SortingMenu.tsxindex c88fa70bd..c270e22df 100644--- a/app/perspectives/grid-perspective/components/SortingMenu.tsx+++ b/app/perspectives/grid-perspective/components/SortingMenu.tsx@@ -24,29 +24,21 @@ import MenuItem from '@mui/material/MenuItem';import ArrowUpIcon from '@mui/icons-material/KeyboardArrowUp';import ArrowDownIcon from '@mui/icons-material/KeyboardArrowDown';import { useTranslation } from 'react-i18next';+import { useSortedDirContext } from '-/perspectives/grid-perspective/hooks/useSortedDirContext';interface Props {open: boolean;onClose: () => void;anchorEl: Element;handleSortBy: (sortType: string) => void;- sortBy: string;- orderBy: null | boolean;searchModeEnabled: boolean;}function SortingMenu(props: Props) {- const {- open,- onClose,- sortBy,- orderBy,- handleSortBy,- anchorEl,- searchModeEnabled- } = props;+ const { open, onClose, handleSortBy, anchorEl, searchModeEnabled } = props;const { t } = useTranslation();+ const { sortBy, orderBy } = useSortedDirContext();return (<Menu anchorEl={anchorEl} open={open} onClose={onClose}>{/* <ListSubHeader>Sort by</ListSubHeader> */}diff --git a/app/perspectives/grid-perspective/hooks/SortedDirContextProvider.tsx b/app/perspectives/grid-perspective/hooks/SortedDirContextProvider.tsxindex 0bd0af889..39ee1b7e6 100644--- a/app/perspectives/grid-perspective/hooks/SortedDirContextProvider.tsx+++ b/app/perspectives/grid-perspective/hooks/SortedDirContextProvider.tsx@@ -16,7 +16,7 @@**/-import React, { createContext, useMemo } from 'react';+import React, { createContext, useMemo, useState } from 'react';import { useSelector } from 'react-redux';import {getDirectoryContent,@@ -34,10 +34,18 @@ import { defaultSettings } from '-/perspectives/grid-perspective';type SortedDirContextData = {sortedDirContent: Array<TS.FileSystemEntry>;+ sortBy: string;+ orderBy: null | boolean;+ setSortBy: (sort: string) => void;+ setOrderBy: (isAsc: null | boolean) => void;};export const SortedDirContext = createContext<SortedDirContextData>({- sortedDirContent: undefined+ sortedDirContent: undefined,+ sortBy: defaultSettings.sortBy,+ orderBy: defaultSettings.orderBy,+ setSortBy: () => {},+ setOrderBy: () => {}});export type SortedDirContextProviderProps = {@@ -56,6 +64,15 @@ export const SortedDirContextProvider = ({const editedEntryPaths: Array<TS.EditedEntryPath> = useSelector(getEditedEntryPaths);+ const settings = getSettings();+ const [sortBy, setSortBy] = useState<string>(+ settings && settings.sortBy ? settings.sortBy : defaultSettings.sortBy+ );+ const [orderBy, setOrderBy] = useState<null | boolean>(+ settings && typeof settings.orderBy !== 'undefined'+ ? settings.orderBy+ : defaultSettings.orderBy+ );function getSettings(): TS.FolderSettings {if (@@ -72,13 +89,6 @@ export const SortedDirContextProvider = ({}function getSortedDirContent() {- const settings = getSettings();- const sortBy =- settings && settings.sortBy ? settings.sortBy : defaultSettings.sortBy;- const orderBy =- settings && typeof settings.orderBy !== 'undefined'- ? settings.orderBy- : defaultSettings.orderBy;if (!lastSearchTimestamp) {// not in search modereturn sortByCriteria(directoryContent, sortBy, orderBy);@@ -107,18 +117,21 @@ export const SortedDirContextProvider = ({}const context = useMemo(() => {- const sortedDirContent = !lastSearchTimestamp- ? getSortedDirContent()- : GlobalSearch.getInstance().getResults();return {- sortedDirContent+ sortedDirContent: getSortedDirContent(),+ sortBy,+ orderBy,+ setSortBy,+ setOrderBy};}, [directoryContent,lastSearchTimestamp,directoryMeta,searchFilter,- editedEntryPaths+ editedEntryPaths,+ sortBy,+ orderBy]);return (diff --git a/app/perspectives/list/components/MainContainer.tsx b/app/perspectives/list/components/MainContainer.tsxindex 2add279c5..2bfef8ce0 100644--- a/app/perspectives/list/components/MainContainer.tsx+++ b/app/perspectives/list/components/MainContainer.tsx@@ -81,7 +81,13 @@ function ListPerspective(props: Props) {const dispatch: AppDispatch = useDispatch();- const { sortedDirContent } = useSortedDirContext();+ const {+ sortedDirContent,+ sortBy,+ orderBy,+ setSortBy,+ setOrderBy+ } = useSortedDirContext();const directoryMeta: TS.FileSystemEntryMeta = useSelector(getDirectoryMeta);const readOnlyMode = useSelector(isReadOnlyMode);const lastSearchTimestamp = useSelector(getLastSearchTimestamp);@@ -156,14 +162,14 @@ function ListPerspective(props: Props) {const [isAddTagDialogOpened, setIsAddTagDialogOpened] = useState<TS.Tag>(undefined);- const sortBy = useRef<string>(+ /*const sortBy = useRef<string>(settings && settings.sortBy ? settings.sortBy : defaultSettings.sortBy);const orderBy = useRef<null | boolean>(settings && typeof settings.orderBy !== 'undefined'? settings.orderBy: defaultSettings.orderBy- );+ );*/const layoutType = useRef<string>(settings && settings.layoutType? settings.layoutType@@ -239,90 +245,6 @@ function ListPerspective(props: Props) {}}, [selectedEntries]);- /*useEffect(() => {- if (!firstRender) {- sortedDirContent.current = sortByCriteria(- searchFilter- ? directoryContent.filter(entry =>- entry.name.toLowerCase().includes(searchFilter.toLowerCase())- )- : directoryContent,- sortBy.current,- orderBy.current- );- forceUpdate();- }- }, [searchFilter]);*/-- /*useEffect(() => {- if (!firstRender) {- sortedDirContent.current = getSortedDirContent();- forceUpdate();- }- }, [- directoryContent, // open subdirs todo rethink this (replace with useEffect for currDirPath changes only)- // currentDirectoryPath,- sortBy.current,- orderBy.current- ]);*/-- /*useEffect(() => {- if (!firstRender) {- if (lastSearchTimestamp) {- sortBy.current = 'byRelevance';- // orderBy.current = false;- } else {- sortBy.current =- settings && settings.sortBy- ? settings.sortBy- : defaultSettings.sortBy;- orderBy.current =- settings && typeof settings.orderBy !== 'undefined'- ? settings.orderBy- : defaultSettings.orderBy;- }- sortedDirContent.current = getSortedDirContent();- forceUpdate();- }- }, [lastSearchTimestamp]);*/-- /*function getSortedDirContent() {- if (!lastSearchTimestamp) {- // not in search mode- return sortByCriteria(directoryContent, sortBy.current, orderBy.current);- } else {- if (sortBy.current === 'byRelevance') {- // initial search results is sorted by relevance- if (orderBy.current) {- return GlobalSearch.getInstance().getResults();- } else {- return [...GlobalSearch.getInstance().getResults()].reverse();- }- } else {- return sortByCriteria(- searchFilter- ? GlobalSearch.getInstance()- .getResults()- .filter(entry =>- entry.name.toLowerCase().includes(searchFilter.toLowerCase())- )- : GlobalSearch.getInstance().getResults(),- sortBy.current,- orderBy.current- );- }- }- //forceUpdate();- }*/-- /*useEffect(() => {- // HANDLE (ADD/REMOVE sidecar TAGS) IN SEARCH RESULTS- if (!firstRender && lastSearchTimestamp) {- sortedDirContent.current = GlobalSearch.getInstance().getResults();- forceUpdate();- }- }, [editedEntryPaths]);*/-useEffect(() => {if (!firstRender) {const perspectiveSettings = getSettings(directoryMeta);@@ -348,14 +270,16 @@ function ListPerspective(props: Props) {? perspectiveSettings.showTags: defaultSettings.showTags;layoutType.current = defaultSettings.layoutType;- orderBy.current =+ setOrderBy(perspectiveSettings && perspectiveSettings.orderBy !== undefined? perspectiveSettings.orderBy- : defaultSettings.orderBy;- sortBy.current =+ : defaultSettings.orderBy+ );+ setSortBy(perspectiveSettings && perspectiveSettings.sortBy !== undefined? perspectiveSettings.sortBy- : defaultSettings.sortBy;+ : defaultSettings.sortBy+ );singleClickAction.current =perspectiveSettings &&perspectiveSettings.singleClickAction !== undefined@@ -386,8 +310,8 @@ function ListPerspective(props: Props) {showDetails: showDetails.current,showTags: showTags.current,layoutType: layoutType.current,- orderBy: orderBy.current,- sortBy: sortBy.current,+ orderBy: orderBy,+ sortBy: sortBy,singleClickAction: singleClickAction.current,entrySize: entrySize.current,thumbnailMode: thumbnailMode.current,@@ -418,8 +342,8 @@ function ListPerspective(props: Props) {showDetails.current,showTags.current,layoutType.current,- orderBy.current,- sortBy.current,+ orderBy,+ sortBy,singleClickAction.current,entrySize.current,thumbnailMode.current,@@ -455,10 +379,10 @@ function ListPerspective(props: Props) {};const handleSortBy = handleSort => {- if (sortBy.current !== handleSort) {- sortBy.current = handleSort;+ if (sortBy !== handleSort) {+ setSortBy(handleSort);} else {- orderBy.current = !orderBy.current;+ setOrderBy(!orderBy);}// forceUpdate();setSortingContextMenuAnchorEl(null);@@ -820,8 +744,6 @@ function ListPerspective(props: Props) {changeSingleClickAction={changeSingleClickAction}singleClickAction={singleClickAction.current}openHelpWebPage={openHelpWebPage}- sortBy={sortBy.current}- orderBy={orderBy.current}handleSortingMenu={handleSortingMenu}isLocal={isLocal}resetLocalSettings={() => {@@ -895,8 +817,6 @@ function ListPerspective(props: Props) {open={Boolean(sortingContextMenuAnchorEl)}onClose={() => setSortingContextMenuAnchorEl(null)}anchorEl={sortingContextMenuAnchorEl}- sortBy={sortBy.current}- orderBy={orderBy.current}handleSortBy={handleSortBy}searchModeEnabled={lastSearchTimestamp !== undefined}/>