-
Notifications
You must be signed in to change notification settings - Fork 34
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds support for async react renders for admin extensions #1678
Conversation
This comment has been minimized.
This comment has been minimized.
| (( | ||
api: ApiForRenderExtension<ExtensionTarget>, | ||
) => Promise<ReactElement<any>>) | ||
| ((api: ApiForRenderExtension<ExtensionTarget>) => ReactElement<any>), |
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.
Instead of a union of function types, can we change the return type to be a union (ReactElement<any> | Promise<ReactElement<any>>
?
{render(api as ApiForRenderExtension<ExtensionTarget>)} | ||
</ExtensionApiContext.Provider>, | ||
root, | ||
() => { |
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.
In Checkout's version of this registration function (https://github.com/Shopify/ui-extensions/blob/unstable/packages/ui-extensions-react/src/surfaces/checkout/render.tsx#L44-L62), we kept the new Promise
wrapper, so that we could resolve the promise only in the post-rendering callback React gives us. I think we should match that implementation here.
2f512c4
to
7919228
Compare
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.
馃帺'ed with your extension code and some validation code as well, code matches checkout's implementation less the error boundary, not sure if that's needed now or if we should follow up.
return extension<'Playground'>(target as any, async (root, api) => { | ||
const element = await render(api as ApiForRenderExtension<ExtensionTarget>); | ||
|
||
return new Promise<void>((resolve, reject) => { |
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.
Checkout await
s this, which I personally like a little better. But it ends up working out to the same thing for the caller of this function, so it's up to you if you'd prefer to match or not.
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.
Good catch! I didn't intend to leave it with the return. I was playing around with it and didn't catch it when I was putting it back
0fecfb2
to
800152f
Compare
.changeset/afraid-ladybugs-begin.md
Outdated
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.
Nit: is this really a minor
change or a patch
? I would argue that its more of a small change (patch) to an existing feature of rendering extensions.
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.
You're right! The tool made me think I had to select major or minor lol
800152f
to
cf51fb0
Compare
Background
As per this comment in https://github.com/Shopify/ui-api-design/issues/227 we want to lift where an extension can fetch data to the extension callback
Solution
This solution mirrors the implementation from checkout .
馃帺
You can try out my spin instance which has both async and sync rendering react and js extensions, lemme know if my dev server isn't running.
https://admin.web.arbok.thomas-marcucci.us.spin.dev/store/development-store-1/products/2
Otherwise if you want to test it on your own:
spin up checkout-ui-extension-dev
git clone git@github.com:Shopify/ui-extensions.git
cd
intoui-extensions/packages/ui-extensions
andyarn link
cd
intoui-extensions/packages/ui-extensions-react
andyarn link
cd
intoui-extensions
root andyarn build
create an app and generate an admin block extension
cd
into your extension's folder andyarn link @shopify/ui-extensions
andyarn link @shopify/ui-extensions-react
change the render function in your extension to be be async
Now
yarn dev
and open up the extension preview. It should work as normal.If you throw an exception it should show in the extension's place.If you throw an exception in the render, it should be swallowed and the extension will render its default version (extension name as title and empty content).Checklist