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

NETOBSERV-1097 react-router-dom upgrade with dynamic loader #350

Merged
merged 4 commits into from
Jul 17, 2023

Conversation

jpinsonneau
Copy link
Contributor

@jpinsonneau jpinsonneau commented Jun 29, 2023

This PR introduce a dynamic loader component that allows plugin to switch between useHistory and useNavigate hooks provided by react-router-dom lib embedded in OCP console.

It's a better approach than #348 since it works on any OCP version.
The downside of this approach is that we still rely on v5 API until OCP console move to v6 (no usage of react-router-dom-v5-compat lib)

Comment on lines +26 to +35
const genericReactRouterDom = reactRouterDom as any;
if (genericReactRouterDom['useNavigate']) {
console.log('loading nav function from react-router useNavigate');
setNavFunction(genericReactRouterDom['useNavigate']());
} else if (genericReactRouterDom['useHistory']) {
console.log('loading nav function from react-router useHistory');
setNavFunction(genericReactRouterDom['useHistory']().push);
} else {
console.error("can't load nav function from react-router");
}
Copy link
Contributor Author

@jpinsonneau jpinsonneau Jun 29, 2023

Choose a reason for hiding this comment

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

useNavigate is the preferred hook and will be taken from the import if available.
If not, reactRouterDom.useNavigate equals 0 and it fallback on useHistory.push

Copy link
Member

Choose a reason for hiding this comment

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

Just tested on my 4.13 cluster and it uses useHistory. I guess I should spin up a 4.14 to test useNavigate?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

useNavigate will be used only when console plugin team will finish the migration introducing v6 API.

  • prior to 4.14 we rely on v5 api
  • ocp 4.14 will introduce react-router-dom-v5-compat with v6 capabilities using this compat lib. Since we don't import it it will still rely on v5 api
  • a later version will introduce v6 api, that's when we'll switch to useNavigate

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comment on lines +56 to +60
<DynamicLoader>
<AlertFetcher>
<NetflowTraffic forcedFilters={null} />
</AlertFetcher>
</DynamicLoader>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

DynamicLoader FC is loaded on top of everything to ensure the hooks are up to date.

@codecov
Copy link

codecov bot commented Jun 29, 2023

Codecov Report

Merging #350 (d0faae7) into main (575ea20) will decrease coverage by 0.69%.
The diff coverage is 63.76%.

@@            Coverage Diff             @@
##             main     #350      +/-   ##
==========================================
- Coverage   58.67%   57.99%   -0.69%     
==========================================
  Files         150      152       +2     
  Lines        6619     6706      +87     
  Branches      800      804       +4     
==========================================
+ Hits         3884     3889       +5     
- Misses       2518     2595      +77     
- Partials      217      222       +5     
Flag Coverage Δ
uitests 58.91% <63.76%> (-0.09%) ⬇️
unittests 55.39% <ø> (-2.34%) ⬇️

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

Impacted Files Coverage Δ
web/src/api/ipfix.ts 100.00% <ø> (ø)
web/src/components/messages/loki-error.tsx 23.18% <0.00%> (-1.10%) ⬇️
...eb/src/components/netflow-overview/panel-kebab.tsx 66.66% <ø> (ø)
web/src/components/alerts/banner.tsx 47.05% <33.33%> (+2.61%) ⬆️
web/src/components/filters/filters-toolbar.tsx 59.18% <50.00%> (-0.42%) ⬇️
web/src/utils/url.ts 71.73% <57.14%> (-4.86%) ⬇️
...b/src/components/dynamic-loader/dynamic-loader.tsx 60.00% <60.00%> (ø)
web/src/components/netflow-traffic.tsx 53.43% <72.72%> (-0.10%) ⬇️
web/src/utils/router.ts 57.31% <73.91%> (-2.44%) ⬇️
web/src/components/netflow-traffic-parent.tsx 46.15% <100.00%> (+2.15%) ⬆️

... and 1 file with indirect coverage changes

@jpinsonneau jpinsonneau requested a review from jotak June 29, 2023 14:03
@jpinsonneau jpinsonneau changed the title NETOBSERV-1097 deps upgrade with dynamic loader NETOBSERV-1097 react-router-dom upgrade with dynamic loader Jun 29, 2023
};

export const setURLParam = (param: URLParam, value: string) => {
export const setURLParam = (param: URLParam, value: string, replace?: boolean) => {
Copy link
Member

Choose a reason for hiding this comment

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

previously with useHistory, we did never replace the url, right? Is it necessary to do this distinction when using useNavigate, can't we just have always replace=false?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We do in fact

history.replace(`${url.pathname}?${params.toString()}${url.hash}`);

The replace is usefull to avoid having a huge history at startup, when React hooks set url parameters one by one

// Rewrite URL params on state change
React.useEffect(() => {
setURLFilters(forcedFilters || filters);
}, [filters, forcedFilters]);
React.useEffect(() => {
setURLRange(range);
}, [range]);
React.useEffect(() => {
setURLLimit(limit);
}, [limit]);
React.useEffect(() => {
setURLMatch(match);
}, [match]);

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I see

@jotak
Copy link
Member

jotak commented Jul 13, 2023

Thanks @jpinsonneau !
Quick-tested on 4.13 and 4.14
/lgtm

@jotak
Copy link
Member

jotak commented Jul 13, 2023

@memodi I guess this one can be merged? There will be regression tests to do on all supported OCPs so I guess it can be part of the usual pre-release tests

@jotak
Copy link
Member

jotak commented Jul 17, 2023

/lgtm

@openshift-ci openshift-ci bot added the lgtm label Jul 17, 2023
@jotak
Copy link
Member

jotak commented Jul 17, 2023

/approve

@openshift-ci
Copy link

openshift-ci bot commented Jul 17, 2023

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: jotak

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-merge-robot openshift-merge-robot merged commit e6d08c3 into netobserv:main Jul 17, 2023
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants