From 0b2c3c834a74fb84dc8cb6b22589530f3420828b Mon Sep 17 00:00:00 2001 From: Vendicated Date: Fri, 5 May 2023 18:08:12 +0200 Subject: [PATCH] MemberCount: Fix not properly updating on channel switch --- src/plugins/memberCount.tsx | 68 ++++++++++++++----------------------- 1 file changed, 25 insertions(+), 43 deletions(-) diff --git a/src/plugins/memberCount.tsx b/src/plugins/memberCount.tsx index 0b5409c0..a18cf6e1 100644 --- a/src/plugins/memberCount.tsx +++ b/src/plugins/memberCount.tsx @@ -20,31 +20,34 @@ import ErrorBoundary from "@components/ErrorBoundary"; import { Flex } from "@components/Flex"; import { Devs } from "@utils/constants"; import { getCurrentChannel } from "@utils/discord"; -import { useForceUpdater } from "@utils/misc"; import definePlugin from "@utils/types"; import { findStoreLazy } from "@webpack"; -import { Tooltip } from "@webpack/common"; +import { SelectedChannelStore, Tooltip, useStateFromStores } from "@webpack/common"; +import { FluxStore } from "@webpack/types"; -const counts = {} as Record; -let forceUpdate: () => void; - -const GuildMemberCountStore = findStoreLazy("GuildMemberCountStore"); +const GuildMemberCountStore = findStoreLazy("GuildMemberCountStore") as FluxStore & { getMemberCount(guildId: string): number | null; }; +const ChannelMemberStore = findStoreLazy("ChannelMemberStore") as FluxStore & { + getProps(guildId: string, channelId: string): { groups: { count: number; id: string; }[]; }; +}; function MemberCount() { - const guildId = getCurrentChannel().guild_id; - const c = counts[guildId]; + const { id: channelId, guild_id: guildId } = useStateFromStores([SelectedChannelStore], () => getCurrentChannel()); + const { groups } = useStateFromStores( + [ChannelMemberStore], + () => ChannelMemberStore.getProps(guildId, channelId) + ); + const total = useStateFromStores( + [GuildMemberCountStore], + () => GuildMemberCountStore.getMemberCount(guildId) + ); - forceUpdate = useForceUpdater(); + if (total == null) + return null; - if (!c) return null; - - let total = c[0].toLocaleString(); - if (total === "0" && c[1] > 0) { - const approx = GuildMemberCountStore.getMemberCount(guildId); - total = approx ? approx.toLocaleString() : "Loading..."; - } - - const online = c[1].toLocaleString(); + const online = + (groups.length === 1 && groups[0].id === "unknown") + ? 0 + : groups.reduce((count, curr) => count + curr.count, 0); return ( - + {props => (
)} - + {props => (
( - - - - ) + render: ErrorBoundary.wrap(MemberCount, { noop: true }) });