This commit is contained in:
Fhilipe Coelho 2024-04-11 12:39:57 -03:00
commit 53f85503e2
8 changed files with 649 additions and 360 deletions

View File

@ -88,6 +88,9 @@
"delete": "Remove all files"
},
"settings": {
"error_title_modal": "Error",
"error_modal_download": "Your selected folder does not have write permissions or does not exist, please check the folder in your \"Settings\" tab",
"error_description_modal": "The selected directory does not have write permissions or does not exist, please choose another folder",
"downloads_path": "Downloads path",
"change": "Update",
"notifications": "Notifications",

View File

@ -88,6 +88,9 @@
"delete": "Apagar arquivos"
},
"settings": {
"error_title_modal": "Erro",
"error_modal_download": "Você selecionou uma pasta que não tem permissão de escrita ou não existe, por favor verifique na área \"Configurações\"",
"error_description_modal": "O diretório selecionado não possui permissão de escrita ou não existe, favor selecione outra pasta",
"downloads_path": "Diretório dos downloads",
"change": "Mudar",
"notifications": "Notificações",

View File

@ -0,0 +1,10 @@
import fs from "fs";
export default function validatePath(path: string): Error | undefined {
try {
fs.accessSync(path, fs.constants.W_OK);
return;
} catch (error) {
return error as Error;
}
}

View File

@ -8,6 +8,9 @@ import type { GameShop } from "@types";
import { getDownloadsPath } from "../helpers/get-downloads-path";
import { getImageBase64 } from "@main/helpers";
import { In } from "typeorm";
import validatePath from "./helpers/validate-path";
import { dialog } from "electron";
import { t } from "i18next";
const startGameDownload = async (
_event: Electron.IpcMainInvokeEvent,
@ -38,6 +41,20 @@ const startGameDownload = async (
writePipe.write({ action: "pause" });
const downloadsPath = game?.downloadPath ?? (await getDownloadsPath());
const error = validatePath(downloadsPath);
if (error) {
dialog.showErrorBox(
t("error_title_modal", {
ns: "settings",
lng: "en",
}),
`${t("error_modal_download", {
ns: "settings",
lng: "en",
})}${error instanceof Error ? "\n" + error.message : ""}`
);
return;
}
await gameRepository.update(
{

View File

@ -1,19 +1,44 @@
import { userPreferencesRepository } from "@main/repository";
import { registerEvent } from "../register-event";
import { registerEvent } from "./register-event";
import { dialog } from "electron";
import { t } from "i18next";
import type { UserPreferences } from "@types";
import validatePath from "./helpers/validate-path";
const updateUserPreferences = async (
_event: Electron.IpcMainInvokeEvent,
preferences: Partial<UserPreferences>
) => {
await userPreferencesRepository.upsert(
{
id: 1,
...preferences,
},
["id"]
);
const payload = async () =>
await userPreferencesRepository.upsert(
{
id: 1,
...preferences,
},
["id"]
);
if (preferences.downloadsPath) {
const error = validatePath(preferences.downloadsPath);
if (!error) {
payload();
return true;
}
dialog.showErrorBox(
t("error_title_modal", {
ns: "settings",
lng: "en",
}),
`${t("error_description_modal", {
ns: "settings",
lng: "en",
})}${error instanceof Error ? "\n" + error.message : ""}`
);
return false;
}
payload();
return true;
};
registerEvent(updateUserPreferences, {

View File

@ -52,7 +52,7 @@ declare global {
getUserPreferences: () => Promise<UserPreferences | null>;
updateUserPreferences: (
preferences: Partial<UserPreferences>
) => Promise<void>;
) => Promise<boolean>;
/* Hardware */
getDiskFreeSpace: () => Promise<DiskSpace>;

View File

@ -29,15 +29,14 @@ export function Settings() {
});
}, []);
const updateUserPreferences = <T extends keyof UserPreferences>(
const updateUserPreferences = async <T extends keyof UserPreferences>(
field: T,
value: UserPreferences[T]
) => {
setForm((prev) => ({ ...prev, [field]: value }));
window.electron.updateUserPreferences({
const payload = await window.electron.updateUserPreferences({
[field]: value,
});
setForm((prev) => (payload ? { ...prev, [field]: value } : prev));
};
const handleChooseDownloadsPath = async () => {

924
yarn.lock

File diff suppressed because it is too large Load Diff