Skip to content

Commit

Permalink
fix(coremods): message popover plaintext patch
Browse files Browse the repository at this point in the history
  • Loading branch information
FedeIlLeone committed Sep 7, 2024
1 parent 26acb64 commit 225b94b
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 10 deletions.
58 changes: 58 additions & 0 deletions src/renderer/coremods/messagePopover/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import type { Channel, Message } from "discord-types/general";
import type { GetButtonItem, IconButtonProps } from "../../../types/coremods/message";
import { Logger } from "../../modules/logger";

const logger = Logger.api("MessagePopover");

export const buttons = new Map<GetButtonItem, string>();

/**
* Adds a button to any MessagePopover
* @param item The function that creates the button to add
* @param key Optional key for button
* @returns A callback to remove the button from set.
*/
export function addButton(item: GetButtonItem, key?: string): () => void {
buttons.set(item, `${key || "repluggedButton"}-${Math.random().toString(36).substring(2)}`);

return () => removeButton(item);
}

/**
* Removes a button from MessagePopover
* @param item The function that creates the button to add
* @returns
*/
export function removeButton(item: GetButtonItem): void {
buttons.delete(item);
}

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

buttons.forEach((key, getItem) => {
try {
const item = getItem(msg, channel);
try {
if (item) {
item.key = key;
items.push(<IconButton {...item} />);
}
} catch (err) {
logger.error(`Error in making the button [${item?.key}]`, err, item);
}
} catch (err) {
logger.error("Error while running GetButtonItem function", err, getItem);
}
});

return items;
}
8 changes: 4 additions & 4 deletions src/renderer/coremods/messagePopover/plaintextPatches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import type { PlaintextPatch } from "src/types";

export default [
{
find: 'key:"copy-id"',
find: "Messages.MESSAGE_UTILITIES_A11Y_LABEL",
replacements: [
{
match:
/(Fragment,{children:\[)(.{0,200}children:\[.{0,20}?(\w{1,3})\({.{0,5}\s?key:"copy-id".{0,20}channel:(.{1,3})[,}].{0,20}message:(.{1,3})[,}])/,
replace: (_, prefix, suffix, makeButton, channel, message) =>
`${prefix}...(replugged.coremods.coremods.messagePopover?._buildPopoverElements(${message},${channel},${makeButton}) ?? []),${suffix}`,
/(\.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}`,
},
],
},
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/modules/injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ export class Injector {
* By default, items are placed in a group for custom items, though that can be customized with `sectionId` and `indexInSection`
* @param navId The id of the menu to add to
* @param item The function that creates the item to add
* @param sectionId — The number of the section to add to. Defaults to replugged's section
* @param sectionId — The number of the section to add to. Defaults to Replugged's section
* @param indexInSection — The index in the section to add to. Defaults to the end position
* @returns A callback to de-register the function
*
Expand Down
10 changes: 5 additions & 5 deletions src/types/coremods/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ interface ButtonPopoverProps extends React.ComponentPropsWithoutRef<"div"> {
dangerous?: boolean;
}

export interface IconButtonProps extends Omit<ButtonPopoverProps, "onClick"> {
export interface IconButtonProps extends ButtonPopoverProps {
label: string;
channel?: Channel;
message?: Message;
onClick: (channel: Channel, message: Message, event: React.MouseEvent<HTMLDivElement>) => void;
ariaLabel?: string;
tooltipText?: string;
tooltipColor?: string;
icon: React.ComponentType<unknown>;
iconProps?: Record<string, unknown>;
key?: string;
onTooltipShow?: () => void;
onTooltipHide?: () => void;
separator?: boolean;
sparkle?: boolean;
showNewBadge?: boolean;
buttonClassName?: string;
}

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

0 comments on commit 225b94b

Please sign in to comment.