Skip to content

PUI Plugin Panels - #7470

Merged
SchrodingersGat merged 105 commits into
inventree:masterfrom
SchrodingersGat:pui-plugins
Sep 16, 2024
Merged

SchrodingersGat merged 105 commits into
inventree:masterfrom
SchrodingersGat:pui-plugins

Conversation

@SchrodingersGat

@SchrodingersGat SchrodingersGat commented Jun 18, 2024 •

Copy link
Copy Markdown
Member

This PR serves as a "proof of concept" as to how client-side data could be served by plugins, in the new react / platform interface.

In this instance, custom "panels" are loaded via the API, and rendered on the page based on what is delivered to the server.

Currently this only serves raw text - much more work would be required to render:

  • Raw html data
  • Content rendered on server via template
  • Content served via /static/ and loaded dynamically
  • Compiled react components etc

Related Issues

TODO

  • Update documentation
  • include a way to embed javascript / typescript code into the documentation
  • Mark old plugin mixin as deprecated (in documentation at least)
  • Work out a way to support "themes" (observe dark mode, etc)
  • Test if passing context objects (such as user, navigate, api) works with "compiled" code (currently works with vite dev server)
  • Playwright unit tests

Future Work

  • Make a "demo" plugin (published on PIP) which includes staticfiles, compiled javascript, etc

Resources

Pinning some resources here for further reading:

@SchrodingersGat SchrodingersGat added enhancement This is an suggested enhancement or new feature user interface plugin Plugin ecosystem feature User Interface Related to the frontend / User Interface labels Jun 18, 2024
@SchrodingersGat SchrodingersGat added this to the 1.0.0 milestone Jun 18, 2024
@SchrodingersGat
SchrodingersGat requested a review from matmair as a code owner June 18, 2024 13:22
@netlify

netlify Bot commented Jun 18, 2024 •

Copy link
Copy Markdown

✅ Deploy Preview for inventree-web-pui-preview ready!

Name Link
🔨 Latest commit 43fa64f
🔍 Latest deploy log https://app.netlify.com/sites/inventree-web-pui-preview/deploys/66e5356e8daa290008b6e9ae
😎 Deploy Preview https://deploy-preview-7470--inventree-web-pui-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 100 (🟢 up 1 from production)
Accessibility: 86 (no change from production)
Best Practices: 92 (no change from production)
SEO: 70 (no change from production)
PWA: -
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify site configuration.

@SchrodingersGat
SchrodingersGat requested a review from wolflu05 June 18, 2024 13:23
@codecov

codecov Bot commented Jun 18, 2024 •

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 75.67568% with 72 lines in your changes missing coverage. Please review.

Project coverage is 84.39%. Comparing base (7fbc1fb) to head (43fa64f).
Report is 298 commits behind head on master.

Files with missing lines Patch % Lines
...rc/frontend/src/components/plugins/PluginPanel.tsx 6.25% 30 Missing ⚠️
...c/frontend/src/components/plugins/PluginSource.tsx 0.00% 15 Missing ⚠️
src/frontend/src/hooks/UsePluginPanels.tsx 72.97% 10 Missing ⚠️
src/backend/InvenTree/plugin/staticfiles.py 50.00% 6 Missing ⚠️
src/frontend/src/tables/plugin/PluginListTable.tsx 37.50% 5 Missing ⚠️
...lugin/samples/integration/user_interface_sample.py 90.32% 3 Missing ⚠️
...Tree/plugin/base/integration/UserInterfaceMixin.py 94.44% 1 Missing ⚠️
src/backend/InvenTree/plugin/installer.py 80.00% 1 Missing ⚠️
src/frontend/src/components/nav/PanelGroup.tsx 90.90% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #7470      +/-   ##
==========================================
+ Coverage   84.36%   84.39%   +0.03%     
==========================================
  Files        1143     1149       +6     
  Lines       51218    51463     +245     
  Branches     1791     1808      +17     
==========================================
+ Hits        43209    43434     +225     
- Misses       7804     7823      +19     
- Partials      205      206       +1     
Flag Coverage Δ
backend 85.81% <94.11%> (+0.03%) ⬆️
pui 68.32% <44.03%> (+0.15%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@wolflu05

Copy link
Copy Markdown
Member

Thanks that you already started a draft for this long standing issue. I just want to add my thoughts about this now:

I See two ways how developers could build PUI integrations:

1. They provide everything they need on thrown. E.g. they want to use react, they need to setup their own react spa, bundle it and ship the static files. Inventree PUI just loads embeds a specified entry html document.

✅ Advantages ❌ Disadavantages
Plugin devs are not limited to any particular framework, they could use preact, react, solidjs, svelte, ... and even just plain HTML or vanilla js If multiple panel plugins using the same framework are installed and being serverd, multiple copies of the core frameworks code is needed to be loaded by the client
Developers have to make their own efforts to align the UI of their plugin panel with the ui design of PUI, and rebuild helper functions, UI elements on their own

To fix these disadvantages, I see two methods:

  1. use a concept called module federation (search for the terms microfrontend, this concept is often used there). That way only one instance of react, mantine, ... needs to be used. But that would have the downside of version conflicts. E.g. if we do some breaking change in a component (e.g. add or remove a required prop), plugin panels using that federated component could just break
  2. ship the inventree ui and utility functions as npm package, panel developers could use. This would have the advantage, that developers have a predictable environment (e.g. they can pin this inventree ui dependency) and this works with all versions.

2. They export a render function which gets called by PUI with a target DOM element they should render their content in. (Also with this method devs could use their framework of choice.) I Already made some tests with this a year ago: https://github.com/wolflu05/spa-preact-test

This would also add us some more possibilities, like we could let plugins provide some other UI things than just custom plugins. E.g. full new pages, custom label/report editors in the admin center (they also need a backchannel to talk to PUI).


Personally, I think I like a combination of all from above. We bundle all inventree components and utilities into a custom npm package developers could use. Yes, this adds the overhead of duplicate code, but I think that's much more reliable than somehow hack module federation to work correctly. Maybe we could module federation for the react dependency which has no breaking changes in minor version changes, and/or provide a global copy of preact, ... . Custom panels (and pages) use the HTML providing method (1), and custom editors could export a function (2).

@SchrodingersGat

Copy link
Copy Markdown
Member Author

@wolflu05 some great ideas here. I think that whatever approach we choose:

  • Enforce a single approach
  • Provide some well-worked examples
  • Publish a comprehensive set of common components
  • Make it future-proof!

@SchrodingersGat

Copy link
Copy Markdown
Member Author

@wolflu05 if you can provide a small POC for how we might load an entrypoint into a panel, that would be amazing.

@wolflu05

Copy link
Copy Markdown
Member

The questions is what approach we should use. For my first suggestion, it would be as easy use using: dangerouslySetInnerHTML

And for the second, I actually made a POC last year linked in my initial post. I also played a bit there with using the main react version + mantine to use in panels (commented out), but that doesn't worked well that way, as react was complaining, that the rendering react version is not the same instance as the one creating elements, but that might be an issue from me to that time.

@SchrodingersGat

Copy link
Copy Markdown
Member Author

I would rather not use the first approach. I'll try to get your demo running when I have some time!

@matmair

matmair commented Jun 20, 2024

Copy link
Copy Markdown
Member

I really like @wolflu05 second idea; to keep the UI consistent providing a default set of UI components would be nice

@wolflu05

Copy link
Copy Markdown
Member

Just one thought, maybe we can build the panel system in a way so that we later could also add those pages to the mobile app via a web view as an option (when plugin devs expose a panel, they could specify the target: web,mobile,...).

@matmair

matmair commented Sep 3, 2024

Copy link
Copy Markdown
Member

This looks very promising although some parts seem a bit brittle - is unit and e2e testing part of the scope for this PR?

@SchrodingersGat

Copy link
Copy Markdown
Member Author

is unit and e2e testing part of the scope for this PR?

Some testing will definitely be added! What parts in particular do you think look brittle?

@SchrodingersGat
SchrodingersGat merged commit 12d2865 into inventree:master Sep 16, 2024
@SchrodingersGat
SchrodingersGat deleted the pui-plugins branch September 16, 2024 00:36
@jacobfelknor

Copy link
Copy Markdown
Contributor

@SchrodingersGat

I apologize if this has been addressed elsewhere... but having trouble finding it if it is. I have a collection of CUI plugins (including panel plugins) that I've written and continue to maintain for my InvenTree instance. I use many "InvenTree builtins", like inventreeTable(), in my plugins in order to take advantage of all the reusability it has to offer. The switch to PUI, while exciting, makes me nervous that I'll have to rewrite all my plugins all at once.

Is there a world where switching to PUI for the server and updating my plugins to also use PUI can be incremental for me? In other words, would it ever be possible for me to continue using my existing CUI plugins with PUI on the instance?

@SchrodingersGat

Copy link
Copy Markdown
Member Author

@jacobfelknor this is a good point. I am going to write some documentation around panel plugin migration, before we force the switch.

@wolflu05

Copy link
Copy Markdown
Member

I think first we need to publish the inventree npm package with the set of components, so that we can migrate e.g. the old form framework (I have used this in a panel) to the new react form framework. And same with other things.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement This is an suggested enhancement or new feature feature plugin Plugin ecosystem User Interface Related to the frontend / User Interface

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Automatic plugin static files collection broken after install

4 participants