2022-12-02 09:38:52 -06:00
|
|
|
/*
|
|
|
|
* Vencord, a modification for Discord's desktop app
|
|
|
|
* Copyright (c) 2022 Vendicated and contributors
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2024-08-03 11:02:26 +02:00
|
|
|
import ErrorBoundary from "@components/ErrorBoundary";
|
2023-05-06 01:36:00 +02:00
|
|
|
import { Logger } from "@utils/Logger";
|
2022-12-02 09:38:52 -06:00
|
|
|
import { Channel, Message } from "discord-types/general";
|
2024-08-03 11:02:26 +02:00
|
|
|
import type { ComponentType, MouseEventHandler } from "react";
|
2022-12-02 09:38:52 -06:00
|
|
|
|
|
|
|
const logger = new Logger("MessagePopover");
|
|
|
|
|
2025-01-23 02:48:44 +01:00
|
|
|
export interface MessagePopoverButtonItem {
|
2022-12-02 09:38:52 -06:00
|
|
|
key?: string,
|
|
|
|
label: string,
|
2024-08-02 21:17:47 -03:00
|
|
|
icon: ComponentType<any>,
|
2022-12-02 09:38:52 -06:00
|
|
|
message: Message,
|
|
|
|
channel: Channel,
|
|
|
|
onClick?: MouseEventHandler<HTMLButtonElement>,
|
|
|
|
onContextMenu?: MouseEventHandler<HTMLButtonElement>;
|
|
|
|
}
|
|
|
|
|
2025-01-23 02:48:44 +01:00
|
|
|
export type MessagePopoverButtonFactory = (message: Message) => MessagePopoverButtonItem | null;
|
2022-12-02 09:38:52 -06:00
|
|
|
|
2025-01-23 02:48:44 +01:00
|
|
|
export const buttons = new Map<string, MessagePopoverButtonFactory>();
|
2022-12-02 09:38:52 -06:00
|
|
|
|
2025-01-23 02:48:44 +01:00
|
|
|
export function addMessagePopoverButton(
|
2022-12-02 09:38:52 -06:00
|
|
|
identifier: string,
|
2025-01-23 02:48:44 +01:00
|
|
|
item: MessagePopoverButtonFactory,
|
2022-12-02 09:38:52 -06:00
|
|
|
) {
|
|
|
|
buttons.set(identifier, item);
|
|
|
|
}
|
|
|
|
|
2025-01-23 02:48:44 +01:00
|
|
|
export function removeMessagePopoverButton(identifier: string) {
|
2022-12-02 09:38:52 -06:00
|
|
|
buttons.delete(identifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function _buildPopoverElements(
|
2025-01-23 02:48:44 +01:00
|
|
|
Component: React.ComponentType<MessagePopoverButtonItem>,
|
2024-08-03 17:54:25 +02:00
|
|
|
message: Message
|
2022-12-02 09:38:52 -06:00
|
|
|
) {
|
2024-08-03 11:02:26 +02:00
|
|
|
const items: React.ReactNode[] = [];
|
2022-12-02 09:38:52 -06:00
|
|
|
|
|
|
|
for (const [identifier, getItem] of buttons.entries()) {
|
|
|
|
try {
|
2024-08-03 17:54:25 +02:00
|
|
|
const item = getItem(message);
|
2022-12-02 09:38:52 -06:00
|
|
|
if (item) {
|
|
|
|
item.key ??= identifier;
|
2024-08-03 11:02:26 +02:00
|
|
|
items.push(
|
|
|
|
<ErrorBoundary noop>
|
2024-08-03 17:54:25 +02:00
|
|
|
<Component {...item} />
|
2024-08-03 11:02:26 +02:00
|
|
|
</ErrorBoundary>
|
|
|
|
);
|
2022-12-02 09:38:52 -06:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
logger.error(`[${identifier}]`, err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-03 11:02:26 +02:00
|
|
|
return <>{items}</>;
|
2022-12-02 09:38:52 -06:00
|
|
|
}
|