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:
1
package-lock.json
generated
1
package-lock.json
generated
@@ -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": {
|
||||||
|
|||||||
@@ -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,
|
||||||
|
});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user