Remove useChannels.

Add servers to MobX.
This commit is contained in:
Paul 2021-07-29 18:49:49 +01:00
parent 411fac2527
commit bde9a2e8f7
4 changed files with 88 additions and 19 deletions

View file

@ -17,11 +17,7 @@ import { Unreads } from "../../../redux/reducers/unreads";
import { useIntermediate } from "../../../context/intermediate/Intermediate"; import { useIntermediate } from "../../../context/intermediate/Intermediate";
import { useClient } from "../../../context/revoltjs/RevoltClient"; import { useClient } from "../../../context/revoltjs/RevoltClient";
import { import { useForceUpdate, useServers } from "../../../context/revoltjs/hooks";
useChannels,
useForceUpdate,
useServers,
} from "../../../context/revoltjs/hooks";
import logoSVG from "../../../assets/logo.svg"; import logoSVG from "../../../assets/logo.svg";
import ServerIcon from "../../common/ServerIcon"; import ServerIcon from "../../common/ServerIcon";

View file

@ -14,11 +14,7 @@ import { dispatch } from "../../../redux";
import { connectState } from "../../../redux/connector"; import { connectState } from "../../../redux/connector";
import { Unreads } from "../../../redux/reducers/unreads"; import { Unreads } from "../../../redux/reducers/unreads";
import { import { useForceUpdate, useServer } from "../../../context/revoltjs/hooks";
useChannels,
useForceUpdate,
useServer,
} from "../../../context/revoltjs/hooks";
import CollapsibleSection from "../../common/CollapsibleSection"; import CollapsibleSection from "../../common/CollapsibleSection";
import ServerHeader from "../../common/ServerHeader"; import ServerHeader from "../../common/ServerHeader";

View file

@ -77,13 +77,6 @@ function useObject(
: map.toArray(); : map.toArray();
} }
export function useChannels(ids?: string[], context?: HookContext) {
return useObject("channels", ids, context) as (
| Readonly<Channels.Channel>
| undefined
)[];
}
export function useServer(id?: string, context?: HookContext) { export function useServer(id?: string, context?: HookContext) {
if (typeof id === "undefined") return; if (typeof id === "undefined") return;
return useObject("servers", id, context) as return useObject("servers", id, context) as

View file

@ -9,8 +9,17 @@ import {
action, action,
extendObservable, extendObservable,
} from "mobx"; } from "mobx";
import { Attachment, Channels, Users } from "revolt.js/dist/api/objects"; import {
import { RemoveChannelField, RemoveUserField } from "revolt.js/dist/api/routes"; Attachment,
Channels,
Servers,
Users,
} from "revolt.js/dist/api/objects";
import {
RemoveChannelField,
RemoveServerField,
RemoveUserField,
} from "revolt.js/dist/api/routes";
import { ClientboundNotification } from "revolt.js/dist/websocket/notifications"; import { ClientboundNotification } from "revolt.js/dist/websocket/notifications";
type Nullable<T> = T | null; type Nullable<T> = T | null;
@ -172,9 +181,80 @@ export class Channel {
} }
} }
export class Server {
_id: string;
owner: string;
name: string;
description: Nullable<string> = null;
channels: string[] = [];
categories: Nullable<Servers.Category[]> = null;
system_messages: Nullable<Servers.SystemMessageChannels> = null;
roles: Nullable<{ [key: string]: Servers.Role }> = null;
default_permissions: Servers.PermissionTuple;
icon: Nullable<Attachment> = null;
banner: Nullable<Attachment> = null;
constructor(data: Servers.Server) {
this._id = data._id;
this.owner = data.owner;
this.name = data.name;
this.description = toNullable(data.description);
this.channels = data.channels;
this.categories = toNullable(data.categories);
this.system_messages = toNullable(data.system_messages);
this.roles = toNullable(data.roles);
this.default_permissions = data.default_permissions;
this.icon = toNullable(data.icon);
this.banner = toNullable(data.banner);
makeAutoObservable(this);
}
@action update(data: Partial<Servers.Server>, clear?: RemoveServerField) {
const apply = (key: string) => {
// This code has been tested.
// @ts-expect-error
if (data[key] && !isEqual(this[key], data[key])) {
// @ts-expect-error
this[key] = data[key];
}
};
switch (clear) {
case "Banner":
this.banner = null;
break;
case "Description":
this.description = null;
break;
case "Icon":
this.icon = null;
break;
}
apply("owner");
apply("name");
apply("description");
apply("channels");
apply("categories");
apply("system_messages");
apply("roles");
apply("default_permissions");
apply("icon");
apply("banner");
}
}
export class DataStore { export class DataStore {
@observable users = new Map<string, User>(); @observable users = new Map<string, User>();
@observable channels = new Map<string, Channel>(); @observable channels = new Map<string, Channel>();
@observable servers = new Map<string, Server>();
constructor() { constructor() {
makeAutoObservable(this); makeAutoObservable(this);
@ -192,6 +272,10 @@ export class DataStore {
this.channels.set(channel._id, new Channel(channel)); this.channels.set(channel._id, new Channel(channel));
} }
for (let server of packet.servers) {
this.servers.set(server._id, new Server(server));
}
break; break;
} }
case "UserUpdate": { case "UserUpdate": {