diff options
author | Vendicated <vendicated@riseup.net> | 2023-05-05 18:08:12 +0200 |
---|---|---|
committer | Vendicated <vendicated@riseup.net> | 2023-05-05 18:41:04 +0200 |
commit | 0b2c3c834a74fb84dc8cb6b22589530f3420828b (patch) | |
tree | 8641af0b44ea196c9fcc30d5b13bb6c1644719ce /src | |
parent | 3a54a24c70775c0af17263d30e088950ca551e7c (diff) | |
download | Vencord-0b2c3c834a74fb84dc8cb6b22589530f3420828b.tar.gz Vencord-0b2c3c834a74fb84dc8cb6b22589530f3420828b.tar.bz2 Vencord-0b2c3c834a74fb84dc8cb6b22589530f3420828b.zip |
MemberCount: Fix not properly updating on channel switch
Diffstat (limited to 'src')
-rw-r--r-- | src/plugins/memberCount.tsx | 68 |
1 files changed, 25 insertions, 43 deletions
diff --git a/src/plugins/memberCount.tsx b/src/plugins/memberCount.tsx index 0b5409c..a18cf6e 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<string, [number, number]>; -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]; - - forceUpdate = useForceUpdater(); - - if (!c) return null; + const { id: channelId, guild_id: guildId } = useStateFromStores([SelectedChannelStore], () => getCurrentChannel()); + const { groups } = useStateFromStores( + [ChannelMemberStore], + () => ChannelMemberStore.getProps(guildId, channelId) + ); + const total = useStateFromStores( + [GuildMemberCountStore], + () => GuildMemberCountStore.getMemberCount(guildId) + ); - let total = c[0].toLocaleString(); - if (total === "0" && c[1] > 0) { - const approx = GuildMemberCountStore.getMemberCount(guildId); - total = approx ? approx.toLocaleString() : "Loading..."; - } + if (total == null) + return null; - const online = c[1].toLocaleString(); + const online = + (groups.length === 1 && groups[0].id === "unknown") + ? 0 + : groups.reduce((count, curr) => count + curr.count, 0); return ( <Flex id="vc-membercount" style={{ @@ -55,7 +58,7 @@ function MemberCount() { alignContent: "center", gap: 0 }}> - <Tooltip text={`${online} Online`} position="bottom"> + <Tooltip text={`${online} Online in this Channel`} position="bottom"> {props => ( <div {...props}> <span @@ -72,7 +75,7 @@ function MemberCount() { </div> )} </Tooltip> - <Tooltip text={`${total} Total Members`} position="bottom"> + <Tooltip text={`${total} Total Server Members`} position="bottom"> {props => ( <div {...props}> <span @@ -102,31 +105,10 @@ export default definePlugin({ patches: [{ find: ".isSidebarVisible,", replacement: { - match: /(var (.)=.\.className.+?children):\[(.\.useMemo[^}]+"aria-multiselectable")/, + match: /(var (\i)=\i\.className.+?children):\[(\i\.useMemo[^}]+"aria-multiselectable")/, replace: "$1:[$2.startsWith('members')?$self.render():null,$3" } }], - flux: { - GUILD_MEMBER_LIST_UPDATE({ guildId, groups, memberCount, id }) { - // eeeeeh - sometimes it has really wrong counts??? like 10 times less than actual - // but if we only listen to everyone updates, sometimes we never get the count? - // this seems to work but isn't optional - if (id !== "everyone" && counts[guildId]) return; - - let count = 0; - for (const group of groups) { - if (group.id !== "offline") - count += group.count; - } - counts[guildId] = [memberCount, count]; - forceUpdate?.(); - } - }, - - render: () => ( - <ErrorBoundary noop> - <MemberCount /> - </ErrorBoundary> - ) + render: ErrorBoundary.wrap(MemberCount, { noop: true }) }); |