chore(mobx): add legacy redux migations

This commit is contained in:
Paul 2021-12-21 12:31:14 +00:00
parent 68578d2620
commit f7be9df980
10 changed files with 99 additions and 16 deletions

84
src/mobx/legacy/redux.ts Normal file
View file

@ -0,0 +1,84 @@
import { AuthState } from "../../redux/reducers/auth";
import { Language } from "../../context/Locale";
import { Fonts, MonospaceFonts, Overrides } from "../../context/Theme";
import { Data as DataAuth } from "../stores/Auth";
import { Data as DataLocaleOptions } from "../stores/LocaleOptions";
import { Data as DataNotificationOptions } from "../stores/NotificationOptions";
import { ISettings } from "../stores/Settings";
export type LegacyTheme = Overrides & {
light?: boolean;
font?: Fonts;
css?: string;
monospaceFont?: MonospaceFonts;
};
export interface LegacyThemeOptions {
base?: string;
ligatures?: boolean;
custom?: Partial<LegacyTheme>;
}
export type LegacyEmojiPacks = "mutant" | "twemoji" | "noto" | "openmoji";
export interface LegacyAppearanceOptions {
emojiPack?: LegacyEmojiPacks;
}
export type LegacyNotificationState = "all" | "mention" | "none" | "muted";
export type LegacyNotifications = {
[key: string]: LegacyNotificationState;
};
export interface LegacySyncData {
locale?: Language;
theme?: LegacyThemeOptions;
appearance?: LegacyAppearanceOptions;
notifications?: LegacyNotifications;
}
function legacyMigrateAuth(auth: AuthState): DataAuth {
return {
current: auth.active,
sessions: auth.accounts,
};
}
function legacyMigrateLocale(lang: Language): DataLocaleOptions {
return {
lang,
};
}
function legacyMigrateTheme(theme: LegacyThemeOptions): Partial<ISettings> {
const { light, font, css, monospaceFont, ...variables } =
theme.custom ?? {};
return {
"appearance:ligatures": theme.ligatures,
"appearance:theme:base": theme.base === "light" ? "light" : "dark",
"appearance:theme:light": light,
"appearance:theme:font": font,
"appearance:theme:monoFont": monospaceFont,
"appearance:theme:css": css,
"appearance:theme:overrides": variables,
};
}
function legacyMigrateAppearance(
appearance: LegacyAppearanceOptions,
): Partial<ISettings> {
return {
"appearance:emoji": appearance.emojiPack,
};
}
function legacyMigrateNotification(
channel: LegacyNotifications,
): DataNotificationOptions {
return {
channel,
};
}

View file

@ -2,6 +2,8 @@ import { action, computed, makeAutoObservable, ObservableMap } from "mobx";
import { Session } from "revolt-api/types/Auth"; import { Session } from "revolt-api/types/Auth";
import { Nullable } from "revolt.js/dist/util/null"; import { Nullable } from "revolt.js/dist/util/null";
import { mapToRecord } from "../../lib/conversion";
import Persistent from "../interfaces/Persistent"; import Persistent from "../interfaces/Persistent";
import Store from "../interfaces/Store"; import Store from "../interfaces/Store";
@ -9,8 +11,8 @@ interface Account {
session: Session; session: Session;
} }
interface Data { export interface Data {
sessions: Record<string, Account> | [string, Account][]; sessions: Record<string, Account>;
current?: string; current?: string;
} }
@ -37,7 +39,7 @@ export default class Auth implements Store, Persistent<Data> {
@action toJSON() { @action toJSON() {
return { return {
sessions: JSON.parse(JSON.stringify(this.sessions)), sessions: JSON.parse(JSON.stringify(mapToRecord(this.sessions))),
current: this.current ?? undefined, current: this.current ?? undefined,
}; };
} }

View file

@ -5,7 +5,7 @@ import { mapToRecord } from "../../lib/conversion";
import Persistent from "../interfaces/Persistent"; import Persistent from "../interfaces/Persistent";
import Store from "../interfaces/Store"; import Store from "../interfaces/Store";
interface Data { export interface Data {
drafts: Record<string, string>; drafts: Record<string, string>;
} }

View file

@ -35,7 +35,7 @@ export const EXPERIMENTS: {
}, },
}; };
interface Data { export interface Data {
enabled?: Experiment[]; enabled?: Experiment[];
} }

View file

@ -5,7 +5,7 @@ import { mapToRecord } from "../../lib/conversion";
import Persistent from "../interfaces/Persistent"; import Persistent from "../interfaces/Persistent";
import Store from "../interfaces/Store"; import Store from "../interfaces/Store";
interface Data { export interface Data {
lastSection?: "home" | "server"; lastSection?: "home" | "server";
lastHomePath?: string; lastHomePath?: string;
lastOpened?: Record<string, string>; lastOpened?: Record<string, string>;

View file

@ -5,7 +5,7 @@ import { Language, Languages } from "../../context/Locale";
import Persistent from "../interfaces/Persistent"; import Persistent from "../interfaces/Persistent";
import Store from "../interfaces/Store"; import Store from "../interfaces/Store";
interface Data { export interface Data {
lang: Language; lang: Language;
} }

View file

@ -34,7 +34,7 @@ export const DEFAULT_STATES: {
*/ */
export const DEFAULT_SERVER_STATE: NotificationState = "mention"; export const DEFAULT_SERVER_STATE: NotificationState = "mention";
interface Data { export interface Data {
server?: Record<string, NotificationState>; server?: Record<string, NotificationState>;
channel?: Record<string, NotificationState>; channel?: Record<string, NotificationState>;
} }

View file

@ -6,10 +6,6 @@ import { Nullable } from "revolt.js/dist/util/null";
import Persistent from "../interfaces/Persistent"; import Persistent from "../interfaces/Persistent";
import Store from "../interfaces/Store"; import Store from "../interfaces/Store";
interface Data {
config?: RevoltConfiguration;
}
/** /**
* Stores server configuration data. * Stores server configuration data.
*/ */

View file

@ -12,7 +12,7 @@ import SAudio, { SoundOptions } from "./helpers/SAudio";
import SSecurity from "./helpers/SSecurity"; import SSecurity from "./helpers/SSecurity";
import STheme from "./helpers/STheme"; import STheme from "./helpers/STheme";
interface ISettings { export interface ISettings {
"notifications:desktop": boolean; "notifications:desktop": boolean;
"notifications:sounds": SoundOptions; "notifications:sounds": SoundOptions;
@ -60,8 +60,9 @@ export default class Settings implements Store, Persistent<ISettings> {
} }
@action hydrate(data: ISettings) { @action hydrate(data: ISettings) {
Object.keys(data).forEach((key) => Object.keys(data).forEach(
this.data.set(key, (data as any)[key]), (key) =>
(data as any)[key] && this.data.set(key, (data as any)[key]),
); );
} }

View file

@ -21,7 +21,7 @@ export const SYNC_KEYS: SyncKeys[] = [
"notifications", "notifications",
]; ];
interface Data { export interface Data {
disabled: SyncKeys[]; disabled: SyncKeys[];
} }