-
Notifications
You must be signed in to change notification settings - Fork 542
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
Support JSX and Interactive UI in simulator #2409
base: main
Are you sure you want to change the base?
Conversation
New dependencies detected. Learn more about Socket for GitHub ↗︎
|
6bf80d6
to
26f6de0
Compare
👍 Dependency issues cleared. Learn more about Socket for GitHub ↗︎ This PR previously contained dependency changes with security issues that have been resolved, removed, or ignored. |
45829da
to
9e8ebe8
Compare
return false; | ||
} | ||
|
||
return node.type === 'Input' || node.type === 'Button'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fields can also take dropdowns and soon file inputs. Can we make this more generic somehow?
.map(componentToCode) | ||
.join(',\n')}\n])`; | ||
} | ||
function getElementProps(props: Record<string, unknown>) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may be able to share some code between this and snaps-jest
here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you point me to what part you were thinking we could reuse ? I've searched in snaps-jest
but nothing seems relevant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean this part:
snaps/packages/snaps-jest/src/matchers.ts
Lines 187 to 248 in 3866525
/** | |
* Serialise a JSX prop to a string. | |
* | |
* @param prop - The JSX prop. | |
* @returns The serialised JSX prop. | |
*/ | |
function serialiseProp(prop: unknown): string { | |
if (typeof prop === 'string') { | |
return `"${prop}"`; | |
} | |
return `{${JSON.stringify(prop)}}`; | |
} | |
/** | |
* Serialise JSX props to a string. | |
* | |
* @param props - The JSX props. | |
* @returns The serialised JSX props. | |
*/ | |
function serialiseProps(props: Record<string, unknown>): string { | |
return Object.entries(props) | |
.filter(([key]) => key !== 'children') | |
.sort(([a], [b]) => a.localeCompare(b)) | |
.map(([key, value]) => ` ${key}=${serialiseProp(value)}`) | |
.join(''); | |
} | |
/** | |
* Serialise a JSX node to a string. | |
* | |
* @param node - The JSX node. | |
* @param indentation - The indentation level. Defaults to `0`. This should not | |
* be set by the caller, as it is used for recursion. | |
* @returns The serialised JSX node. | |
*/ | |
export function serialiseJsx(node: SnapNode, indentation = 0): string { | |
if (Array.isArray(node)) { | |
return node.map((child) => serialiseJsx(child, indentation)).join(''); | |
} | |
const indent = ' '.repeat(indentation); | |
if (typeof node === 'string') { | |
return `${indent}${node}\n`; | |
} | |
if (!node) { | |
return ''; | |
} | |
const { type, props } = node; | |
const trailingNewline = indentation > 0 ? '\n' : ''; | |
if (hasProperty(props, 'children')) { | |
const children = serialiseJsx(props.children as SnapNode, indentation + 1); | |
return `${indent}<${type}${serialiseProps( | |
props, | |
)}>\n${children}${indent}</${type}>${trailingNewline}`; | |
} | |
return `${indent}<${type}${serialiseProps(props)} />${trailingNewline}`; | |
} |
It turns a rendered JSX node into code.
Description
This PR allows the simulator to consume the new JSX components from Snaps custom UI.
It also enable interactive interfaces to work in the simulator.
Breakdown:
Renderer:
OnRpcRequest
andOnTransaction
:SnapInterfaceController
UI Builder:
Field
component