Equicord/src/components/settings/tabs/patchHelper/ReplacementInput.tsx

86 lines
2.7 KiB
TypeScript
Raw Normal View History

/*
* Vencord, a Discord client mod
* Copyright (c) 2025 Vendicated and contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
2025-10-03 00:18:39 +02:00
import { FormSwitch } from "@components/FormSwitch";
2025-10-10 21:11:31 -04:00
import { Heading } from "@components/Heading";
2025-10-11 21:50:14 -04:00
import { Paragraph } from "@components/Paragraph";
import { Margins } from "@utils/margins";
2025-10-11 21:50:14 -04:00
import { Parser, TextInput, useEffect, useState } from "@webpack/common";
const RegexGuide = {
"\\i": "Special regex escape sequence that matches identifiers (varnames, classnames, etc.)",
"$$": "Insert a $",
"$&": "Insert the entire match",
"$`\u200b": "Insert the substring before the match",
"$'": "Insert the substring after the match",
"$n": "Insert the nth capturing group ($1, $2...)",
"$self": "Insert the plugin instance",
} as const;
export function ReplacementInput({ replacement, setReplacement, replacementError }) {
const [isFunc, setIsFunc] = useState(false);
const [error, setError] = useState<string>();
function onChange(v: string) {
setError(void 0);
if (isFunc) {
try {
const func = (0, eval)(v);
if (typeof func === "function")
setReplacement(() => func);
else
setError("Replacement must be a function");
} catch (e) {
setReplacement(v);
setError((e as Error).message);
}
} else {
setReplacement(v);
}
}
useEffect(() => {
if (isFunc)
onChange(replacement);
else
setError(void 0);
}, [isFunc]);
return (
<>
{/* FormTitle adds a class if className is not set, so we set it to an empty string to prevent that */}
2025-10-10 21:11:31 -04:00
<Heading className="">Replacement</Heading>
<TextInput
value={replacement?.toString()}
onChange={onChange}
error={error ?? replacementError}
/>
{!isFunc && (
<div>
2025-10-10 21:11:31 -04:00
<Heading className={Margins.top8}>Cheat Sheet</Heading>
{Object.entries(RegexGuide).map(([placeholder, desc]) => (
2025-10-11 21:50:14 -04:00
<Paragraph key={placeholder}>
{Parser.parse("`" + placeholder + "`")}: {desc}
2025-10-11 21:50:14 -04:00
</Paragraph>
))}
</div>
)}
2025-10-03 00:18:39 +02:00
<FormSwitch
className={Margins.top16}
value={isFunc}
onChange={setIsFunc}
title={"Treat Replacement as function"}
description='"Replacement" will be evaluated as a function if this is enabled'
hideBorder
2025-10-03 00:18:39 +02:00
/>
</>
);
}