Add ComponentUpdaterAPI
This commit is contained in:
parent
d7ac418e05
commit
a11d5a9111
29
src/api/ComponentUpdater.ts
Normal file
29
src/api/ComponentUpdater.ts
Normal file
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { proxyLazy } from "@utils/lazy";
|
||||
|
||||
const p = proxyLazy<typeof import("plugins/_api/componentUpdater").default>(() => 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)?.();
|
||||
}
|
@ -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;
|
||||
|
51
src/plugins/_api/componentUpdater.ts
Normal file
51
src/plugins/_api/componentUpdater.ts
Normal file
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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<string, () => 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
|
||||
});
|
Loading…
Reference in New Issue
Block a user