TagSpaces is an offline, open source, document manager with tagging support
chore: small code optimizations
Ilian Sapundshiev07f723614b25
diff --git a/src/renderer/components/EntryIcon.tsx b/src/renderer/components/EntryIcon.tsxindex 203872f62..276be9d9a 100644--- a/src/renderer/components/EntryIcon.tsx+++ b/src/renderer/components/EntryIcon.tsx@@ -16,73 +16,74 @@**/-import React from 'react';import AppConfig from '-/AppConfig';+import React, { useMemo } from 'react';interface Props {isFile: boolean;fileExtension?: string;}-const folderIcon = (- <path d="M20 6h-8l-2-2H4c-1.1 0-1.99.9-1.99 2L2 18c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V8c0-1.1-.9-2-2-2zm0 12H4V8h16v10z" />-);-const genericFileIcon = (- <path d="M14 2H6c-1.1 0-1.99.9-1.99 2L4 20c0 1.1.89 2 1.99 2H18c1.1 0 2-.9 2-2V8l-6-6zM6 20V4h7v5h5v11H6z" />-);-const audioFileIcon = (- <path d="M18.5 12c0-1.77-1.02-3.29-2.5-4.03v8.05c1.48-.73 2.5-2.25 2.5-4.02zM5 9v6h4l5 5V4L9 9H5z" />-);-const videoFileIcon = (- <path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.41 0-8-3.59-8-8s3.59-8 8-8 8 3.59 8 8-3.59 8-8 8zm-2.5-3.5 7-4.5-7-4.5v9z" />-);-const imageFileIcon = (- <path d="M19 5v14H5V5h14m0-2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm-4.86 8.86-3 3.87L9 13.14 6 17h12l-3.86-5.14z"></path>-);-const noteFileIcon = (- <>- <path d="M19 5v14H5V5h14m0-2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2z"></path>- <path d="M14 17H7v-2h7v2zm3-4H7v-2h10v2zm0-4H7V7h10v2z"></path>- </>-);-const bookmarkIcon = (- <path d="M17 3H7c-1.1 0-1.99.9-1.99 2L5 21l7-3 7 3V5c0-1.1-.9-2-2-2zm0 15-5-2.18L7 18V5h10v13z"></path>-);-const emailIcon = (- <path d="M20 4H4c-1.1 0-1.99.9-1.99 2L2 18c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 14H4V8l8 5 8-5v10zm-8-7L4 6h16l-8 5z"></path>-);-const ebookIcon = (- <>- <path d="M21 5c-1.11-.35-2.33-.5-3.5-.5-1.95 0-4.05.4-5.5 1.5-1.45-1.1-3.55-1.5-5.5-1.5S2.45 4.9 1 6v14.65c0 .25.25.5.5.5.1 0 .15-.05.25-.05C3.1 20.45 5.05 20 6.5 20c1.95 0 4.05.4 5.5 1.5 1.35-.85 3.8-1.5 5.5-1.5 1.65 0 3.35.3 4.75 1.05.1.05.15.05.25.05.25 0 .5-.25.5-.5V6c-.6-.45-1.25-.75-2-1zm0 13.5c-1.1-.35-2.3-.5-3.5-.5-1.7 0-4.15.65-5.5 1.5V8c1.35-.85 3.8-1.5 5.5-1.5 1.2 0 2.4.15 3.5.5v11.5z"></path>- <path d="M17.5 10.5c.88 0 1.73.09 2.5.26V9.24c-.79-.15-1.64-.24-2.5-.24-1.7 0-3.24.29-4.5.83v1.66c1.13-.64 2.7-.99 4.5-.99zM13 12.49v1.66c1.13-.64 2.7-.99 4.5-.99.88 0 1.73.09 2.5.26V11.9c-.79-.15-1.64-.24-2.5-.24-1.7 0-3.24.3-4.5.83zm4.5 1.84c-1.7 0-3.24.29-4.5.83v1.66c1.13-.64 2.7-.99 4.5-.99.88 0 1.73.09 2.5.26v-1.52c-.79-.16-1.64-.24-2.5-.24z"></path>- </>-);+const ICONS = {+ folder: (+ <path d="M20 6h-8l-2-2H4c-1.1 0-1.99.9-1.99 2L2 18c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V8c0-1.1-.9-2-2-2zm0 12H4V8h16v10z" />+ ),+ genericFile: (+ <path d="M14 2H6c-1.1 0-1.99.9-1.99 2L4 20c0 1.1.89 2 1.99 2H18c1.1 0 2-.9 2-2V8l-6-6zM6 20V4h7v5h5v11H6z" />+ ),+ audio: (+ <path d="M18.5 12c0-1.77-1.02-3.29-2.5-4.03v8.05c1.48-.73 2.5-2.25 2.5-4.02zM5 9v6h4l5 5V4L9 9H5z" />+ ),+ video: (+ <path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.41 0-8-3.59-8-8s3.59-8 8-8 8 3.59 8 8-3.59 8-8 8zm-2.5-3.5 7-4.5-7-4.5v9z" />+ ),+ image: (+ <path d="M19 5v14H5V5h14m0-2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm-4.86 8.86-3 3.87L9 13.14 6 17h12l-3.86-5.14z"></path>+ ),+ note: (+ <>+ <path d="M19 5v14H5V5h14m0-2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2z"></path>+ <path d="M14 17H7v-2h7v2zm3-4H7v-2h10v2zm0-4H7V7h10v2z"></path>+ </>+ ),+ bookmark: (+ <path d="M17 3H7c-1.1 0-1.99.9-1.99 2L5 21l7-3 7 3V5c0-1.1-.9-2-2-2zm0 15-5-2.18L7 18V5h10v13z"></path>+ ),+ email: (+ <path d="M20 4H4c-1.1 0-1.99.9-1.99 2L2 18c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 14H4V8l8 5 8-5v10zm-8-7L4 6h16l-8 5z"></path>+ ),+ ebook: (+ <>+ <path d="M21 5c-1.11-.35-2.33-.5-3.5-.5-1.95 0-4.05.4-5.5 1.5-1.45-1.1-3.55-1.5-5.5-1.5S2.45 4.9 1 6v14.65c0 .25.25.5.5.5.1 0 .15-.05.25-.05C3.1 20.45 5.05 20 6.5 20c1.95 0 4.05.4 5.5 1.5 1.35-.85 3.8-1.5 5.5-1.5 1.65 0 3.35.3 4.75 1.05.1.05.15.05.25.05.25 0 .5-.25.5-.5V6c-.6-.45-1.25-.75-2-1zm0 13.5c-1.1-.35-2.3-.5-3.5-.5-1.7 0-4.15.65-5.5 1.5V8c1.35-.85 3.8-1.5 5.5-1.5 1.2 0 2.4.15 3.5.5v11.5z"></path>+ <path d="M17.5 10.5c.88 0 1.73.09 2.5.26V9.24c-.79-.15-1.64-.24-2.5-.24-1.7 0-3.24.29-4.5.83v1.66c1.13-.64 2.7-.99 4.5-.99zM13 12.49v1.66c1.13-.64 2.7-.99 4.5-.99.88 0 1.73.09 2.5.26V11.9c-.79-.15-1.64-.24-2.5-.24-1.7 0-3.24.3-4.5.83zm4.5 1.84c-1.7 0-3.24.29-4.5.83v1.66c1.13-.64 2.7-.99 4.5-.99.88 0 1.73.09 2.5.26v-1.52c-.79-.16-1.64-.24-2.5-.24z"></path>+ </>+ ),+};-function EntryIcon(props: Props) {- const { isFile, fileExtension = '' } = props;- let iconSVGPath = genericFileIcon;- if (!isFile) {- iconSVGPath = folderIcon;- } else if (AppConfig.SearchTypeGroups.audio.includes(fileExtension)) {- iconSVGPath = audioFileIcon;- } else if (AppConfig.SearchTypeGroups.video.includes(fileExtension)) {- iconSVGPath = videoFileIcon;- } else if (AppConfig.SearchTypeGroups.images.includes(fileExtension)) {- iconSVGPath = imageFileIcon;- } else if (AppConfig.SearchTypeGroups.notes.includes(fileExtension)) {- iconSVGPath = noteFileIcon;- } else if (AppConfig.SearchTypeGroups.bookmarks.includes(fileExtension)) {- iconSVGPath = bookmarkIcon;- } else if (AppConfig.SearchTypeGroups.emails.includes(fileExtension)) {- iconSVGPath = emailIcon;- } else if (AppConfig.SearchTypeGroups.ebooks.includes(fileExtension)) {- iconSVGPath = ebookIcon;- }+function getIcon(isFile: boolean, fileExtension: string): React.ReactNode {+ if (!isFile) return ICONS.folder;+ const { audio, video, images, notes, bookmarks, emails, ebooks } =+ AppConfig.SearchTypeGroups;+ if (audio.includes(fileExtension)) return ICONS.audio;+ if (video.includes(fileExtension)) return ICONS.video;+ if (images.includes(fileExtension)) return ICONS.image;+ if (notes.includes(fileExtension)) return ICONS.note;+ if (bookmarks.includes(fileExtension)) return ICONS.bookmark;+ if (emails.includes(fileExtension)) return ICONS.email;+ if (ebooks.includes(fileExtension)) return ICONS.ebook;+ return ICONS.genericFile;+}++function EntryIcon({ isFile, fileExtension = '' }: Props) {+ // Memoize icon selection for performance+ const iconSVGPath = useMemo(+ () => getIcon(isFile, fileExtension),+ [isFile, fileExtension],+ );return (<svgwidth="100%"- // height="auto"xmlns="http://www.w3.org/2000/svg"viewBox="0 0 24 24"fill="#bbbbbb22"@@ -91,4 +92,5 @@ function EntryIcon(props: Props) {</svg>);}+export default React.memo(EntryIcon);diff --git a/src/renderer/components/FolderContainer.tsx b/src/renderer/components/FolderContainer.tsxindex 7afa1bed9..bf1a3d8de 100644--- a/src/renderer/components/FolderContainer.tsx+++ b/src/renderer/components/FolderContainer.tsx@@ -70,7 +70,7 @@ interface Props {}function FolderContainer({ toggleDrawer, drawerOpened, hidden }: Props) {- const devMode: boolean = useSelector(isDevMode);+ const devMode = useSelector(isDevMode);const { t } = useTranslation();const theme = useTheme();const keyBindings = useSelector(getKeyBindingObject);@@ -108,6 +108,77 @@ function FolderContainer({ toggleDrawer, drawerOpened, hidden }: Props) {const showWelcomePanel =!currentDirectoryPath && currentDirectoryEntries.length < 1;+ // Memoized progress value calculation+ const getProgressValue = useCallback(() => {+ const objProgress = progress.find(+ (fileProgress) =>+ fileProgress.progress < 100 && fileProgress.progress > -1,+ );+ return objProgress ? objProgress.progress : 100;+ }, [progress]);++ // Memoized search key binding+ const openSearchKeyBinding = useMemo(+ () => adjustKeyBinding(keyBindings.openSearch),+ [keyBindings.openSearch],+ );++ // Memoized toggle buttons for perspectives+ const perspectiveToggleButtons = useMemo(+ () =>+ AvailablePerspectives.map((perspective) => (+ <TsToggleButton+ value={perspective.id}+ aria-label={perspective.id}+ key={perspective.id}+ data-tid={perspective.key}+ onClick={() => switchPerspective(perspective.id)}+ sx={{+ opacity: 0.9,+ backgroundColor: theme.palette.background.default,+ border: `1px solid ${theme.palette.divider}`,+ }}+ >+ <Tooltip+ title={+ perspective.title ++ (perspective.beta ? ' ' + t('core:betaStatus').toUpperCase() : '')+ }+ >+ <Box sx={{ display: 'flex' }}>{perspective.icon}</Box>+ </Tooltip>+ </TsToggleButton>+ )),+ [theme.palette.background.default, theme.palette.divider, t],+ );++ // Memoized menu items for perspectives+ const perspectiveMenuItems = useMemo(+ () =>+ AvailablePerspectives.map((perspective) => (+ <MenuItem+ key={perspective.key}+ data-tid={perspective.key}+ onClick={() => {+ handlePerspectiveMenuClose();+ switchPerspective(perspective.id);+ }}+ >+ <ListItemIcon>{perspective.icon}</ListItemIcon>+ <ListItemText+ primary={+ <>+ {perspective.title}+ {perspective.beta && <BetaLabel />}+ </>+ }+ />+ </MenuItem>+ )),+ [handlePerspectiveMenuClose],+ );++ // Memoized CircularProgress with labelconst CircularProgressWithLabel = useCallback((prop: { value: number }) => (<Box position="relative" display="inline-flex">@@ -135,14 +206,7 @@ function FolderContainer({ toggleDrawer, drawerOpened, hidden }: Props) {[theme.palette.text.primary],);- const getProgressValue = useCallback(() => {- const objProgress = progress.find(- (fileProgress) =>- fileProgress.progress < 100 && fileProgress.progress > -1,- );- return objProgress ? objProgress.progress : 100;- }, [progress]);-+ // Memoized switchPerspective handlerconst switchPerspective = useCallback((perspectiveId: string) => {if (@@ -165,79 +229,13 @@ function FolderContainer({ toggleDrawer, drawerOpened, hidden }: Props) {[setManualDirectoryPerspective, openProTeaserDialog],);- const perspectiveToggleButtons = useMemo(- () =>- AvailablePerspectives.map((perspective) => (- <TsToggleButton- value={perspective.id}- aria-label={perspective.id}- key={perspective.id}- data-tid={perspective.key}- onClick={() => switchPerspective(perspective.id)}- sx={{- opacity: '0.9',- backgroundColor: theme.palette.background.default,- border: '1px solid ' + theme.palette.divider,- }}- >- <Tooltip- title={- perspective.title +- (perspective.beta ? ' ' + t('core:betaStatus').toUpperCase() : '')- }- >- <Box sx={{ display: 'flex' }}>{perspective.icon}</Box>- </Tooltip>- </TsToggleButton>- )),- [- devMode,- switchPerspective,- theme.palette.background.default,- theme.palette.divider,- t,- ],- );-const openSearchMode = useCallback(() => {enterSearchMode();}, [enterSearchMode]);- const openSearchKeyBinding = useMemo(- () => `${adjustKeyBinding(keyBindings.openSearch)}`,- [keyBindings.openSearch],- );const smallScreen = useMediaQuery(theme.breakpoints.down('md'));const readOnlyLocation = findLocation()?.isReadOnly;- const perspectiveMenuItems = useMemo(- () =>- AvailablePerspectives.map((perspective) => {- let badge = perspective.beta ? <BetaLabel /> : null;- return (- <MenuItem- key={perspective.key}- data-tid={perspective.key}- onClick={() => {- handlePerspectiveMenuClose();- switchPerspective(perspective.id);- }}- >- <ListItemIcon>{perspective.icon}</ListItemIcon>- <ListItemText- primary={- <>- {perspective.title}- {badge}- </>- }- />- </MenuItem>- );- }),- [devMode, handlePerspectiveMenuClose, switchPerspective],- );-return (<Boxsx={{@@ -250,6 +248,7 @@ function FolderContainer({ toggleDrawer, drawerOpened, hidden }: Props) {}}data-tid="folderContainerTID">+ {/* Top Bar */}<Boxsx={{paddingLeft: '5px',@@ -269,7 +268,7 @@ function FolderContainer({ toggleDrawer, drawerOpened, hidden }: Props) {><TsIconButtonid="mobileMenuButton"- sx={{ WebkitAppRegion: 'no-drag' } as React.CSSProperties}+ sx={{ WebkitAppRegion: 'no-drag' }}onClick={toggleDrawer}tooltip={t('core:toggleSidebar')}>@@ -282,7 +281,7 @@ function FolderContainer({ toggleDrawer, drawerOpened, hidden }: Props) {id="goBackButton"disabled={historyIndex === 0}onClick={goBack}- sx={{ WebkitAppRegion: 'no-drag' } as React.CSSProperties}+ sx={{ WebkitAppRegion: 'no-drag' }}><GoBackIcon /></TsIconButton>@@ -292,7 +291,7 @@ function FolderContainer({ toggleDrawer, drawerOpened, hidden }: Props) {id="goForwardButton"disabled={historyIndex === 0}onClick={goForward}- sx={{ WebkitAppRegion: 'no-drag' } as React.CSSProperties}+ sx={{ WebkitAppRegion: 'no-drag' }}><GoForwardIcon /></TsIconButton>@@ -303,7 +302,7 @@ function FolderContainer({ toggleDrawer, drawerOpened, hidden }: Props) {<><Boxsx={{- margin: '0 10px 0 10px',+ margin: '0 10px',flex: '1 1 1%',display: 'flex',flexDirection: 'column',@@ -311,17 +310,13 @@ function FolderContainer({ toggleDrawer, drawerOpened, hidden }: Props) {>{smallScreen ? (<TsIconButton- tooltip={- t('core:openSearch') + ' (' + openSearchKeyBinding + ')'- }+ tooltip={`${t('core:openSearch')} (${openSearchKeyBinding})`}data-tid="toggleSearch"onClick={openSearchMode}- sx={- {- maxWidth: 100,- WebkitAppRegion: 'no-drag',- } as React.CSSProperties- }+ sx={{+ maxWidth: 100,+ WebkitAppRegion: 'no-drag',+ }}><SearchIcon /></TsIconButton>@@ -331,20 +326,18 @@ function FolderContainer({ toggleDrawer, drawerOpened, hidden }: Props) {onClick={openSearchMode}color="secondary"startIcon={<SearchIcon />}- sx={- {- marginTop: '-2px',- marginRight: '5px',- minWidth: 100,- maxHeight: 32,- width: 'stretch',- maxWidth: 300,- margin: '0 auto',- whiteSpace: 'nowrap',- overflow: 'hidden',- WebkitAppRegion: 'no-drag',- } as React.CSSProperties- }+ sx={{+ marginTop: '-2px',+ marginRight: '5px',+ minWidth: 100,+ maxHeight: 32,+ width: 'stretch',+ maxWidth: 300,+ margin: '0 auto',+ whiteSpace: 'nowrap',+ overflow: 'hidden',+ WebkitAppRegion: 'no-drag',+ }}>{t('core:searchTitle')}<Box sx={{ width: 10 }} />@@ -358,14 +351,12 @@ function FolderContainer({ toggleDrawer, drawerOpened, hidden }: Props) {title={t('core:progress')}data-tid="uploadProgress"onClick={() => openFileUploadDialog()}- sx={- {- position: 'relative',- padding: '8px 12px 6px 8px',- margin: 0,- WebkitAppRegion: 'no-drag',- } as React.CSSProperties- }+ sx={{+ position: 'relative',+ padding: '8px 12px 6px 8px',+ margin: 0,+ WebkitAppRegion: 'no-drag',+ }}><CircularProgressWithLabel value={getProgressValue()} /></TsIconButton>@@ -377,11 +368,13 @@ function FolderContainer({ toggleDrawer, drawerOpened, hidden }: Props) {</>)}</Box>+ {/* Content Area */}<Box sx={{ minHeight: '100%', width: '100%', overflowY: 'auto' }}>{/* eslint-disable-next-line jsx-a11y/anchor-has-content,jsx-a11y/anchor-is-valid */}<a href="#" id="downloadFile" /><RenderPerspective /></Box>+ {/* Perspective Switcher */}{isDesktopMode ? (<Boxsx={{@@ -420,11 +413,8 @@ function FolderContainer({ toggleDrawer, drawerOpened, hidden }: Props) {data-tid="chatTID"sx={{marginLeft: '5px',- // ...(readOnlyLocation- // ? {}- // : { color: theme.palette.primary.main }),backgroundColor: theme.palette.background.default,- border: '1px solid ' + theme.palette.divider,+ border: `1px solid ${theme.palette.divider}`,}}onClick={() => {if (readOnlyLocation) return;