Skip to content

Commit

Permalink
feat: backup review presets, fonts (closes #26)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lemmmy committed Jan 18, 2025
1 parent d71e10b commit 892af99
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/pages/settings/backup/SettingsBackupButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
// This file is part of KanjiSchool under AGPL-3.0.
// Full details: https://github.com/Lemmmy/KanjiSchool/blob/master/LICENSE

import { AnySettingName } from "@utils";
import { AnySettingName, OtherLocalStorageSettingName } from "@utils";

import { SettingsExportButton } from "./SettingsExportButton.tsx";
import { SettingsImportButton } from "./SettingsImportButton.tsx";

export interface SettingsExportFile extends Partial<Record<AnySettingName, string>> {
export interface SettingsExportFile extends Partial<Record<AnySettingName | OtherLocalStorageSettingName, string>> {
_exportedAt: string;
_version: string;
}
Expand Down
12 changes: 11 additions & 1 deletion src/pages/settings/backup/SettingsExportButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { SettingsExportFile } from "./SettingsBackupButtons.tsx";
import dayjs from "dayjs";
import { saveAs } from "file-saver";

import { DEFAULT_SETTINGS, AnySettingName, getSettingKey } from "@utils";
import { DEFAULT_SETTINGS, AnySettingName, getSettingKey, OTHER_LOCAL_STORAGE_SETTING_NAMES, lsGetKey } from "@utils";

import { globalNotification } from "@global/AntInterface.tsx";
import useBreakpoint from "antd/es/grid/hooks/useBreakpoint";
Expand Down Expand Up @@ -48,6 +48,16 @@ export function exportSettings(): void {
}
}

// Other settings (e.g. review presets, fonts)
for (const lsKey of OTHER_LOCAL_STORAGE_SETTING_NAMES) {
const stored = localStorage.getItem(lsGetKey(lsKey));

if (stored !== null) {
out[lsKey] = stored;
count++;
}
}

const outData = JSON.stringify(out);
const filename = `KanjiSchool-settings-${dayjs().format("YYYY-MM-DD--HH-mm-ss")}.json`;
const blob = new Blob([outData], { type: "application/json;charset=utf-8" });
Expand Down
12 changes: 11 additions & 1 deletion src/pages/settings/backup/SettingsImportButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ImportOutlined } from "@ant-design/icons";

import { SettingsExportFile } from "./SettingsBackupButtons.tsx";

import { AnySettingName, DEFAULT_SETTINGS, getSettingKey } from "@utils";
import { AnySettingName, DEFAULT_SETTINGS, getSettingKey, lsGetKey, OTHER_LOCAL_STORAGE_SETTING_NAMES } from "@utils";

import { globalNotification } from "@global/AntInterface.tsx";
import { Button } from "antd";
Expand Down Expand Up @@ -89,6 +89,16 @@ export function importSettings(contents: string): void {
}
}

// Other settings (e.g. review presets, fonts)
for (const lsKey of OTHER_LOCAL_STORAGE_SETTING_NAMES) {
const stored = data[lsKey];

if (stored !== undefined) {
localStorage.setItem(lsGetKey(lsKey), stored);
count++;
}
}

globalNotification.success({ message: `Import ${count} settings. Site will refresh.` });
setTimeout(() => { location.reload(); }, 2000);
} catch (e) {
Expand Down
9 changes: 9 additions & 0 deletions src/utils/settings/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,15 @@ export const DEFAULT_SETTINGS: SettingsState = {
subjectInfoDebug: false
};

/** Other local storage keys that will be included in backups. */
export const OTHER_LOCAL_STORAGE_SETTING_NAMES = [
"lessonPresets",
"reviewPresets",
"customFonts",
"getReviewsWarning"
] as const;
export type OtherLocalStorageSettingName = typeof OTHER_LOCAL_STORAGE_SETTING_NAMES[number];

export const SETTING_CONFIGS: Partial<Record<AnySettingName, IntegerSettingConfig | undefined>> = {
dashboardReviewChartDays: { min: 1, max: 7 },
dashboardCriticalThreshold: { min: 1, max: 100 },
Expand Down

0 comments on commit 892af99

Please sign in to comment.