From 30ac25607023752031aa98060cbf8a736109992d Mon Sep 17 00:00:00 2001 From: V Date: Sun, 24 Sep 2023 16:02:18 +0200 Subject: migrate all plugins to folders --- src/plugins/serverListIndicators/index.tsx | 137 +++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 src/plugins/serverListIndicators/index.tsx (limited to 'src/plugins/serverListIndicators') diff --git a/src/plugins/serverListIndicators/index.tsx b/src/plugins/serverListIndicators/index.tsx new file mode 100644 index 0000000..96833d8 --- /dev/null +++ b/src/plugins/serverListIndicators/index.tsx @@ -0,0 +1,137 @@ +/* + * Vencord, a modification for Discord's desktop app + * Copyright (c) 2022 Sofia Lima + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +*/ + +import { addServerListElement, removeServerListElement, ServerListRenderPosition } from "@api/ServerList"; +import { Settings } from "@api/Settings"; +import ErrorBoundary from "@components/ErrorBoundary"; +import { Devs } from "@utils/constants"; +import { useForceUpdater } from "@utils/react"; +import definePlugin, { OptionType } from "@utils/types"; +import { GuildStore, PresenceStore, RelationshipStore } from "@webpack/common"; + +const enum IndicatorType { + SERVER = 1 << 0, + FRIEND = 1 << 1, + BOTH = SERVER | FRIEND, +} + +let onlineFriends = 0; +let guildCount = 0; +let forceUpdateFriendCount: () => void; +let forceUpdateGuildCount: () => void; + +function FriendsIndicator() { + forceUpdateFriendCount = useForceUpdater(); + + return ( + + {onlineFriends} online + + ); +} + +function ServersIndicator() { + forceUpdateGuildCount = useForceUpdater(); + + return ( + + {guildCount} servers + + ); +} + +function handlePresenceUpdate() { + onlineFriends = 0; + const relations = RelationshipStore.getRelationships(); + for (const id of Object.keys(relations)) { + const type = relations[id]; + // FRIEND relationship type + if (type === 1 && PresenceStore.getStatus(id) !== "offline") { + onlineFriends += 1; + } + } + forceUpdateFriendCount?.(); +} + +function handleGuildUpdate() { + guildCount = GuildStore.getGuildCount(); + forceUpdateGuildCount?.(); +} + +export default definePlugin({ + name: "ServerListIndicators", + description: "Add online friend count or server count in the server list", + authors: [Devs.dzshn], + dependencies: ["ServerListAPI"], + + options: { + mode: { + description: "mode", + type: OptionType.SELECT, + options: [ + { label: "Only online friend count", value: IndicatorType.FRIEND, default: true }, + { label: "Only server count", value: IndicatorType.SERVER }, + { label: "Both server and online friend counts", value: IndicatorType.BOTH }, + ] + } + }, + + renderIndicator: () => { + const { mode } = Settings.plugins.ServerListIndicators; + return +
+ {!!(mode & IndicatorType.FRIEND) && } + {!!(mode & IndicatorType.SERVER) && } +
+
; + }, + + flux: { + PRESENCE_UPDATES: handlePresenceUpdate, + GUILD_CREATE: handleGuildUpdate, + GUILD_DELETE: handleGuildUpdate, + }, + + + start() { + addServerListElement(ServerListRenderPosition.Above, this.renderIndicator); + + handlePresenceUpdate(); + handleGuildUpdate(); + }, + + stop() { + removeServerListElement(ServerListRenderPosition.Above, this.renderIndicator); + } +}); -- cgit