Skip to content

Commit

Permalink
Fix settings export/import to also handle notes
Browse files Browse the repository at this point in the history
  • Loading branch information
tfedor committed May 28, 2024
1 parent 18b826d commit 55e8907
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
4 changes: 4 additions & 0 deletions src/js/Content/Features/Store/Common/UserNotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export default class UserNotes {
return this._adapter.get(...appids);
}

export(): Promise<Record<number, string>> {
return this._adapter.export();
}

async set(appid: number, note: string): Promise<boolean> {
let capInfo = null;

Expand Down
8 changes: 4 additions & 4 deletions src/js/Options/Data/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {SettingsSchema} from "./_types";
import type {SchemaKeys, SchemaValue, StorageInterface} from "@Core/Storage/Storage";
import {SyncedStorage} from "@Core/Storage/SyncedStorage";

const DefaultSettings: SettingsSchema = Object.freeze({
export const DefaultSettings: SettingsSchema = Object.freeze({
"language": "english",

"version": Info.version,
Expand Down Expand Up @@ -268,10 +268,10 @@ export class SettingsStore {
this.data = structuredClone(DefaultSettings);
}

static import(data: SettingsSchema): void {
static async import(data: Partial<SettingsSchema>): Promise<void> {
// TODO check data for valid keys and values
this.storage.setObject(data);
this.data = data;
await this.storage.setObject(data);
await this.load();
}

static asObject(): SettingsSchema {
Expand Down
24 changes: 20 additions & 4 deletions src/js/Options/Modules/Footer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
__options_settingsMngmt_reset
} from "@Strings/_strings";
import {L} from "@Core/Localization/Localization";
import {SettingsStore} from "../Data/Settings";
import {DefaultSettings, SettingsStore} from "../Data/Settings";
import Downloader from "@Core/Downloader";
import {Info} from "@Core/Info";
import UserNotes from "@Content/Features/Store/Common/UserNotes";
import type {SettingsSchema} from "@Options/Data/_types";
import UserNotesAdapter from "@Content/Modules/UserNotes/UserNotesAdapter";
function importSettings(e: Event & {currentTarget: EventTarget & HTMLInputElement}) {
Expand All @@ -23,8 +26,17 @@
const data = reader.result as string;
let importedSettings;
let importedNotes;
try {
importedSettings = JSON.parse(data);
const importedData = JSON.parse(data);
const knownKeys = new Set(Object.keys(DefaultSettings));
importedSettings = Object.fromEntries(
Object.entries(importedData)
.filter(([key, _value]) => knownKeys.has(key))
) as Partial<SettingsSchema>;
importedNotes = importedData.user_notes ?? {};
} catch (err) {
console.group("Import");
console.error("Failed to read settings file");
Expand All @@ -39,6 +51,7 @@
try {
console.log(importedSettings);
SettingsStore.import(importedSettings);
UserNotesAdapter.getAdapter().import(importedNotes);
} catch (err) {
console.group("Import");
console.error("Failed to write settings to storage");
Expand All @@ -61,10 +74,13 @@
}
}
function exportSettings() {
async function exportSettings() {
const result: Record<string, any> = SettingsStore.asObject();
result.user_notes = await (new UserNotes()).export();
Downloader.download(
new Blob([
JSON.stringify(SettingsStore.asObject(), null, " ")
JSON.stringify(result, null, " ")
]),
`AugmentedSteam_v${Info.version}.json`
);
Expand Down

0 comments on commit 55e8907

Please sign in to comment.