import { useAtom, useAtomValue } from "jotai"; import React, { useEffect, useMemo, useState } from "react"; import { Tooltip } from "react-tooltip"; import { themeChange } from "theme-change"; import { TModelsList, modelsListAtom } from "../../../atoms/models-list-atom"; import useLogger from "../../hooks/use-logger"; import { savedOutputPathAtom, progressAtom, rememberOutputFolderAtom, scaleAtom, customWidthAtom, useCustomWidthAtom, } from "../../../atoms/user-settings-atom"; import { FEATURE_FLAGS } from "@common/feature-flags"; import { ELECTRON_COMMANDS } from "@common/electron-commands"; import Select from "react-select"; import { cn } from "@/lib/utils"; import { useToast } from "@/components/ui/use-toast"; import { translationAtom } from "@/atoms/translations-atom"; import { SelectImageScale } from "../settings-tab/select-image-scale"; 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 UpscaylSteps({ 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 [outputPath, setOutputPath] = useAtom(savedOutputPathAtom); const [progress, setProgress] = useAtom(progressAtom); const rememberOutputFolder = useAtomValue(rememberOutputFolderAtom); const [open, setOpen] = React.useState(false); const customWidth = useAtomValue(customWidthAtom); const useCustomWidth = useAtomValue(useCustomWidthAtom); const logit = useLogger(); const { toast } = useToast(); const t = useAtomValue(translationAtom); const outputHandler = async () => { const path = await window.electron.invoke(ELECTRON_COMMANDS.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]); const upscaylResolution = useMemo(() => { 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.BATCH_MODE.TITLE")}

{/* STEP 1 */}

{t("APP.FILE_SELECTION.TITLE")}

{/* STEP 2 */}

{t("APP.MODEL_SELECTION.TITLE")}

{t("APP.MODEL_SELECTION.DESCRIPTION")}

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

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

)}
{/* STEP 3 */}
{t("APP.OUTPUT_PATH_SELECTION.TITLE")} {FEATURE_FLAGS.APP_STORE_BUILD && ( )}
{!outputPath && FEATURE_FLAGS.APP_STORE_BUILD && (
{t("APP.OUTPUT_PATH_SELECTION.NOT_SELECTED")}
)}
{!batchMode && !FEATURE_FLAGS.APP_STORE_BUILD && (

{!batchMode ? t("APP.OUTPUT_PATH_SELECTION.DEFAULT_IMG_PATH") : t("APP.OUTPUT_PATH_SELECTION.DEFAULT_FOLDER_PATH")}

)}
{/* STEP 4 */}

{t("APP.SCALE_SELECTION.TITLE")}

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

{t("APP.SCALE_SELECTION.FROM_TITLE")} {dimensions.width}x{dimensions.height} {t("APP.SCALE_SELECTION.TO_TITLE")} {upscaylResolution.width}x{upscaylResolution.height}

)}
); } export default UpscaylSteps;