diff --git a/src/plugins/memberCount.tsx b/src/plugins/memberCount.tsx index 0b5409c0c..a18cf6e14 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 }) });