import { useAtom, useAtomValue } from "jotai"; import React, { useCallback, useEffect, useState } from "react"; import { Tooltip } from "react-tooltip"; import { themeChange } from "theme-change"; import { TModelsList, modelsListAtom } from "../../../atoms/modelsListAtom"; import useLog from "../../hooks/useLog"; import { noImageProcessingAtom, savedOutputPathAtom, progressAtom, rememberOutputFolderAtom, scaleAtom, customWidthAtom, useCustomWidthAtom, } from "../../../atoms/userSettingsAtom"; import { featureFlags } from "@common/feature-flags"; import getModelScale from "@common/check-model-scale"; import COMMAND from "@common/commands"; import Select from "react-select"; import { cn } from "@/lib/utils"; import { useToast } from "@/components/ui/use-toast"; import { ImageScaleSelect } from "@/components/settings-tab/ImageScaleSelect"; import { translationAtom } from "@/atoms/translations-atom"; interface IProps { selectImageHandler: () => Promise; selectFolderHandler: () => Promise; handleModelChange: (e: any) => void; upscaylHandler: () => Promise; batchMode: boolean; setBatchMode: React.Dispatch>; imagePath: string; doubleUpscayl: boolean; setDoubleUpscayl: React.Dispatch>; dimensions: { width: number | null; height: number | null; }; setSaveImageAs: React.Dispatch>; model: string; setModel: React.Dispatch>; setGpuId: React.Dispatch>; } function LeftPaneImageSteps({ selectImageHandler, selectFolderHandler, handleModelChange, upscaylHandler, batchMode, setBatchMode, imagePath, doubleUpscayl, setDoubleUpscayl, dimensions, setSaveImageAs, model, setModel, setGpuId, }: IProps) { const [currentModel, setCurrentModel] = useState({ label: null, value: null, }); const modelOptions = useAtomValue(modelsListAtom); const [scale, setScale] = useAtom(scaleAtom); const noImageProcessing = useAtomValue(noImageProcessingAtom); const [outputPath, setOutputPath] = useAtom(savedOutputPathAtom); const [progress, setProgress] = useAtom(progressAtom); const rememberOutputFolder = useAtomValue(rememberOutputFolderAtom); const [open, setOpen] = React.useState(false); const [customWidth, setCustomWidth] = useAtom(customWidthAtom); const [useCustomWidth, setUseCustomWidth] = useAtom(useCustomWidthAtom); const [targetWidth, setTargetWidth] = useState(null); const [targetHeight, setTargetHeight] = useState(null); const { logit } = useLog(); const { toast } = useToast(); const t = useAtomValue(translationAtom); const outputHandler = async () => { var path = await window.electron.invoke(COMMAND.SELECT_FOLDER); if (path !== null) { logit("🗂 Setting Output Path: ", path); setOutputPath(path); } else { setOutputPath(null); } }; useEffect(() => { themeChange(false); if (!localStorage.getItem("saveImageAs")) { logit("⚙️ Setting saveImageAs to png"); localStorage.setItem("saveImageAs", "png"); } else { const currentlySavedImageFormat = localStorage.getItem("saveImageAs"); logit( "⚙️ Getting saveImageAs from localStorage: ", currentlySavedImageFormat, ); setSaveImageAs(currentlySavedImageFormat); } if (!localStorage.getItem("model")) { setCurrentModel(modelOptions[0]); setModel(modelOptions[0].value); localStorage.setItem("model", JSON.stringify(modelOptions[0])); logit("🔀 Setting model to", modelOptions[0].value); } else { const currentlySavedModel = JSON.parse( localStorage.getItem("model"), ) as (typeof modelOptions)[0]; setCurrentModel(currentlySavedModel); setModel(currentlySavedModel.value); logit( "⚙️ Getting model from localStorage: ", JSON.stringify(currentlySavedModel), ); } if (!localStorage.getItem("gpuId")) { localStorage.setItem("gpuId", ""); logit("⚙️ Setting gpuId to empty string"); } else { const currentlySavedGpuId = localStorage.getItem("gpuId"); setGpuId(currentlySavedGpuId); logit("⚙️ Getting gpuId from localStorage: ", currentlySavedGpuId); } }, []); useEffect(() => { logit("🔀 Setting model to", currentModel.value); }, [currentModel]); useEffect(() => { setTargetWidth(getUpscaleResolution().width); setTargetHeight(getUpscaleResolution().height); }, [dimensions.width, dimensions.height, doubleUpscayl, scale]); const getUpscaleResolution = useCallback(() => { const newDimensions = { width: dimensions.width, height: dimensions.height, }; let doubleScale = parseInt(scale) * parseInt(scale); let singleScale = parseInt(scale); if (doubleUpscayl) { if (useCustomWidth) { newDimensions.width = customWidth; newDimensions.height = Math.round( customWidth * (dimensions.height / dimensions.width), ); } else { const newWidth = dimensions.width * doubleScale; const newHeight = dimensions.height * doubleScale; newDimensions.width = newWidth; newDimensions.height = newHeight; } } else { if (useCustomWidth) { newDimensions.width = customWidth; newDimensions.height = Math.round( customWidth * (dimensions.height / dimensions.width), ); } else { newDimensions.width = dimensions.width * singleScale; newDimensions.height = dimensions.height * singleScale; } } return newDimensions; }, [dimensions.width, dimensions.height, doubleUpscayl, scale]); return (
{/* BATCH OPTION */}
{ if (!rememberOutputFolder) { setOutputPath(""); } setProgress(""); setBatchMode((oldValue) => !oldValue); }} >

{t("APP.INFOS.LEFT_PANE_PROCESS.BATCH.TITLE")}

{/* STEP 1 */}

{t("APP.INFOS.LEFT_PANE_PROCESS.STEP_1.TITLE")}

{/* STEP 2 */}

{t("APP.INFOS.LEFT_PANE_PROCESS.STEP_2.TITLE")}

{t("APP.INFOS.LEFT_PANE_PROCESS.STEP_2.SELECT_MODEL")}

{ if (e.target.checked) { setDoubleUpscayl(true); } else { setDoubleUpscayl(false); } }} />

{ setDoubleUpscayl(!doubleUpscayl); }} > {t("APP.INFOS.LEFT_PANE_PROCESS.DOUBLE_UPSCAYL.TITLE")}

)}
{/* STEP 3 */}
{t("APP.INFOS.LEFT_PANE_PROCESS.STEP_3.TITLE")} {featureFlags.APP_STORE_BUILD && ( )}
{!outputPath && featureFlags.APP_STORE_BUILD && (
{t("APP.INFOS.LEFT_PANE_PROCESS.STEP_3.NOT_SELECTED")}
)}
{!batchMode && !featureFlags.APP_STORE_BUILD && (

{!batchMode ? t("APP.INFOS.LEFT_PANE_PROCESS.STEP_3.DEFAULT_IMG_PATH") : t("APP.INFOS.LEFT_PANE_PROCESS.STEP_3.DEFAULT_FOLDER_PATH")}

)}
{/* STEP 4 */}

{t("APP.INFOS.LEFT_PANE_PROCESS.STEP_4.TITLE")}

{dimensions.width && dimensions.height && (

{t("APP.INFOS.LEFT_PANE_PROCESS.STEP_4.UPSCAYL_FROM")} {dimensions.width}x{dimensions.height} {t("APP.INFOS.LEFT_PANE_PROCESS.STEP_4.UPSCAYL_TO")} {getUpscaleResolution().width}x{getUpscaleResolution().height}

)}
); } export default LeftPaneImageSteps;