"use client"; import { useState, useEffect, useCallback } from "react"; import commands from "../../electron/commands"; import { ReactCompareSlider } from "react-compare-slider"; import Header from "../components/Header"; import Footer from "../components/Footer"; import ProgressBar from "../components/upscayl-tab/view/ProgressBar"; import RightPaneInfo from "../components/upscayl-tab/view/RightPaneInfo"; import ImageOptions from "../components/upscayl-tab/view/ImageOptions"; import LeftPaneImageSteps from "../components/upscayl-tab/config/LeftPaneImageSteps"; import Tabs from "../components/Tabs"; import SettingsTab from "../components/settings-tab"; import { useAtom } from "jotai"; import { logAtom } from "../atoms/logAtom"; import { modelsListAtom } from "../atoms/modelsListAtom"; import { batchModeAtom, dontShowCloudModalAtom, scaleAtom, } from "../atoms/userSettingsAtom"; import useLog from "../components/hooks/useLog"; import { UpscaylCloudModal } from "../components/UpscaylCloudModal"; const Home = () => { // STATES const [os, setOs] = useState<"linux" | "mac" | "win" | undefined>(undefined); const [imagePath, SetImagePath] = useState(""); const [upscaledImagePath, setUpscaledImagePath] = useState(""); const [outputPath, setOutputPath] = useState(""); const [scaleFactor] = useState(4); const [progress, setProgress] = useState(""); const [model, setModel] = useState("realesrgan-x4plus"); const [loaded, setLoaded] = useState(false); const [version, setVersion] = useState(""); const [batchMode, setBatchMode] = useAtom(batchModeAtom); const [batchFolderPath, setBatchFolderPath] = useState(""); const [upscaledBatchFolderPath, setUpscaledBatchFolderPath] = useState(""); const [doubleUpscayl, setDoubleUpscayl] = useState(false); const [overwrite, setOverwrite] = useState(false); const [doubleUpscaylCounter, setDoubleUpscaylCounter] = useState(0); const [quality, setQuality] = useState(0); const [gpuId, setGpuId] = useState(""); const [saveImageAs, setSaveImageAs] = useState("png"); const [zoomAmount, setZoomAmount] = useState("100%"); const [backgroundPosition, setBackgroundPosition] = useState("0% 0%"); const [dimensions, setDimensions] = useState({ width: null, height: null, }); const [selectedTab, setSelectedTab] = useState(0); const [logData, setLogData] = useAtom(logAtom); const [modelOptions, setModelOptions] = useAtom(modelsListAtom); const [scale] = useAtom(scaleAtom); const [dontShowCloudModal, setDontShowCloudModal] = useAtom( dontShowCloudModalAtom ); const [showCloudModal, setShowCloudModal] = useState(false); const { logit } = useLog(); // EFFECTS useEffect(() => { setLoaded(true); setVersion(navigator?.userAgent?.match(/Upscayl\/([\d\.]+\d+)/)[1]); const handleErrors = (data: string) => { if (data.includes("invalid gpu")) { alert( "Error. Please make sure you have a Vulkan compatible GPU (Most modern GPUs support Vulkan). Upscayl does not work with CPU or iGPU sadly." ); resetImagePaths(); } else if (data.includes("failed")) { if (batchMode) return; alert( data.includes("encode") ? "ENCODING ERROR => " : "DECODING ERROR => " + "This image is possibly corrupt or not supported by Upscayl, or your GPU drivers are acting funny (Did you check if your GPU is compatible and drivers are alright?). You could try converting the image into another format and upscaling again. Also make sure that the output path is correct and you have the proper write permissions for the directory. If not, then unfortuantely there's not much we can do to help, sorry." ); resetImagePaths(); } else if (data.includes("uncaughtException")) { alert( "Upscayl encountered an error. Possibly, the upscayl binary failed to execute the commands properly. Try checking the logs to see if you get any information. You can post an issue on Upscayl's GitHub repository for more help." ); resetImagePaths(); } }; window.electron.on( commands.OS, (_, data: "linux" | "mac" | "win" | undefined) => { if (data) { setOs(data); } } ); // LOG window.electron.on(commands.LOG, (_, data: string) => { logit(`๐ BACKEND REPORTED: `, data); }); window.electron.on(commands.SCALING_AND_CONVERTING, (_, data: string) => { setProgress("Processing the image..."); }); window.electron.on(commands.UPSCAYL_ERROR, (_, data: string) => { alert(data); resetImagePaths(); }); // UPSCAYL PROGRESS window.electron.on(commands.UPSCAYL_PROGRESS, (_, data: string) => { if (data.length > 0 && data.length < 10) { setProgress(data); } else if (data.includes("converting")) { setProgress("Scaling and converting image..."); } handleErrors(data); logit(`๐ง UPSCAYL_PROGRESS: `, data); }); // FOLDER UPSCAYL PROGRESS window.electron.on(commands.FOLDER_UPSCAYL_PROGRESS, (_, data: string) => { if (data.length > 0 && data.length < 10) { setProgress(data); } handleErrors(data); logit(`๐ง FOLDER_UPSCAYL_PROGRESS: `, data); }); // DOUBLE UPSCAYL PROGRESS window.electron.on(commands.DOUBLE_UPSCAYL_PROGRESS, (_, data: string) => { if (data.length > 0 && data.length < 10) { if (data === "0.00%") { setDoubleUpscaylCounter(doubleUpscaylCounter + 1); } setProgress(data); } handleErrors(data); logit(`๐ง DOUBLE_UPSCAYL_PROGRESS: `, data); }); // UPSCAYL DONE window.electron.on(commands.UPSCAYL_DONE, (_, data: string) => { setProgress(""); setTimeout(() => setUpscaledImagePath(data), 500); logit("upscaledImagePath: ", data); logit(`๐ฏ UPSCAYL_DONE: `, data); }); // FOLDER UPSCAYL DONE window.electron.on(commands.FOLDER_UPSCAYL_DONE, (_, data: string) => { setProgress(""); setUpscaledBatchFolderPath(data); logit(`๐ฏ FOLDER_UPSCAYL_DONE: `, data); }); // DOUBLE UPSCAYL DONE window.electron.on(commands.DOUBLE_UPSCAYL_DONE, (_, data: string) => { setProgress(""); setDoubleUpscaylCounter(0); setUpscaledImagePath(data); logit(`๐ฏ DOUBLE_UPSCAYL_DONE: `, data); }); // CUSTOM FOLDER LISTENER window.electron.on( commands.CUSTOM_MODEL_FILES_LIST, (_, data: string[]) => { logit(`๐ CUSTOM_MODEL_FILES_LIST: `, data); const newModelOptions = data.map((model) => { return { value: model, label: model, }; }); // Add newModelsList to modelOptions and remove duplicates const combinedModelOptions = [...modelOptions, ...newModelOptions]; const uniqueModelOptions = combinedModelOptions.filter( // Check if any model in the array appears more than once (model, index, array) => array.findIndex((t) => t.value === model.value) === index ); setModelOptions(uniqueModelOptions); } ); if (!localStorage.getItem("upscaylCloudModalShown")) { logit("โ๏ธ upscayl cloud show to true"); localStorage.setItem("upscaylCloudModalShown", "true"); setShowCloudModal(true); } }, []); useEffect(() => { const customModelsPath = JSON.parse( localStorage.getItem("customModelsPath") ); if (customModelsPath !== null) { window.electron.send(commands.GET_MODELS_LIST, customModelsPath); logit("๐ฏ GET_MODELS_LIST: ", customModelsPath); } }, []); useEffect(() => { const rememberOutputFolder = localStorage.getItem("rememberOutputFolder"); const lastOutputFolderPath = localStorage.getItem("lastOutputFolderPath"); if (rememberOutputFolder === "true") { setOutputPath(lastOutputFolderPath); } else { setOutputPath(""); localStorage.removeItem("lastOutputFolderPath"); } }, []); useEffect(() => { setProgress(""); setOutputPath(""); }, [batchMode]); useEffect(() => { if (imagePath.length > 0) { logit("๐ผ imagePath: ", imagePath); const extension = imagePath.toLocaleLowerCase().split(".").pop(); logit("๐ค Extension: ", extension); if (!allowedFileTypes.includes(extension.toLowerCase())) { alert("Please select an image"); resetImagePaths(); } } else { resetImagePaths(); } }, [imagePath]); const resetImagePaths = () => { logit("๐ Resetting image paths"); setDimensions({ width: null, height: null, }); setProgress(""); SetImagePath(""); setUpscaledImagePath(""); setBatchFolderPath(""); setUpscaledBatchFolderPath(""); }; // HANDLERS const handleMouseMove = useCallback((e: any) => { const { left, top, width, height } = e.target.getBoundingClientRect(); const x = ((e.pageX - left) / width) * 100; const y = ((e.pageY - top) / height) * 100; setBackgroundPosition(`${x}% ${y}%`); }, []); const selectImageHandler = async () => { resetImagePaths(); var path = await window.electron.invoke(commands.SELECT_FILE); if (path !== null) { logit("๐ผ Selected Image Path: ", path); SetImagePath(path); var dirname = path.match(/(.*)[\/\\]/)[1] || ""; logit("๐ Selected Image Directory: ", dirname); setOutputPath(dirname); } }; const selectFolderHandler = async () => { resetImagePaths(); var path = await window.electron.invoke(commands.SELECT_FOLDER); if (path !== null) { logit("๐ผ Selected Folder Path: ", path); setBatchFolderPath(path); setOutputPath(path + "_upscayled"); } else { logit("๐ซ Folder selection cancelled"); setBatchFolderPath(""); setOutputPath(""); } }; const handleModelChange = (e: any) => { setModel(e.value); logit("๐ Model changed: ", e.value); localStorage.setItem( "model", JSON.stringify({ label: e.label, value: e.value }) ); }; // DRAG AND DROP HANDLERS const handleDragEnter = (e) => { e.preventDefault(); console.log("drag enter"); }; const handleDragLeave = (e) => { e.preventDefault(); console.log("drag leave"); }; const handleDragOver = (e) => { e.preventDefault(); console.log("drag over"); }; const openFolderHandler = (e) => { logit("๐ OPEN_FOLDER: ", upscaledBatchFolderPath); window.electron.send(commands.OPEN_FOLDER, upscaledBatchFolderPath); }; const handleDrop = (e) => { e.preventDefault(); resetImagePaths(); if ( e.dataTransfer.items.length === 0 || e.dataTransfer.files.length === 0 ) { logit("๐ No valid files dropped"); alert("Please drag and drop an image"); return; } const type = e.dataTransfer.items[0].type; const filePath = e.dataTransfer.files[0].path; const extension = e.dataTransfer.files[0].name.split(".").at(-1); logit("โคต๏ธ Dropped file: ", JSON.stringify({ type, filePath, extension })); if ( !type.includes("image") || !allowedFileTypes.includes(extension.toLowerCase()) ) { logit("๐ซ Invalid file dropped"); alert("Please drag and drop an image"); } else { logit("๐ผ Setting image path: ", filePath); SetImagePath(filePath); var dirname = filePath.match(/(.*)[\/\\]/)[1] || ""; logit("๐ Setting output path: ", dirname); setOutputPath(dirname); } }; const handlePaste = (e) => { resetImagePaths(); e.preventDefault(); const type = e.clipboardData.items[0].type; const filePath = e.clipboardData.files[0].path; const extension = e.clipboardData.files[0].name.split(".").at(-1); logit("๐ Pasted file: ", JSON.stringify({ type, filePath, extension })); if ( !type.includes("image") && !allowedFileTypes.includes(extension.toLowerCase()) ) { alert("Please drag and drop an image"); } else { SetImagePath(filePath); var dirname = filePath.match(/(.*)[\/\\]/)[1] || ""; logit("๐ Setting output path: ", dirname); setOutputPath(dirname); } }; const outputHandler = async () => { var path = await window.electron.invoke(commands.SELECT_FOLDER); if (path !== null) { logit("๐ Setting Output Path: ", path); setOutputPath(path); const rememberOutputFolder = localStorage.getItem("rememberOutputFolder"); if (rememberOutputFolder) { logit("๐ง Remembering Output Folder: ", path); localStorage.setItem("lastOutputFolderPath", path); } } else { setOutputPath(""); } }; const upscaylHandler = async () => { logit("๐ Resetting Upscaled Image Path"); setUpscaledImagePath(""); setUpscaledBatchFolderPath(""); if (imagePath !== "" || batchFolderPath !== "") { setProgress("Hold on..."); if (doubleUpscayl) { window.electron.send(commands.DOUBLE_UPSCAYL, { imagePath, outputPath, model, gpuId: gpuId.length === 0 ? null : gpuId, saveImageAs, scale, }); logit("๐ DOUBLE_UPSCAYL"); } else if (batchMode) { setDoubleUpscayl(false); window.electron.send(commands.FOLDER_UPSCAYL, { scaleFactor, batchFolderPath, outputPath, model, gpuId: gpuId.length === 0 ? null : gpuId, saveImageAs, scale, }); logit("๐ FOLDER_UPSCAYL"); } else { window.electron.send(commands.UPSCAYL, { scaleFactor, imagePath, outputPath, model, gpuId: gpuId.length === 0 ? null : gpuId, saveImageAs, scale, overwrite, }); logit("๐ UPSCAYL"); } } // else if (isVideo && videoPath !== "") { // window.electron.send(commands.UPSCAYL_VIDEO, { // scaleFactor, // videoPath, // outputPath, // model, // gpuId: gpuId.length === 0 ? null : gpuId, // saveImageAs, // }); // } else { alert(`Please select an image to upscale`); logit("๐ซ No valid image selected"); } }; const stopHandler = () => { window.electron.send(commands.STOP); logit("๐ Stopping Upscayl"); resetImagePaths(); }; const allowedFileTypes = ["png", "jpg", "jpeg", "webp"]; if (typeof window === "undefined") { return ( ); } return (
Selected folder:{" "} {batchFolderPath}
)} {/* BATCH UPSCALE DONE INFO */} {batchMode && upscaledBatchFolderPath.length > 0 && ( <>All done!
> )} {/* COMPARISON SLIDER */} {!batchMode && imagePath.length > 0 && upscaledImagePath.length > 0 && ( <>Original
> } itemTwo={ <>Upscayled
{ console.log(e.currentTarget); }} alt="Upscayl" style={{ objectFit: "contain", backgroundPosition: "0% 0%", transformOrigin: backgroundPosition, }} onMouseMove={handleMouseMove} className={`h-full w-full bg-[#1d1c23] transition-transform group-hover:scale-[${zoomAmount}]`} /> > } className="group h-screen" /> > )}