clean up useIsAllowedToUpscale since its no longer necessary

This commit is contained in:
chainchompa 2024-07-23 16:43:30 -04:00 committed by psychedelicious
parent cb0bffedd5
commit 4017609b91
3 changed files with 5 additions and 108 deletions

View File

@ -3,7 +3,6 @@ import { logger } from 'app/logging/logger';
import type { AppStartListening } from 'app/store/middleware/listenerMiddleware';
import { parseify } from 'common/util/serialize';
import { buildAdHocUpscaleGraph } from 'features/nodes/util/graph/buildAdHocUpscaleGraph';
import { createIsAllowedToUpscaleSelector } from 'features/parameters/hooks/useIsAllowedToUpscale';
import { toast } from 'features/toast/toast';
import { t } from 'i18next';
import { queueApi } from 'services/api/endpoints/queue';
@ -20,22 +19,6 @@ export const addUpscaleRequestedListener = (startAppListening: AppStartListening
const { imageDTO } = action.payload;
const state = getState();
const { isAllowedToUpscale, detailTKey } = createIsAllowedToUpscaleSelector(imageDTO)(state);
// if we can't upscale, show a toast and return
if (!isAllowedToUpscale) {
log.error(
{ imageDTO },
t(detailTKey ?? 'parameters.isAllowedToUpscale.tooLarge') // should never coalesce
);
toast({
id: 'NOT_ALLOWED_TO_UPSCALE',
title: t(detailTKey ?? 'parameters.isAllowedToUpscale.tooLarge'), // should never coalesce
status: 'error',
});
return;
}
const enqueueBatchArg: BatchConfig = {
prepend: true,
batch: {

View File

@ -9,8 +9,7 @@ import {
useDisclosure,
} from '@invoke-ai/ui-library';
import { upscaleRequested } from 'app/store/middleware/listenerMiddleware/listeners/upscaleRequested';
import { useAppDispatch } from 'app/store/storeHooks';
import { useIsAllowedToUpscale } from 'features/parameters/hooks/useIsAllowedToUpscale';
import { useAppDispatch, useAppSelector } from 'app/store/storeHooks';
import { useIsQueueMutationInProgress } from 'features/queue/hooks/useIsQueueMutationInProgress';
import { UpscaleWarning } from 'features/settingsAccordions/components/UpscaleSettingsAccordion/UpscaleWarning';
import { memo, useCallback } from 'react';
@ -25,18 +24,18 @@ type Props = { imageDTO?: ImageDTO };
const ParamUpscalePopover = (props: Props) => {
const { imageDTO } = props;
const dispatch = useAppDispatch();
const { simpleUpscaleModel } = useAppSelector((s) => s.upscale);
const inProgress = useIsQueueMutationInProgress();
const { t } = useTranslation();
const { isOpen, onOpen, onClose } = useDisclosure();
const { isAllowedToUpscale, detail } = useIsAllowedToUpscale(imageDTO);
const handleClickUpscale = useCallback(() => {
onClose();
if (!imageDTO || !isAllowedToUpscale) {
if (!imageDTO) {
return;
}
dispatch(upscaleRequested({ imageDTO }));
}, [dispatch, imageDTO, isAllowedToUpscale, onClose]);
}, [dispatch, imageDTO, onClose]);
return (
<Popover isOpen={isOpen} onClose={onClose} isLazy>
@ -54,9 +53,8 @@ const ParamUpscalePopover = (props: Props) => {
<ParamSpandrelModel isMultidiffusion={false} />
<UpscaleWarning usesTile={false} />
<Button
tooltip={detail}
size="sm"
isDisabled={!imageDTO || inProgress || !isAllowedToUpscale}
isDisabled={!imageDTO || inProgress || !simpleUpscaleModel}
onClick={handleClickUpscale}
>
{t('parameters.upscaleImage')}

View File

@ -1,84 +0,0 @@
import { createMemoizedSelector } from 'app/store/createMemoizedSelector';
import { useAppSelector } from 'app/store/storeHooks';
import { selectUpscalelice } from 'features/parameters/store/upscaleSlice';
import { selectConfigSlice } from 'features/system/store/configSlice';
import { useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import type { ImageDTO } from 'services/api/types';
const getUpscaledPixels = (imageDTO?: ImageDTO, maxUpscalePixels?: number) => {
if (!imageDTO) {
return;
}
if (!maxUpscalePixels) {
return;
}
const { width, height } = imageDTO;
const x4 = height * 4 * width * 4;
const x2 = height * 2 * width * 2;
return { x4, x2 };
};
const getIsAllowedToUpscale = (upscaledPixels?: ReturnType<typeof getUpscaledPixels>, maxUpscalePixels?: number) => {
if (!upscaledPixels || !maxUpscalePixels) {
return { x4: true, x2: true };
}
const isAllowedToUpscale = { x4: false, x2: false };
if (upscaledPixels.x4 <= maxUpscalePixels) {
isAllowedToUpscale.x4 = true;
}
if (upscaledPixels.x2 <= maxUpscalePixels) {
isAllowedToUpscale.x2 = true;
}
return isAllowedToUpscale;
};
const getDetailTKey = (isAllowedToUpscale?: ReturnType<typeof getIsAllowedToUpscale>, scaleFactor?: number) => {
if (!isAllowedToUpscale || !scaleFactor) {
return;
}
if (isAllowedToUpscale.x4 && isAllowedToUpscale.x2) {
return;
}
if (!isAllowedToUpscale.x2 && !isAllowedToUpscale.x4) {
return 'parameters.isAllowedToUpscale.tooLarge';
}
if (!isAllowedToUpscale.x4 && isAllowedToUpscale.x2 && scaleFactor === 4) {
return 'parameters.isAllowedToUpscale.useX2Model';
}
return;
};
export const createIsAllowedToUpscaleSelector = (imageDTO?: ImageDTO) =>
createMemoizedSelector(selectUpscalelice, selectConfigSlice, (upscale, config) => {
const { simpleUpscaleModel } = upscale;
const { maxUpscalePixels } = config;
if (!simpleUpscaleModel) {
return { isAllowedToUpscale: false, detailTKey: undefined };
}
const upscaledPixels = getUpscaledPixels(imageDTO, maxUpscalePixels);
const isAllowedToUpscale = getIsAllowedToUpscale(upscaledPixels, maxUpscalePixels);
const scaleFactor = simpleUpscaleModel.name.includes('x2') ? 2 : 4;
const detailTKey = getDetailTKey(isAllowedToUpscale, scaleFactor);
return {
isAllowedToUpscale: scaleFactor === 2 ? isAllowedToUpscale.x2 : isAllowedToUpscale.x4,
detailTKey,
};
});
export const useIsAllowedToUpscale = (imageDTO?: ImageDTO) => {
const { t } = useTranslation();
const selectIsAllowedToUpscale = useMemo(() => createIsAllowedToUpscaleSelector(imageDTO), [imageDTO]);
const { isAllowedToUpscale, detailTKey } = useAppSelector(selectIsAllowedToUpscale);
return {
isAllowedToUpscale,
detail: detailTKey ? t(detailTKey) : undefined,
};
};