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,55 +144,82 @@ export const saveFileDialog = (filePath: string, themeOnly: boolean): void => {
const response = dialog.run(); const response = dialog.run();
if (response === Gtk.ResponseType.ACCEPT) { try {
const file_path = dialog.get_filename(); const original_file_path = filePath;
console.info(`Original file path: ${file_path}`);
const getIncrementedFilePath = (filePath: string): string => { const file = Gio.File.new_for_path(original_file_path);
let increment = 1; const [success, content] = file.load_contents(null);
const baseName = filePath.replace(/(\.\w+)$/, '');
const match = filePath.match(/(\.\w+)$/);
const extension = match ? match[0] : '';
let newFilePath = filePath; if (!success) {
let file = Gio.File.new_for_path(newFilePath); console.error(`Could not find 'config.json' at ${TMP}`);
return;
}
while (file.query_exists(null)) { const jsonString = new TextDecoder('utf-8').decode(content);
newFilePath = `${baseName}_${increment}${extension}`; const jsonObject = JSON.parse(jsonString);
file = Gio.File.new_for_path(newFilePath);
increment++;
}
return newFilePath; const filteredJsonObject = themeOnly ? filterHexColorPairs(jsonObject) : filterOutHexColorPairs(jsonObject);
}; const filteredContent = JSON.stringify(filteredJsonObject, null, 2);
const finalFilePath = getIncrementedFilePath(file_path as string); if (response === Gtk.ResponseType.ACCEPT) {
console.info(`File will be saved at: ${finalFilePath}`); const file_path = dialog.get_filename();
console.info(`Original file path: ${file_path}`);
try { const getIncrementedFilePath = (filePath: string): string => {
const save_file = Gio.File.new_for_path(finalFilePath); let increment = 1;
const outputStream = save_file.replace(null, false, Gio.FileCreateFlags.NONE, null); const baseName = filePath.replace(/(\.\w+)$/, '');
const dataOutputStream = new Gio.DataOutputStream({ const match = filePath.match(/(\.\w+)$/);
base_stream: outputStream, const extension = match ? match[0] : '';
});
dataOutputStream.put_string(filteredContent, null); let newFilePath = filePath;
let file = Gio.File.new_for_path(newFilePath);
dataOutputStream.close(null); while (file.query_exists(null)) {
newFilePath = `${baseName}_${increment}${extension}`;
file = Gio.File.new_for_path(newFilePath);
increment++;
}
Notify({ return newFilePath;
summary: 'File Saved Successfully', };
body: `At ${finalFilePath}.`,
iconName: icons.ui.info, const finalFilePath = getIncrementedFilePath(file_path as string);
}); console.info(`File will be saved at: ${finalFilePath}`);
} catch (e) {
if (e instanceof Error) { try {
console.error('Failed to write to file:', e.message); const save_file = Gio.File.new_for_path(finalFilePath);
const outputStream = save_file.replace(null, false, Gio.FileCreateFlags.NONE, null);
const dataOutputStream = new Gio.DataOutputStream({
base_stream: outputStream,
});
dataOutputStream.put_string(filteredContent, null);
dataOutputStream.close(null);
Notify({
summary: 'File Saved Successfully',
body: `At ${finalFilePath}.`,
iconName: icons.ui.info,
});
} catch (e) {
if (e instanceof Error) {
console.error('Failed to write to file:', e.message);
}
} }
} }
}
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,63 +242,74 @@ export const importFiles = (themeOnly: boolean = false): void => {
const response = dialog.run(); const response = dialog.run();
if (response === Gtk.ResponseType.CANCEL) { try {
dialog.destroy(); if (response === Gtk.ResponseType.CANCEL) {
return;
}
if (response === Gtk.ResponseType.ACCEPT) {
const filePath: string | null = dialog.get_filename();
if (filePath === null) {
Notify({
summary: 'Failed to import',
body: 'No file selected.',
iconName: icons.ui.warning,
});
return;
}
const importedConfig = loadJsonFile(filePath);
if (!importedConfig) {
dialog.destroy(); dialog.destroy();
return; return;
} }
if (response === Gtk.ResponseType.ACCEPT) {
const filePath: string | null = dialog.get_filename();
if (filePath === null) {
Notify({
summary: 'Failed to import',
body: 'No file selected.',
iconName: icons.ui.warning,
});
return;
}
const importedConfig = loadJsonFile(filePath);
if (!importedConfig) {
dialog.destroy();
return;
}
Notify({
summary: `Importing ${themeOnly ? 'Theme' : 'Config'}`,
body: `Importing: ${filePath}`,
iconName: icons.ui.info,
});
const tmpConfigFile = Gio.File.new_for_path(`${TMP}/config.json`);
const optionsConfigFile = Gio.File.new_for_path(CONFIG);
const [tmpSuccess, tmpContent] = tmpConfigFile.load_contents(null);
const [optionsSuccess, optionsContent] = optionsConfigFile.load_contents(null);
if (!tmpSuccess || !optionsSuccess) {
console.error('Failed to read existing configuration files.');
dialog.destroy();
return;
}
let tmpConfig = JSON.parse(new TextDecoder('utf-8').decode(tmpContent));
let optionsConfig = JSON.parse(new TextDecoder('utf-8').decode(optionsContent));
if (themeOnly) {
const filteredConfig = filterConfigForThemeOnly(importedConfig);
tmpConfig = { ...tmpConfig, ...filteredConfig };
optionsConfig = { ...optionsConfig, ...filteredConfig };
} else {
const filteredConfig = filterConfigForNonTheme(importedConfig);
tmpConfig = { ...tmpConfig, ...filteredConfig };
optionsConfig = { ...optionsConfig, ...filteredConfig };
}
saveConfigToFile(tmpConfig, `${TMP}/config.json`);
saveConfigToFile(optionsConfig, CONFIG);
}
dialog.destroy();
bash(restartCommand.get());
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
dialog.destroy();
Notify({ Notify({
summary: `Importing ${themeOnly ? 'Theme' : 'Config'}`, summary: `${themeOnly ? 'Theme' : 'Config'} Import Failed`,
body: `Importing: ${filePath}`, body: errorMessage ?? 'An unknown error occurred.',
iconName: icons.ui.info, iconName: icons.ui.warning,
}); });
const tmpConfigFile = Gio.File.new_for_path(`${TMP}/config.json`);
const optionsConfigFile = Gio.File.new_for_path(CONFIG);
const [tmpSuccess, tmpContent] = tmpConfigFile.load_contents(null);
const [optionsSuccess, optionsContent] = optionsConfigFile.load_contents(null);
if (!tmpSuccess || !optionsSuccess) {
console.error('Failed to read existing configuration files.');
dialog.destroy();
return;
}
let tmpConfig = JSON.parse(new TextDecoder('utf-8').decode(tmpContent));
let optionsConfig = JSON.parse(new TextDecoder('utf-8').decode(optionsContent));
if (themeOnly) {
const filteredConfig = filterConfigForThemeOnly(importedConfig);
tmpConfig = { ...tmpConfig, ...filteredConfig };
optionsConfig = { ...optionsConfig, ...filteredConfig };
} else {
const filteredConfig = filterConfigForNonTheme(importedConfig);
tmpConfig = { ...tmpConfig, ...filteredConfig };
optionsConfig = { ...optionsConfig, ...filteredConfig };
}
saveConfigToFile(tmpConfig, `${TMP}/config.json`);
saveConfigToFile(optionsConfig, CONFIG);
} }
dialog.destroy();
bash(restartCommand.get());
}; };