Skip to content

Commit

Permalink
Fix MessageLongPressActionSheet patching on 166+
Browse files Browse the repository at this point in the history
  • Loading branch information
FieryFlames committed Feb 21, 2023
1 parent 3586144 commit 820e2e6
Showing 1 changed file with 49 additions and 42 deletions.
91 changes: 49 additions & 42 deletions src/patches/MessageLongPressActionSheet.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { findByDisplayName, findByProps } from "@vendetta/metro";
import { after } from "@vendetta/patcher";
import { after, before } from "@vendetta/patcher";
import { storage } from "@vendetta/plugin";
import { getAssetIDByName } from "@vendetta/ui/assets";
import { Forms } from "@vendetta/ui/components";
Expand All @@ -9,53 +9,60 @@ const LazyActionSheet = findByProps("openLazy", "hideActionSheet");

// Components
const { FormRow } = Forms;
const MessageLongPressActionSheet = findByProps("EmojiRow");
const Icon = findByDisplayName("Icon");

const JSON_CODEBLOCK_PATTERN = /^```(?:json)\n([\s\S]*?)```$/gm

const Download = getAssetIDByName("ic_download_24px");

export default function patchMessageLongPressActionSheet() {
return after("default", MessageLongPressActionSheet, ([{ message }], res) => {
// Get rules from message
const rules = message.content.match(JSON_CODEBLOCK_PATTERN)?.map((rule: string) => {
// Remove codeblock stuff
return rule.slice(7, rule.length - 3);
}).map((rule: string) => {
// Turn into a object
try {
return JSON.parse(rule);
} catch {
return undefined;
};
// Filter out undefined
}).filter((rule) => rule).filter((rule) =>
// Check it's a valid rule
typeof rule.name == "string" && rule.name &&
typeof rule.match == "string" && typeof rule.replace == "string" &&
typeof rule.flags == "string" && typeof rule.regex == "boolean"
);

// Don't add anything if we have no importable rules
if (!rules || rules.length == 0) return;

let buttons = res?.props?.children?.props?.children?.props?.children[1];

for (const rule of rules) {
const importRuleCallback = () => {
storage.rules.push(rule);
showToast(`Imported rule ${rule.name}`, Download);
LazyActionSheet.hideActionSheet();
};

const importRuleButton = (<FormRow
leading={<Icon source={Download} />}
label={`Import ${rule.name}`}
onPress={importRuleCallback}
/>);

buttons.unshift(importRuleButton);
}
return before("openLazy", LazyActionSheet, ([sheet, name]) => {
if (name !== "MessageLongPressActionSheet") return

sheet.then((instance) => {
const unpatchInstance = after("default", instance, ([{ message }], res) => {
React.useEffect(() => () => { unpatchInstance() }, [])

// Get rules from message
const rules = message.content.match(JSON_CODEBLOCK_PATTERN)?.map((rule: string) => {
// Remove codeblock stuff
return rule.slice(7, rule.length - 3);
}).map((rule: string) => {
// Turn into a object
try {
return JSON.parse(rule);
} catch {
return undefined;
};
// Filter out undefined
}).filter((rule) => rule).filter((rule) =>
// Check it's a valid rule
typeof rule.name == "string" && rule.name &&
typeof rule.match == "string" && typeof rule.replace == "string" &&
typeof rule.flags == "string" && typeof rule.regex == "boolean"
);

// Don't add anything if we have no importable rules
if (!rules || rules.length == 0) return;

let buttons = res?.props?.children?.props?.children?.props?.children[1];

for (const rule of rules) {
const importRuleCallback = () => {
storage.rules.push(rule);
showToast(`Imported rule ${rule.name}`, Download);
LazyActionSheet.hideActionSheet();
};

const importRuleButton = (<FormRow
leading={<Icon source={Download} />}
label={`Import ${rule.name}`}
onPress={importRuleCallback}
/>);

buttons.unshift(importRuleButton);
}
})
});
});
};

0 comments on commit 820e2e6

Please sign in to comment.