diff options
Diffstat (limited to 'src/api')
-rw-r--r-- | src/api/MessagePopover.ts | 69 | ||||
-rw-r--r-- | src/api/index.ts | 7 |
2 files changed, 75 insertions, 1 deletions
diff --git a/src/api/MessagePopover.ts b/src/api/MessagePopover.ts new file mode 100644 index 0000000..85dff9c --- /dev/null +++ b/src/api/MessagePopover.ts @@ -0,0 +1,69 @@ +/* + * 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/>. +*/ + +import Logger from "@utils/Logger"; +import { Channel, Message } from "discord-types/general"; +import type { MouseEventHandler } from "react"; + +const logger = new Logger("MessagePopover"); + +export interface ButtonItem { + key?: string, + label: string, + icon: React.ComponentType<any>, + message: Message, + channel: Channel, + onClick?: MouseEventHandler<HTMLButtonElement>, + onContextMenu?: MouseEventHandler<HTMLButtonElement>; +} + +export type getButtonItem = (message: Message) => ButtonItem | null; + +export const buttons = new Map<string, getButtonItem>(); + +export function addButton( + identifier: string, + item: getButtonItem, +) { + buttons.set(identifier, item); +} + +export function removeButton(identifier: string) { + buttons.delete(identifier); +} + +export function _buildPopoverElements( + msg: Message, + makeButton: (item: ButtonItem) => React.ComponentType +) { + const items = [] as React.ComponentType[]; + + for (const [identifier, getItem] of buttons.entries()) { + try { + const item = getItem(msg); + if (item) { + item.key ??= identifier; + items.push(makeButton(item)); + } + } catch (err) { + logger.error(`[${identifier}]`, err); + } + } + + return items; +} diff --git a/src/api/index.ts b/src/api/index.ts index 98fc6a4..b74da6e 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -21,6 +21,7 @@ import * as $Commands from "./Commands"; import * as $DataStore from "./DataStore"; import * as $MessageAccessories from "./MessageAccessories"; import * as $MessageEventsAPI from "./MessageEvents"; +import * as $MessagePopover from "./MessagePopover"; import * as $Notices from "./Notices"; import * as $ServerList from "./ServerList"; @@ -60,6 +61,10 @@ const DataStore = $DataStore; */ const MessageAccessories = $MessageAccessories; /** + * An API allowing you to add custom buttons in the message popover + */ +const MessagePopover = $MessagePopover; +/** * An API allowing you to add badges to user profiles */ const Badges = $Badges; @@ -68,4 +73,4 @@ const Badges = $Badges; */ const ServerList = $ServerList; -export { Badges, Commands, DataStore, MessageAccessories, MessageEvents, Notices, ServerList }; +export { Badges, Commands, DataStore, MessageAccessories, MessageEvents, MessagePopover, Notices, ServerList }; |