From dc81e9cfad71c3314189f40c1cd09874d9818741 Mon Sep 17 00:00:00 2001 From: pylixonly <82711525+pylixonly@users.noreply.github.com> Date: Sun, 13 Oct 2024 11:45:52 +0800 Subject: [PATCH] refactor(api/react): rename stuff --- src/core/plugins/badges/index.tsx | 4 ++-- src/index.ts | 4 ++-- src/lib/api/index.ts | 1 + src/lib/api/react/index.ts | 1 + src/lib/api/{jsx/index.ts => react/jsx.ts} | 17 ++++++++++------- 5 files changed, 16 insertions(+), 11 deletions(-) create mode 100644 src/lib/api/react/index.ts rename src/lib/api/{jsx/index.ts => react/jsx.ts} (61%) diff --git a/src/core/plugins/badges/index.tsx b/src/core/plugins/badges/index.tsx index af46ca0..9964c51 100644 --- a/src/core/plugins/badges/index.tsx +++ b/src/core/plugins/badges/index.tsx @@ -1,4 +1,4 @@ -import { addJSXCallback } from "@lib/api/jsx"; +import { onJsxCreate } from "@lib/api/react/jsx"; import { after } from "@lib/api/patcher"; import { findByName } from "@metro"; import { useEffect, useState } from "react"; @@ -24,7 +24,7 @@ export default defineCorePlugin({ const propHolder = {} as Record; const badgeCache = {} as Record; - addJSXCallback("RenderedBadge", (_, ret) => { + onJsxCreate("RenderedBadge", (_, ret) => { if (ret.props.id.match(/bunny-\d+-\d+/)) { Object.assign(ret.props, propHolder[ret.props.id]); } diff --git a/src/index.ts b/src/index.ts index 7c89e52..e496394 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,7 +6,7 @@ import { VdPluginManager } from "@core/vendetta/plugins"; import { patchCommands } from "@lib/api/commands"; import { patchLogHook } from "@lib/api/debug"; import { injectFluxInterceptor } from "@lib/api/flux"; -import { patchJSX } from "@lib/api/jsx"; +import { patchJsx } from "@lib/api/react/jsx"; import { removeFile, writeFile } from "@lib/api/native/fs"; import { isPyonLoader, isThemeSupported } from "@lib/api/native/loader"; import { FileManager } from "@lib/api/native/modules"; @@ -43,7 +43,7 @@ export default async () => { patchLogHook(), patchCommands(), patchChatBackground(), - patchJSX(), + patchJsx(), initVendettaObject(), initFetchI18nStrings(), initSettings(), diff --git a/src/lib/api/index.ts b/src/lib/api/index.ts index bb3b475..081fd14 100644 --- a/src/lib/api/index.ts +++ b/src/lib/api/index.ts @@ -6,3 +6,4 @@ export * as native from "./native"; export * as patcher from "./patcher"; export * as settings from "./settings"; export * as storage from "./storage"; +export * as react from "./react"; diff --git a/src/lib/api/react/index.ts b/src/lib/api/react/index.ts new file mode 100644 index 0000000..e7c5695 --- /dev/null +++ b/src/lib/api/react/index.ts @@ -0,0 +1 @@ +export * as jsx from "./jsx"; \ No newline at end of file diff --git a/src/lib/api/jsx/index.ts b/src/lib/api/react/jsx.ts similarity index 61% rename from src/lib/api/jsx/index.ts rename to src/lib/api/react/jsx.ts index 8d563ef..571eebc 100644 --- a/src/lib/api/jsx/index.ts +++ b/src/lib/api/react/jsx.ts @@ -1,17 +1,17 @@ import { after } from "@lib/api/patcher"; import { findByPropsLazy } from "@metro"; -type Callback = (Component: any, ret: JSX.Element) => void; +type Callback = (Component: any, ret: JSX.Element) => JSX.Element; const callbacks = new Map(); const jsxRuntime = findByPropsLazy("jsx", "jsxs"); -export function addJSXCallback(Component: string, callback: Callback) { +export function onJsxCreate(Component: string, callback: Callback) { if (!callbacks.has(Component)) callbacks.set(Component, []); callbacks.get(Component)!.push(callback); } -export function removeJSXCallback(Component: string, callback: Callback) { +export function deleteJsxCreate(Component: string, callback: Callback) { if (!callbacks.has(Component)) return; const cbs = callbacks.get(Component)!; cbs.splice(cbs.indexOf(callback), 1); @@ -21,12 +21,15 @@ export function removeJSXCallback(Component: string, callback: Callback) { /** * @internal */ -export function patchJSX() { - // Only a simple name check for now - const callback = ([Component, props]: any[], ret: any) => { +export function patchJsx() { + const callback = ([Component]: unknown[], ret: JSX.Element) => { + // The check could be more complex, but this is fine for now to avoid overhead if (typeof Component === "function" && callbacks.has(Component.name)) { const cbs = callbacks.get(Component.name)!; - for (const cb of cbs) ret = cb(Component, ret); + for (const cb of cbs) { + const _ret = cb(Component, ret); + if (_ret !== undefined) ret = _ret; + } return ret; } };