Skip to content

Commit

Permalink
Get homeHref from uiSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
tsullivan committed Nov 30, 2023
1 parent 5b1f254 commit beac26f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
13 changes: 13 additions & 0 deletions src/plugins/share/public/lib/get_home_href.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { HttpSetup, IUiSettingsClient } from '@kbn/core/public';

export function getHomeHref(http: HttpSetup, uiSettings: IUiSettingsClient) {
return http.basePath.prepend(uiSettings.get('defaultRoute'));
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,20 @@ const defaultBody = i18n.translate('share.urlService.redirect.components.Error.b
export interface ErrorProps {
title?: string;
body?: string;
homeHref: string;
docTitle: ChromeDocTitle;
error: Error;
}

export const RedirectEmptyPrompt: React.FC<ErrorProps> = ({
title = defaultTitle,
body = defaultBody,
homeHref,
docTitle,
error,
}) => {
// eslint-disable-next-line no-console
console.error('Short URL Redirect Error', error);

// Using the current URL containing "/app/r/", make a URL to the root basePath
// by trimming that part to end up at the Home app or project home.
const currentUrl = window.location.href;
const newUrl = currentUrl.replace(/\/app\/r\/.*/, '');
console.error('Short URL redirect error', error);

docTitle.change(
i18n.translate('share.urlService.redirect.components.docTitle', { defaultMessage: 'Not Found' })
Expand All @@ -58,7 +55,7 @@ export const RedirectEmptyPrompt: React.FC<ErrorProps> = ({
actions={
<EuiButtonEmpty
iconType="arrowLeft"
href={newUrl}
href={homeHref}
data-test-subj="redirectErrorEmptyPromptButton"
>
{i18n.translate('share.urlService.redirect.components.Error.homeButton', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,28 @@ import { RedirectEmptyPrompt } from './empty_prompt';
import { Spinner } from './spinner';

export interface PageProps {
homeHref: string;
docTitle: ChromeDocTitle;
customBranding: CustomBrandingSetup;
manager: Pick<RedirectManager, 'error$'>;
theme: ThemeServiceSetup;
}

export const Page: React.FC<PageProps> = ({ manager, customBranding, docTitle, theme }) => {
export const Page: React.FC<PageProps> = ({
manager,
homeHref,
customBranding,
docTitle,
theme,
}) => {
const error = useObservable(manager.error$);
const hasCustomBranding = useObservable(customBranding.hasCustomBranding$);

if (error) {
return (
<KibanaThemeProvider theme={{ theme$: theme.theme$ }}>
<EuiPageTemplate>
<RedirectEmptyPrompt docTitle={docTitle} error={error} />
<RedirectEmptyPrompt docTitle={docTitle} error={error} homeHref={homeHref} />
</EuiPageTemplate>
</KibanaThemeProvider>
);
Expand Down
23 changes: 15 additions & 8 deletions src/plugins/share/public/url_service/redirect/redirect_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@

import type { CoreSetup } from '@kbn/core/public';
import { i18n } from '@kbn/i18n';
import { BehaviorSubject } from 'rxjs';
import type { Location } from 'history';
import { migrateToLatest } from '@kbn/kibana-utils-plugin/common';
import type { Location } from 'history';
import { BehaviorSubject } from 'rxjs';
import type { UrlService } from '../../../common/url_service';
import { parseSearchParams, RedirectOptions } from '../../../common/url_service/locators/redirect';
import {
LEGACY_SHORT_URL_LOCATOR_ID,
LegacyShortUrlLocatorParams,
} from '../../../common/url_service/locators/legacy_short_url_locator';
import { parseSearchParams, RedirectOptions } from '../../../common/url_service/locators/redirect';
import { getHomeHref } from '../../lib/get_home_href';

export interface RedirectManagerDependencies {
url: UrlService;
Expand All @@ -28,21 +29,27 @@ export class RedirectManager {
constructor(public readonly deps: RedirectManagerDependencies) {}

public registerLocatorRedirectApp(core: CoreSetup) {
core.application.register({
const { application, customBranding, http, theme } = core;

application.register({
id: 'r',
title: 'Redirect endpoint',
chromeless: true,
mount: async (params) => {
const { render } = await import('./render');
const [coreStart] = await core.getStartServices();
const [start] = await core.getStartServices();
const { chrome, uiSettings } = start;

const unmount = render(params.element, {
manager: this,
customBranding: core.customBranding,
docTitle: coreStart.chrome.docTitle,
theme: core.theme,
customBranding,
docTitle: chrome.docTitle,
theme,
homeHref: getHomeHref(http, uiSettings),
});

this.onMount(params.history.location);

return () => {
unmount();
};
Expand Down

0 comments on commit beac26f

Please sign in to comment.