mirror of
https://github.com/stoatchat/for-legacy-web.git
synced 2026-03-07 01:15:28 +00:00
feat(mobx): add drafts and state context
This commit is contained in:
70
src/mobx/stores/Auth.ts
Normal file
70
src/mobx/stores/Auth.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import { makeAutoObservable } from "mobx";
|
||||
import { Session } from "revolt-api/types/Auth";
|
||||
import { Nullable } from "revolt.js/dist/util/null";
|
||||
|
||||
import Persistent from "../Persistent";
|
||||
import { deleteKey } from "../objectUtil";
|
||||
|
||||
interface Data {
|
||||
sessions: Record<string, Session>;
|
||||
current?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles account authentication, managing multiple
|
||||
* accounts and their sessions.
|
||||
*/
|
||||
export default class Auth implements Persistent<Data> {
|
||||
private sessions: Record<string, Session>;
|
||||
private current: Nullable<string>;
|
||||
|
||||
/**
|
||||
* Construct new Auth store.
|
||||
*/
|
||||
constructor() {
|
||||
this.sessions = {};
|
||||
this.current = null;
|
||||
makeAutoObservable(this);
|
||||
}
|
||||
|
||||
// eslint-disable-next-line require-jsdoc
|
||||
toJSON() {
|
||||
return {
|
||||
sessions: this.sessions,
|
||||
current: this.current,
|
||||
};
|
||||
}
|
||||
|
||||
// eslint-disable-next-line require-jsdoc
|
||||
hydrate(data: Data) {
|
||||
this.sessions = data.sessions;
|
||||
if (data.current && this.sessions[data.current]) {
|
||||
this.current = data.current;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new session to the auth manager.
|
||||
* @param session Session
|
||||
*/
|
||||
setSession(session: Session) {
|
||||
this.sessions = {
|
||||
...this.sessions,
|
||||
[session.user_id]: session,
|
||||
};
|
||||
|
||||
this.current = session.user_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove existing session by user ID.
|
||||
* @param user_id User ID tied to session
|
||||
*/
|
||||
removeSession(user_id: string) {
|
||||
this.sessions = deleteKey(this.sessions, user_id);
|
||||
|
||||
if (user_id == this.current) {
|
||||
this.current = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
80
src/mobx/stores/Draft.ts
Normal file
80
src/mobx/stores/Draft.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
import { action, computed, makeAutoObservable, ObservableMap } from "mobx";
|
||||
|
||||
import Persistent from "../Persistent";
|
||||
|
||||
interface Data {
|
||||
drafts: Record<string, string>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles storing draft (currently being written) messages.
|
||||
*/
|
||||
export default class Draft implements Persistent<Data> {
|
||||
private drafts: ObservableMap<string, string>;
|
||||
|
||||
/**
|
||||
* Construct new Draft store.
|
||||
*/
|
||||
constructor() {
|
||||
this.drafts = new ObservableMap();
|
||||
makeAutoObservable(this);
|
||||
}
|
||||
|
||||
// eslint-disable-next-line require-jsdoc
|
||||
toJSON() {
|
||||
return {
|
||||
drafts: this.drafts,
|
||||
};
|
||||
}
|
||||
|
||||
// eslint-disable-next-line require-jsdoc
|
||||
@action hydrate(data: Data) {
|
||||
Object.keys(data.drafts).forEach((key) =>
|
||||
this.drafts.set(key, data.drafts[key]),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get draft for a channel.
|
||||
* @param channel Channel ID
|
||||
*/
|
||||
@computed get(channel: string) {
|
||||
return this.drafts.get(channel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a channel has a draft.
|
||||
* @param channel Channel ID
|
||||
*/
|
||||
@computed has(channel: string) {
|
||||
return this.drafts.has(channel) && this.drafts.get(channel)!.length > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set draft for a channel.
|
||||
* @param channel Channel ID
|
||||
* @param content Draft content
|
||||
*/
|
||||
@action set(channel: string, content?: string) {
|
||||
if (typeof content === "undefined") {
|
||||
return this.clear(channel);
|
||||
}
|
||||
|
||||
this.drafts.set(channel, content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear draft from a channel.
|
||||
* @param channel Channel ID
|
||||
*/
|
||||
@action clear(channel: string) {
|
||||
this.drafts.delete(channel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset and clear all drafts.
|
||||
*/
|
||||
@action reset() {
|
||||
this.drafts.clear();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user