mirror of
https://github.com/invoke-ai/InvokeAI
synced 2024-08-30 20:32:17 +00:00
feat(ui): memoize ImageContextMenu selector
Without the selector itself being memoized, the gallery was rerendering on every progress image.
This commit is contained in:
@ -1,49 +1,32 @@
|
|||||||
import { MenuItem, MenuList } from '@chakra-ui/react';
|
|
||||||
import { useAppDispatch, useAppSelector } from 'app/store/storeHooks';
|
|
||||||
import { memo, useCallback, useContext } from 'react';
|
|
||||||
import {
|
|
||||||
FaExpand,
|
|
||||||
FaFolder,
|
|
||||||
FaFolderPlus,
|
|
||||||
FaShare,
|
|
||||||
FaTrash,
|
|
||||||
} from 'react-icons/fa';
|
|
||||||
import { ContextMenu, ContextMenuProps } from 'chakra-ui-contextmenu';
|
|
||||||
import {
|
|
||||||
resizeAndScaleCanvas,
|
|
||||||
setInitialCanvasImage,
|
|
||||||
} from 'features/canvas/store/canvasSlice';
|
|
||||||
import { setActiveTab } from 'features/ui/store/uiSlice';
|
|
||||||
import { useTranslation } from 'react-i18next';
|
|
||||||
import { ExternalLinkIcon } from '@chakra-ui/icons';
|
import { ExternalLinkIcon } from '@chakra-ui/icons';
|
||||||
import { IoArrowUndoCircleOutline } from 'react-icons/io5';
|
import { MenuItem, MenuList } from '@chakra-ui/react';
|
||||||
import { createSelector } from '@reduxjs/toolkit';
|
import { createSelector } from '@reduxjs/toolkit';
|
||||||
import { useFeatureStatus } from 'features/system/hooks/useFeatureStatus';
|
|
||||||
import { useRecallParameters } from 'features/parameters/hooks/useRecallParameters';
|
|
||||||
import { initialImageSelected } from 'features/parameters/store/actions';
|
|
||||||
import { sentImageToCanvas, sentImageToImg2Img } from '../store/actions';
|
|
||||||
import { useAppToaster } from 'app/components/Toaster';
|
import { useAppToaster } from 'app/components/Toaster';
|
||||||
import { AddImageToBoardContext } from '../../../app/contexts/AddImageToBoardContext';
|
import { stateSelector } from 'app/store/store';
|
||||||
import { useRemoveImageFromBoardMutation } from 'services/api/endpoints/boardImages';
|
import { useAppDispatch, useAppSelector } from 'app/store/storeHooks';
|
||||||
import { ImageDTO } from 'services/api/types';
|
import { defaultSelectorOptions } from 'app/store/util/defaultMemoizeOptions';
|
||||||
import { RootState, stateSelector } from 'app/store/store';
|
import { ContextMenu, ContextMenuProps } from 'chakra-ui-contextmenu';
|
||||||
import {
|
import {
|
||||||
imagesAddedToBatch,
|
imagesAddedToBatch,
|
||||||
selectionAddedToBatch,
|
selectionAddedToBatch,
|
||||||
} from 'features/batch/store/batchSlice';
|
} from 'features/batch/store/batchSlice';
|
||||||
import { defaultSelectorOptions } from 'app/store/util/defaultMemoizeOptions';
|
import {
|
||||||
|
resizeAndScaleCanvas,
|
||||||
|
setInitialCanvasImage,
|
||||||
|
} from 'features/canvas/store/canvasSlice';
|
||||||
import { imageToDeleteSelected } from 'features/imageDeletion/store/imageDeletionSlice';
|
import { imageToDeleteSelected } from 'features/imageDeletion/store/imageDeletionSlice';
|
||||||
|
import { useRecallParameters } from 'features/parameters/hooks/useRecallParameters';
|
||||||
const selector = createSelector(
|
import { initialImageSelected } from 'features/parameters/store/actions';
|
||||||
[stateSelector, (state: RootState, imageDTO: ImageDTO) => imageDTO],
|
import { useFeatureStatus } from 'features/system/hooks/useFeatureStatus';
|
||||||
({ gallery, batch }, imageDTO) => {
|
import { setActiveTab } from 'features/ui/store/uiSlice';
|
||||||
const selectionCount = gallery.selection.length;
|
import { memo, useCallback, useContext, useMemo } from 'react';
|
||||||
const isInBatch = batch.imageNames.includes(imageDTO.image_name);
|
import { useTranslation } from 'react-i18next';
|
||||||
|
import { FaExpand, FaFolder, FaShare, FaTrash } from 'react-icons/fa';
|
||||||
return { selectionCount, isInBatch };
|
import { IoArrowUndoCircleOutline } from 'react-icons/io5';
|
||||||
},
|
import { useRemoveImageFromBoardMutation } from 'services/api/endpoints/boardImages';
|
||||||
defaultSelectorOptions
|
import { ImageDTO } from 'services/api/types';
|
||||||
);
|
import { AddImageToBoardContext } from '../../../app/contexts/AddImageToBoardContext';
|
||||||
|
import { sentImageToCanvas, sentImageToImg2Img } from '../store/actions';
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
image: ImageDTO;
|
image: ImageDTO;
|
||||||
@ -51,9 +34,21 @@ type Props = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const ImageContextMenu = ({ image, children }: Props) => {
|
const ImageContextMenu = ({ image, children }: Props) => {
|
||||||
const { selectionCount, isInBatch } = useAppSelector((state) =>
|
const selector = useMemo(
|
||||||
selector(state, image)
|
() =>
|
||||||
|
createSelector(
|
||||||
|
[stateSelector],
|
||||||
|
({ gallery, batch }) => {
|
||||||
|
const selectionCount = gallery.selection.length;
|
||||||
|
const isInBatch = batch.imageNames.includes(image.image_name);
|
||||||
|
|
||||||
|
return { selectionCount, isInBatch };
|
||||||
|
},
|
||||||
|
defaultSelectorOptions
|
||||||
|
),
|
||||||
|
[image.image_name]
|
||||||
);
|
);
|
||||||
|
const { selectionCount, isInBatch } = useAppSelector(selector);
|
||||||
const dispatch = useAppDispatch();
|
const dispatch = useAppDispatch();
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user