Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

context menu api fix #520

Open
wants to merge 80 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
be3f51b
fml
yofukashino Jul 21, 2023
6c2ccb7
removed testing log
yofukashino Jul 21, 2023
338d024
back to plain text patch
yofukashino Jul 23, 2023
f9e1e03
cspell will explod
yofukashino Jul 23, 2023
3f38f20
Merge branch 'main' into context-menu-api-fix
yofukashino Jul 23, 2023
fc4dafa
why do i have to format twice?
yofukashino Jul 23, 2023
237735d
Merge branch 'main' into context-menu-api-fix
yofukashino Jul 23, 2023
1b80faf
;-;
yofukashino Jul 23, 2023
23dbb25
fix some crashes sometimes
yofukashino Jul 23, 2023
859d7e4
Merge branch 'main' into context-menu-api-fix
yofukashino Jul 24, 2023
5f86eef
handle non array
yofukashino Jul 24, 2023
6c625bc
dumb me forgot this.
yofukashino Jul 25, 2023
06b0d06
done
yofukashino Jul 26, 2023
482e3dc
handle adding props at right place. would add for submenu items too now
yofukashino Jul 26, 2023
9c172c4
pretty now?
yofukashino Jul 26, 2023
eec81e3
handle some more edge cases
yofukashino Jul 26, 2023
166f4cd
dont add falsy values to menu
yofukashino Jul 26, 2023
0295196
requested changes
yofukashino Jul 27, 2023
bfeb5e1
prettier
yofukashino Jul 27, 2023
ffa0fc3
should be it
yofukashino Aug 11, 2023
9f8c076
better than how its already handled
yofukashino Aug 11, 2023
c77797f
personality >>> looks
yofukashino Aug 11, 2023
58baaf9
Merge branch 'main' into context-menu-api-fix
yofukashino Aug 19, 2023
5cfe18c
Merge branch 'main' into context-menu-api-fix
yofukashino Aug 22, 2023
3c05bdb
Merge branch 'main' into context-menu-api-fix
yofukashino Sep 1, 2023
4f30cfd
Merge branch 'main' into context-menu-api-fix
yofukashino Sep 5, 2023
46791d6
Merge branch 'main' into context-menu-api-fix
asportnoy Sep 7, 2023
26dee9e
Merge branch 'main' into context-menu-api-fix
yofukashino Sep 17, 2023
2d7e80c
Merge https://github.com/replugged-org/replugged into context-menu-ap…
yofukashino Sep 21, 2023
8933ba8
Merge remote-tracking branch 'refs/remotes/origin/context-menu-api-fi…
yofukashino Sep 21, 2023
6eae5a5
okay you asked for it
yofukashino Sep 21, 2023
eb0ded9
made it warning instead
yofukashino Sep 24, 2023
336cddb
Merge branch 'main' into context-menu-api-fix
yofukashino Sep 24, 2023
5b83882
Merge branch 'main' into context-menu-api-fix
yofukashino Sep 26, 2023
3e48742
Merge branch 'main' into context-menu-api-fix
yofukashino Sep 30, 2023
b372e21
should pass
yofukashino Sep 30, 2023
40fd42a
Merge branch 'main' into context-menu-api-fix
yofukashino Sep 30, 2023
c584a38
pretty?
yofukashino Sep 30, 2023
0dbac6c
Merge branch 'context-menu-api-fix' of https://github.com/Tharki-God/…
yofukashino Sep 30, 2023
ba5d23b
Merge branch 'main' into context-menu-api-fix
yofukashino Oct 10, 2023
5dd6c7e
Merge branch 'main' into context-menu-api-fix
yofukashino Oct 17, 2023
5b014bf
Merge branch 'main' into context-menu-api-fix
yofukashino Oct 19, 2023
552d2a8
Merge branch 'main' into context-menu-api-fix
yofukashino Oct 20, 2023
33e4a34
Merge branch 'main' into context-menu-api-fix
yofukashino Oct 22, 2023
d8eb887
better plain text patch
yofukashino Oct 23, 2023
e6dc5f6
Merge branch 'main' into context-menu-api-fix
yofukashino Nov 2, 2023
9b67560
fixed for unswc
yofukashino Nov 2, 2023
1d4b48b
Merge branch 'context-menu-api-fix' of https://github.com/Tharki-God/…
yofukashino Nov 2, 2023
8bff7ac
idk
yofukashino Nov 2, 2023
ea82bd3
fixed me dumb dumb
yofukashino Nov 5, 2023
2418881
Merge branch 'main' into context-menu-api-fix
yofukashino Dec 1, 2023
684c8c2
Merge branch 'main' into context-menu-api-fix
yofukashino Dec 3, 2023
ff7370f
Merge branch 'main' into context-menu-api-fix
yofukashino Dec 16, 2023
3530b9a
Merge branch 'main' into context-menu-api-fix
yofukashino Jan 13, 2024
9d7ecf9
syntx?
yofukashino Jan 30, 2024
8681f8e
Merge branch 'context-menu-api-fix' of https://github.com/Tharki-God/…
yofukashino Jan 30, 2024
32b9053
Merge branch 'main' into context-menu-api-fix
yofukashino Jan 30, 2024
6a2b027
pnpm replug
yofukashino Feb 21, 2024
dc4ca50
Merge branch 'context-menu-api-fix' of https://github.com/Tharki-God/…
yofukashino Feb 21, 2024
660484b
Merge branch 'main' into context-menu-api-fix
yofukashino Feb 21, 2024
99a50ba
better?
yofukashino Feb 21, 2024
9af98b0
Merge branch 'context-menu-api-fix' of https://github.com/Tharki-God/…
yofukashino Feb 21, 2024
4df27da
better?
yofukashino Feb 21, 2024
50bc857
type fix
yofukashino Feb 28, 2024
7941c99
Merge branch 'main' into context-menu-api-fix
yofukashino Apr 4, 2024
1683626
Merge branch 'main' into context-menu-api-fix
yofukashino Apr 7, 2024
8271cc8
Merge branch 'main' into context-menu-api-fix
yofukashino Apr 8, 2024
6cec1fe
Merge branch 'main' into context-menu-api-fix
yofukashino May 4, 2024
77b6bf4
Merge branch 'main' into context-menu-api-fix
yofukashino May 20, 2024
97930b3
Merge branch 'main' into context-menu-api-fix
yofukashino May 31, 2024
5ddd1b5
Merge branch 'main' into context-menu-api-fix
yofukashino Jun 14, 2024
22a4081
Merge branch 'main' into context-menu-api-fix
yofukashino Jun 21, 2024
8254073
fix
yofukashino Jun 21, 2024
dff6ede
Merge branch 'main' into context-menu-api-fix
yofukashino Jun 26, 2024
e9c279e
pretty
yofukashino Jun 26, 2024
e7f00d3
Merge branch 'main' into context-menu-api-fix
colin273 Jul 6, 2024
8e3f82e
Merge branch 'main' into context-menu-api-fix
yofukashino Jul 9, 2024
6f9d524
prettier
yofukashino Jul 13, 2024
7be48ab
Merge branch 'main' into context-menu-api-fix
yofukashino Sep 9, 2024
7573ade
Merge branch 'main' into context-menu-api-fix
yofukashino Oct 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 65 additions & 46 deletions src/renderer/coremods/contextMenu/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { Injector } from "src/renderer/modules/injector";
import { Logger } from "src/renderer/modules/logger";
import { filters, waitForModule } from "src/renderer/modules/webpack";
import { React } from "@common";
import type { ContextMenuProps } from "@components/ContextMenu";
import type {
Expand All @@ -6,9 +9,9 @@ import type {
GetContextItem,
RawContextItem,
} from "../../../types/coremods/contextMenu";
import { Logger } from "../../modules/logger";
import { getByProps } from "../../modules/webpack";
import { AnyFunction } from "src/types";

let injector: Injector;
const logger = Logger.api("ContextMenu");

export const menuItems = {} as Record<
Expand Down Expand Up @@ -88,53 +91,69 @@ type ContextMenuData = ContextMenuProps["ContextMenu"] & {
* @internal
yofukashino marked this conversation as resolved.
Show resolved Hide resolved
* @hidden
*/
yofukashino marked this conversation as resolved.
Show resolved Hide resolved
export function _insertMenuItems(menu: ContextMenuData): void {
const { navId } = menu;

// No items to insert
if (!menuItems[navId]) return;

// Already inserted items
// If this isn't here, another group of items is added every update
if (menu.plugged) return;

// We delay getting the items until now, as importing at the start of the file causes Discord to hang
// Using `await import(...)` is undesirable because the new items will only appear once the menu is interacted with
const { MenuGroup } = getByProps<Record<string, React.ComponentType>>([
async function injectMenu(): Promise<void> {
const RepluggedComponents = await waitForModule(filters.byProps("Menu", "MenuGroup", "MenuItem"));
yofukashino marked this conversation as resolved.
Show resolved Hide resolved
injector.instead(
RepluggedComponents as Record<string, AnyFunction>,
"Menu",
"MenuItem",
"MenuGroup",
])!;

if (!MenuGroup) return;

// The data as passed as Arguments from the calling function, so we just grab what we want from it
const data = menu.data[0];

const repluggedGroup = <MenuGroup />;
repluggedGroup.props.id = "replugged";
repluggedGroup.props.children = [];

// Add in the new menu items right above the DevMode Copy ID
// If the user doesn't have DevMode enabled, the new items will be at the bottom
menu.children.splice(-1, 0, repluggedGroup);

menuItems[navId].forEach((item) => {
try {
const res = makeItem(item.getItem(data, menu)) as ContextItem & { props: { id?: string } };

if (res?.props) {
// add in unique ids
res.props.id = `${res.props.id || "repluggedItem"}-${Math.random()
FedeIlLeone marked this conversation as resolved.
Show resolved Hide resolved
.toString(36)
.substring(2)}`;
([menu]: [ContextMenuData], res) => {
const ContextMenu = res as React.FunctionComponent<ContextMenuData>;
const { navId } = menu;
const { MenuGroup } = RepluggedComponents as Record<string, React.ComponentType>;

// No items to insert
// Or MenuGroup Component is not available
if (!menuItems[navId] || !MenuGroup) {
return <ContextMenu {...menu} />;
}

menu.children.at(item.sectionId)?.props?.children?.splice(item.indexInSection, 0, res);
} catch (err) {
logger.error("Error while running GetContextItem function", err, item.getItem);
}
});
// The data as passed as Arguments from the calling function, so we just grab what we want from it
const data = menu.data[0];

const repluggedGroup = <MenuGroup />;
repluggedGroup.props.id = "replugged";
repluggedGroup.props.children = [];

// Add in the new menu items right above the DevMode Copy ID
// If the user doesn't have DevMode enabled, the new items will be at the bottom
menu.children.splice(-1, 0, repluggedGroup);
menuItems[navId].forEach((item) => {
try {
const itemRet = makeItem(item.getItem(data, menu)) as ContextItem & {
props: { id?: string };
};

if (itemRet?.props) {
// add in unique ids
itemRet.props.id = `${itemRet.props.id || "repluggedItem"}-${Math.random()
.toString(36)
.substring(2)}`;
}

if (!menu.children.at(item.sectionId)?.props?.plugged) {
// menu props can stay same while having different children
// so we check children props if its plugged or not
menu.children
.at(item.sectionId)
?.props.children?.splice(item.indexInSection, 0, itemRet);
if (menu.children.at(item.sectionId)?.props)
menu.children.at(item.sectionId)!.props.plugged = true;
}
} catch (err) {
logger.error("Error while running GetContextItem function", err, item.getItem);
}
});

return <ContextMenu {...menu} />;
},
);
}
export function start(): void {
injector = new Injector();
void injectMenu();
yofukashino marked this conversation as resolved.
Show resolved Hide resolved
}

menu.plugged = true;
export function stop(): void {
injector.uninjectAll();
}
10 changes: 0 additions & 10 deletions src/renderer/coremods/contextMenu/plaintextPatches.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
import type { PlaintextPatch } from "src/types";

export default [
{
find: 'Error("Menu',
replacements: [
{
match: /var \w,\w=(.)\.navId/,
replace: (vars, menu) =>
`replugged.coremods.coremods.contextMenu._insertMenuItems(${menu});${vars}`,
},
],
},
{
find: "navId:",
replacements: [
Expand Down
Loading