From 5646fe402ac0da45e9a6a9f25d2594cb721527cf Mon Sep 17 00:00:00 2001 From: MrDiamondDog <84212701+MrDiamondDog@users.noreply.github.com> Date: Thu, 21 Mar 2024 17:59:25 -0600 Subject: [PATCH] feat(PatchHelper): Paste Full Patch (#1982) --- src/components/CheckedTextInput.tsx | 6 +- .../VencordSettings/PatchHelperTab.tsx | 65 ++++++++++++++++++- 2 files changed, 68 insertions(+), 3 deletions(-) diff --git a/src/components/CheckedTextInput.tsx b/src/components/CheckedTextInput.tsx index cf4aa119b..de2d7ae04 100644 --- a/src/components/CheckedTextInput.tsx +++ b/src/components/CheckedTextInput.tsx @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import { React, TextInput } from "@webpack/common"; +import { React, TextInput, useEffect } from "@webpack/common"; // TODO: Refactor settings to use this as well interface TextInputProps { @@ -55,6 +55,10 @@ export function CheckedTextInput({ value: initialValue, onChange, validate }: Te } } + useEffect(() => { + handleChange(initialValue); + }, [initialValue]); + return ( <> (""); + const [fullPatchError, setFullPatchError] = React.useState(""); + + function update() { + if (fullPatch === "") { + setFullPatchError(""); + + setFind(""); + setMatch(""); + setReplacement(""); + return; + } + + try { + const parsed = (0, eval)(`(${fullPatch})`) as Patch; + + if (!parsed.find) throw new Error("No 'find' field"); + if (!parsed.replacement) throw new Error("No 'replacement' field"); + + if (parsed.replacement instanceof Array) { + if (parsed.replacement.length === 0) throw new Error("Invalid replacement"); + + parsed.replacement = { + match: parsed.replacement[0].match, + replace: parsed.replacement[0].replace + }; + } + + 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); + setMatch(parsed.replacement.match instanceof RegExp ? parsed.replacement.match.source : parsed.replacement.match); + setReplacement(parsed.replacement.replace); + setFullPatchError(""); + } catch (e) { + setFullPatchError((e as Error).message); + } + } + + return <> + Paste your full JSON patch here to fill out the fields +