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

Explore: Fix Browser title not updated on Navigation to Explore #34651

Merged
merged 4 commits into from May 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions public/app/features/explore/Wrapper.test.tsx
Expand Up @@ -253,6 +253,12 @@ describe('Wrapper', () => {
await screen.findByText(`elastic Editor input: error`);
await screen.findByText(`loki Editor input: { label="value"}`);
});

it('changes the document title of the explore page', async () => {
setup({ datasources: [] });
await waitFor(() => expect(document.querySelector('head')).toMatchSnapshot());
await waitFor(() => expect(document.title).toEqual('Explore - Grafana'));
});
});

type DatasourceSetup = { settings: DataSourceInstanceSettings; api: DataSourceApi };
Expand Down Expand Up @@ -299,6 +305,16 @@ function setup(options?: SetupOptions): { datasources: { [name: string]: DataSou
timeZone: 'utc',
};

store.getState().navIndex = {
explore: {
id: 'explore',
text: 'Explore',
subTitle: 'Explore your data',
icon: 'compass',
url: '/explore',
},
};

locationService.push({ pathname: '/explore' });

if (options?.query) {
Expand Down
47 changes: 35 additions & 12 deletions public/app/features/explore/Wrapper.tsx
@@ -1,18 +1,43 @@
import React, { Component } from 'react';
import { connect } from 'react-redux';
import React, { PureComponent } from 'react';
Copy link
Contributor

Choose a reason for hiding this comment

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

The change from Component to PureComponent should be fine AFAIK but maybe @ivanahuckova or @aocenas can confirm this too?

Copy link
Member

Choose a reason for hiding this comment

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

As far as I know, the change should be okay. 🙂

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 think it should break anything but I have a feeling:

navModel: getNavModel(state.navIndex, 'explore'),

will break any benefit as it seems to create a new object every time.

Copy link
Member Author

Choose a reason for hiding this comment

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

great 🙌🏾, thank you @ivanahuckova for taking a look, I will proceed to merge :)

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think it should break anything but I have a feeling:

navModel: getNavModel(state.navIndex, 'explore'),

will break any benefit as it seems to create a new object every time.

Hey, @aocenas thanks for checking out this PR 😄 , I was looking into getNavModel function, even though it generates a new object every time, the values are always the same if the parameters don't change, and because PureComponent only does a shallow comparison on the ComponentShouldUpdate I think will keep the benefits. I also verified the children's Wrapper component to confirm they are also PureComponents (to be sure these changers are not breaking anything).

let me know if I am mistaken(I still lacking context in the project 😅 ) , I will merge the PR but ofc, if we find later on this change don't add any benefits, we can put it back as before :)

import { connect, ConnectedProps } from 'react-redux';
import { ExploreId, ExploreQueryParams } from 'app/types/explore';
import { ErrorBoundaryAlert } from '@grafana/ui';
import { lastSavedUrl, resetExploreAction, richHistoryUpdatedAction } from './state/main';
import { getRichHistory } from '../../core/utils/richHistory';
import { ExplorePaneContainer } from './ExplorePaneContainer';
import { GrafanaRouteComponentProps } from 'app/core/navigation/types';
import { NavModel } from '@grafana/data';
import { Branding } from '../../core/components/Branding/Branding';

interface WrapperProps extends GrafanaRouteComponentProps<{}, ExploreQueryParams> {
resetExploreAction: typeof resetExploreAction;
richHistoryUpdatedAction: typeof richHistoryUpdatedAction;
}
import { getNavModel } from '../../core/selectors/navModel';
import { StoreState } from 'app/types';

interface RouteProps extends GrafanaRouteComponentProps<{}, ExploreQueryParams> {}
interface OwnProps {}

const mapStateToProps = (state: StoreState) => {
return {
navModel: getNavModel(state.navIndex, 'explore'),
};
};

const mapDispatchToProps = {
resetExploreAction,
richHistoryUpdatedAction,
};

const connector = connect(mapStateToProps, mapDispatchToProps);

type Props = OwnProps & RouteProps & ConnectedProps<typeof connector>;
class WrapperUnconnected extends PureComponent<Props> {
updatePageDocumentTitle(navModel: NavModel) {
if (navModel) {
document.title = `${navModel.main.text} - ${Branding.AppTitle}`;
} else {
document.title = Branding.AppTitle;
}
}

export class Wrapper extends Component<WrapperProps> {
componentWillUnmount() {
this.props.resetExploreAction({});
}
Expand All @@ -23,6 +48,7 @@ export class Wrapper extends Component<WrapperProps> {

const richHistory = getRichHistory();
this.props.richHistoryUpdatedAction({ richHistory });
this.updatePageDocumentTitle(this.props.navModel);
}

render() {
Expand All @@ -46,9 +72,6 @@ export class Wrapper extends Component<WrapperProps> {
}
}

const mapDispatchToProps = {
resetExploreAction,
richHistoryUpdatedAction,
};
const Wrapper = connector(WrapperUnconnected);

export default connect(null, mapDispatchToProps)(Wrapper);
export default Wrapper;