MemberCount: fix null safety
and improve types for PopoutPosition Co-Authored-By: fres621 <126067139+fres621@users.noreply.github.com>
This commit is contained in:
parent
99cd423efb
commit
be02baffaa
5 changed files with 22 additions and 20 deletions
|
@ -14,7 +14,7 @@ import { OnlineMemberCountStore } from "./OnlineMemberCountStore";
|
|||
export function MemberCount({ isTooltip, tooltipGuildId }: { isTooltip?: true; tooltipGuildId?: string; }) {
|
||||
const currentChannel = useStateFromStores([SelectedChannelStore], () => getCurrentChannel());
|
||||
|
||||
const guildId = isTooltip ? tooltipGuildId! : currentChannel.guild_id;
|
||||
const guildId = isTooltip ? tooltipGuildId! : currentChannel?.guild_id;
|
||||
|
||||
const totalCount = useStateFromStores(
|
||||
[GuildMemberCountStore],
|
||||
|
@ -33,7 +33,7 @@ export function MemberCount({ isTooltip, tooltipGuildId }: { isTooltip?: true; t
|
|||
|
||||
const threadGroups = useStateFromStores(
|
||||
[ThreadMemberListStore],
|
||||
() => ThreadMemberListStore.getMemberListSections(currentChannel.id)
|
||||
() => ThreadMemberListStore.getMemberListSections(currentChannel?.id)
|
||||
);
|
||||
|
||||
if (!isTooltip && (groups.length >= 1 || groups[0].id !== "unknown")) {
|
||||
|
|
|
@ -15,8 +15,8 @@ export const OnlineMemberCountStore = proxyLazy(() => {
|
|||
const onlineMemberMap = new Map<string, number>();
|
||||
|
||||
class OnlineMemberCountStore extends Flux.Store {
|
||||
getCount(guildId: string) {
|
||||
return onlineMemberMap.get(guildId);
|
||||
getCount(guildId?: string) {
|
||||
return onlineMemberMap.get(guildId!);
|
||||
}
|
||||
|
||||
async _ensureCount(guildId: string) {
|
||||
|
@ -25,8 +25,8 @@ export const OnlineMemberCountStore = proxyLazy(() => {
|
|||
await PrivateChannelsStore.preload(guildId, GuildChannelStore.getDefaultChannel(guildId).id);
|
||||
}
|
||||
|
||||
ensureCount(guildId: string) {
|
||||
if (onlineMemberMap.has(guildId)) return;
|
||||
ensureCount(guildId?: string) {
|
||||
if (!guildId || onlineMemberMap.has(guildId)) return;
|
||||
|
||||
preloadQueue.push(() =>
|
||||
this._ensureCount(guildId)
|
||||
|
|
|
@ -28,12 +28,12 @@ import { FluxStore } from "@webpack/types";
|
|||
|
||||
import { MemberCount } from "./MemberCount";
|
||||
|
||||
export const GuildMemberCountStore = findStoreLazy("GuildMemberCountStore") as FluxStore & { getMemberCount(guildId: string): number | null; };
|
||||
export const GuildMemberCountStore = findStoreLazy("GuildMemberCountStore") as FluxStore & { getMemberCount(guildId?: string): number | null; };
|
||||
export const ChannelMemberStore = findStoreLazy("ChannelMemberStore") as FluxStore & {
|
||||
getProps(guildId: string, channelId: string): { groups: { count: number; id: string; }[]; };
|
||||
getProps(guildId?: string, channelId?: string): { groups: { count: number; id: string; }[]; };
|
||||
};
|
||||
export const ThreadMemberListStore = findStoreLazy("ThreadMemberListStore") as FluxStore & {
|
||||
getMemberListSections(channelId: string): { [sectionId: string]: { sectionId: string; userIds: string[]; }; };
|
||||
getMemberListSections(channelId?: string): { [sectionId: string]: { sectionId: string; userIds: string[]; }; };
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
|
||||
import { MessageObject } from "@api/MessageEvents";
|
||||
import { ChannelStore, ComponentDispatch, Constants, FluxDispatcher, GuildStore, InviteActions, MaskedLink, MessageActions, ModalImageClasses, PrivateChannelsStore, RestAPI, SelectedChannelStore, SelectedGuildStore, UserProfileActions, UserProfileStore, UserSettingsActionCreators, UserUtils } from "@webpack/common";
|
||||
import { Guild, Message, User } from "discord-types/general";
|
||||
import { Channel, Guild, Message, User } from "discord-types/general";
|
||||
|
||||
import { ImageModal, ModalRoot, ModalSize, openModal } from "./modal";
|
||||
|
||||
|
@ -54,12 +54,12 @@ export async function openInviteModal(code: string) {
|
|||
});
|
||||
}
|
||||
|
||||
export function getCurrentChannel() {
|
||||
export function getCurrentChannel(): Channel | undefined {
|
||||
return ChannelStore.getChannel(SelectedChannelStore.getChannelId());
|
||||
}
|
||||
|
||||
export function getCurrentGuild(): Guild | undefined {
|
||||
return GuildStore.getGuild(getCurrentChannel()?.guild_id);
|
||||
return GuildStore.getGuild(getCurrentChannel()?.guild_id!);
|
||||
}
|
||||
|
||||
export function openPrivateChannel(userId: string) {
|
||||
|
|
18
src/webpack/common/types/components.d.ts
vendored
18
src/webpack/common/types/components.d.ts
vendored
|
@ -91,7 +91,7 @@ export type Tooltip = ComponentType<{
|
|||
/** Tooltip.Colors.BLACK */
|
||||
color?: string;
|
||||
/** TooltipPositions.TOP */
|
||||
position?: string;
|
||||
position?: PopoutPosition;
|
||||
|
||||
tooltipClassName?: string;
|
||||
tooltipContentClassName?: string;
|
||||
|
@ -110,7 +110,7 @@ export type TooltipContainer = ComponentType<PropsWithChildren<{
|
|||
/** Tooltip.Colors.BLACK */
|
||||
color?: string;
|
||||
/** TooltipPositions.TOP */
|
||||
position?: string;
|
||||
position?: PopoutPosition;
|
||||
spacing?: number;
|
||||
|
||||
className?: string;
|
||||
|
@ -252,7 +252,7 @@ export type Select = ComponentType<PropsWithChildren<{
|
|||
look?: 0 | 1;
|
||||
className?: string;
|
||||
popoutClassName?: string;
|
||||
popoutPosition?: "top" | "left" | "right" | "bottom" | "center" | "window_center";
|
||||
popoutPosition?: PopoutPosition;
|
||||
optionClassName?: string;
|
||||
|
||||
autoFocus?: boolean;
|
||||
|
@ -293,7 +293,7 @@ export type SearchableSelect = ComponentType<PropsWithChildren<{
|
|||
className?: string;
|
||||
popoutClassName?: string;
|
||||
wrapperClassName?: string;
|
||||
popoutPosition?: "top" | "left" | "right" | "bottom" | "center" | "window_center";
|
||||
popoutPosition?: PopoutPosition;
|
||||
optionClassName?: string;
|
||||
|
||||
autoFocus?: boolean;
|
||||
|
@ -376,6 +376,8 @@ declare enum PopoutAnimation {
|
|||
FADE = "4"
|
||||
}
|
||||
|
||||
type PopoutPosition = "top" | "bottom" | "left" | "right" | "center" | "window_center";
|
||||
|
||||
export type Popout = ComponentType<{
|
||||
children(
|
||||
thing: {
|
||||
|
@ -387,7 +389,7 @@ export type Popout = ComponentType<{
|
|||
},
|
||||
data: {
|
||||
isShown: boolean;
|
||||
position: string;
|
||||
position: PopoutPosition;
|
||||
}
|
||||
): ReactNode;
|
||||
shouldShow?: boolean;
|
||||
|
@ -395,7 +397,7 @@ export type Popout = ComponentType<{
|
|||
closePopout(): void;
|
||||
isPositioned: boolean;
|
||||
nudge: number;
|
||||
position: string;
|
||||
position: PopoutPosition;
|
||||
setPopoutRef(ref: any): void;
|
||||
updatePosition(): void;
|
||||
}): ReactNode;
|
||||
|
@ -404,13 +406,13 @@ export type Popout = ComponentType<{
|
|||
onRequestClose?(): void;
|
||||
|
||||
/** "center" and others */
|
||||
align?: string;
|
||||
align?: "left" | "right" | "center";
|
||||
/** Popout.Animation */
|
||||
animation?: PopoutAnimation;
|
||||
autoInvert?: boolean;
|
||||
nudgeAlignIntoViewport?: boolean;
|
||||
/** "bottom" and others */
|
||||
position?: string;
|
||||
position?: PopoutPosition;
|
||||
positionKey?: string;
|
||||
spacing?: number;
|
||||
}> & {
|
||||
|
|
Loading…
Reference in a new issue