Skip to content

Commit

Permalink
[Dashboard] Fix Missing Legacy Redirect (#87246) (#87510)
Browse files Browse the repository at this point in the history
* Added dashboard no match to run legacy redirect. Added functional test coverage
  • Loading branch information
ThomThomson authored Jan 6, 2021
1 parent 0987604 commit 1c80c58
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/plugins/dashboard/public/application/dashboard_router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
PluginInitializerContext,
ScopedHistory,
} from '../services/core';
import { DashboardNoMatch } from './listing/dashboard_no_match';

export const dashboardUrlParams = {
showTopMenu: 'show-top-menu',
Expand Down Expand Up @@ -77,6 +78,7 @@ export async function mountApp({
const {
navigation,
savedObjects,
urlForwarding,
data: dataStart,
share: shareStart,
embeddable: embeddableStart,
Expand All @@ -88,6 +90,7 @@ export async function mountApp({
navigation,
onAppLeave,
savedObjects,
urlForwarding,
usageCollection,
core: coreStart,
data: dataStart,
Expand Down Expand Up @@ -180,6 +183,10 @@ export async function mountApp({
);
};

const renderNoMatch = (routeProps: RouteComponentProps) => {
return <DashboardNoMatch history={routeProps.history} />;
};

// make sure the index pattern list is up to date
await dataStart.indexPatterns.clearCache();

Expand All @@ -202,9 +209,10 @@ export async function mountApp({
render={renderDashboard}
/>
<Route exact path={DashboardConstants.LANDING_PAGE_PATH} render={renderListingPage} />
<Route path="/">
<Route exact path="/">
<Redirect to={DashboardConstants.LANDING_PAGE_PATH} />
</Route>
<Route render={renderNoMatch} />
</Switch>
</HashRouter>
</KibanaContextProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { dataPluginMock } from '../../../../data/public/mocks';
import { chromeServiceMock, coreMock } from '../../../../../core/public/mocks';
import { I18nProvider } from '@kbn/i18n/react';
import React from 'react';
import { UrlForwardingStart } from '../../../../url_forwarding/public';

function makeDefaultServices(): DashboardAppServices {
const core = coreMock.createStart();
Expand Down Expand Up @@ -71,6 +72,7 @@ function makeDefaultServices(): DashboardAppServices {
scopedHistory: () => ({} as ScopedHistory),
savedQueryService: {} as SavedQueryService,
setHeaderActionMenu: (mountPoint) => {},
urlForwarding: {} as UrlForwardingStart,
uiSettings: {} as IUiSettingsClient,
restorePreviousUrl: () => {},
onAppLeave: (handler) => {},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import React, { useEffect } from 'react';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n/react';
import { EuiCallOut } from '@elastic/eui';

import { RouteComponentProps } from 'react-router-dom';
import { useKibana, toMountPoint } from '../../services/kibana_react';
import { DashboardAppServices } from '../types';
import { DashboardConstants } from '../..';

let bannerId: string | undefined;

export const DashboardNoMatch = ({ history }: { history: RouteComponentProps['history'] }) => {
const { services } = useKibana<DashboardAppServices>();

useEffect(() => {
services.restorePreviousUrl();

const { navigated } = services.urlForwarding.navigateToLegacyKibanaUrl(
history.location.pathname
);

if (!navigated) {
const bannerMessage = i18n.translate('dashboard.noMatchRoute.bannerTitleText', {
defaultMessage: 'Page not found',
});

bannerId = services.core.overlays.banners.replace(
bannerId,
toMountPoint(
<EuiCallOut color="warning" iconType="iInCircle" title={bannerMessage}>
<p>
<FormattedMessage
id="dashboard.noMatchRoute.bannerText"
defaultMessage="Dashboard application doesn't recognize this route: {route}."
values={{
route: history.location.pathname,
}}
/>
</p>
</EuiCallOut>
)
);

// hide the message after the user has had a chance to acknowledge it -- so it doesn't permanently stick around
setTimeout(() => {
if (bannerId) {
services.core.overlays.banners.remove(bannerId);
}
}, 15000);

history.replace(DashboardConstants.LANDING_PAGE_PATH);
}
}, [services, history]);

return null;
};
2 changes: 2 additions & 0 deletions src/plugins/dashboard/public/application/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { NavigationPublicPluginStart } from '../services/navigation';
import { SavedObjectsTaggingApi } from '../services/saved_objects_tagging_oss';
import { DataPublicPluginStart, IndexPatternsContract } from '../services/data';
import { SavedObjectLoader, SavedObjectsStart } from '../services/saved_objects';
import { UrlForwardingStart } from '../../../url_forwarding/public';

export type DashboardRedirect = (props: RedirectToProps) => void;
export type RedirectToProps =
Expand Down Expand Up @@ -75,6 +76,7 @@ export interface DashboardAppServices {
uiSettings: IUiSettingsClient;
restorePreviousUrl: () => void;
savedObjects: SavedObjectsStart;
urlForwarding: UrlForwardingStart;
savedDashboards: SavedObjectLoader;
scopedHistory: () => ScopedHistory;
indexPatterns: IndexPatternsContract;
Expand Down
15 changes: 15 additions & 0 deletions test/functional/apps/dashboard/legacy_urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) {
await PageObjects.visualize.clickMarkdownWidget();
await PageObjects.visEditor.setMarkdownTxt(`[abc](#/dashboard/${testDashboardId})`);
await PageObjects.visEditor.clickGo();

await PageObjects.visualize.saveVisualizationExpectSuccess('legacy url markdown');

(await find.byLinkText('abc')).click();

await PageObjects.header.waitUntilLoadingHasFinished();
Expand All @@ -109,6 +112,18 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) {
await PageObjects.visEditor.expectMarkdownTextArea();
await browser.goForward();
});

it('resolves markdown link from dashboard', async () => {
await PageObjects.common.navigateToApp('dashboard');
await PageObjects.dashboard.clickNewDashboard();
await dashboardAddPanel.addVisualization('legacy url markdown');
(await find.byLinkText('abc')).click();
await PageObjects.header.waitUntilLoadingHasFinished();
await PageObjects.timePicker.setDefaultDataRange();

await PageObjects.dashboard.waitForRenderComplete();
await pieChart.expectPieSliceCount(5);
});
});
});
}

0 comments on commit 1c80c58

Please sign in to comment.