From f7be9df980c231d9dbf8bcd0b97a33416274749b Mon Sep 17 00:00:00 2001 From: Paul Date: Tue, 21 Dec 2021 12:31:14 +0000 Subject: [PATCH] chore(mobx): add legacy redux migations --- src/mobx/legacy/redux.ts | 84 ++++++++++++++++++++++++++ src/mobx/stores/Auth.ts | 8 ++- src/mobx/stores/Draft.ts | 2 +- src/mobx/stores/Experiments.ts | 2 +- src/mobx/stores/Layout.ts | 2 +- src/mobx/stores/LocaleOptions.ts | 2 +- src/mobx/stores/NotificationOptions.ts | 2 +- src/mobx/stores/ServerConfig.ts | 4 -- src/mobx/stores/Settings.ts | 7 ++- src/mobx/stores/Sync.ts | 2 +- 10 files changed, 99 insertions(+), 16 deletions(-) create mode 100644 src/mobx/legacy/redux.ts diff --git a/src/mobx/legacy/redux.ts b/src/mobx/legacy/redux.ts new file mode 100644 index 00000000..73154743 --- /dev/null +++ b/src/mobx/legacy/redux.ts @@ -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; +} + +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 { + 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 { + return { + "appearance:emoji": appearance.emojiPack, + }; +} + +function legacyMigrateNotification( + channel: LegacyNotifications, +): DataNotificationOptions { + return { + channel, + }; +} diff --git a/src/mobx/stores/Auth.ts b/src/mobx/stores/Auth.ts index da989274..b2ab3914 100644 --- a/src/mobx/stores/Auth.ts +++ b/src/mobx/stores/Auth.ts @@ -2,6 +2,8 @@ import { action, computed, makeAutoObservable, ObservableMap } from "mobx"; import { Session } from "revolt-api/types/Auth"; import { Nullable } from "revolt.js/dist/util/null"; +import { mapToRecord } from "../../lib/conversion"; + import Persistent from "../interfaces/Persistent"; import Store from "../interfaces/Store"; @@ -9,8 +11,8 @@ interface Account { session: Session; } -interface Data { - sessions: Record | [string, Account][]; +export interface Data { + sessions: Record; current?: string; } @@ -37,7 +39,7 @@ export default class Auth implements Store, Persistent { @action toJSON() { return { - sessions: JSON.parse(JSON.stringify(this.sessions)), + sessions: JSON.parse(JSON.stringify(mapToRecord(this.sessions))), current: this.current ?? undefined, }; } diff --git a/src/mobx/stores/Draft.ts b/src/mobx/stores/Draft.ts index e245ee15..de84c1f9 100644 --- a/src/mobx/stores/Draft.ts +++ b/src/mobx/stores/Draft.ts @@ -5,7 +5,7 @@ import { mapToRecord } from "../../lib/conversion"; import Persistent from "../interfaces/Persistent"; import Store from "../interfaces/Store"; -interface Data { +export interface Data { drafts: Record; } diff --git a/src/mobx/stores/Experiments.ts b/src/mobx/stores/Experiments.ts index 37d5a4e7..a3245945 100644 --- a/src/mobx/stores/Experiments.ts +++ b/src/mobx/stores/Experiments.ts @@ -35,7 +35,7 @@ export const EXPERIMENTS: { }, }; -interface Data { +export interface Data { enabled?: Experiment[]; } diff --git a/src/mobx/stores/Layout.ts b/src/mobx/stores/Layout.ts index 6f8ce3e7..cd17a4ac 100644 --- a/src/mobx/stores/Layout.ts +++ b/src/mobx/stores/Layout.ts @@ -5,7 +5,7 @@ import { mapToRecord } from "../../lib/conversion"; import Persistent from "../interfaces/Persistent"; import Store from "../interfaces/Store"; -interface Data { +export interface Data { lastSection?: "home" | "server"; lastHomePath?: string; lastOpened?: Record; diff --git a/src/mobx/stores/LocaleOptions.ts b/src/mobx/stores/LocaleOptions.ts index 842a3616..9c708d6e 100644 --- a/src/mobx/stores/LocaleOptions.ts +++ b/src/mobx/stores/LocaleOptions.ts @@ -5,7 +5,7 @@ import { Language, Languages } from "../../context/Locale"; import Persistent from "../interfaces/Persistent"; import Store from "../interfaces/Store"; -interface Data { +export interface Data { lang: Language; } diff --git a/src/mobx/stores/NotificationOptions.ts b/src/mobx/stores/NotificationOptions.ts index eb4ee908..9bb3b23e 100644 --- a/src/mobx/stores/NotificationOptions.ts +++ b/src/mobx/stores/NotificationOptions.ts @@ -34,7 +34,7 @@ export const DEFAULT_STATES: { */ export const DEFAULT_SERVER_STATE: NotificationState = "mention"; -interface Data { +export interface Data { server?: Record; channel?: Record; } diff --git a/src/mobx/stores/ServerConfig.ts b/src/mobx/stores/ServerConfig.ts index 1b1d0498..6e4b5d9d 100644 --- a/src/mobx/stores/ServerConfig.ts +++ b/src/mobx/stores/ServerConfig.ts @@ -6,10 +6,6 @@ import { Nullable } from "revolt.js/dist/util/null"; import Persistent from "../interfaces/Persistent"; import Store from "../interfaces/Store"; -interface Data { - config?: RevoltConfiguration; -} - /** * Stores server configuration data. */ diff --git a/src/mobx/stores/Settings.ts b/src/mobx/stores/Settings.ts index f2be8dee..80a75253 100644 --- a/src/mobx/stores/Settings.ts +++ b/src/mobx/stores/Settings.ts @@ -12,7 +12,7 @@ import SAudio, { SoundOptions } from "./helpers/SAudio"; import SSecurity from "./helpers/SSecurity"; import STheme from "./helpers/STheme"; -interface ISettings { +export interface ISettings { "notifications:desktop": boolean; "notifications:sounds": SoundOptions; @@ -60,8 +60,9 @@ export default class Settings implements Store, Persistent { } @action hydrate(data: ISettings) { - Object.keys(data).forEach((key) => - this.data.set(key, (data as any)[key]), + Object.keys(data).forEach( + (key) => + (data as any)[key] && this.data.set(key, (data as any)[key]), ); } diff --git a/src/mobx/stores/Sync.ts b/src/mobx/stores/Sync.ts index d18a21fd..42d76edc 100644 --- a/src/mobx/stores/Sync.ts +++ b/src/mobx/stores/Sync.ts @@ -21,7 +21,7 @@ export const SYNC_KEYS: SyncKeys[] = [ "notifications", ]; -interface Data { +export interface Data { disabled: SyncKeys[]; }