generated from Fierdetta/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
402cbe3
commit 97de01a
Showing
2 changed files
with
64 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
}; |