feat(mobx): refactor and remove (react-)redux

This commit is contained in:
Paul
2021-12-23 21:43:11 +00:00
parent c5f9fc2873
commit 0591d44a31
55 changed files with 249 additions and 1522 deletions

View File

@@ -12,6 +12,11 @@ export interface Data {
openSections?: Record<string, boolean>;
}
export const SIDEBAR_MEMBERS = "sidebar_members";
export const SIDEBAR_CHANNELS = "sidebar_channels";
export const SECTION_MENTION = "mention";
export const SECTION_NSFW = "nsfw";
/**
* Keeps track of the last open channels, tabs, etc.
* Handles providing good UX experience on navigating
@@ -165,4 +170,13 @@ export default class Layout implements Store, Persistent<Data> {
this.openSections.set(id, value);
}
}
/**
* Toggle state of a section.
* @param id Section ID
* @param def Default state value
*/
@action toggleSectionState(id: string, def?: boolean) {
this.setSectionState(id, !this.getSectionState(id, def));
}
}

View File

@@ -78,10 +78,14 @@ export default class Settings implements Store, Persistent<ISettings> {
/**
* Get a settings key.
* @param key Colon-divided key
* @param defaultValue Default value if not present
* @returns Value at key
*/
@computed get<T extends keyof ISettings>(key: T) {
return this.data.get(key) as ISettings[T] | undefined;
@computed get<T extends keyof ISettings>(
key: T,
defaultValue?: ISettings[T],
) {
return (this.data.get(key) as ISettings[T] | undefined) ?? defaultValue;
}
@action remove<T extends keyof ISettings>(key: T) {

View File

@@ -23,6 +23,9 @@ export const SYNC_KEYS: SyncKeys[] = [
export interface Data {
disabled: SyncKeys[];
revision: {
[key: string]: number;
};
}
/**
@@ -30,12 +33,14 @@ export interface Data {
*/
export default class Sync implements Store, Persistent<Data> {
private disabled: ObservableSet<SyncKeys>;
private revision: ObservableMap<SyncKeys, number>;
/**
* Construct new Sync store.
*/
constructor() {
this.disabled = new ObservableSet();
this.revision = new ObservableMap();
makeAutoObservable(this);
this.isEnabled = this.isEnabled.bind(this);
}
@@ -47,6 +52,7 @@ export default class Sync implements Store, Persistent<Data> {
toJSON() {
return {
enabled: [...this.disabled],
revision: mapToRecord(this.revision),
};
}
@@ -58,6 +64,22 @@ export default class Sync implements Store, Persistent<Data> {
}
}
@action enable(key: SyncKeys) {
this.disabled.delete(key);
}
@action disable(key: SyncKeys) {
this.disabled.add(key);
}
@action toggle(key: SyncKeys) {
if (this.isEnabled(key)) {
this.disable(key);
} else {
this.enable(key);
}
}
@computed isEnabled(key: SyncKeys) {
return !this.disabled.has(key);
}