From fe3748b900cef8094df133b8c5ae574bcb61ceee Mon Sep 17 00:00:00 2001 From: subnub Date: Sun, 23 Jun 2024 20:35:34 -0400 Subject: [PATCH] added list item optimizations --- backend/db/utils/fileUtils/index.ts | 8 ++- src/actions/selectedItem.js | 1 - src/components/FileItem/index.jsx | 20 +++--- src/components/Files/index.jsx | 61 ++++++++++--------- src/components/FolderItem/index.jsx | 19 +++--- .../FolderTreeStorage/FolderTreeStorage.jsx | 8 ++- src/components/Folders/index.jsx | 2 - src/components/Homepage/Homepage2.jsx | 20 +----- src/components/MainSection/index.jsx | 8 --- src/components/QuickAccessItem/index.jsx | 17 ++---- src/components/RightSection/RightSection.jsx | 2 +- .../UploadStorageSwitcher.jsx | 40 +++++++++--- 12 files changed, 100 insertions(+), 106 deletions(-) diff --git a/backend/db/utils/fileUtils/index.ts b/backend/db/utils/fileUtils/index.ts index bee31cf..9683590 100644 --- a/backend/db/utils/fileUtils/index.ts +++ b/backend/db/utils/fileUtils/index.ts @@ -73,8 +73,12 @@ class DbUtil { }; getFileInfo = async (fileID: string, userID: string) => { - console.log("info diff", userID.toString()); - const file = await File.findOne({ + //TODO: Using mongoose like this causes the object to be returned in raw form + // const file = await File.findOne({ + // "metadata.owner": userID, + // _id: new ObjectId(fileID), + // }); + const file = await conn.db.collection("fs.files").findOne({ "metadata.owner": userID, _id: new ObjectId(fileID), }); diff --git a/src/actions/selectedItem.js b/src/actions/selectedItem.js index c42077f..c7264b7 100644 --- a/src/actions/selectedItem.js +++ b/src/actions/selectedItem.js @@ -13,7 +13,6 @@ export const startSetSelectedItem = ( ) => { return (dispatch) => { const currentDate = Date.now(); - console.log("curr", currentDate); dispatch(setLastSelected(currentDate)); diff --git a/src/components/FileItem/index.jsx b/src/components/FileItem/index.jsx index 99ef091..63ca324 100644 --- a/src/components/FileItem/index.jsx +++ b/src/components/FileItem/index.jsx @@ -1,6 +1,6 @@ import capitalize from "../../utils/capitalize"; import moment from "moment"; -import React, { useMemo, useRef } from "react"; +import React, { useCallback, useMemo, useRef } from "react"; import ContextMenu from "../ContextMenu"; import mobilecheck from "../../utils/mobileCheck"; import { useContextMenu } from "../../hooks/contextMenu"; @@ -12,10 +12,10 @@ import { startSetSelectedItem } from "../../actions/selectedItem"; import { setPopupFile } from "../../actions/popupFile"; import bytes from "bytes"; -const FileItem = (props) => { +const FileItem = React.memo((props) => { const { file } = props; - const currentSelectedItem = useSelector( - (state) => state.selectedItem.selected + const elementSelected = useSelector( + (state) => state.selectedItem.selected === file._id ); const listView = useSelector((state) => state.filter.listView); const { image, hasThumbnail, imageOnError } = useThumbnail( @@ -42,12 +42,8 @@ const FileItem = (props) => { () => getFileColor(file.filename), [file.filename] ); - const elementSelected = useMemo( - () => file._id === currentSelectedItem, - [file._id, currentSelectedItem] - ); - const fileClick = () => { + const fileClick = useCallback(() => { const currentDate = Date.now(); if (!elementSelected) { @@ -61,7 +57,7 @@ const FileItem = (props) => { } lastSelected.current = Date.now(); - }; + }, [startSetSelectedItem, dispatch, setPopupFile, mobilecheck, file._id]); if (listView) { return ( @@ -185,7 +181,7 @@ const FileItem = (props) => { ) : ( { ); } -}; +}); export default FileItem; diff --git a/src/components/Files/index.jsx b/src/components/Files/index.jsx index 9df3c8e..77e477c 100644 --- a/src/components/Files/index.jsx +++ b/src/components/Files/index.jsx @@ -94,34 +94,39 @@ const Files = () => { ) : ( - - - - - - - - {files?.pages.map((filePage, index) => ( - - {filePage.map((file) => ( - - ))} - - ))} + + + + + + + + + + {files?.pages.map((filePage, index) => ( + + {filePage.map((file) => ( + + ))} + + ))} +
-
-

Name

-
-
-

Size

-
-

- Created -

-
-

- Actions -

-
+
+

Name

+
+
+

+ Size +

+
+

+ Created +

+
+

+ Actions +

+
)} diff --git a/src/components/FolderItem/index.jsx b/src/components/FolderItem/index.jsx index d8f99d3..d6079c7 100644 --- a/src/components/FolderItem/index.jsx +++ b/src/components/FolderItem/index.jsx @@ -1,4 +1,4 @@ -import React, { useMemo, useRef } from "react"; +import React, { useCallback, useMemo, useRef } from "react"; import ContextMenu from "../ContextMenu"; import { useContextMenu } from "../../hooks/contextMenu"; import classNames from "classnames"; @@ -8,10 +8,10 @@ import { startSetSelectedItem } from "../../actions/selectedItem"; import mobilecheck from "../../utils/mobileCheck"; import moment from "moment"; -const FolderItem = (props) => { +const FolderItem = React.memo((props) => { const { folder } = props; - const currentSelectedItem = useSelector( - (state) => state.selectedItem.selected + const elementSelected = useSelector( + (state) => state.selectedItem.selected === folder._id ); const lastSelected = useRef(0); const navigate = useNavigate(); @@ -26,12 +26,7 @@ const FolderItem = (props) => { ...contextMenuState } = useContextMenu(); - const elementSelected = useMemo( - () => props.folder._id === currentSelectedItem, - [props.folder._id, currentSelectedItem] - ); - - const folderClick = () => { + const folderClick = useCallback(() => { const currentDate = Date.now(); if (!elementSelected) { @@ -45,7 +40,7 @@ const FolderItem = (props) => { } lastSelected.current = Date.now(); - }; + }, [dispatch, startSetSelectedItem, mobilecheck, navigate, folder._id]); return (
{

); -}; +}); export default FolderItem; diff --git a/src/components/FolderTreeStorage/FolderTreeStorage.jsx b/src/components/FolderTreeStorage/FolderTreeStorage.jsx index 0e3361c..4698e2c 100644 --- a/src/components/FolderTreeStorage/FolderTreeStorage.jsx +++ b/src/components/FolderTreeStorage/FolderTreeStorage.jsx @@ -30,7 +30,13 @@ const FolderTreeStorage = (props) => (
{props.state.open && props.state.folders.length !== 0 ? props.state.folders.map((folder) => { - return ; + return ( + + ); }) : undefined}
diff --git a/src/components/Folders/index.jsx b/src/components/Folders/index.jsx index 09ee665..2985c90 100644 --- a/src/components/Folders/index.jsx +++ b/src/components/Folders/index.jsx @@ -9,7 +9,6 @@ const Folder = () => { const sortBy = useSelector((state) => state.filter.sortBy); const parent = useSelector((state) => state.parent.parent); const dispatch = useDispatch(); - console.log("rerenderfolder", sortBy); const switchOrderSortBy = useCallback(() => { let newSortBy = ""; @@ -35,7 +34,6 @@ const Folder = () => { break; } } - console.log("new sortBy", newSortBy); dispatch(setSortBy(newSortBy)); }, [setSortBy, dispatch]); diff --git a/src/components/Homepage/Homepage2.jsx b/src/components/Homepage/Homepage2.jsx index 90918d1..02a0448 100644 --- a/src/components/Homepage/Homepage2.jsx +++ b/src/components/Homepage/Homepage2.jsx @@ -1,37 +1,19 @@ import Header from "../Header"; -import LeftSection from "../LeftSection"; import MainSection from "../MainSection"; import Uploader from "../Uploader"; import React from "react"; -import PhotoViewer from "../PhotoViewer"; import UploadOverlay from "../UploadOverlay"; import HomepageSpinner from "../HomepageSpinner"; import MobileContextMenuContainer from "../MobileContextMenu"; import ShareModelWrapper from "../ShareModelWrapper"; -import { useLocation, useNavigate, useParams } from "react-router-dom"; -import { useSelector } from "react-redux"; -import { useInfiniteQuery, useQuery } from "react-query"; -import { getFilesList } from "../../api/filesAPI"; -import { getFoldersList } from "../../api/foldersAPI"; -import { useFiles } from "../../hooks/files"; const Homepage2 = () => { - const navigate = useNavigate(); - const location = useLocation(); - const params = useParams(); - const photoID = useSelector((state) => state.photoViewer.id); - useFiles(); - - const goHome = () => { - navigate("/home"); - }; - return (
-
+
diff --git a/src/components/MainSection/index.jsx b/src/components/MainSection/index.jsx index 0bf7a74..6390a85 100644 --- a/src/components/MainSection/index.jsx +++ b/src/components/MainSection/index.jsx @@ -119,26 +119,18 @@ class MainSectionContainer extends React.Component { }; componentDidUpdate = () => { - console.log("main updated"); - // console.log("update ID main", getUpdateSettingsID()); - // if (this.lastSettingsUpdateID !== getUpdateSettingsID()) { // console.log("Settings Update!"); // this.getSettings(); // } - // this.lastSettingsUpdateID = getUpdateSettingsID(); - // console.log("update settings id", updateSettingsID); - // console.log("Main Section Updated", this.props.resetSettingsMain); - // if (this.lastSettingsUpdateID !== this.props.resetSettingsMain) { // console.log("Settings Update!"); // this.getSettings(); // } - // this.lastSettingsUpdateID = this.props.resetSettingsMain; }; diff --git a/src/components/QuickAccessItem/index.jsx b/src/components/QuickAccessItem/index.jsx index 7d44a96..4652ce7 100644 --- a/src/components/QuickAccessItem/index.jsx +++ b/src/components/QuickAccessItem/index.jsx @@ -1,6 +1,6 @@ import capitalize from "../../utils/capitalize"; import moment from "moment"; -import React, { useMemo, useRef } from "react"; +import React, { memo, useMemo, useRef } from "react"; import ContextMenu from "../ContextMenu"; import classNames from "classnames"; import { getFileColor, getFileExtension } from "../../utils/files"; @@ -11,10 +11,10 @@ import mobilecheck from "../../utils/mobileCheck"; import { startSetSelectedItem } from "../../actions/selectedItem"; import { setPopupFile } from "../../actions/popupFile"; -const QuickAccessItem = (props) => { +const QuickAccessItem = memo((props) => { const { file } = props; - const currentSelectedItem = useSelector( - (state) => state.selectedItem.selected + const elementSelected = useSelector( + (state) => state.selectedItem.selected === `quick-${file._id}` ); const { image, hasThumbnail, imageOnError } = useThumbnail( file.metadata.hasThumbnail, @@ -43,11 +43,6 @@ const QuickAccessItem = (props) => { [file.filename] ); - const elementSelected = useMemo( - () => `quick-${file._id}` === currentSelectedItem, - [file._id, currentSelectedItem] - ); - const quickItemClick = () => { const currentDate = Date.now(); @@ -104,7 +99,7 @@ const QuickAccessItem = (props) => { ) : ( {
); -}; +}); export default QuickAccessItem; diff --git a/src/components/RightSection/RightSection.jsx b/src/components/RightSection/RightSection.jsx index 61f0b10..5a7cdb7 100644 --- a/src/components/RightSection/RightSection.jsx +++ b/src/components/RightSection/RightSection.jsx @@ -12,7 +12,7 @@ class RightSection extends React.Component { render() { return (
( -
- {props.state.options.length !== 0 ? - : -

No Storage Accounts

} -
-) +
+ {props.state.options.length !== 0 ? ( + + ) : ( +

No Storage Accounts

+ )} +
+); -export default UploadStorageSwitcher; \ No newline at end of file +export default UploadStorageSwitcher;