web/TransferSettings: friendlier error messages

This commit is contained in:
wukko 2024-07-31 14:57:34 +06:00
parent 585ebd9cb4
commit e7587a2ec6
No known key found for this signature in database
GPG key ID: 3E30B3F26C7B4AA2
3 changed files with 22 additions and 19 deletions

5
web/i18n/en/error.json Normal file
View file

@ -0,0 +1,5 @@
{
"import.no_data": "there's nothing to load from the file. are you sure it's the right one?",
"import.invalid": "your file doesn't have valid cobalt settings to import. are you sure it's the right one?",
"import.unknown": "couldn't load data from the file. it may be corrupted or of wrong format. here's the error i got:\n{{ value }}"
}

View file

@ -103,7 +103,5 @@
"advanced.data": "settings data", "advanced.data": "settings data",
"advanced.reset": "reset all settings", "advanced.reset": "reset all settings",
"advanced.import": "import", "advanced.import": "import",
"advanced.import.no_data": "failed loading setting data from file",
"advanced.import.invalid": "file does not contain valid cobalt settings",
"advanced.export": "export" "advanced.export": "export"
} }

View file

@ -4,7 +4,7 @@
import { import {
storedSettings, storedSettings,
updateSetting, updateSetting,
loadFromString loadFromString,
} from "$lib/state/settings"; } from "$lib/state/settings";
import { validateSettings } from "$lib/settings/validate"; import { validateSettings } from "$lib/settings/validate";
@ -16,12 +16,11 @@
const updateSettings = (reader: FileReader) => { const updateSettings = (reader: FileReader) => {
try { try {
const data = reader.result?.toString(); const data = reader.result?.toString();
if (!data) if (!data) throw $t("error.import.no_data");
throw $t('settings.advanced.import.no_data');
const loadedSettings = loadFromString(data); const loadedSettings = loadFromString(data);
if (!validateSettings(loadedSettings)) if (!validateSettings(loadedSettings))
throw $t('settings.advanced.import.invalid'); throw $t("error.import.invalid");
createDialog({ createDialog({
id: "import-confirm", id: "import-confirm",
@ -40,19 +39,19 @@
color: "red", color: "red",
main: true, main: true,
timeout: 5000, timeout: 5000,
action: () => updateSetting(loadFromString(data)) action: () => updateSetting(loadFromString(data)),
}, },
], ],
}); });
} catch (e) { } catch (e) {
let message; let message = $t("error.import.no_data");
if (e instanceof Error) if (e instanceof Error) {
message = e.message; console.error("settings import error:", e);
else if (typeof e === 'string') message = $t("error.import.unknown", { value: e.message });
} else if (typeof e === "string") {
message = e; message = e;
else }
message = $t('settings.advanced.import.no_data');
createDialog({ createDialog({
id: "settings-import-error", id: "settings-import-error",
@ -88,10 +87,9 @@
}; };
const exportSettings = () => { const exportSettings = () => {
const blob = new Blob( const blob = new Blob([JSON.stringify($storedSettings, null, 2)], {
[ JSON.stringify($storedSettings, null, 2) ], type: "application/json",
{ type: "application/json" } });
);
const pseudolink = document.createElement("a"); const pseudolink = document.createElement("a");
pseudolink.href = URL.createObjectURL(blob); pseudolink.href = URL.createObjectURL(blob);
@ -102,11 +100,13 @@
<div class="button-row" id="settings-data-transfer"> <div class="button-row" id="settings-data-transfer">
<ActionButton id="import-settings" click={importSettings}> <ActionButton id="import-settings" click={importSettings}>
<IconFileImport /> {$t("settings.advanced.import")} <IconFileImport />
{$t("settings.advanced.import")}
</ActionButton> </ActionButton>
{#if $storedSettings.schemaVersion} {#if $storedSettings.schemaVersion}
<ActionButton id="export-settings" click={exportSettings}> <ActionButton id="export-settings" click={exportSettings}>
<IconFileExport /> {$t("settings.advanced.export")} <IconFileExport />
{$t("settings.advanced.export")}
</ActionButton> </ActionButton>
{/if} {/if}
</div> </div>