Files
homarr/packages/custom-widgets/src/jsx/runtime-component-policy.ts
T
Thomas 5e23659d99 feat(assistant): optimize MCP and custom widget authoring (#6743)
* feat(assistant): optimize MCP and custom widget authoring

* fix(assistant): isolate server-safe JSX analyzer

* refactor(assistant): generate MCP tool staging

* fix(assistant): preserve widget placement follow-up

* fix(ci): allow fast gate to finish

* fix(custom-widgets): address review findings

* fix(custom-widgets): split preview evidence module

* style: format assistant custom widget files

* fix(custom-widgets): sync formatted skill schema
2026-08-30 23:43:29 +02:00

34 lines
1.3 KiB
TypeScript

export function isSafeCustomJsxUrl(value: unknown) {
if (typeof value !== "string") return false;
if (value.startsWith("#")) return true;
if (value.startsWith("/") && !value.startsWith("//")) return true;
try {
const url = new URL(value);
return (url.protocol === "http:" || url.protocol === "https:") && !url.username && !url.password;
} catch {
return false;
}
}
export function getScopedCustomJsxControlName(scopeId: string, name: string) {
const safeScope = scopeId.replaceAll(/[^A-Za-z0-9_-]/gu, "_");
const safeName = encodeURIComponent(name).replaceAll("%", "_");
return `custom-widget-${safeScope}-${safeName}`;
}
export function getInvalidCustomJsxPropValueReason(componentName: string, propName: string, value: unknown) {
if (propName === "resetKey" && value !== null) {
const scalar =
typeof value === "string" || typeof value === "boolean" || (typeof value === "number" && Number.isFinite(value));
if (!scalar) return "resetKey supports only a finite number, string, boolean, or null";
}
if (
["DateTimePicker", "InlineDateTimePicker"].includes(componentName) &&
propName === "type" &&
![undefined, "default", "range"].includes(value as undefined | string)
) {
return `${componentName}.type supports only 'default' or 'range'`;
}
return null;
}