diff --git a/src/components/navigation/left/ServerListSidebar.tsx b/src/components/navigation/left/ServerListSidebar.tsx index bf8253f7..b0a462f9 100644 --- a/src/components/navigation/left/ServerListSidebar.tsx +++ b/src/components/navigation/left/ServerListSidebar.tsx @@ -17,11 +17,7 @@ import { Unreads } from "../../../redux/reducers/unreads"; import { useIntermediate } from "../../../context/intermediate/Intermediate"; import { useClient } from "../../../context/revoltjs/RevoltClient"; -import { - useChannels, - useForceUpdate, - useServers, -} from "../../../context/revoltjs/hooks"; +import { useForceUpdate, useServers } from "../../../context/revoltjs/hooks"; import logoSVG from "../../../assets/logo.svg"; import ServerIcon from "../../common/ServerIcon"; diff --git a/src/components/navigation/left/ServerSidebar.tsx b/src/components/navigation/left/ServerSidebar.tsx index e21b33b7..bd49a772 100644 --- a/src/components/navigation/left/ServerSidebar.tsx +++ b/src/components/navigation/left/ServerSidebar.tsx @@ -14,11 +14,7 @@ import { dispatch } from "../../../redux"; import { connectState } from "../../../redux/connector"; import { Unreads } from "../../../redux/reducers/unreads"; -import { - useChannels, - useForceUpdate, - useServer, -} from "../../../context/revoltjs/hooks"; +import { useForceUpdate, useServer } from "../../../context/revoltjs/hooks"; import CollapsibleSection from "../../common/CollapsibleSection"; import ServerHeader from "../../common/ServerHeader"; diff --git a/src/context/revoltjs/hooks.ts b/src/context/revoltjs/hooks.ts index 99823c58..1f97286c 100644 --- a/src/context/revoltjs/hooks.ts +++ b/src/context/revoltjs/hooks.ts @@ -77,13 +77,6 @@ function useObject( : map.toArray(); } -export function useChannels(ids?: string[], context?: HookContext) { - return useObject("channels", ids, context) as ( - | Readonly - | undefined - )[]; -} - export function useServer(id?: string, context?: HookContext) { if (typeof id === "undefined") return; return useObject("servers", id, context) as diff --git a/src/mobx/index.ts b/src/mobx/index.ts index 700c5b2e..6cb7615f 100644 --- a/src/mobx/index.ts +++ b/src/mobx/index.ts @@ -9,8 +9,17 @@ import { action, extendObservable, } from "mobx"; -import { Attachment, Channels, Users } from "revolt.js/dist/api/objects"; -import { RemoveChannelField, RemoveUserField } from "revolt.js/dist/api/routes"; +import { + 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"; type Nullable = T | null; @@ -172,9 +181,80 @@ export class Channel { } } +export class Server { + _id: string; + owner: string; + name: string; + description: Nullable = null; + + channels: string[] = []; + categories: Nullable = null; + system_messages: Nullable = null; + + roles: Nullable<{ [key: string]: Servers.Role }> = null; + default_permissions: Servers.PermissionTuple; + + icon: Nullable = null; + banner: Nullable = 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, 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 { @observable users = new Map(); @observable channels = new Map(); + @observable servers = new Map(); constructor() { makeAutoObservable(this); @@ -192,6 +272,10 @@ export class DataStore { this.channels.set(channel._id, new Channel(channel)); } + for (let server of packet.servers) { + this.servers.set(server._id, new Server(server)); + } + break; } case "UserUpdate": {