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

Improve routes handling #1192

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Conversation

gkarat
Copy link
Contributor

@gkarat gkarat commented Aug 19, 2024

Description

Associated Jira ticket: No jira.

This slightly improves the routes handling in Patch. Also removing ErrorComponent prop passing from ZeroState since it's not simply supported.

How to test the PR

No functionality should change. The routes must work correctly.

Before the change

The prior routes setup is more error prone or has unnecessary layers.

After the change

Life should be... uhm... better.

Dependent work link

Checklist:

  • The commit message has the Jira ticket linked
  • PR has a short description
  • Screenshots before and after the change are added
  • Tests for the changes have been added
  • README.md is updated if necessary
  • Needs additional dependent work

@gkarat gkarat self-assigned this Aug 19, 2024
@gkarat gkarat requested a review from a team as a code owner August 19, 2024 11:12
src/Routes.js Outdated
<Routes>
<Route
element={
<PermissionRoute requiredPermissions={generalPermissions} />
Copy link
Member

Choose a reason for hiding this comment

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

I'm not to sure about doing the permission handling this way. I'd rather have a plain component that renders or not renders the children passed, rather than utilising react-router and it's "Outlet".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, completely agree. Refactored in the last commit.

@codecov-commenter
Copy link

codecov-commenter commented Aug 19, 2024

Codecov Report

Attention: Patch coverage is 0% with 11 lines in your changes missing coverage. Please review.

Project coverage is 63.97%. Comparing base (c1e56c0) to head (c7be848).

Files with missing lines Patch % Lines
src/AccessWrapper.js 0.00% 8 Missing ⚠️
src/Routes.js 0.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1192      +/-   ##
==========================================
- Coverage   64.01%   63.97%   -0.04%     
==========================================
  Files         124      125       +1     
  Lines        3235     3237       +2     
  Branches      831      833       +2     
==========================================
  Hits         2071     2071              
- Misses       1164     1166       +2     

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

@gkarat gkarat requested a review from a team August 22, 2024 10:11
src/Routes.js Outdated
@@ -109,41 +90,35 @@ const PatchRoutes = () => {
</Suspense>
) : (
<Routes>
<Route path="/advisories" element={<Advisories />} />
Copy link
Member

Choose a reason for hiding this comment

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

I don't know why it work here with absolute paths, but these should be relative. All other applications use relative paths for their routes[0].

However, I think we should try to make these relative as well and also make sure we consistently use the insights navigate stuff, so links resolve properly.

[0] https://github.com/RedHatInsights/compliance-frontend/blob/master/src/Routes.js#L23

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know why it work here with absolute paths

It simply matches the routes starting by the basename (which is / in our case because /insights/patch pathname prefix is cut by chrome/federated modules related reason). Navigating to /advisories will match with the route having a path /advisories. As well as it will match the route with just advisories path. In our case, it doesn't have any disadvantages but inconsistency with other apps, as you mentioned. Though, all apps are inconsistent, Inventory has the same "root slash preference": https://github.com/RedHatInsights/insights-inventory-frontend/blob/master/src/Routes.js.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would say these paths starting with a slash are actually more specific, they are fully matching the route. Though, if for example path="advisories" would be by accident placed under some other route, it will lead to the situation where any path that has some slices and ends with "advisories" will match. It's very in theory though, but just imagining.

Do you think it's worth consolidating?

Copy link
Member

Choose a reason for hiding this comment

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

I think this here works maybe only because of some trickery we do in the routing in chrome, but I don't think it is really correct and might cause issues.

We should make these relative and remove the / from the paths (here and in all apps where we have this). These routes are nested within the /insights/patch path and starting the path for them with a / is not correct.

We should also consistently use the InsightsLink and useInsightsNavigate, because these will turn relative paths like "advisories" into "/insights/patch/advisories".

These changes seem unnecessary and everything kinda works at the moment, but this is only because of trickery and luck. What these changes do is make our routing in apps comply with React router conventions in order to reduce the burden on routing.

I also saw some ".." as a to attribute of Link components, these are dangerous, because they can lead to different places under a different context, for example with federated modules.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think I get your point, this makes sense to use relative paths in accordance to the react-router documentation. Regarding the cut paths: I don't think it's just of trickery and luck :D IIRC, it's made on purpose that our applications have baseName set to / even though we always have some /insights/abc preceding. Which is made to avoid real base name rewrite or make the navigation work properly.

I will move the things to relative paths now.

@gkarat gkarat requested a review from a team August 29, 2024 10:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants