2023-09-03 11:16:48 +02:00
|
|
|
"use client";
|
2024-10-04 11:15:54 +02:00
|
|
|
import { useState, useEffect } from "react";
|
|
|
|
import { ELECTRON_COMMANDS } from "@common/electron-commands";
|
2024-10-06 09:15:44 +02:00
|
|
|
import { useAtomValue, useSetAtom } from "jotai";
|
|
|
|
import { customModelIdsAtom } from "../atoms/models-list-atom";
|
2023-09-03 11:16:48 +02:00
|
|
|
import {
|
|
|
|
batchModeAtom,
|
2024-04-09 20:11:24 +02:00
|
|
|
savedOutputPathAtom,
|
2023-11-10 12:41:35 +01:00
|
|
|
progressAtom,
|
2024-02-14 07:32:52 +01:00
|
|
|
rememberOutputFolderAtom,
|
2024-10-04 11:15:54 +02:00
|
|
|
} from "../atoms/user-settings-atom";
|
|
|
|
import useLogger from "../components/hooks/use-logger";
|
2024-04-21 16:04:59 +02:00
|
|
|
import { useToast } from "@/components/ui/use-toast";
|
|
|
|
import { ToastAction } from "@/components/ui/toast";
|
2024-10-04 11:15:54 +02:00
|
|
|
import UpscaylSVGLogo from "@/components/icons/upscayl-logo-svg";
|
2024-09-01 14:01:45 +02:00
|
|
|
import { translationAtom } from "@/atoms/translations-atom";
|
2024-10-04 11:15:54 +02:00
|
|
|
import Sidebar from "@/components/sidebar";
|
|
|
|
import MainContent from "@/components/main-content";
|
|
|
|
import getDirectoryFromPath from "@common/get-directory-from-path";
|
|
|
|
import { FEATURE_FLAGS } from "@common/feature-flags";
|
2024-10-06 09:15:44 +02:00
|
|
|
import { ImageFormat, VALID_IMAGE_FORMATS } from "@/lib/valid-formats";
|
2024-10-04 11:15:54 +02:00
|
|
|
import { initCustomModels } from "@/components/hooks/use-custom-models";
|
2023-03-31 12:33:48 +02:00
|
|
|
|
2022-11-11 21:39:28 +01:00
|
|
|
const Home = () => {
|
2024-09-01 14:01:45 +02:00
|
|
|
const t = useAtomValue(translationAtom);
|
2024-10-04 11:15:54 +02:00
|
|
|
const logit = useLogger();
|
|
|
|
const { toast } = useToast();
|
|
|
|
|
|
|
|
initCustomModels();
|
|
|
|
|
|
|
|
const [isLoading, setIsLoading] = useState(true);
|
2023-11-23 09:53:09 +01:00
|
|
|
const [imagePath, setImagePath] = useState("");
|
2023-09-05 17:25:44 +02:00
|
|
|
const [upscaledImagePath, setUpscaledImagePath] = useState("");
|
2022-12-24 08:17:54 +01:00
|
|
|
const [dimensions, setDimensions] = useState({
|
|
|
|
width: null,
|
|
|
|
height: null,
|
|
|
|
});
|
2024-10-04 11:15:54 +02:00
|
|
|
const setOutputPath = useSetAtom(savedOutputPathAtom);
|
|
|
|
const rememberOutputFolder = useAtomValue(rememberOutputFolderAtom);
|
|
|
|
const batchMode = useAtomValue(batchModeAtom);
|
|
|
|
const [batchFolderPath, setBatchFolderPath] = useState("");
|
|
|
|
const [upscaledBatchFolderPath, setUpscaledBatchFolderPath] = useState("");
|
|
|
|
const setProgress = useSetAtom(progressAtom);
|
|
|
|
const [doubleUpscaylCounter, setDoubleUpscaylCounter] = useState(0);
|
2024-10-06 09:15:44 +02:00
|
|
|
const setModelIds = useSetAtom(customModelIdsAtom);
|
2024-04-24 21:29:51 +02:00
|
|
|
|
2024-10-04 11:15:54 +02:00
|
|
|
const selectImageHandler = async () => {
|
|
|
|
resetImagePaths();
|
|
|
|
const path = await window.electron.invoke(ELECTRON_COMMANDS.SELECT_FILE);
|
|
|
|
if (path === null) return;
|
|
|
|
logit("🖼 Selected Image Path: ", path);
|
|
|
|
setImagePath(path);
|
|
|
|
const dirname = getDirectoryFromPath(path);
|
|
|
|
logit("📁 Selected Image Directory: ", dirname);
|
|
|
|
if (!FEATURE_FLAGS.APP_STORE_BUILD) {
|
|
|
|
if (!rememberOutputFolder) {
|
|
|
|
setOutputPath(dirname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
validateImagePath(path);
|
|
|
|
};
|
2024-04-24 21:29:51 +02:00
|
|
|
|
2024-10-04 11:15:54 +02:00
|
|
|
const selectFolderHandler = async () => {
|
|
|
|
resetImagePaths();
|
|
|
|
const path = await window.electron.invoke(ELECTRON_COMMANDS.SELECT_FOLDER);
|
|
|
|
if (path !== null) {
|
|
|
|
logit("🖼 Selected Folder Path: ", path);
|
|
|
|
setBatchFolderPath(path);
|
|
|
|
if (!rememberOutputFolder) {
|
|
|
|
setOutputPath(path);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logit("🚫 Folder selection cancelled");
|
|
|
|
setBatchFolderPath("");
|
|
|
|
if (!rememberOutputFolder) {
|
|
|
|
setOutputPath("");
|
|
|
|
}
|
2024-09-22 03:36:13 +02:00
|
|
|
}
|
2024-01-23 09:44:32 +01:00
|
|
|
};
|
|
|
|
|
2024-10-04 11:15:54 +02:00
|
|
|
const validateImagePath = (path: string) => {
|
|
|
|
if (path.length > 0) {
|
|
|
|
logit("🖼 imagePath: ", path);
|
2024-10-06 09:15:44 +02:00
|
|
|
const extension = path.split(".").pop().toLowerCase() as ImageFormat;
|
2024-10-04 11:15:54 +02:00
|
|
|
logit("🔤 Extension: ", extension);
|
|
|
|
if (!VALID_IMAGE_FORMATS.includes(extension)) {
|
|
|
|
toast({
|
|
|
|
title: t("ERRORS.INVALID_IMAGE_ERROR.TITLE"),
|
|
|
|
description: t("ERRORS.INVALID_IMAGE_ERROR.DESCRIPTION"),
|
|
|
|
});
|
|
|
|
resetImagePaths();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
resetImagePaths();
|
|
|
|
}
|
|
|
|
};
|
2023-11-23 09:53:09 +01:00
|
|
|
|
2023-11-23 10:25:55 +01:00
|
|
|
// ELECTRON EVENT LISTENERS
|
2023-11-23 09:53:09 +01:00
|
|
|
useEffect(() => {
|
2023-04-08 08:53:32 +02:00
|
|
|
const handleErrors = (data: string) => {
|
2024-04-21 16:04:59 +02:00
|
|
|
if (data.includes("Invalid GPU")) {
|
|
|
|
toast({
|
2024-09-03 09:34:58 +02:00
|
|
|
title: t("ERRORS.GPU_ERROR.TITLE"),
|
|
|
|
description: t("ERRORS.GPU_ERROR.DESCRIPTION", { data }),
|
2024-04-21 16:04:59 +02:00
|
|
|
action: (
|
2024-04-22 09:55:44 +02:00
|
|
|
<div className="flex flex-col gap-2">
|
|
|
|
<ToastAction
|
2024-09-03 09:34:58 +02:00
|
|
|
altText={t("ERRORS.COPY_ERROR.TITLE")}
|
2024-04-22 09:55:44 +02:00
|
|
|
onClick={() => {
|
|
|
|
navigator.clipboard.writeText(data);
|
|
|
|
}}
|
|
|
|
>
|
2024-09-03 09:34:58 +02:00
|
|
|
{t("ERRORS.COPY_ERROR.TITLE")}
|
2024-04-22 09:55:44 +02:00
|
|
|
</ToastAction>
|
2024-04-24 22:28:03 +02:00
|
|
|
<a href="https://docs.upscayl.org/" target="_blank">
|
2024-09-03 09:34:58 +02:00
|
|
|
<ToastAction altText={t("ERRORS.OPEN_DOCS_TITLE")}>
|
|
|
|
{t("ERRORS.OPEN_DOCS_BUTTON_TITLE")}
|
2024-09-01 14:01:45 +02:00
|
|
|
</ToastAction>
|
2024-04-22 09:55:44 +02:00
|
|
|
</a>
|
|
|
|
</div>
|
2024-04-21 16:04:59 +02:00
|
|
|
),
|
|
|
|
});
|
2022-11-11 21:39:28 +01:00
|
|
|
resetImagePaths();
|
2024-04-21 16:04:59 +02:00
|
|
|
} else if (data.includes("write") || data.includes("read")) {
|
2022-11-11 21:39:28 +01:00
|
|
|
if (batchMode) return;
|
2024-04-21 16:04:59 +02:00
|
|
|
toast({
|
2024-09-03 09:34:58 +02:00
|
|
|
title: t("ERRORS.READ_WRITE_ERROR.TITLE"),
|
|
|
|
description: t("ERRORS.READ_WRITE_ERROR.DESCRIPTION", { data }),
|
2024-04-21 16:04:59 +02:00
|
|
|
action: (
|
2024-04-22 09:55:44 +02:00
|
|
|
<div className="flex flex-col gap-2">
|
|
|
|
<ToastAction
|
|
|
|
altText="Copy Error"
|
|
|
|
onClick={() => {
|
|
|
|
navigator.clipboard.writeText(data);
|
|
|
|
}}
|
|
|
|
>
|
2024-09-03 09:34:58 +02:00
|
|
|
{t("ERRORS.COPY_ERROR.TITLE")}
|
2024-04-22 09:55:44 +02:00
|
|
|
</ToastAction>
|
2024-04-24 22:28:03 +02:00
|
|
|
<a href="https://docs.upscayl.org/" target="_blank">
|
2024-09-03 09:34:58 +02:00
|
|
|
<ToastAction altText={t("ERRORS.OPEN_DOCS_TITLE")}>
|
|
|
|
{t("ERRORS.OPEN_DOCS_BUTTON_TITLE")}
|
2024-09-01 14:01:45 +02:00
|
|
|
</ToastAction>
|
2024-04-22 09:55:44 +02:00
|
|
|
</a>
|
|
|
|
</div>
|
2024-04-21 16:04:59 +02:00
|
|
|
),
|
|
|
|
});
|
|
|
|
resetImagePaths();
|
|
|
|
} else if (data.includes("tile size")) {
|
|
|
|
toast({
|
2024-09-03 09:34:58 +02:00
|
|
|
title: t("ERRORS.TILE_SIZE_ERROR.TITLE"),
|
|
|
|
description: t("ERRORS.TILE_SIZE_ERROR.DESCRIPTION", { data }),
|
2024-04-21 16:04:59 +02:00
|
|
|
});
|
2022-11-11 21:39:28 +01:00
|
|
|
resetImagePaths();
|
|
|
|
} else if (data.includes("uncaughtException")) {
|
2024-04-21 16:04:59 +02:00
|
|
|
toast({
|
2024-09-03 09:34:58 +02:00
|
|
|
title: t("ERRORS.EXCEPTION_ERROR.TITLE"),
|
|
|
|
description: t("ERRORS.EXCEPTION_ERROR.DESCRIPTION"),
|
2024-04-21 16:04:59 +02:00
|
|
|
});
|
2022-11-11 21:39:28 +01:00
|
|
|
resetImagePaths();
|
|
|
|
}
|
|
|
|
};
|
2023-04-14 12:31:37 +02:00
|
|
|
// LOG
|
2024-10-04 11:15:54 +02:00
|
|
|
window.electron.on(ELECTRON_COMMANDS.LOG, (_, data: string) => {
|
2024-04-25 12:43:16 +02:00
|
|
|
logit(`🎒 BACKEND REPORTED: `, data);
|
2023-04-14 12:31:37 +02:00
|
|
|
});
|
2023-11-23 09:53:09 +01:00
|
|
|
// SCALING AND CONVERTING
|
2024-10-04 11:15:54 +02:00
|
|
|
window.electron.on(
|
|
|
|
ELECTRON_COMMANDS.SCALING_AND_CONVERTING,
|
|
|
|
(_, data: string) => {
|
|
|
|
setProgress(t("APP.PROGRESS.PROCESSING_TITLE"));
|
|
|
|
},
|
|
|
|
);
|
2023-11-23 09:53:09 +01:00
|
|
|
// UPSCAYL ERROR
|
2024-10-04 11:15:54 +02:00
|
|
|
window.electron.on(ELECTRON_COMMANDS.UPSCAYL_ERROR, (_, data: string) => {
|
2024-04-21 16:04:59 +02:00
|
|
|
toast({
|
2024-09-03 09:34:58 +02:00
|
|
|
title: t("ERRORS.GENERIC_ERROR.TITLE"),
|
2024-04-21 16:04:59 +02:00
|
|
|
description: data,
|
|
|
|
});
|
2023-09-09 13:03:16 +02:00
|
|
|
resetImagePaths();
|
|
|
|
});
|
2022-12-16 17:20:46 +01:00
|
|
|
// UPSCAYL PROGRESS
|
2024-10-04 11:15:54 +02:00
|
|
|
window.electron.on(
|
|
|
|
ELECTRON_COMMANDS.UPSCAYL_PROGRESS,
|
|
|
|
(_, data: string) => {
|
|
|
|
if (data.length > 0 && data.length < 10) {
|
|
|
|
setProgress(data);
|
|
|
|
} else if (data.includes("converting")) {
|
|
|
|
setProgress(t("APP.PROGRESS.SCALING_CONVERTING_TITLE"));
|
|
|
|
} else if (data.includes("Successful")) {
|
|
|
|
setProgress(t("APP.PROGRESS.SUCCESS_TITLE"));
|
|
|
|
}
|
|
|
|
handleErrors(data);
|
|
|
|
logit(`🚧 UPSCAYL_PROGRESS: `, data);
|
|
|
|
},
|
|
|
|
);
|
2022-12-16 17:20:46 +01:00
|
|
|
// FOLDER UPSCAYL PROGRESS
|
2024-10-04 11:15:54 +02:00
|
|
|
window.electron.on(
|
|
|
|
ELECTRON_COMMANDS.FOLDER_UPSCAYL_PROGRESS,
|
|
|
|
(_, data: string) => {
|
|
|
|
if (data.includes("Successful")) {
|
|
|
|
setProgress(t("APP.PROGRESS.SUCCESS_TITLE"));
|
|
|
|
}
|
|
|
|
if (data.length > 0 && data.length < 10) {
|
|
|
|
setProgress(data);
|
|
|
|
}
|
|
|
|
handleErrors(data);
|
|
|
|
logit(`🚧 FOLDER_UPSCAYL_PROGRESS: `, data);
|
|
|
|
},
|
|
|
|
);
|
2022-12-16 17:20:46 +01:00
|
|
|
// DOUBLE UPSCAYL PROGRESS
|
2024-10-04 11:15:54 +02:00
|
|
|
window.electron.on(
|
|
|
|
ELECTRON_COMMANDS.DOUBLE_UPSCAYL_PROGRESS,
|
|
|
|
(_, data: string) => {
|
|
|
|
if (data.length > 0 && data.length < 10) {
|
|
|
|
if (data === "0.00%") {
|
|
|
|
setDoubleUpscaylCounter(doubleUpscaylCounter + 1);
|
|
|
|
}
|
|
|
|
setProgress(data);
|
2022-12-02 15:21:42 +01:00
|
|
|
}
|
2024-10-04 11:15:54 +02:00
|
|
|
handleErrors(data);
|
|
|
|
logit(`🚧 DOUBLE_UPSCAYL_PROGRESS: `, data);
|
|
|
|
},
|
|
|
|
);
|
2022-12-16 17:20:46 +01:00
|
|
|
// UPSCAYL DONE
|
2024-10-04 11:15:54 +02:00
|
|
|
window.electron.on(ELECTRON_COMMANDS.UPSCAYL_DONE, (_, data: string) => {
|
2022-11-11 21:39:28 +01:00
|
|
|
setProgress("");
|
2023-09-13 18:15:42 +02:00
|
|
|
setUpscaledImagePath(data);
|
2023-07-22 15:00:46 +02:00
|
|
|
logit("upscaledImagePath: ", data);
|
2023-05-06 07:17:35 +02:00
|
|
|
logit(`💯 UPSCAYL_DONE: `, data);
|
2022-11-11 21:39:28 +01:00
|
|
|
});
|
2022-12-16 17:20:46 +01:00
|
|
|
// FOLDER UPSCAYL DONE
|
2024-10-04 11:15:54 +02:00
|
|
|
window.electron.on(
|
|
|
|
ELECTRON_COMMANDS.FOLDER_UPSCAYL_DONE,
|
|
|
|
(_, data: string) => {
|
|
|
|
setProgress("");
|
|
|
|
setUpscaledBatchFolderPath(data);
|
|
|
|
logit(`💯 FOLDER_UPSCAYL_DONE: `, data);
|
|
|
|
},
|
|
|
|
);
|
2022-12-16 17:20:46 +01:00
|
|
|
// DOUBLE UPSCAYL DONE
|
2024-10-04 11:15:54 +02:00
|
|
|
window.electron.on(
|
|
|
|
ELECTRON_COMMANDS.DOUBLE_UPSCAYL_DONE,
|
|
|
|
(_, data: string) => {
|
|
|
|
setProgress("");
|
|
|
|
setTimeout(() => setUpscaledImagePath(data), 500);
|
|
|
|
setDoubleUpscaylCounter(0);
|
|
|
|
logit(`💯 DOUBLE_UPSCAYL_DONE: `, data);
|
|
|
|
},
|
|
|
|
);
|
2023-04-09 07:48:53 +02:00
|
|
|
// CUSTOM FOLDER LISTENER
|
2024-10-04 11:15:54 +02:00
|
|
|
window.electron.on(
|
|
|
|
ELECTRON_COMMANDS.CUSTOM_MODEL_FILES_LIST,
|
|
|
|
(_, data: string[]) => {
|
|
|
|
logit(`📜 CUSTOM_MODEL_FILES_LIST: `, data);
|
2024-10-06 09:15:44 +02:00
|
|
|
console.log("🚀 => data:", data);
|
|
|
|
setModelIds(data);
|
2024-10-04 11:15:54 +02:00
|
|
|
},
|
2023-04-09 07:48:53 +02:00
|
|
|
);
|
2022-11-11 21:39:28 +01:00
|
|
|
}, []);
|
|
|
|
|
2023-11-23 09:53:09 +01:00
|
|
|
// LOADING STATE
|
2023-10-15 05:57:11 +02:00
|
|
|
useEffect(() => {
|
|
|
|
setIsLoading(false);
|
|
|
|
}, []);
|
|
|
|
|
2024-04-24 21:29:51 +02:00
|
|
|
// HANDLERS
|
2022-12-16 17:20:46 +01:00
|
|
|
const resetImagePaths = () => {
|
2023-05-06 07:17:35 +02:00
|
|
|
logit("🔄 Resetting image paths");
|
2022-12-24 08:17:54 +01:00
|
|
|
setDimensions({
|
|
|
|
width: null,
|
|
|
|
height: null,
|
|
|
|
});
|
2022-12-16 17:20:46 +01:00
|
|
|
setProgress("");
|
2023-11-23 09:53:09 +01:00
|
|
|
setImagePath("");
|
2022-12-16 17:20:46 +01:00
|
|
|
setUpscaledImagePath("");
|
|
|
|
setBatchFolderPath("");
|
|
|
|
setUpscaledBatchFolderPath("");
|
|
|
|
};
|
|
|
|
|
2023-10-15 05:57:11 +02:00
|
|
|
if (isLoading) {
|
2023-08-30 06:54:16 +02:00
|
|
|
return (
|
2024-10-04 11:15:54 +02:00
|
|
|
<UpscaylSVGLogo className="absolute left-1/2 top-1/2 w-36 -translate-x-1/2 -translate-y-1/2 animate-pulse" />
|
2023-08-30 06:54:16 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-11-11 21:39:28 +01:00
|
|
|
return (
|
2022-11-11 22:32:24 +01:00
|
|
|
<div className="flex h-screen w-screen flex-row overflow-hidden bg-base-300">
|
2024-10-04 11:15:54 +02:00
|
|
|
<Sidebar
|
|
|
|
imagePath={imagePath}
|
|
|
|
dimensions={dimensions}
|
|
|
|
setUpscaledImagePath={setUpscaledImagePath}
|
|
|
|
batchFolderPath={batchFolderPath}
|
|
|
|
setUpscaledBatchFolderPath={setUpscaledBatchFolderPath}
|
|
|
|
selectImageHandler={selectImageHandler}
|
|
|
|
selectFolderHandler={selectFolderHandler}
|
|
|
|
/>
|
|
|
|
<MainContent
|
|
|
|
imagePath={imagePath}
|
|
|
|
resetImagePaths={resetImagePaths}
|
|
|
|
upscaledBatchFolderPath={upscaledBatchFolderPath}
|
|
|
|
setImagePath={setImagePath}
|
|
|
|
validateImagePath={validateImagePath}
|
|
|
|
selectFolderHandler={selectFolderHandler}
|
|
|
|
selectImageHandler={selectImageHandler}
|
|
|
|
batchFolderPath={batchFolderPath}
|
|
|
|
upscaledImagePath={upscaledImagePath}
|
|
|
|
doubleUpscaylCounter={doubleUpscaylCounter}
|
|
|
|
setDimensions={setDimensions}
|
|
|
|
/>
|
2022-11-11 21:39:28 +01:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Home;
|