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

AppPlugins: Expose react-router to apps #33775

Merged
merged 13 commits into from
May 19, 2021
Merged

AppPlugins: Expose react-router to apps #33775

merged 13 commits into from
May 19, 2021

Conversation

dprokop
Copy link
Member

@dprokop dprokop commented May 6, 2021

An alternative solution to #33759

This simply makes the core app route not to e exact, and allows app plugins to use react-router directly. This gives the app plugin authors max flexiility to define their routes in the app.

Tricky thing is the base path for an app. I've changed the AppRootProps to expose basename property for navigation and routes creation downstream.

Note: react-router v6 will make the nested routes much easier. I did a quicl sketch of rr5 -> 6 update, but there is too big a risk to make this step right before v8 as there is plenty of changes to do. And RR6 is still in beta and learning from my previous experiences with RR they tend to change the APIs a lot before releasing stable.

See https://github.com/grafana/marketplace-app/pull/4 for a usage example.

TODO

  • Figure out how to build the nav model - do we need to build it on the backend?

@dprokop dprokop added this to the 8.0.0 milestone May 6, 2021
@dprokop dprokop requested a review from a team May 6, 2021 11:08
@dprokop dprokop self-assigned this May 6, 2021
@dprokop dprokop requested review from mckn and removed request for a team May 6, 2021 11:08
@domasx2
Copy link
Contributor

domasx2 commented May 10, 2021

Lovely!
Great if we can make core routes not exact as well, have use cases in unified alerting.

The only nit I see is that if plugins start defining routes with the basename included, it will break if we move to rr6 and basename becomes not needed anymore. Not sure what we can do about it though

@dprokop
Copy link
Member Author

dprokop commented May 10, 2021

Great if we can make core routes not exact as well, have use cases in unified alerting.

@domasx2 not sure what and how would you accomplish anything with other routes not being exact. I mean - the App routes are rendered only under a/:pluginId segment which means you cannot use app route anywhere but under that segment. Wanna tell me something more about the use case you are describing?

@domasx2
Copy link
Contributor

domasx2 commented May 10, 2021

Great if we can make core routes not exact as well, have use cases in unified alerting.

@domasx2 not sure what and how would you accomplish anything with other routes not being exact. I mean - the App routes are rendered only under a/:pluginId segment which means you cannot use app route anywhere but under that segment. Wanna tell me something more about the use case you are describing?

I think with this merged we'd be able to use exact={false} with any route in routes.tsx, not only related to plugins?

One example use case:
Let's say there are pages /alerting/silences, /alerting/silences/new and /alerting/silences/:id/edit. All of these share some common UI elements and load some common data. So I'd like to render only /alerting/silences in routes.tsx, with exact={false}. It would render some root silences component wich implements the common stuff and has further routing to nested pages. So that if you're going from /alerting/silences to /alerting/silences/new, the root silences component is not re-mounted, and common data is not re-fetched

@dprokop
Copy link
Member Author

dprokop commented May 11, 2021

Ah, of course, this will be possible. I thought you were thinking of apps extending native routes.

@torkelo
Copy link
Member

torkelo commented May 11, 2021

The only nit I see is that if plugins start defining routes with the basename included, it will break if we move to rr6 and basename becomes not needed anymore. Not sure what we can do about it though

@domasx2 what do you mean? we do not use this for any routes currently. what is changing in v6?

@domasx2
Copy link
Contributor

domasx2 commented May 11, 2021

The only nit I see is that if plugins start defining routes with the basename included, it will break if we move to rr6 and basename becomes not needed anymore. Not sure what we can do about it though

@domasx2 what do you mean? we do not use this for any routes currently. what is changing in v6?

With this merged plugin devs will be able to use <Route /> with full path, eg /a/some-plugin/a-plugin-page. Then if later we upgrade to rr6 and want to put plugin under <Routes />, I assume that such existing routes would be interpreted as /a/some-plugin/a/some-plugin/a-plugin-page and stop working. So wondering if we can make a plan how to migrate them

@dprokop dprokop modified the milestones: 8.0.0-beta1, 8.0.0-beta2 May 13, 2021
@dprokop
Copy link
Member Author

dprokop commented May 18, 2021

@ryantxu - have you seen https://github.com/grafana/marketplace-app/pull/4 ? It does exactly what you are trying to do in the last commit :)

@dprokop dprokop requested review from oddlittlebird, achatterjee-grafana and osg-grafana and removed request for a team May 18, 2021 23:34
@grafanabot grafanabot added type/docs Flags the technical writing team for documentation support; auto adds to org-wide docs project area/backend labels May 18, 2021
@@ -12,3 +12,4 @@ export * from './slate-plugins';

// Exposes standard editors for registries of optionsUi config and panel options UI
export { getStandardFieldConfigs, getStandardOptionEditors } from './utils/standardEditors';
export { Route, useLocation, useParams } from 'react-router-dom';
Copy link
Member

Choose a reason for hiding this comment

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

Why export from here rather than just have people import directly? Is this to potentially help backwards compatibility? (if so, can you add a comment?)

Copy link
Member Author

Choose a reason for hiding this comment

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

no exports anymore, we are exposing RR to plugins and updating the plugin starters now

@dprokop dprokop requested a review from a team as a code owner May 18, 2021 23:54
@dprokop
Copy link
Member Author

dprokop commented May 19, 2021

This is a valid point. Would it be worth to try to upgrade to RR6 prior to adding this to the app API?

@mckn I've tried. It's plenty of work that's too risky to do during beta. And, the current relwase of RR6 is a prelease(beta0) so I would wait for a stable.

Regarding the support for RR6 -me and @torkelo talked about this and we think this approach should work OK with RR6 if we just change the root path passed to the app plugin from /a/:pluginId to /. Then all of the routes will become "nested" under the ap path.

@dprokop dprokop changed the title POC: Allow Route component usage in app plugins AppPlugins: Expose react-router to apps May 19, 2021
@domasx2
Copy link
Contributor

domasx2 commented May 19, 2021

Regarding the support for RR6 -me and @torkelo talked about this and we think this approach should work OK with RR6 if we just change the root path passed to the app plugin from /a/:pluginId to /. Then all of the routes will become "nested" under the ap path.

Ahh, perfect 👍

@@ -59,7 +59,7 @@ export class AppWrapper extends React.Component<AppWrapperProps, AppWrapperState

return (
<Route
exact
exact={route.exact === undefined ? true : route.exact}
Copy link
Contributor

Choose a reason for hiding this comment

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

One of the great newish js features, fyi 😸

Suggested change
exact={route.exact === undefined ? true : route.exact}
exact={route.exact ?? true}

@dprokop dprokop merged commit 4cbffae into main May 19, 2021
@dprokop dprokop deleted the routing-ng-app-plugins-2 branch May 19, 2021 17:10
ryantxu added a commit that referenced this pull request May 20, 2021
* Allow Route component usage in app plugins

* i tried

* fix catalog app

* fix catalog app

* remove catalog changes from this PR

* remove extra files

* feat(plugins): expose react-router to plugins rather than export via grafana-ui

* Bring back query and pathname to AppRootPage and add deprecation notice

Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
Co-authored-by: Jack Westbrook <jack.westbrook@gmail.com>
@mckn
Copy link
Contributor

mckn commented May 20, 2021

This is a valid point. Would it be worth to try to upgrade to RR6 prior to adding this to the app API?

@mckn I've tried. It's plenty of work that's too risky to do during beta. And, the current relwase of RR6 is a prelease(beta0) so I would wait for a stable.

Regarding the support for RR6 -me and @torkelo talked about this and we think this approach should work OK with RR6 if we just change the root path passed to the app plugin from /a/:pluginId to /. Then all of the routes will become "nested" under the ap path.

Ok ok, sounds good! Will this also prevent users from registering routes outside of the plugin path?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
add to changelog area/backend area/frontend area/plugins/app App Plugins area/routing type/docs Flags the technical writing team for documentation support; auto adds to org-wide docs project
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants