Skip to content

Commit

Permalink
Add importing rules from a message
Browse files Browse the repository at this point in the history
  • Loading branch information
FieryFlames committed Feb 5, 2023
1 parent 402cbe3 commit 97de01a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { storage } from "@vendetta/plugin";
import patchMessageLongPressActionSheet from "./patches/MessageLongPressActionSheet";
import patchSendMessage from "./patches/sendMessage";
import Settings from "./ui/pages/Settings";

Expand All @@ -8,7 +9,8 @@ export default {
onLoad: () => {
storage.rules ??= [];
patches = [
patchSendMessage()
patchSendMessage(),
patchMessageLongPressActionSheet()
];
},
onUnload: () => {
Expand Down
61 changes: 61 additions & 0 deletions src/patches/MessageLongPressActionSheet.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { findByDisplayName, findByProps } from "@vendetta/metro";
import { after } from "@vendetta/patcher";
import { storage } from "@vendetta/plugin";
import { getAssetIDByName } from "@vendetta/ui/assets";
import { Forms } from "@vendetta/ui/components";
import { showToast } from "@vendetta/ui/toasts";

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);
}
});
};

0 comments on commit 97de01a

Please sign in to comment.