Fix FavouriteAnything attachment preview crash (#884)
This commit is contained in:
parent
34feac8a6c
commit
4a3f0c9e21
1 changed files with 20 additions and 7 deletions
|
|
@ -7,9 +7,9 @@
|
||||||
import { BaseText } from "@components/BaseText";
|
import { BaseText } from "@components/BaseText";
|
||||||
import { Button } from "@components/Button";
|
import { Button } from "@components/Button";
|
||||||
import { LazyComponentWrapper } from "@utils/lazyReact";
|
import { LazyComponentWrapper } from "@utils/lazyReact";
|
||||||
import { Channel, Message, MessageAttachment, ScrollerBaseRef } from "@vencord/discord-types";
|
import { Message, MessageAttachment, ScrollerBaseRef } from "@vencord/discord-types";
|
||||||
import { ChannelType } from "@vencord/discord-types/enums";
|
import { ChannelType } from "@vencord/discord-types/enums";
|
||||||
import { findByCodeLazy, findByPropsLazy, findComponentByCode, findComponentByCodeLazy, findCssClassesLazy, proxyLazyWebpack } from "@webpack";
|
import { findByCodeLazy, findComponentByCode, findComponentByCodeLazy, findCssClassesLazy, proxyLazyWebpack } from "@webpack";
|
||||||
import { ChannelStore, ExpressionPickerStore, ListScrollerThin, lodash, PermissionsBits, PermissionStore, React, useCallback, useEffect, useMemo, useRef, useState, useStateFromStores } from "@webpack/common";
|
import { ChannelStore, ExpressionPickerStore, ListScrollerThin, lodash, PermissionsBits, PermissionStore, React, useCallback, useEffect, useMemo, useRef, useState, useStateFromStores } from "@webpack/common";
|
||||||
import { ReactNode } from "react";
|
import { ReactNode } from "react";
|
||||||
|
|
||||||
|
|
@ -22,11 +22,24 @@ const ManaSearchBar = findComponentByCodeLazy<ManaSearchBarProps>("#{intl::SEARC
|
||||||
const FavoriteButton = findComponentByCodeLazy<FavoriteButtonProps>("#{intl::GIF_TOOLTIP_ADD_TO_FAVORITES}");
|
const FavoriteButton = findComponentByCodeLazy<FavoriteButtonProps>("#{intl::GIF_TOOLTIP_ADD_TO_FAVORITES}");
|
||||||
const SendIcon = findComponentByCodeLazy("M6.6 10.02 14 11.4a.6.6");
|
const SendIcon = findComponentByCodeLazy("M6.6 10.02 14 11.4a.6.6");
|
||||||
|
|
||||||
const DmChannel: Channel & { new(base?: Partial<Channel>): Channel; } = findByPropsLazy("fromServer", "sortRecipients");
|
const createChannelRecordFromServer = findByCodeLazy(".GUILD_TEXT]", "fromServer)");
|
||||||
const MessageClass: Message & { new(base?: Partial<Message>): Message; } = findByCodeLazy(";mentionRoles;mentionChannels;");
|
const createMessageRecord = findByCodeLazy(".createFromServer(", ".isBlockedForMessage", "messageReference:");
|
||||||
|
|
||||||
const Classes = findCssClassesLazy("gifFavoriteButton", "ctaButtonContainer");
|
const Classes = findCssClassesLazy("gifFavoriteButton", "ctaButtonContainer");
|
||||||
|
|
||||||
|
function createPreviewMessage(attachment: MessageAttachment, channelId: string) {
|
||||||
|
const previewMessage = {
|
||||||
|
id: `favourite-anything-preview-${attachment.id}`,
|
||||||
|
attachments: [attachment],
|
||||||
|
channel_id: channelId,
|
||||||
|
content: "",
|
||||||
|
type: 0,
|
||||||
|
timestamp: new Date().toISOString()
|
||||||
|
};
|
||||||
|
|
||||||
|
return createMessageRecord(previewMessage) as Message;
|
||||||
|
}
|
||||||
|
|
||||||
export const AttachmentPreview = proxyLazyWebpack(() => {
|
export const AttachmentPreview = proxyLazyWebpack(() => {
|
||||||
// findComponentByCodeLazy doesn't work properly with component classes, this must be kept within the lazy scope
|
// findComponentByCodeLazy doesn't work properly with component classes, this must be kept within the lazy scope
|
||||||
const MessageComponent = findComponentByCode("this.renderAttachments") as LazyComponentWrapper<MessageComponentClass>;
|
const MessageComponent = findComponentByCode("this.renderAttachments") as LazyComponentWrapper<MessageComponentClass>;
|
||||||
|
|
@ -37,12 +50,12 @@ export const AttachmentPreview = proxyLazyWebpack(() => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const channel = Object.freeze(new DmChannel({ id: "0", type: ChannelType.GUILD_TEXT }));
|
const channel = Object.freeze(createChannelRecordFromServer({ id: "0", type: ChannelType.GUILD_TEXT }));
|
||||||
|
|
||||||
return function AttachmentPreview({ attachment }: AttachmentsComponentProps) {
|
return function AttachmentPreview({ attachment }: AttachmentsComponentProps) {
|
||||||
const message = useMemo(
|
const message = useMemo(
|
||||||
() => new MessageClass({ attachments: [attachment], channel_id: channel.id }),
|
() => createPreviewMessage(attachment, channel.id),
|
||||||
[attachment]
|
[attachment, channel.id]
|
||||||
);
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue