Skip to content

Commit

Permalink
refactor(messagePopover): simplify the plaintext patch by getting the…
Browse files Browse the repository at this point in the history
… component by source
  • Loading branch information
FedeIlLeone committed Oct 17, 2024
1 parent f10b372 commit bb7e6e2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
26 changes: 18 additions & 8 deletions src/renderer/coremods/messagePopover/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getBySource, getFunctionBySource } from "@webpack";
import type { Channel, Message } from "discord-types/general";
import type { GetButtonItem, IconButtonProps } from "../../../types/coremods/message";
import type { GetButtonItem, HoverBarButtonProps } from "../../../types/coremods/message";
import { Logger } from "../../modules/logger";

const logger = Logger.api("MessagePopover");
Expand Down Expand Up @@ -27,24 +28,33 @@ export function removeButton(item: GetButtonItem): void {
buttons.delete(item);
}

type HoverBarButton = React.FC<HoverBarButtonProps>;

/**
* @internal
* @hidden
*/
export function _buildPopoverElements(
msg: Message,
channel: Channel,
IconButton: React.FC<IconButtonProps>,
): React.ReactElement[] {
const items = [] as React.ReactElement[];
export function _buildPopoverElements(msg: Message, channel: Channel): React.ReactElement[] {
const items: React.ReactElement[] = [];

// Waiting for the module is not necessary, as it is already loaded by the time this function is called
const hoverBarButtonStr = ".hoverBarButton";
const HoverBarButton = getFunctionBySource<HoverBarButton>(
getBySource(hoverBarButtonStr),
hoverBarButtonStr,
);
if (!HoverBarButton) {
logger.error("Could not find HoverBarButton");
return items;
}

buttons.forEach((key, getItem) => {
try {
const item = getItem(msg, channel);
try {
if (item) {
item.key = key;
items.push(<IconButton {...item} />);
items.push(<HoverBarButton {...item} />);
}
} catch (err) {
logger.error(`Error in making the button [${item?.key}]`, err, item);
Expand Down
7 changes: 3 additions & 4 deletions src/renderer/coremods/messagePopover/plaintextPatches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ export default [
find: "Messages.MESSAGE_UTILITIES_A11Y_LABEL",
replacements: [
{
match:
/(\.Fragment,{children:\[)(.{1,25}Fragment.{1,50}(\w+\.\w+),{label:\w+\.\w+\.Messages\.COPY_ID_MESSAGE)/,
replace: (_, prefix, suffix, makeButton) =>
`${prefix}...(replugged.coremods.coremods.messagePopover?._buildPopoverElements(arguments[0]?.message,arguments[0]?.channel,${makeButton}) ?? []),${suffix}`,
match: /(\.Fragment,{children:\[)(.{1,75},{label:\w+\.\w+\.Messages\.COPY_ID_MESSAGE)/,
replace: (_, prefix, suffix) =>
`${prefix}...(replugged.coremods.coremods.messagePopover?._buildPopoverElements(arguments[0]?.message,arguments[0]?.channel) ?? []),${suffix}`,
},
],
},
Expand Down
4 changes: 2 additions & 2 deletions src/types/coremods/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface ButtonPopoverProps extends React.ComponentPropsWithoutRef<"div"> {
dangerous?: boolean;
}

export interface IconButtonProps extends ButtonPopoverProps {
export interface HoverBarButtonProps extends ButtonPopoverProps {
label: string;
ariaLabel?: string;
tooltipText?: string;
Expand All @@ -22,4 +22,4 @@ export interface IconButtonProps extends ButtonPopoverProps {
buttonClassName?: string;
}

export type GetButtonItem = (message: Message, channel: Channel) => IconButtonProps | null;
export type GetButtonItem = (message: Message, channel: Channel) => HoverBarButtonProps | null;

0 comments on commit bb7e6e2

Please sign in to comment.