Refactor ContextMenuAPI (#2236)

This commit is contained in:
Kyuuhachi 2024-03-07 11:06:24 +01:00 committed by GitHub
parent 612fdf8952
commit 42a9fa2d47
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
25 changed files with 220 additions and 245 deletions

View file

@ -16,11 +16,11 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { addContextMenuPatch, findGroupChildrenByChildId, NavContextMenuPatchCallback, removeContextMenuPatch } from "@api/ContextMenu";
import { findGroupChildrenByChildId } from "@api/ContextMenu";
import { definePluginSettings } from "@api/Settings";
import { Devs } from "@utils/constants";
import definePlugin, { OptionType } from "@utils/types";
import { ContextMenuApi, Menu } from "@webpack/common";
import { Menu } from "@webpack/common";
const settings = definePluginSettings({
forceServerHome: {
@ -30,25 +30,11 @@ const settings = definePluginSettings({
}
});
const contextMenuPatch: NavContextMenuPatchCallback = (children, props) => () => {
if (!props?.guild) return;
function useForceServerHome() {
const { forceServerHome } = settings.use(["forceServerHome"]);
const group = findGroupChildrenByChildId("hide-muted-channels", children);
group?.unshift(
<Menu.MenuCheckboxItem
key="force-server-home"
id="force-server-home"
label="Force Server Home"
checked={settings.store.forceServerHome}
action={() => {
settings.store.forceServerHome = !settings.store.forceServerHome;
ContextMenuApi.closeContextMenu();
}}
/>
);
};
return forceServerHome;
}
export default definePlugin({
name: "ResurrectHome",
@ -109,17 +95,25 @@ export default definePlugin({
}
],
start() {
addContextMenuPatch("guild-context", contextMenuPatch);
},
useForceServerHome,
stop() {
removeContextMenuPatch("guild-context", contextMenuPatch);
},
contextMenus: {
"guild-context"(children, props) {
const forceServerHome = useForceServerHome();
useForceServerHome() {
const { forceServerHome } = settings.use(["forceServerHome"]);
if (!props?.guild) return;
return forceServerHome;
const group = findGroupChildrenByChildId("hide-muted-channels", children);
group?.unshift(
<Menu.MenuCheckboxItem
key="force-server-home"
id="force-server-home"
label="Force Server Home"
checked={forceServerHome}
action={() => settings.store.forceServerHome = !forceServerHome}
/>
);
}
}
});