From 5636f9d9797bf64170db56447f4bf38f8fadf169 Mon Sep 17 00:00:00 2001
From: Nuckyz <61953774+Nuckyz@users.noreply.github.com>
Date: Tue, 9 Apr 2024 17:09:23 -0300
Subject: [PATCH] Array support for find + ResurrectHome: View Server Home
Button on Server Guide (#2283)
---
scripts/generateReport.ts | 3 +
.../VencordSettings/PatchHelperTab.tsx | 52 ++++++++---
src/plugins/index.ts | 2 +
src/plugins/resurrectHome/index.tsx | 92 +++++++++++++++++--
src/utils/types.ts | 2 +-
src/webpack/patchWebpack.ts | 3 +-
6 files changed, 131 insertions(+), 23 deletions(-)
diff --git a/scripts/generateReport.ts b/scripts/generateReport.ts
index 41e38429..bb764063 100644
--- a/scripts/generateReport.ts
+++ b/scripts/generateReport.ts
@@ -299,6 +299,9 @@ function runTime(token: string) {
delete patch.predicate;
delete patch.group;
+ if (!Array.isArray(patch.find))
+ patch.find = [patch.find];
+
if (!Array.isArray(patch.replacement))
patch.replacement = [patch.replacement];
diff --git a/src/components/VencordSettings/PatchHelperTab.tsx b/src/components/VencordSettings/PatchHelperTab.tsx
index 064c872a..c08c366c 100644
--- a/src/components/VencordSettings/PatchHelperTab.tsx
+++ b/src/components/VencordSettings/PatchHelperTab.tsx
@@ -33,8 +33,8 @@ if (IS_DEV) {
var differ = require("diff") as typeof import("diff");
}
-const findCandidates = debounce(function ({ find, setModule, setError }) {
- const candidates = search(find);
+const findCandidates = debounce(function ({ finds, setModule, setError }) {
+ const candidates = search(...finds);
const keys = Object.keys(candidates);
const len = keys.length;
if (len === 0)
@@ -180,7 +180,8 @@ function ReplacementInput({ replacement, setReplacement, replacementError }) {
return (
<>
- replacement
+ {/* FormTitle adds a class if className is not set, so we set it to an empty string to prevent that */}
+ replacement
{!isFunc && (
-
Cheat Sheet
+
Cheat Sheet
{Object.entries({
"\\i": "Special regex escape sequence that matches identifiers (varnames, classnames, etc.)",
"$$": "Insert a $",
@@ -220,11 +221,12 @@ function ReplacementInput({ replacement, setReplacement, replacementError }) {
interface FullPatchInputProps {
setFind(v: string): void;
+ setFinds(v: string[]): void;
setMatch(v: string): void;
setReplacement(v: string | ReplaceFn): void;
}
-function FullPatchInput({ setFind, setMatch, setReplacement }: FullPatchInputProps) {
+function FullPatchInput({ setFind, setFinds, setMatch, setReplacement }: FullPatchInputProps) {
const [fullPatch, setFullPatch] = React.useState
("");
const [fullPatchError, setFullPatchError] = React.useState("");
@@ -256,7 +258,8 @@ function FullPatchInput({ setFind, setMatch, setReplacement }: FullPatchInputPro
if (!parsed.replacement.match) throw new Error("No 'replacement.match' field");
if (!parsed.replacement.replace) throw new Error("No 'replacement.replace' field");
- setFind(parsed.find);
+ setFind(JSON.stringify(parsed.find));
+ setFinds(parsed.find instanceof Array ? parsed.find : [parsed.find]);
setMatch(parsed.replacement.match instanceof RegExp ? parsed.replacement.match.source : parsed.replacement.match);
setReplacement(parsed.replacement.replace);
setFullPatchError("");
@@ -266,7 +269,7 @@ function FullPatchInput({ setFind, setMatch, setReplacement }: FullPatchInputPro
}
return <>
- Paste your full JSON patch here to fill out the fields
+ Paste your full JSON patch here to fill out the fields
{fullPatchError !== "" && {fullPatchError}}
>;
@@ -274,6 +277,7 @@ function FullPatchInput({ setFind, setMatch, setReplacement }: FullPatchInputPro
function PatchHelper() {
const [find, setFind] = React.useState("");
+ const [finds, setFinds] = React.useState([]);
const [match, setMatch] = React.useState("");
const [replacement, setReplacement] = React.useState("");
@@ -285,20 +289,39 @@ function PatchHelper() {
const code = React.useMemo(() => {
return `
{
- find: ${JSON.stringify(find)},
+ find: ${finds.length > 1 ? `[${finds.map(f => JSON.stringify(f)).join(", ")}]` : finds.length > 0 ? JSON.stringify(finds[0]) : "[]"},
replacement: {
match: /${match.replace(/(?full patch
- find
+ find
- match
+ match
+
>>();
+
+function ViewServerHomeButton() {
+ return (
+
+ {tooltipProps => (
+
+ )}
+
+ );
+}
+
+function useForceServerHome() {
+ const { forceServerHome } = settings.use(["forceServerHome"]);
+ const [shouldViewServerHome, setShouldViewServerHome] = useState(currentShouldViewServerHome);
+
+ useEffect(() => {
+ shouldViewServerHomeStates.add(setShouldViewServerHome);
+
+ return () => {
+ shouldViewServerHomeStates.delete(setShouldViewServerHome);
+ };
+ }, []);
+
+ return shouldViewServerHome || forceServerHome;
+}
+
+function useDisableViewServerHome() {
+ useEffect(() => () => {
+ currentShouldViewServerHome = false;
+ for (const setState of shouldViewServerHomeStates) {
+ setState(false);
+ }
+ }, []);
+}
const settings = definePluginSettings({
forceServerHome: {
@@ -30,12 +89,6 @@ const settings = definePluginSettings({
}
});
-function useForceServerHome() {
- const { forceServerHome } = settings.use(["forceServerHome"]);
-
- return forceServerHome;
-}
-
export default definePlugin({
name: "ResurrectHome",
description: "Re-enables the Server Home tab when there isn't a Server Guide. Also has an option to force the Server Home over the Server Guide, which is accessible through right-clicking the Server Guide.",
@@ -92,14 +145,37 @@ export default definePlugin({
match: /getMutableGuildChannelsForGuild\(\i\);return\(0,\i\.useStateFromStores\).+?\]\)(?=}function)/,
replace: m => `${m}&&!$self.useForceServerHome()`
}
+ },
+ // Add View Server Home Button to Server Guide
+ {
+ find: "487e85_1",
+ replacement: {
+ match: /(?<=text:(\i)\?\i\.\i\.Messages\.SERVER_GUIDE:\i\.\i\.Messages\.GUILD_HOME,)/,
+ replace: "badge:$self.ViewServerHomeButton({serverGuide:$1}),"
+ }
+ },
+ // Disable view Server Home override when the Server Home is unmouted
+ {
+ find: "69386d_5",
+ replacement: {
+ match: /location:"69386d_5".+?;/,
+ replace: "$&$self.useDisableViewServerHome();"
+ }
}
],
+ ViewServerHomeButton: ErrorBoundary.wrap(({ serverGuide }: { serverGuide?: boolean; }) => {
+ if (serverGuide !== true) return null;
+
+ return ;
+ }),
+
useForceServerHome,
+ useDisableViewServerHome,
contextMenus: {
"guild-context"(children, props) {
- const forceServerHome = useForceServerHome();
+ const { forceServerHome } = settings.use(["forceServerHome"]);
if (!props?.guild) return;
diff --git a/src/utils/types.ts b/src/utils/types.ts
index bec7cb0b..fc34b96d 100644
--- a/src/utils/types.ts
+++ b/src/utils/types.ts
@@ -36,7 +36,7 @@ export interface PatchReplacement {
export interface Patch {
plugin: string;
- find: string;
+ find: string | string[];
replacement: PatchReplacement | PatchReplacement[];
/** Whether this patch should apply to multiple modules */
all?: boolean;
diff --git a/src/webpack/patchWebpack.ts b/src/webpack/patchWebpack.ts
index db47c875..2ce4939b 100644
--- a/src/webpack/patchWebpack.ts
+++ b/src/webpack/patchWebpack.ts
@@ -186,7 +186,8 @@ function patchFactories(factories: Record code.replace(match, replace));
if (patch.predicate && !patch.predicate()) continue;
- if (code.includes(patch.find)) {
+ // we change all patch.find to array in plugins/index
+ if ((patch.find as string[]).every(f => code.includes(f))) {
patchedBy.add(patch.plugin);
const previousMod = mod;