Skip to content

Commit

Permalink
[8.9] Fix theming for error toasts (elastic#160219) (elastic#160765)
Browse files Browse the repository at this point in the history
# Backport

This will backport the following commits from `main` to `8.9`:
- [Fix theming for error toasts
(elastic#160219)](elastic#160219)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Lukas
Olson","email":"[email protected]"},"sourceCommit":{"committedDate":"2023-06-28T14:58:33Z","message":"Fix
theming for error toasts (elastic#160219)\n\n## Summary\r\n\r\nFixes
https://github.com/elastic/kibana/issues/159153.\r\n\r\nApplies the
current theme (dark/light) to error
toasts.\r\n\r\nBefore:\r\n\r\n\r\n![image](https://github.com/elastic/kibana/assets/1178348/b0a05412-3e07-4980-b3e8-8dcdb602119f)\r\n\r\nAfter:\r\n\r\n\r\n![image](https://github.com/elastic/kibana/assets/1178348/96bbaf58-25a6-47a3-b9ba-b3caf7a90cd9)","sha":"1223c3f55f5f6d9cd36e8cc55aa46005073cdd3f","branchLabelMapping":{"^v8.10.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Team:Core","release_note:fix","Team:DataDiscovery","v8.9.0","v8.10.0"],"number":160219,"url":"https://github.com/elastic/kibana/pull/160219","mergeCommit":{"message":"Fix
theming for error toasts (elastic#160219)\n\n## Summary\r\n\r\nFixes
https://github.com/elastic/kibana/issues/159153.\r\n\r\nApplies the
current theme (dark/light) to error
toasts.\r\n\r\nBefore:\r\n\r\n\r\n![image](https://github.com/elastic/kibana/assets/1178348/b0a05412-3e07-4980-b3e8-8dcdb602119f)\r\n\r\nAfter:\r\n\r\n\r\n![image](https://github.com/elastic/kibana/assets/1178348/96bbaf58-25a6-47a3-b9ba-b3caf7a90cd9)","sha":"1223c3f55f5f6d9cd36e8cc55aa46005073cdd3f"}},"sourceBranch":"main","suggestedTargetBranches":["8.9"],"targetPullRequestStates":[{"branch":"8.9","label":"v8.9.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.10.0","labelRegex":"^v8.10.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/160219","number":160219,"mergeCommit":{"message":"Fix
theming for error toasts (elastic#160219)\n\n## Summary\r\n\r\nFixes
https://github.com/elastic/kibana/issues/159153.\r\n\r\nApplies the
current theme (dark/light) to error
toasts.\r\n\r\nBefore:\r\n\r\n\r\n![image](https://github.com/elastic/kibana/assets/1178348/b0a05412-3e07-4980-b3e8-8dcdb602119f)\r\n\r\nAfter:\r\n\r\n\r\n![image](https://github.com/elastic/kibana/assets/1178348/96bbaf58-25a6-47a3-b9ba-b3caf7a90cd9)","sha":"1223c3f55f5f6d9cd36e8cc55aa46005073cdd3f"}}]}]
BACKPORT-->

Co-authored-by: Lukas Olson <[email protected]>
  • Loading branch information
kibanamachine and lukasolson authored Jun 28, 2023
1 parent 8840872 commit 524dc02
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ export class NotificationsService {
title,
error,
openModal: overlays.openModal,
i18nContext: () => i18nDep.Context,
i18n: i18nDep,
theme,
}),
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import React from 'react';
import { mountWithIntl } from '@kbn/test-jest-helpers';

import { ErrorToast } from './error_toast';
import { themeServiceMock } from '@kbn/core-theme-browser-mocks';
import { i18nServiceMock } from '@kbn/core-i18n-browser-mocks';

interface ErrorToastProps {
error?: Error;
Expand All @@ -19,6 +21,8 @@ interface ErrorToastProps {
}

let openModal: jest.Mock;
const mockTheme = themeServiceMock.createStartContract();
const mockI18n = i18nServiceMock.createStartContract();

beforeEach(() => (openModal = jest.fn()));

Expand All @@ -29,9 +33,8 @@ function render(props: ErrorToastProps = {}) {
error={props.error || new Error('error message')}
title={props.title || 'An error occured'}
toastMessage={props.toastMessage || 'This is the toast message'}
i18nContext={() =>
({ children }) =>
<React.Fragment>{children}</React.Fragment>}
i18n={mockI18n}
theme={mockTheme}
/>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ import { EuiSpacer } from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n-react';
import type { I18nStart } from '@kbn/core-i18n-browser';
import type { OverlayStart } from '@kbn/core-overlays-browser';
import { ThemeServiceStart } from '@kbn/core-theme-browser';
import { CoreContextProvider } from '@kbn/core-theme-browser-internal';

interface ErrorToastProps {
title: string;
error: Error;
toastMessage: string;
openModal: OverlayStart['openModal'];
i18nContext: () => I18nStart['Context'];
i18n: I18nStart;
theme: ThemeServiceStart;
}

interface RequestError extends Error {
Expand All @@ -52,9 +55,9 @@ export function showErrorDialog({
title,
error,
openModal,
i18nContext,
}: Pick<ErrorToastProps, 'error' | 'title' | 'openModal' | 'i18nContext'>) {
const I18nContext = i18nContext();
i18n,
theme,
}: Pick<ErrorToastProps, 'error' | 'title' | 'openModal' | 'i18n' | 'theme'>) {
let text = '';

if (isRequestError(error)) {
Expand All @@ -68,32 +71,30 @@ export function showErrorDialog({

const modal = openModal(
mount(
<React.Fragment>
<I18nContext>
<EuiModalHeader>
<EuiModalHeaderTitle>{title}</EuiModalHeaderTitle>
</EuiModalHeader>
<EuiModalBody data-test-subj="errorModalBody">
<EuiCallOut size="s" color="danger" iconType="error" title={error.message} />
{text && (
<React.Fragment>
<EuiSpacer size="s" />
<EuiCodeBlock isCopyable={true} paddingSize="s">
{text}
</EuiCodeBlock>
</React.Fragment>
)}
</EuiModalBody>
<EuiModalFooter>
<EuiButton onClick={() => modal.close()} fill>
<FormattedMessage
id="core.notifications.errorToast.closeModal"
defaultMessage="Close"
/>
</EuiButton>
</EuiModalFooter>
</I18nContext>
</React.Fragment>
<CoreContextProvider i18n={i18n} theme={theme}>
<EuiModalHeader>
<EuiModalHeaderTitle>{title}</EuiModalHeaderTitle>
</EuiModalHeader>
<EuiModalBody data-test-subj="errorModalBody">
<EuiCallOut size="s" color="danger" iconType="error" title={error.message} />
{text && (
<React.Fragment>
<EuiSpacer size="s" />
<EuiCodeBlock isCopyable={true} paddingSize="s">
{text}
</EuiCodeBlock>
</React.Fragment>
)}
</EuiModalBody>
<EuiModalFooter>
<EuiButton onClick={() => modal.close()} fill>
<FormattedMessage
id="core.notifications.errorToast.closeModal"
defaultMessage="Close"
/>
</EuiButton>
</EuiModalFooter>
</CoreContextProvider>
)
);
}
Expand All @@ -103,7 +104,8 @@ export function ErrorToast({
error,
toastMessage,
openModal,
i18nContext,
i18n,
theme,
}: ErrorToastProps) {
return (
<React.Fragment>
Expand All @@ -113,7 +115,7 @@ export function ErrorToast({
size="s"
color="danger"
data-test-subj="errorToastBtn"
onClick={() => showErrorDialog({ title, error, openModal, i18nContext })}
onClick={() => showErrorDialog({ title, error, openModal, i18n, theme })}
>
<FormattedMessage
id="core.toasts.errorToast.seeFullError"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { ToastsApi } from './toasts_api';

import { uiSettingsServiceMock } from '@kbn/core-ui-settings-browser-mocks';
import { i18nServiceMock } from '@kbn/core-i18n-browser-mocks';
import { themeServiceMock } from '@kbn/core-theme-browser-mocks';

async function getCurrentToasts(toasts: ToastsApi) {
return await firstValueFrom(toasts.get$());
Expand Down Expand Up @@ -41,7 +42,11 @@ function toastDeps() {
}

function startDeps() {
return { overlays: {} as any, i18n: i18nServiceMock.createStartContract() };
return {
overlays: {} as any,
i18n: i18nServiceMock.createStartContract(),
theme: themeServiceMock.createStartContract(),
};
}

describe('#get$()', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import type {
ToastInputFields,
ToastOptions,
} from '@kbn/core-notifications-browser';
import { ThemeServiceStart } from '@kbn/core-theme-browser';
import { ErrorToast } from './error_toast';

const normalizeToast = (toastOrTitle: ToastInput): ToastInputFields => {
Expand All @@ -44,15 +45,25 @@ export class ToastsApi implements IToasts {

private overlays?: OverlayStart;
private i18n?: I18nStart;
private theme?: ThemeServiceStart;

constructor(deps: { uiSettings: IUiSettingsClient }) {
this.uiSettings = deps.uiSettings;
}

/** @internal */
public start({ overlays, i18n }: { overlays: OverlayStart; i18n: I18nStart }) {
public start({
overlays,
i18n,
theme,
}: {
overlays: OverlayStart;
i18n: I18nStart;
theme: ThemeServiceStart;
}) {
this.overlays = overlays;
this.i18n = i18n;
this.theme = theme;
}

/** Observable of the toast messages to show to the user. */
Expand Down Expand Up @@ -176,7 +187,8 @@ export class ToastsApi implements IToasts {
error={error}
title={options.title}
toastMessage={message}
i18nContext={() => this.i18n!.Context}
i18n={this.i18n!}
theme={this.theme!}
/>
),
...options,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class ToastsService {
}

public start({ i18n, overlays, theme, targetDomElement }: StartDeps) {
this.api!.start({ overlays, i18n });
this.api!.start({ overlays, i18n, theme });
this.targetDomElement = targetDomElement;

render(
Expand Down

0 comments on commit 524dc02

Please sign in to comment.