Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add custom widget to anvil #37875

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
(function (nativeConsole) {
const postMessage = (method, args) => {
window.parent.postMessage(
{
type: "CUSTOM_WIDGET_CONSOLE_EVENT",
data: {
type: method,
args: args.map((d) => ({
message: d,
})),
},
},
"*",
);
};
Comment on lines +3 to +15
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Enhance security of postMessage communication

Using "*" as the targetOrigin in postMessage is a security risk. Consider restricting it to specific origins.

-      "*",
+      process.env.PARENT_ORIGIN || window.origin,
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
window.parent.postMessage(
{
type: "CUSTOM_WIDGET_CONSOLE_EVENT",
data: {
type: method,
args: args.map((d) => ({
message: d,
})),
},
},
"*",
);
};
window.parent.postMessage(
{
type: "CUSTOM_WIDGET_CONSOLE_EVENT",
data: {
type: method,
args: args.map((d) => ({
message: d,
})),
},
},
process.env.PARENT_ORIGIN || window.origin,
);
};


const createProxy = (method) =>
new Proxy(nativeConsole[method], {
apply(target, _this, args) {
try {
postMessage(method, args);
} finally {
return Reflect.apply(target, _this, args);
}
},
});
Comment on lines +17 to +26
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix the return statement in finally block

The return statement in the finally block can override returns from the try block, potentially masking errors.

 const createProxy = (method) =>
   new Proxy(nativeConsole[method], {
     apply(target, _this, args) {
+      let result;
       try {
         postMessage(method, args);
+        result = Reflect.apply(target, _this, args);
       } finally {
-        return Reflect.apply(target, _this, args);
+        return result;
       }
     },
   });
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const createProxy = (method) =>
new Proxy(nativeConsole[method], {
apply(target, _this, args) {
try {
postMessage(method, args);
} finally {
return Reflect.apply(target, _this, args);
}
},
});
const createProxy = (method) =>
new Proxy(nativeConsole[method], {
apply(target, _this, args) {
let result;
try {
postMessage(method, args);
result = Reflect.apply(target, _this, args);
} finally {
return result;
}
},
});
🧰 Tools
🪛 Biome (1.9.4)

[error] 23-23: Unsafe usage of 'return'.

'return' in 'finally' overwrites the control flow statements inside 'try' and 'catch'.

(lint/correctness/noUnsafeFinally)


["log", "warn", "info"].forEach((method) => {
nativeConsole[method] = createProxy(method);
});

window.addEventListener("error", (event) => {
postMessage("error", [event.message]);
});
})(window.console);
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export const CUSTOM_WIDGET_LOAD_EVENTS = {
STARTED: "started",
DOM_CONTENTED_LOADED: "DOMContentLoaded",
COMPLETED: "completed",
};

export const getAppsmithScriptSchema = (model: Record<string, unknown>) => ({
appsmith: {
mode: "",
model: model,
ui: {
width: 1,
height: 2,
},
Comment on lines +11 to +14
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Avoid hard-coded UI dimensions.

The UI dimensions should be configurable or use meaningful defaults.

   ui: {
-    width: 1,
-    height: 2,
+    width: DEFAULT_WIDGET_WIDTH,
+    height: DEFAULT_WIDGET_HEIGHT,
   },

Committable suggestion skipped: line range outside the PR's diff.

theme: {
primaryColor: "",
backgroundColor: "",
borderRadius: "",
boxShadow: "",
},
Comment on lines +15 to +20
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add type definitions for theme properties.

Theme properties should have proper type definitions for better type safety.

+interface ThemeConfig {
+  primaryColor: string;
+  backgroundColor: string;
+  borderRadius: string;
+  boxShadow: string;
+}
+
   theme: {
     primaryColor: "",
     backgroundColor: "",
     borderRadius: "",
     boxShadow: "",
   },
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
theme: {
primaryColor: "",
backgroundColor: "",
borderRadius: "",
boxShadow: "",
},
interface ThemeConfig {
primaryColor: string;
backgroundColor: string;
borderRadius: string;
boxShadow: string;
}
theme: {
primaryColor: "",
backgroundColor: "",
borderRadius: "",
boxShadow: "",
},

onUiChange: Function,
onModelChange: Function,
onThemeChange: Function,
updateModel: Function,
triggerEvent: Function,
onReady: Function,
Comment on lines +21 to +26
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Replace Function constructor with proper function types.

Using the Function constructor is discouraged as it bypasses type checking. Define proper function types instead.

-    onUiChange: Function,
-    onModelChange: Function,
-    onThemeChange: Function,
-    updateModel: Function,
-    triggerEvent: Function,
-    onReady: Function,
+    onUiChange: (ui: { width: number; height: number }) => void,
+    onModelChange: (model: Record<string, unknown>) => void,
+    onThemeChange: (theme: ThemeConfig) => void,
+    updateModel: (model: Record<string, unknown>) => void,
+    triggerEvent: (eventName: string, eventData?: unknown) => void,
+    onReady: () => void,
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
onUiChange: Function,
onModelChange: Function,
onThemeChange: Function,
updateModel: Function,
triggerEvent: Function,
onReady: Function,
onUiChange: (ui: { width: number; height: number }) => void,
onModelChange: (model: Record<string, unknown>) => void,
onThemeChange: (theme: ThemeConfig) => void,
updateModel: (model: Record<string, unknown>) => void,
triggerEvent: (eventName: string, eventData?: unknown) => void,
onReady: () => void,

},
});
Loading