From bde9a2e8f7fe70e82f6ea90b6b697e3554aa21ab Mon Sep 17 00:00:00 2001
From: Paul <paulmakles@gmail.com>
Date: Thu, 29 Jul 2021 18:49:49 +0100
Subject: [PATCH] Remove useChannels. Add servers to MobX.

---
 .../navigation/left/ServerListSidebar.tsx     |  6 +-
 .../navigation/left/ServerSidebar.tsx         |  6 +-
 src/context/revoltjs/hooks.ts                 |  7 --
 src/mobx/index.ts                             | 88 ++++++++++++++++++-
 4 files changed, 88 insertions(+), 19 deletions(-)

diff --git a/src/components/navigation/left/ServerListSidebar.tsx b/src/components/navigation/left/ServerListSidebar.tsx
index bf8253f..b0a462f 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 e21b33b..bd49a77 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 99823c5..1f97286 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<Channels.Channel>
-        | 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 700c5b2..6cb7615 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> = 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 {
     @observable users = new Map<string, User>();
     @observable channels = new Map<string, Channel>();
+    @observable servers = new Map<string, Server>();
 
     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": {
-- 
GitLab