feat(ui): separate context menu for no board board

Much easier to not need to handle the board being optional in the component.
This commit is contained in:
psychedelicious 2024-06-27 14:36:01 +10:00
parent 20042d99ec
commit 5709f82e5f
4 changed files with 91 additions and 41 deletions

View File

@ -1,9 +1,8 @@
import type { ContextMenuProps } from '@invoke-ai/ui-library'; import type { ContextMenuProps } from '@invoke-ai/ui-library';
import { ContextMenu, MenuGroup, MenuItem, MenuList, Tooltip } from '@invoke-ai/ui-library'; import { ContextMenu, MenuGroup, MenuItem, MenuList } from '@invoke-ai/ui-library';
import { createSelector } from '@reduxjs/toolkit'; import { createSelector } from '@reduxjs/toolkit';
import { useAppDispatch, useAppSelector } from 'app/store/storeHooks'; import { useAppDispatch, useAppSelector } from 'app/store/storeHooks';
import { autoAddBoardIdChanged, selectGallerySlice } from 'features/gallery/store/gallerySlice'; import { autoAddBoardIdChanged, selectGallerySlice } from 'features/gallery/store/gallerySlice';
import type { BoardId } from 'features/gallery/store/types';
import { useFeatureStatus } from 'features/system/hooks/useFeatureStatus'; import { useFeatureStatus } from 'features/system/hooks/useFeatureStatus';
import { memo, useCallback, useMemo } from 'react'; import { memo, useCallback, useMemo } from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
@ -16,54 +15,49 @@ import type { BoardDTO } from 'services/api/types';
import GalleryBoardContextMenuItems from './GalleryBoardContextMenuItems'; import GalleryBoardContextMenuItems from './GalleryBoardContextMenuItems';
type Props = { type Props = {
board?: BoardDTO; board: BoardDTO;
board_id: BoardId;
children: ContextMenuProps<HTMLDivElement>['children']; children: ContextMenuProps<HTMLDivElement>['children'];
setBoardToDelete?: (board?: BoardDTO) => void; setBoardToDelete: (board?: BoardDTO) => void;
}; };
const BoardContextMenu = ({ board, board_id, setBoardToDelete, children }: Props) => { const BoardContextMenu = ({ board, setBoardToDelete, children }: Props) => {
const { t } = useTranslation(); const { t } = useTranslation();
const dispatch = useAppDispatch(); const dispatch = useAppDispatch();
const autoAssignBoardOnClick = useAppSelector((s) => s.gallery.autoAssignBoardOnClick); const autoAssignBoardOnClick = useAppSelector((s) => s.gallery.autoAssignBoardOnClick);
const selectIsSelectedForAutoAdd = useMemo( const selectIsSelectedForAutoAdd = useMemo(
() => createSelector(selectGallerySlice, (gallery) => board && board.board_id === gallery.autoAddBoardId), () => createSelector(selectGallerySlice, (gallery) => board.board_id === gallery.autoAddBoardId),
[board] [board.board_id]
); );
const [updateBoard] = useUpdateBoardMutation(); const [updateBoard] = useUpdateBoardMutation();
const isSelectedForAutoAdd = useAppSelector(selectIsSelectedForAutoAdd); const isSelectedForAutoAdd = useAppSelector(selectIsSelectedForAutoAdd);
const boardName = useBoardName(board_id); const boardName = useBoardName(board.board_id);
const isBulkDownloadEnabled = useFeatureStatus('bulkDownload'); const isBulkDownloadEnabled = useFeatureStatus('bulkDownload');
const [bulkDownload] = useBulkDownloadImagesMutation(); const [bulkDownload] = useBulkDownloadImagesMutation();
const handleSetAutoAdd = useCallback(() => { const handleSetAutoAdd = useCallback(() => {
dispatch(autoAddBoardIdChanged(board_id)); dispatch(autoAddBoardIdChanged(board.board_id));
}, [board_id, dispatch]); }, [board.board_id, dispatch]);
const handleBulkDownload = useCallback(() => { const handleBulkDownload = useCallback(() => {
bulkDownload({ image_names: [], board_id: board_id }); bulkDownload({ image_names: [], board_id: board.board_id });
}, [board_id, bulkDownload]); }, [board.board_id, bulkDownload]);
const handleArchive = useCallback(() => { const handleArchive = useCallback(() => {
updateBoard({ updateBoard({
board_id, board_id: board.board_id,
changes: { archived: true }, changes: { archived: true },
}); });
}, [board_id, updateBoard]); }, [board.board_id, updateBoard]);
const handleUnarchive = useCallback(() => { const handleUnarchive = useCallback(() => {
updateBoard({ updateBoard({
board_id, board_id: board.board_id,
changes: { archived: false }, changes: { archived: false },
}); });
}, [board_id, updateBoard]); }, [board.board_id, updateBoard]);
const isBoardArchived = useMemo(() => {
return !!board?.archived;
}, [board]);
const renderMenuFunc = useCallback( const renderMenuFunc = useCallback(
() => ( () => (
@ -71,7 +65,7 @@ const BoardContextMenu = ({ board, board_id, setBoardToDelete, children }: Props
<MenuGroup title={boardName}> <MenuGroup title={boardName}>
<MenuItem <MenuItem
icon={<PiPlusBold />} icon={<PiPlusBold />}
isDisabled={isSelectedForAutoAdd || autoAssignBoardOnClick || isBoardArchived} isDisabled={isSelectedForAutoAdd || autoAssignBoardOnClick || Boolean(board?.archived)}
onClick={handleSetAutoAdd} onClick={handleSetAutoAdd}
> >
{t('boards.menuItemAutoAdd')} {t('boards.menuItemAutoAdd')}
@ -81,20 +75,20 @@ const BoardContextMenu = ({ board, board_id, setBoardToDelete, children }: Props
{t('boards.downloadBoard')} {t('boards.downloadBoard')}
</MenuItem> </MenuItem>
)} )}
{board &&
(isBoardArchived ? (
<MenuItem icon={<PiArchiveBold />} onClick={handleUnarchive}>
{t('boards.unarchiveBoard')}
</MenuItem>
) : (
<Tooltip label={isSelectedForAutoAdd && 'testing'}>
<MenuItem icon={<PiArchiveFill />} onClick={handleArchive} isDisabled={isSelectedForAutoAdd}>
{t('boards.archiveBoard')}
</MenuItem>
</Tooltip>
))}
{board && <GalleryBoardContextMenuItems board={board} setBoardToDelete={setBoardToDelete} />} {board.archived && (
<MenuItem icon={<PiArchiveBold />} onClick={handleUnarchive}>
{t('boards.unarchiveBoard')}
</MenuItem>
)}
{!board.archived && (
<MenuItem icon={<PiArchiveFill />} onClick={handleArchive} isDisabled={isSelectedForAutoAdd}>
{t('boards.archiveBoard')}
</MenuItem>
)}
<GalleryBoardContextMenuItems board={board} setBoardToDelete={setBoardToDelete} />
</MenuGroup> </MenuGroup>
</MenuList> </MenuList>
), ),
@ -110,7 +104,6 @@ const BoardContextMenu = ({ board, board_id, setBoardToDelete, children }: Props
t, t,
handleArchive, handleArchive,
handleUnarchive, handleUnarchive,
isBoardArchived,
] ]
); );

View File

@ -125,7 +125,7 @@ const GalleryBoard = ({ board, isSelected, setBoardToDelete }: GalleryBoardProps
w="full" w="full"
h="full" h="full"
> >
<BoardContextMenu board={board} board_id={board_id} setBoardToDelete={setBoardToDelete}> <BoardContextMenu board={board} setBoardToDelete={setBoardToDelete}>
{(ref) => ( {(ref) => (
<Tooltip <Tooltip
label={<BoardTotalsTooltip board_id={board.board_id} isArchived={Boolean(board.archived)} />} label={<BoardTotalsTooltip board_id={board.board_id} isArchived={Boolean(board.archived)} />}

View File

@ -4,8 +4,8 @@ import IAIDroppable from 'common/components/IAIDroppable';
import SelectionOverlay from 'common/components/SelectionOverlay'; import SelectionOverlay from 'common/components/SelectionOverlay';
import type { RemoveFromBoardDropData } from 'features/dnd/types'; import type { RemoveFromBoardDropData } from 'features/dnd/types';
import AutoAddIcon from 'features/gallery/components/Boards/AutoAddIcon'; import AutoAddIcon from 'features/gallery/components/Boards/AutoAddIcon';
import BoardContextMenu from 'features/gallery/components/Boards/BoardContextMenu';
import { BoardTotalsTooltip } from 'features/gallery/components/Boards/BoardsList/BoardTotalsTooltip'; import { BoardTotalsTooltip } from 'features/gallery/components/Boards/BoardsList/BoardTotalsTooltip';
import NoBoardBoardContextMenu from 'features/gallery/components/Boards/NoBoardBoardContextMenu';
import { autoAddBoardIdChanged, boardIdSelected } from 'features/gallery/store/gallerySlice'; import { autoAddBoardIdChanged, boardIdSelected } from 'features/gallery/store/gallerySlice';
import InvokeLogoSVG from 'public/assets/images/invoke-symbol-wht-lrg.svg'; import InvokeLogoSVG from 'public/assets/images/invoke-symbol-wht-lrg.svg';
import { memo, useCallback, useMemo, useState } from 'react'; import { memo, useCallback, useMemo, useState } from 'react';
@ -58,9 +58,9 @@ const NoBoardBoard = memo(({ isSelected }: Props) => {
w="full" w="full"
h="full" h="full"
> >
<BoardContextMenu board_id="none"> <NoBoardBoardContextMenu>
{(ref) => ( {(ref) => (
<Tooltip label={<BoardTotalsTooltip board_id="none" />} openDelay={1000}> <Tooltip label={<BoardTotalsTooltip board_id="none" isArchived={false} />} openDelay={1000}>
<Flex <Flex
ref={ref} ref={ref}
onClick={handleSelectBoard} onClick={handleSelectBoard}
@ -111,7 +111,7 @@ const NoBoardBoard = memo(({ isSelected }: Props) => {
</Flex> </Flex>
</Tooltip> </Tooltip>
)} )}
</BoardContextMenu> </NoBoardBoardContextMenu>
</Flex> </Flex>
</Box> </Box>
); );

View File

@ -0,0 +1,57 @@
import type { ContextMenuProps } from '@invoke-ai/ui-library';
import { ContextMenu, MenuGroup, MenuItem, MenuList } from '@invoke-ai/ui-library';
import { useAppDispatch, useAppSelector } from 'app/store/storeHooks';
import { autoAddBoardIdChanged } from 'features/gallery/store/gallerySlice';
import { useFeatureStatus } from 'features/system/hooks/useFeatureStatus';
import { memo, useCallback } from 'react';
import { useTranslation } from 'react-i18next';
import { PiDownloadBold, PiPlusBold } from 'react-icons/pi';
import { useBulkDownloadImagesMutation } from 'services/api/endpoints/images';
type Props = {
children: ContextMenuProps<HTMLDivElement>['children'];
};
const NoBoardBoardContextMenu = ({ children }: Props) => {
const { t } = useTranslation();
const dispatch = useAppDispatch();
const autoAssignBoardOnClick = useAppSelector((s) => s.gallery.autoAssignBoardOnClick);
const isSelectedForAutoAdd = useAppSelector((s) => s.gallery.autoAddBoardId === 'none');
const isBulkDownloadEnabled = useFeatureStatus('bulkDownload');
const [bulkDownload] = useBulkDownloadImagesMutation();
const handleSetAutoAdd = useCallback(() => {
dispatch(autoAddBoardIdChanged('none'));
}, [dispatch]);
const handleBulkDownload = useCallback(() => {
bulkDownload({ image_names: [], board_id: 'none' });
}, [bulkDownload]);
const renderMenuFunc = useCallback(
() => (
<MenuList visibility="visible">
<MenuGroup title={t('boards.uncategorized')}>
<MenuItem
icon={<PiPlusBold />}
isDisabled={isSelectedForAutoAdd || autoAssignBoardOnClick}
onClick={handleSetAutoAdd}
>
{t('boards.menuItemAutoAdd')}
</MenuItem>
{isBulkDownloadEnabled && (
<MenuItem icon={<PiDownloadBold />} onClickCapture={handleBulkDownload}>
{t('boards.downloadBoard')}
</MenuItem>
)}
</MenuGroup>
</MenuList>
),
[autoAssignBoardOnClick, handleBulkDownload, handleSetAutoAdd, isBulkDownloadEnabled, isSelectedForAutoAdd, t]
);
return <ContextMenu renderMenu={renderMenuFunc}>{children}</ContextMenu>;
};
export default memo(NoBoardBoardContextMenu);