Added a notification if an import or export of a config/theme fails. (#664)

* Added a notification if an import or export of a config/file fails.

* Use const
This commit is contained in:
Jas Singh
2024-12-28 18:56:51 -08:00
committed by GitHub
parent 9343ae85a3
commit 2b06b214a8
2 changed files with 128 additions and 105 deletions

1
package-lock.json generated
View File

@@ -25,6 +25,7 @@
} }
}, },
"../../../../usr/share/astal/gjs": { "../../../../usr/share/astal/gjs": {
"name": "astal",
"license": "LGPL-2.1" "license": "LGPL-2.1"
}, },
"node_modules/@eslint-community/eslint-utils": { "node_modules/@eslint-community/eslint-utils": {

View File

@@ -100,19 +100,6 @@ export const filterConfigForNonTheme = (config: Config): Config => {
* @param themeOnly - A flag indicating whether to save only theme-related properties. * @param themeOnly - A flag indicating whether to save only theme-related properties.
*/ */
export const saveFileDialog = (filePath: string, themeOnly: boolean): void => { export const saveFileDialog = (filePath: string, themeOnly: boolean): void => {
const original_file_path = filePath;
const file = Gio.File.new_for_path(original_file_path);
const [success, content] = file.load_contents(null);
if (!success) {
console.error(`Could not find 'config.json' at ${TMP}`);
return;
}
const jsonString = new TextDecoder('utf-8').decode(content);
const jsonObject = JSON.parse(jsonString);
const filterHexColorPairs = (jsonObject: Config): Config => { const filterHexColorPairs = (jsonObject: Config): Config => {
const filteredObject: Config = {}; const filteredObject: Config = {};
@@ -145,9 +132,6 @@ export const saveFileDialog = (filePath: string, themeOnly: boolean): void => {
return filteredObject; return filteredObject;
}; };
const filteredJsonObject = themeOnly ? filterHexColorPairs(jsonObject) : filterOutHexColorPairs(jsonObject);
const filteredContent = JSON.stringify(filteredJsonObject, null, 2);
const dialog = new Gtk.FileChooserDialog({ const dialog = new Gtk.FileChooserDialog({
title: `Save Hyprpanel ${themeOnly ? 'Theme' : 'Config'}`, title: `Save Hyprpanel ${themeOnly ? 'Theme' : 'Config'}`,
action: Gtk.FileChooserAction.SAVE, action: Gtk.FileChooserAction.SAVE,
@@ -160,6 +144,23 @@ export const saveFileDialog = (filePath: string, themeOnly: boolean): void => {
const response = dialog.run(); const response = dialog.run();
try {
const original_file_path = filePath;
const file = Gio.File.new_for_path(original_file_path);
const [success, content] = file.load_contents(null);
if (!success) {
console.error(`Could not find 'config.json' at ${TMP}`);
return;
}
const jsonString = new TextDecoder('utf-8').decode(content);
const jsonObject = JSON.parse(jsonString);
const filteredJsonObject = themeOnly ? filterHexColorPairs(jsonObject) : filterOutHexColorPairs(jsonObject);
const filteredContent = JSON.stringify(filteredJsonObject, null, 2);
if (response === Gtk.ResponseType.ACCEPT) { if (response === Gtk.ResponseType.ACCEPT) {
const file_path = dialog.get_filename(); const file_path = dialog.get_filename();
console.info(`Original file path: ${file_path}`); console.info(`Original file path: ${file_path}`);
@@ -209,6 +210,16 @@ export const saveFileDialog = (filePath: string, themeOnly: boolean): void => {
} }
dialog.destroy(); dialog.destroy();
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
dialog.destroy();
Notify({
summary: `${themeOnly ? 'Theme' : 'Config'} Export Failed`,
body: errorMessage ?? 'An unknown error occurred.',
iconName: icons.ui.warning,
});
}
}; };
/** /**
@@ -231,6 +242,7 @@ export const importFiles = (themeOnly: boolean = false): void => {
const response = dialog.run(); const response = dialog.run();
try {
if (response === Gtk.ResponseType.CANCEL) { if (response === Gtk.ResponseType.CANCEL) {
dialog.destroy(); dialog.destroy();
return; return;
@@ -290,4 +302,14 @@ export const importFiles = (themeOnly: boolean = false): void => {
} }
dialog.destroy(); dialog.destroy();
bash(restartCommand.get()); bash(restartCommand.get());
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
dialog.destroy();
Notify({
summary: `${themeOnly ? 'Theme' : 'Config'} Import Failed`,
body: errorMessage ?? 'An unknown error occurred.',
iconName: icons.ui.warning,
});
}
}; };