1
0
mirror of https://github.com/upscayl/upscayl.git synced 2024-12-19 02:45:54 +01:00
upscayl/renderer/components/sidebar/settings-tab/index.tsx

268 lines
7.9 KiB
TypeScript
Raw Normal View History

import { SelectTheme } from "./select-theme";
import { SaveOutputFolderToggle } from "./save-output-folder-toggle";
import { InputGpuId } from "./input-gpu-id";
import { CustomModelsFolderSelect } from "./select-custom-models-folder";
import { LogArea } from "./log-area";
import { SelectImageScale } from "./select-image-scale";
import { SelectImageFormat } from "./select-image-format";
import { DonateButton } from "./donate-button";
2023-07-22 13:07:53 +02:00
import React, { useEffect, useState } from "react";
import { themeChange } from "theme-change";
import { useAtom, useAtomValue } from "jotai";
import { customModelsPathAtom, scaleAtom } from "@/atoms/user-settings-atom";
import { modelsListAtom } from "@/atoms/models-list-atom";
import useLogger from "@/components/hooks/use-logger";
import { InputCompression } from "./input-compression";
import OverwriteToggle from "./overwrite-toggle";
import { UpscaylCloudModal } from "@/components/upscayl-cloud-modal";
import { ResetSettingsButton } from "./reset-settings-button";
import { FEATURE_FLAGS } from "@common/feature-flags";
import TurnOffNotificationsToggle from "./turn-off-notifications-toggle";
2024-04-18 20:45:12 +02:00
import { cn } from "@/lib/utils";
import { InputCustomResolution } from "./input-custom-resolution";
import { InputTileSize } from "./input-tile-size";
import LanguageSwitcher from "./language-switcher";
import { translationAtom } from "@/atoms/translations-atom";
2023-07-22 13:07:53 +02:00
interface IProps {
batchMode: boolean;
setModel: React.Dispatch<React.SetStateAction<string>>;
saveImageAs: string;
setSaveImageAs: React.Dispatch<React.SetStateAction<string>>;
2023-09-13 16:07:45 +02:00
compression: number;
setCompression: React.Dispatch<React.SetStateAction<number>>;
2023-07-22 13:07:53 +02:00
gpuId: string;
setGpuId: React.Dispatch<React.SetStateAction<string>>;
logData: string[];
2023-09-03 11:16:48 +02:00
show: boolean;
setShow: React.Dispatch<React.SetStateAction<boolean>>;
setDontShowCloudModal: React.Dispatch<React.SetStateAction<boolean>>;
2023-07-22 13:07:53 +02:00
}
function SettingsTab({
batchMode,
setModel,
2023-09-13 16:07:45 +02:00
compression,
setCompression,
2023-07-22 13:07:53 +02:00
gpuId,
setGpuId,
saveImageAs,
setSaveImageAs,
logData,
2023-09-03 11:16:48 +02:00
show,
setShow,
setDontShowCloudModal,
2023-07-22 13:07:53 +02:00
}: IProps) {
const [isCopied, setIsCopied] = useState(false);
const [customModelsPath, setCustomModelsPath] = useAtom(customModelsPathAtom);
const modelOptions = useAtomValue(modelsListAtom);
const [scale, setScale] = useAtom(scaleAtom);
2024-04-18 20:45:12 +02:00
const [enableScrollbar, setEnableScrollbar] = useState(true);
const [timeoutId, setTimeoutId] = useState(null);
const t = useAtomValue(translationAtom);
2023-07-22 13:07:53 +02:00
const logit = useLogger();
2023-07-22 13:07:53 +02:00
useEffect(() => {
themeChange(false);
if (!localStorage.getItem("saveImageAs")) {
logit("⚙️ Setting saveImageAs to png");
localStorage.setItem("saveImageAs", "png");
} else {
const currentlySavedImageFormat = localStorage.getItem("saveImageAs");
logit(
2023-09-13 16:07:45 +02:00
"⚙️ Getting saveImageAs from localStorage: ",
2024-02-08 15:57:35 +01:00
currentlySavedImageFormat,
2023-07-22 13:07:53 +02:00
);
setSaveImageAs(currentlySavedImageFormat);
}
if (!localStorage.getItem("model")) {
setModel(modelOptions[0].value);
localStorage.setItem("model", JSON.stringify(modelOptions[0]));
logit("🔀 Setting model to", modelOptions[0].value);
} else {
2023-09-25 05:39:21 +02:00
let currentlySavedModel = JSON.parse(
2024-02-08 15:57:35 +01:00
localStorage.getItem("model"),
2023-07-22 13:07:53 +02:00
) as (typeof modelOptions)[0];
2023-09-25 05:39:21 +02:00
if (
modelOptions.find(
2024-02-08 15:57:35 +01:00
(model) => model.value === currentlySavedModel.value,
2023-09-25 05:39:21 +02:00
) === undefined
) {
localStorage.setItem("model", JSON.stringify(modelOptions[0]));
logit("🔀 Setting model to", modelOptions[0].value);
currentlySavedModel = modelOptions[0];
}
2023-07-22 13:07:53 +02:00
setModel(currentlySavedModel.value);
logit(
2023-09-13 16:07:45 +02:00
"⚙️ Getting model from localStorage: ",
2024-02-08 15:57:35 +01:00
JSON.stringify(currentlySavedModel),
2023-07-22 13:07:53 +02:00
);
}
if (!localStorage.getItem("gpuId")) {
localStorage.setItem("gpuId", "");
logit("⚙️ Setting gpuId to empty string");
} else {
const currentlySavedGpuId = localStorage.getItem("gpuId");
setGpuId(currentlySavedGpuId);
2023-09-13 16:07:45 +02:00
logit("⚙️ Getting gpuId from localStorage: ", currentlySavedGpuId);
2023-07-22 13:07:53 +02:00
}
2023-09-09 12:13:16 +02:00
}, []);
2023-07-22 13:07:53 +02:00
// HANDLERS
const setExportType = (format: string) => {
setSaveImageAs(format);
localStorage.setItem("saveImageAs", format);
};
const handleCompressionChange = (e) => {
2023-09-13 16:07:45 +02:00
setCompression(e.target.value);
};
2023-07-22 13:07:53 +02:00
const handleGpuIdChange = (e) => {
setGpuId(e.target.value);
localStorage.setItem("gpuId", e.target.value);
};
const copyOnClickHandler = () => {
navigator.clipboard.writeText(logData.join("\n"));
setIsCopied(true);
setTimeout(() => {
setIsCopied(false);
}, 2000);
};
const upscaylVersion = navigator?.userAgent?.match(
2024-02-08 15:57:35 +01:00
/Upscayl\/([\d\.]+\d+)/,
)[1];
2024-04-18 20:45:12 +02:00
function disableScrolling() {
if (timeoutId !== null) {
clearTimeout(timeoutId);
}
setTimeoutId(
setTimeout(function () {
setEnableScrollbar(false);
}, 1000),
);
}
function enableScrolling() {
if (timeoutId !== null) {
clearTimeout(timeoutId);
}
setEnableScrollbar(true);
}
2023-07-22 13:07:53 +02:00
return (
2024-04-18 20:45:12 +02:00
<div
className={cn(
"animate-step-in animate z-50 flex h-screen flex-col gap-7 overflow-y-auto overflow-x-hidden p-5",
enableScrollbar ? "" : "hide-scrollbar",
)}
onScroll={() => {
if (enableScrollbar) disableScrolling();
}}
onWheel={() => {
enableScrolling();
}}
>
<div className="flex flex-col gap-2 text-sm font-medium uppercase">
<p>{t("SETTINGS.SUPPORT.TITLE")}</p>
2023-07-23 08:32:22 +02:00
<a
2024-02-08 15:57:35 +01:00
className="btn btn-primary"
2024-04-24 22:28:03 +02:00
href="https://docs.upscayl.org/"
2024-02-08 15:57:35 +01:00
target="_blank"
>
{t("SETTINGS.SUPPORT.DOCS_BUTTON_TITLE")}
2023-07-23 08:32:22 +02:00
</a>
{FEATURE_FLAGS.APP_STORE_BUILD && (
<a
2024-02-08 15:57:35 +01:00
className="btn btn-primary"
href={`mailto:upscayl@gmail.com?subject=Upscayl%20Issue%3A%20%3CIssue%20name%20here%3E&body=Device%20Name%3A%20%3CYOUR%20DEVICE%20MODEL%3E%0AOperating%20System%3A%20%3CYOUR%20OPERATING%20SYSTEM%20VERSION%3E%0AUpscayl%20Version%3A%20${upscaylVersion}%0A%0AHi%2C%20I'm%20having%20an%20issue%20with%20Upscayl.%20%3CDESCRIBE%20ISSUE%20HERE%3E`}
2024-02-08 15:57:35 +01:00
target="_blank"
>
{t("SETTINGS.SUPPORT.EMAIL_BUTTON_TITLE")}
</a>
)}
{!FEATURE_FLAGS.APP_STORE_BUILD && <DonateButton />}
2023-07-23 08:32:22 +02:00
</div>
2023-07-22 13:07:53 +02:00
<LogArea
copyOnClickHandler={copyOnClickHandler}
isCopied={isCopied}
logData={logData}
/>
2023-07-22 13:07:53 +02:00
{/* THEME SELECTOR */}
<SelectTheme />
2023-07-22 13:07:53 +02:00
<LanguageSwitcher />
{/* IMAGE FORMAT BUTTONS */}
<SelectImageFormat
batchMode={batchMode}
saveImageAs={saveImageAs}
setExportType={setExportType}
/>
2024-04-09 20:21:54 +02:00
{/* IMAGE SCALE */}
<SelectImageScale scale={scale} setScale={setScale} />
2024-02-08 15:57:35 +01:00
<InputCustomResolution />
2024-04-20 17:44:42 +02:00
<InputCompression
2024-04-09 20:21:54 +02:00
compression={compression}
handleCompressionChange={handleCompressionChange}
/>
2023-09-18 20:30:43 +02:00
2024-02-14 07:32:52 +01:00
<SaveOutputFolderToggle />
2023-07-22 13:07:53 +02:00
<OverwriteToggle />
2024-01-16 08:33:43 +01:00
<TurnOffNotificationsToggle />
2023-08-10 12:17:35 +02:00
2023-07-22 13:07:53 +02:00
{/* GPU ID INPUT */}
<InputGpuId gpuId={gpuId} handleGpuIdChange={handleGpuIdChange} />
2023-07-22 13:07:53 +02:00
<InputTileSize />
2023-07-22 13:07:53 +02:00
{/* CUSTOM MODEL */}
<CustomModelsFolderSelect
customModelsPath={customModelsPath}
setCustomModelsPath={setCustomModelsPath}
/>
2023-09-03 11:16:48 +02:00
{/* RESET SETTINGS */}
<ResetSettingsButton />
2023-09-03 11:16:48 +02:00
{FEATURE_FLAGS.SHOW_UPSCAYL_CLOUD_INFO && (
2023-11-02 16:03:21 +01:00
<>
<button
2024-04-20 17:44:42 +02:00
className="mx-5 mb-5 animate-pulse rounded-btn bg-success p-1 text-sm text-slate-50 shadow-lg shadow-success/40"
2023-11-02 16:03:21 +01:00
onClick={() => {
setShow(true);
2024-02-08 15:57:35 +01:00
}}
>
{t("INTRO")}
2023-11-02 16:03:21 +01:00
</button>
2023-09-03 11:16:48 +02:00
2023-11-02 16:03:21 +01:00
<UpscaylCloudModal
show={show}
setShow={setShow}
setDontShowCloudModal={setDontShowCloudModal}
/>
</>
)}
2023-07-22 13:07:53 +02:00
</div>
);
}
export default SettingsTab;