From a11d5a91119b493ad8acc931b70997c849d9857e Mon Sep 17 00:00:00 2001 From: V Date: Sat, 24 Jun 2023 16:46:09 +0200 Subject: [PATCH] Add ComponentUpdaterAPI --- src/api/ComponentUpdater.ts | 29 ++++++++++++++++ src/api/index.ts | 6 ++++ src/plugins/_api/componentUpdater.ts | 51 ++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 src/api/ComponentUpdater.ts create mode 100644 src/plugins/_api/componentUpdater.ts diff --git a/src/api/ComponentUpdater.ts b/src/api/ComponentUpdater.ts new file mode 100644 index 00000000..7406e27b --- /dev/null +++ b/src/api/ComponentUpdater.ts @@ -0,0 +1,29 @@ +/* + * Vencord, a modification for Discord's desktop app + * Copyright (c) 2023 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 . +*/ + +import { proxyLazy } from "@utils/lazy"; + +const p = proxyLazy(() => Vencord.Plugins.plugins.ComponentUpdaterAPI as any); + +/** + * Rerender a specific message + * @param messageId The id of the message to rerender + */ +export function updateMessageComponent(messageId: string) { + p.forceUpdaters.get(messageId)?.(); +} diff --git a/src/api/index.ts b/src/api/index.ts index f2c47e55..abaff862 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -18,6 +18,7 @@ import * as $Badges from "./Badges"; import * as $Commands from "./Commands"; +import * as $ComponentUpdater from "./ComponentUpdater"; import * as $ContextMenu from "./ContextMenu"; import * as $DataStore from "./DataStore"; import * as $MemberListDecorators from "./MemberListDecorators"; @@ -109,3 +110,8 @@ export const Notifications = $Notifications; * An api allowing you to patch and add/remove items to/from context menus */ export const ContextMenu = $ContextMenu; + +/** + * An api allowing you to update/rerender components + */ +export const ComponentUpdater = $ComponentUpdater; diff --git a/src/plugins/_api/componentUpdater.ts b/src/plugins/_api/componentUpdater.ts new file mode 100644 index 00000000..f0dbb3b1 --- /dev/null +++ b/src/plugins/_api/componentUpdater.ts @@ -0,0 +1,51 @@ +/* + * Vencord, a modification for Discord's desktop app + * Copyright (c) 2023 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 . +*/ + +import { Devs } from "@utils/constants"; +import { useForceUpdater } from "@utils/react"; +import definePlugin from "@utils/types"; +import { useEffect } from "@webpack/common"; +import { Channel, Message } from "discord-types/general"; + +const forceUpdaters = new Map void>(); + +function useUpdater(data: { channel: Channel; message: Message; }) { + const forceUpdater = useForceUpdater(); + + useEffect(() => { + forceUpdaters.set(data.message.id, forceUpdater); + return () => void forceUpdaters.delete(data.message.id); + }, [data.message.id]); +} + +export default definePlugin({ + name: "ComponentUpdaterAPI", + description: "API to update / force rerender several components, such as messages", + authors: [Devs.Ven], + + patches: [{ + find: ".renderContentOnly;", + replacement: { + match: /=(\i)\.renderContentOnly;/, + replace: "$&$self.useUpdater($1);" + } + }], + + useUpdater, + forceUpdaters +});