1
0
mirror of https://github.com/upscayl/upscayl.git synced 2024-12-18 18:35:58 +01:00
upscayl/electron/commands/custom-models-select.ts
NayamAmarshe 95843ded88
Refactor Renderer Code (#987)
* Initial refactor

* Remove unused imports

* Update code

* Refactor and Update Code

- Change file names to kebab-caase
- Add new useTranslation Hook
- Change useLog hook name to useLogger
- Update translation hook to provide autocomplete

* Update import and component name

* Rename files and components

* Update locales

* Update electron commands

* Update var

* Change Lowercase

* Replace filter with map

* Add props

* Update flag check

* Add validate paths

* Update formats

* Update import

* Update function

* Update function and translation

* Update handlePaste
2024-10-04 14:45:54 +05:30

69 lines
2.1 KiB
TypeScript

import { MessageBoxOptions, dialog } from "electron";
import {
savedCustomModelsPath,
setSavedCustomModelsPath,
} from "../utils/config-variables";
import logit from "../utils/logit";
import slash from "../utils/slash";
import { ELECTRON_COMMANDS } from "@common/electron-commands";
import getModels from "../utils/get-models";
import { getMainWindow } from "../main-window";
import settings from "electron-settings";
import { FEATURE_FLAGS } from "../../common/feature-flags";
const customModelsSelect = async (event, message) => {
const mainWindow = getMainWindow();
if (!mainWindow) return;
const {
canceled,
filePaths: folderPaths,
bookmarks,
} = await dialog.showOpenDialog({
properties: ["openDirectory"],
title: "Select Custom Models Folder",
defaultPath: savedCustomModelsPath,
securityScopedBookmarks: true,
message: "Select Custom Models Folder that is named 'models'",
});
if (FEATURE_FLAGS.APP_STORE_BUILD && bookmarks && bookmarks.length > 0) {
console.log("🚨 Setting Bookmark: ", bookmarks);
await settings.set("custom-models-bookmarks", bookmarks[0]);
}
if (canceled) {
logit("🚫 Select Custom Models Folder Operation Cancelled");
return null;
} else {
setSavedCustomModelsPath(folderPaths[0]);
if (
!folderPaths[0].endsWith(slash + "models") &&
!folderPaths[0].endsWith(slash + "models" + slash)
) {
logit("❌ Invalid Custom Models Folder Detected: Not a 'models' folder");
const options: MessageBoxOptions = {
type: "error",
title: "Invalid Folder",
message:
"Please make sure that the folder name is 'models' and nothing else.",
buttons: ["OK"],
};
dialog.showMessageBoxSync(options);
return null;
}
const models = await getModels(savedCustomModelsPath);
mainWindow.webContents.send(
ELECTRON_COMMANDS.CUSTOM_MODEL_FILES_LIST,
models,
);
logit("📁 Custom Folder Path: ", savedCustomModelsPath);
return savedCustomModelsPath;
}
};
export default customModelsSelect;