TagSpaces is an offline, open source, document manager with tagging support
chore: optimizations in rendering of grid perspective
Ilian Sapundshievb4014a4d0e93
diff --git a/src/renderer/perspectives/common/CellView.tsx b/src/renderer/perspectives/common/CellView.tsxindex 64978b72a..5b16af44d 100644--- a/src/renderer/perspectives/common/CellView.tsx+++ b/src/renderer/perspectives/common/CellView.tsx@@ -43,7 +43,7 @@ import { Box } from '@mui/material';import Typography from '@mui/material/Typography';import { useTheme } from '@mui/material/styles';import { extractContainingDirectoryPath } from '@tagspaces/tagspaces-common/paths';-import React from 'react';+import React, { memo } from 'react';import { NativeTypes } from 'react-dnd-html5-backend';import { useSelector } from 'react-redux';@@ -87,26 +87,7 @@ function CellView(props: Props) {} = useSelectedEntriesContext();const { sortedDirContent, nativeDragModeEnabled } = useSortedDirContext();const { showNotification } = useNotificationContext();-- //const desktopMode = useSelector(getDesktopMode);const selectedTabName = useSelector(getEntryContainerTab);- // const fileSourceRef = useRef<HTMLDivElement | null>(null);-- /*useEffect(() => {- const dragItem = fileSourceRef.current;- if (dragItem) {- const handleDragStart = (e) => {- e.preventDefault()- window.electronIO.ipcRenderer.startDrag(fsEntry.path);- };-- dragItem.addEventListener('dragstart', handleDragStart);-- return () => {- dragItem.removeEventListener('dragstart', handleDragStart);- };- }- }, [fileSourceRef.current]);*/if (!fsEntry || (!fsEntry.isFile && !showDirectories)) {return null;@@ -145,7 +126,6 @@ function CellView(props: Props) {if (fsEntry.isFile) {setSelectedEntries([fsEntry]);openEntryInternal(fsEntry);- //openEntry(fsEntry.path);} else {console.log('Handle Grid cell db click, selected path : ', fsEntry.path);openDirectory(fsEntry.path);@@ -174,7 +154,6 @@ function CellView(props: Props) {}let entriesToSelect;- // console.log('lastSelectedIndex: ' + lastSelectedIndex + ' currentSelectedIndex: ' + currentSelectedIndex);if (currentSelectedIndex > lastSelectedIndex) {entriesToSelect = sortedDirContent.slice(lastSelectedIndex,@@ -223,7 +202,7 @@ function CellView(props: Props) {openedEntry.isFile ||selectedTabName !== TabNames.aiTab) {- // dont open file if chat mode is enabled+ // do not open file if chat mode is enabledopenEntryInternal(fsEntry);}} else if (singleClickAction === 'openExternal') {@@ -239,7 +218,7 @@ function CellView(props: Props) {'Importing files is disabled because the location is in read-only mode.','error',true,- ); //i18n.t('core:dndDisabledReadOnlyMode')+ );return;}if (item) {@@ -250,10 +229,9 @@ function CellView(props: Props) {selectedEntries.length > 0 &&selectedEntries.some((e) => e.path === entry.path)) {- const arrSelected = selectedEntries- //.map((entry) => entry.path)- // remove target folder selection- .filter((e) => e.path !== item.targetPath);+ const arrSelected = selectedEntries.filter(+ (e) => e.path !== item.targetPath,+ );if (arrSelected.length > 0) {arrEntries = arrSelected;} else {@@ -262,7 +240,6 @@ function CellView(props: Props) {} else if (entry) {arrEntries = [entry];}- //console.log('Dropped files: ' + JSON.stringify(arrEntries));openMoveCopyFilesDialog(arrEntries, item.targetPath, currentLocationId);}};@@ -339,4 +316,4 @@ function CellView(props: Props) {);}-export default CellView;+export default memo(CellView);diff --git a/src/renderer/perspectives/grid/components/GridPagination.tsx b/src/renderer/perspectives/grid/components/GridPagination.tsxindex 067f49b10..0985dc5db 100644--- a/src/renderer/perspectives/grid/components/GridPagination.tsx+++ b/src/renderer/perspectives/grid/components/GridPagination.tsx@@ -101,7 +101,7 @@ function GridPagination(props: Props) {const [, forceUpdate] = useReducer((x) => x + 1, 0);const currentLocation = findLocation();const theme = useTheme();- const pageFiles = getResentPageFiles();+ const pageFiles = useMemo(() => getResentPageFiles(), [getResentPageFiles]);// Memoize derived values for performanceconst showPagination = useMemo(@@ -408,7 +408,7 @@ function GridPagination(props: Props) {<GridCellsContainer>{pageFiles.map((entry, index) => (<CellView- key={entry.uuid + index}+ key={entry.uuid}fsEntry={entry}index={index}cellContent={getCellContent}diff --git a/src/renderer/perspectives/grid/components/MainContainer.tsx b/src/renderer/perspectives/grid/components/MainContainer.tsxindex 40fc4a76a..28556e673 100644--- a/src/renderer/perspectives/grid/components/MainContainer.tsx+++ b/src/renderer/perspectives/grid/components/MainContainer.tsx@@ -46,7 +46,7 @@ import { TS } from '-/tagspaces.namespace';import useFirstRender from '-/utils/useFirstRender';import { Box } from '@mui/material';import Links from 'assets/links';-import React, { useEffect, useRef, useState } from 'react';+import React, { useCallback, useEffect, useRef, useState } from 'react';import { GlobalHotKeys } from 'react-hotkeys';import { useSelector } from 'react-redux';import GridCell from './GridCell';@@ -159,31 +159,28 @@ function GridPerspective(props: Props) {};const openHelpWebPage = () => {- // closeOptionsMenu();openURLExternally(Links.documentationLinks.defaultPerspective, true);};const openSettings = () => {- // closeOptionsMenu();setIsGridSettingsDialogOpened(true);};- const handleTagMenu = (- event: React.ChangeEvent<HTMLInputElement>,- tag: TS.Tag,- entry: TS.FileSystemEntry,- ) => {- event.preventDefault();- event.stopPropagation();-- selectedTag.current = tag;- selectedEntry.current = entry;- setTagContextMenuAnchorEl(event.currentTarget);- };+ const handleTagMenu = useCallback(+ (+ event: React.ChangeEvent<HTMLInputElement>,+ tag: TS.Tag,+ entry: TS.FileSystemEntry,+ ) => {+ event.preventDefault();+ event.stopPropagation();- // const closeOptionsMenu = () => {- // setOptionsContextMenuAnchorEl(null);- // };+ selectedTag.current = tag;+ selectedEntry.current = entry;+ setTagContextMenuAnchorEl(event.currentTarget);+ },+ [],+ );const keyMap = {nextDocument: keyBindings.nextDocument,@@ -278,51 +275,54 @@ function GridPerspective(props: Props) {},};- const getCellContent = (- fsEntry: TS.FileSystemEntry,- selectedEntries: Array<TS.FileSystemEntry>,- index: number,- handleGridContextMenu: (- event: React.MouseEvent<HTMLDivElement>,+ const getCellContent = useCallback(+ (fsEntry: TS.FileSystemEntry,- ) => void,- handleGridCellClick,- handleGridCellDblClick,- isLast?: boolean,- ) => {- let selected = false;- if (- selectedEntries &&- selectedEntries.some((entry) => entry.path === fsEntry.path)- ) {- selected = true;- }+ selectedEntries: Array<TS.FileSystemEntry>,+ index: number,+ handleGridContextMenu: (+ event: React.MouseEvent<HTMLDivElement>,+ fsEntry: TS.FileSystemEntry,+ ) => void,+ handleGridCellClick,+ handleGridCellDblClick,+ isLast?: boolean,+ ) => {+ let selected = false;+ if (+ selectedEntries &&+ selectedEntries.some((entry) => entry.path === fsEntry.path)+ ) {+ selected = true;+ }- const selectionMode = selectedEntries.length > 1;- return (- <FileSourceDnd entry={fsEntry}>- <TagDropContainer entry={fsEntry}>- <GridCell- selected={selected}- fsEntry={fsEntry}- isLast={isLast}- selectionMode={selectionMode}- handleTagMenu={handleTagMenu}- handleGridContextMenu={(- event: React.MouseEvent<HTMLDivElement>,- fsEntry: TS.FileSystemEntry,- ) => {- setMouseX(event.clientX);- setMouseY(event.clientY);- handleGridContextMenu(event, fsEntry);- }}- handleGridCellDblClick={handleGridCellDblClick}- handleGridCellClick={handleGridCellClick}- />- </TagDropContainer>- </FileSourceDnd>- );- };+ const selectionMode = selectedEntries.length > 1;+ return (+ <FileSourceDnd entry={fsEntry}>+ <TagDropContainer entry={fsEntry}>+ <GridCell+ selected={selected}+ fsEntry={fsEntry}+ isLast={isLast}+ selectionMode={selectionMode}+ handleTagMenu={handleTagMenu}+ handleGridContextMenu={(+ event: React.MouseEvent<HTMLDivElement>,+ fsEntry: TS.FileSystemEntry,+ ) => {+ setMouseX(event.clientX);+ setMouseY(event.clientY);+ handleGridContextMenu(event, fsEntry);+ }}+ handleGridCellDblClick={handleGridCellDblClick}+ handleGridCellClick={handleGridCellClick}+ />+ </TagDropContainer>+ </FileSourceDnd>+ );+ },+ [handleTagMenu],+ );return (<Box@@ -345,9 +345,7 @@ function GridPerspective(props: Props) {><GridCellsStyleContextProvider><GridPagination- //directories={sortedDirectories}desktopMode={desktopMode}- //files={sortedFiles}getCellContent={getCellContent}currentDirectoryPath={currentDirectoryPath}onClick={onClick}@@ -390,15 +388,6 @@ function GridPerspective(props: Props) {handleSortBy={handleSortBy}/>)}- {/* {Boolean(optionsContextMenuAnchorEl) && (- <GridOptionsMenu- open={Boolean(optionsContextMenuAnchorEl)}- onClose={closeOptionsMenu}- anchorEl={optionsContextMenuAnchorEl}- openHelpWebPage={openHelpWebPage}- openSettings={openSettings}- />- )} */}</Box>);}