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

[Infra UI] Store asset details page state in the URL state #164601

Merged

Conversation

mykolaharmash
Copy link
Contributor

@mykolaharmash mykolaharmash commented Aug 23, 2023

Closes #164147

Summary

This PR moves the responsibility of updating the URL state into the <AssetDetails> instead of providing callbacks for parent components to manage the state themselves. This way logic for updating the URL state is centralized and shared between the Host details flyout and the Host details page.

How to test

  • Run from the branch locally
  • Navigate to "Hosts" screen
  • Open the host details flyout
  • Switch between tabs and refresh the page
  • Make sure your last active tab stays active after the refresh
  • Type into search fields on the "Metadata", "Processes" or "Logs" tab and refresh the page
  • Make sure the search term persists
  • Change the date range using the date picker and refresh the page
  • Make sure that the date range persists
  • All of the above should work the same for the full-page host details view

@mykolaharmash mykolaharmash force-pushed the 164147-asset-details-url-state branch from fc75cb3 to 844d428 Compare August 23, 2023 15:03
@mykolaharmash mykolaharmash added Feature:Metrics UI Metrics UI feature Team:Infra Monitoring UI - DEPRECATED DEPRECATED - Label for the Infra Monitoring UI team. Use Team:obs-ux-infra_services release_note:skip Skip the PR/issue when compiling release notes backport:skip This commit does not require backporting Feature:ObsHosts Hosts feature within Observability v8.11.0 labels Aug 24, 2023
@mykolaharmash mykolaharmash force-pushed the 164147-asset-details-url-state branch 2 times, most recently from 072f5b6 to 4c44838 Compare August 28, 2023 08:55
@mykolaharmash mykolaharmash force-pushed the 164147-asset-details-url-state branch from 4c44838 to a526204 Compare August 28, 2023 10:24
@mykolaharmash mykolaharmash marked this pull request as ready for review August 28, 2023 10:37
@mykolaharmash mykolaharmash requested a review from a team as a code owner August 28, 2023 10:37
@elasticmachine
Copy link
Contributor

Pinging @elastic/infra-monitoring-ui (Team:Infra Monitoring UI)

@jennypavlova jennypavlova self-requested a review August 28, 2023 10:49
@mykolaharmash
Copy link
Contributor Author

@elasticmachine merge upstream

@mykolaharmash
Copy link
Contributor Author

/oblt-deploy

Copy link
Contributor

@crespocarlos crespocarlos left a comment

Choose a reason for hiding this comment

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

Great job Mycola 👏!

I quickly tested it and everything seems to be working. Just a few comments and one adjustments in the use_asset_details_url_state regarding the itemId attribute.

@@ -53,10 +53,10 @@ const FlyoutTabIdRT = rt.union([
rt.literal(FlyoutTabIds.OSQUERY),
]);

const HostFlyoutStateRT = rt.intersection([
const AssetDetailsStateRT = rt.intersection([
rt.type({
itemId: rt.string,
Copy link
Contributor

Choose a reason for hiding this comment

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

The itemId is only used in the use_hosts_table. It can be moved over to the use_hosts_table state. With that, the asset_details_url_state will contain only attributes relevant to the Asset Details component. Wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, will move it to the useHostsTableUrlState

@@ -28,21 +28,14 @@ export type TabIds = `${FlyoutTabIds}`;

export interface OverridableTabState {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice!

@@ -140,7 +140,7 @@ export const useHostsTable = () => {
} = useKibanaContextForPlugin();
const { dataView } = useMetricsDataViewContext();

const [hostFlyoutState, setHostFlyoutState] = useHostFlyoutUrlState();
const [hostFlyoutState, setHostFlyoutState] = useAssetDetailsUrlState();
Copy link
Contributor

Choose a reason for hiding this comment

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

We're only using the useAssetDetailsUrlState to get the itemId and control the flyout's visibility. It seems like this attribute should be owned by the use_hosts_table.

The URL state I'm thinking of is the useHostsTableUrlState. Looks like itemId could be moved to that hook .

* running effects (like updating the URL) before
* refreshing the page.
*/
await pageObjects.common.sleep(1000);
Copy link
Contributor

Choose a reason for hiding this comment

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

Q: Have you encountered any problems due to a delay in updating the state before refreshing the page?

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, unfortunately sometimes the browser starts to refresh before URL is changed. I assume it's because we don't write to the URL directly but rather use router hooks, making URL updates basically part of the react rendering cycle. Which is convenient for business logic of course, but for these kinds of tests I could not find a better solution other than to introduce an artificial delay, unfortunately 😔

await searchInput.type('test');
await refreshPageWithDelay();

expect(await searchInput.getAttribute('value')).to.be('test');
Copy link
Contributor

@crespocarlos crespocarlos Aug 28, 2023

Choose a reason for hiding this comment

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

Asserts after page refresh are tricky and often cause flakiness.

The "safest" approach would be to wrap the expect in a retry.try function.

   retry.try(async () => {
       expect(await searchInput.getAttribute('value')).to.be('test');
   })

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good to know, thanks!

@@ -284,6 +294,16 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => {
});
});

it('preserves selected tab between page reloads', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

It might be better to centralize these "preserves..." test scenarios in the node_details test suite. The host_view suite would become less focused on the asset details functionality, while the node_details could focus on more in-depth test cases. Wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

agree 👍

AssetDetailsProps,
'asset' | 'assetType' | 'overrides' | 'onTabsStateChange' | 'renderMode'
>;
state: Pick<AssetDetailsProps, 'asset' | 'assetType' | 'overrides' | 'renderMode'>;
}

export function useAssetDetailsState({ state }: UseAssetDetailsStateProps) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm under the impression that the names here have become confusing with the introduction of the use_asset_details_url_state. I wonder if it should be called something along the lines of useAssetDetailsRenderInfo.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

makes sense, will rename it 👍

@crespocarlos crespocarlos changed the title Store asset details page state in the URL state [Infra UI] Store asset details page state in the URL state Aug 28, 2023
Copy link
Member

@jennypavlova jennypavlova left a comment

Choose a reason for hiding this comment

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

Well done! Checked it and it works fine 🙌 Added some small comments (nothing blocking the PR)

x-pack/test/functional/apps/infra/node_details.ts Outdated Show resolved Hide resolved
x-pack/test/functional/apps/infra/hosts_view.ts Outdated Show resolved Hide resolved
@@ -6,7 +6,7 @@
*/

import React from 'react';
import { AssetDetailsStateProvider } from './hooks/use_asset_details_state';
import { AssetDetailsRenderPropsProvider } from './hooks/use_asset_details_render_props';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Settled on AssetDetailsRenderProps name, seemed like it better conveys the purpose, but let me know if that does not look right 🙌

Copy link
Contributor

Choose a reason for hiding this comment

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

nice!

@kibana-ci
Copy link
Collaborator

💚 Build Succeeded

Metrics [docs]

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
infra 2.0MB 2.0MB -780.0B

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

Copy link
Contributor

@crespocarlos crespocarlos left a comment

Choose a reason for hiding this comment

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

LGTM 🚀 ! Thanks for applying the suggestions.

@mykolaharmash mykolaharmash merged commit fc6034a into elastic:main Aug 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:skip This commit does not require backporting Feature:Metrics UI Metrics UI feature Feature:ObsHosts Hosts feature within Observability release_note:skip Skip the PR/issue when compiling release notes Team:Infra Monitoring UI - DEPRECATED DEPRECATED - Label for the Infra Monitoring UI team. Use Team:obs-ux-infra_services v8.11.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Infra UI] Asset Details Page store state in the URL
6 participants