fix: Cannot read property 'call' of null

This commit is contained in:
kmmiio99o 2026-07-24 14:33:44 +02:00
parent 154fd6cf1f
commit 4ac8be74eb
No known key found for this signature in database
GPG key ID: DCC0431E10FADBF1

View file

@ -7,6 +7,7 @@ interface Intercept {
const intercepts = new Map<React.ComponentType<any>, Intercept>(); const intercepts = new Map<React.ComponentType<any>, Intercept>();
let origCreateElement: typeof React.createElement | null = null; let origCreateElement: typeof React.createElement | null = null;
let isPatched = false;
export function registerIntercept( export function registerIntercept(
original: React.ComponentType<any>, original: React.ComponentType<any>,
@ -17,28 +18,38 @@ export function registerIntercept(
} }
export function patchCreateElement(cleanups: (() => void)[]) { export function patchCreateElement(cleanups: (() => void)[]) {
if (origCreateElement) return; if (isPatched) return;
// Guard against React.createElement being null in some environments
origCreateElement = React.createElement; origCreateElement = React.createElement;
if (!origCreateElement) {
console.warn("[ServerDrawer] React.createElement is null, skipping patch");
return;
}
const patched = function (type: any, props: any, ...rest: any[]) { const patched = function (type: any, props: any, ...rest: any[]) {
if (!origCreateElement) {
// Fallback to original createElement if somehow null (should not happen)
return React.createElement(type, props, ...rest);
}
const entry = intercepts.get(type); const entry = intercepts.get(type);
if (entry) { if (entry) {
const newProps = entry.extraProps const newProps = entry.extraProps
? { ...props, ...entry.extraProps } ? { ...props, ...entry.extraProps }
: props; : props;
return origCreateElement!.call(React, entry.replacement, newProps, ...rest); return origCreateElement.call(React, entry.replacement, newProps, ...rest);
} }
return origCreateElement!.call(React, type, props, ...rest); return origCreateElement.call(React, type, props, ...rest);
}; };
Object.assign(patched, origCreateElement); Object.assign(patched, origCreateElement);
React.createElement = patched as typeof React.createElement; React.createElement = patched as typeof React.createElement;
isPatched = true;
cleanups.push(() => { cleanups.push(() => {
if (origCreateElement) { if (isPatched && origCreateElement) {
React.createElement = origCreateElement; React.createElement = origCreateElement;
origCreateElement = null; isPatched = false;
} }
intercepts.clear(); intercepts.clear();
}); });