Skip to content

Commit

Permalink
[ML] Transforms: Use KibanaThemeProvider in transform plugin (#120933)
Browse files Browse the repository at this point in the history
* [ML] Use KibanaThemeProvider in transform plugin

* [ML] Add missing mock

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
peteharverson and kibanamachine authored Dec 13, 2021
1 parent c4e8cb3 commit 1e159f2
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useContext } from 'react';

import type { ScopedHistory } from 'kibana/public';

import { coreMock } from '../../../../../../src/core/public/mocks';
import { coreMock, themeServiceMock } from '../../../../../../src/core/public/mocks';
import { dataPluginMock } from '../../../../../../src/plugins/data/public/mocks';
import { savedObjectsPluginMock } from '../../../../../../src/plugins/saved_objects/public/mocks';
import { SharePluginStart } from '../../../../../../src/plugins/share/public';
Expand Down Expand Up @@ -39,6 +39,7 @@ const appDependencies: AppDependencies = {
savedObjects: coreStart.savedObjects,
storage: { get: jest.fn() } as unknown as Storage,
overlays: coreStart.overlays,
theme: themeServiceMock.createStartContract(),
http: coreSetup.http,
history: {} as ScopedHistory,
savedObjectsPlugin: savedObjectsPluginMock.createStartContract(),
Expand Down
21 changes: 13 additions & 8 deletions x-pack/plugins/transform/public/app/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import { EuiErrorBoundary } from '@elastic/eui';

import { FormattedMessage } from '@kbn/i18n-react';

import { KibanaContextProvider } from '../../../../../src/plugins/kibana_react/public';
import {
KibanaContextProvider,
KibanaThemeProvider,
} from '../../../../../src/plugins/kibana_react/public';

import { API_BASE_PATH } from '../../common/constants';

Expand Down Expand Up @@ -65,13 +68,15 @@ export const renderApp = (element: HTMLElement, appDependencies: AppDependencies

render(
<EuiErrorBoundary>
<KibanaContextProvider services={appDependencies}>
<AuthorizationProvider privilegesEndpoint={`${API_BASE_PATH}privileges`}>
<I18nContext>
<App history={appDependencies.history} />
</I18nContext>
</AuthorizationProvider>
</KibanaContextProvider>
<KibanaThemeProvider theme$={appDependencies.theme.theme$}>
<KibanaContextProvider services={appDependencies}>
<AuthorizationProvider privilegesEndpoint={`${API_BASE_PATH}privileges`}>
<I18nContext>
<App history={appDependencies.history} />
</I18nContext>
</AuthorizationProvider>
</KibanaContextProvider>
</KibanaThemeProvider>
</EuiErrorBoundary>,
element
);
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/transform/public/app/app_dependencies.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface AppDependencies {
savedObjects: CoreStart['savedObjects'];
storage: Storage;
overlays: CoreStart['overlays'];
theme: CoreStart['theme'];
history: ScopedHistory;
savedObjectsPlugin: SavedObjectsStart;
share: SharePluginStart;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import React from 'react';
import { render } from '@testing-library/react';

import { CoreStart } from 'src/core/public';
import { themeServiceMock } from 'src/core/public/mocks';

import { ToastNotificationText } from './toast_notification_text';

Expand All @@ -20,6 +21,7 @@ describe('ToastNotificationText', () => {
const props = {
overlays: {} as CoreStart['overlays'],
text: 'a short text message',
theme: themeServiceMock.createStartContract(),
};
const { container } = render(<ToastNotificationText {...props} />);
expect(container.textContent).toBe('a short text message');
Expand All @@ -29,6 +31,7 @@ describe('ToastNotificationText', () => {
const props = {
overlays: {} as CoreStart['overlays'],
text: 'a text message that is longer than 140 characters. a text message that is longer than 140 characters. a text message that is longer than 140 characters. ',
theme: themeServiceMock.createStartContract(),
};
const { container } = render(<ToastNotificationText {...props} />);
expect(container.textContent).toBe(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ const MAX_SIMPLE_MESSAGE_LENGTH = 140;
// That's why we need to pass in `overlays` as a prop cannot get it via context.
interface ToastNotificationTextProps {
overlays: CoreStart['overlays'];
theme: CoreStart['theme'];
text: any;
previewTextLength?: number;
}

export const ToastNotificationText: FC<ToastNotificationTextProps> = ({
overlays,
text,
theme,
previewTextLength,
}) => {
if (typeof text === 'string' && text.length <= MAX_SIMPLE_MESSAGE_LENGTH) {
Expand Down Expand Up @@ -80,7 +82,8 @@ export const ToastNotificationText: FC<ToastNotificationTextProps> = ({
})}
</EuiButtonEmpty>
</EuiModalFooter>
</EuiModal>
</EuiModal>,
{ theme$: theme.theme$ }
)
);
};
Expand Down
30 changes: 25 additions & 5 deletions x-pack/plugins/transform/public/app/hooks/use_delete_transform.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export const useDeleteIndexAndTargetIndex = (items: TransformListRow[]) => {
type SuccessCountField = keyof Omit<DeleteTransformStatus, 'destinationIndex'>;

export const useDeleteTransforms = () => {
const { overlays } = useAppDependencies();
const { overlays, theme } = useAppDependencies();
const toastNotifications = useToastNotifications();
const api = useApi();

Expand All @@ -136,8 +136,10 @@ export const useDeleteTransforms = () => {
<ToastNotificationText
previewTextLength={50}
overlays={overlays}
theme={theme}
text={getErrorMessage(results)}
/>
/>,
{ theme$: theme.theme$ }
),
});
return;
Expand Down Expand Up @@ -203,7 +205,13 @@ export const useDeleteTransforms = () => {
values: { transformId },
}),
text: toMountPoint(
<ToastNotificationText previewTextLength={50} overlays={overlays} text={error} />
<ToastNotificationText
previewTextLength={50}
overlays={overlays}
theme={theme}
text={error}
/>,
{ theme$: theme.theme$ }
),
});
}
Expand All @@ -219,7 +227,13 @@ export const useDeleteTransforms = () => {
}
),
text: toMountPoint(
<ToastNotificationText previewTextLength={50} overlays={overlays} text={error} />
<ToastNotificationText
previewTextLength={50}
overlays={overlays}
theme={theme}
text={error}
/>,
{ theme$: theme.theme$ }
),
});
}
Expand All @@ -235,7 +249,13 @@ export const useDeleteTransforms = () => {
}
),
text: toMountPoint(
<ToastNotificationText previewTextLength={50} overlays={overlays} text={error} />
<ToastNotificationText
previewTextLength={50}
overlays={overlays}
theme={theme}
text={error}
/>,
{ theme$: theme.theme$ }
),
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { ToastNotificationText } from '../components';
import { useApi } from './use_api';

export const useStartTransforms = () => {
const deps = useAppDependencies();
const { overlays, theme } = useAppDependencies();
const toastNotifications = useToastNotifications();
const api = useApi();

Expand All @@ -39,7 +39,12 @@ export const useStartTransforms = () => {
}
),
text: toMountPoint(
<ToastNotificationText overlays={deps.overlays} text={getErrorMessage(results)} />
<ToastNotificationText
overlays={overlays}
theme={theme}
text={getErrorMessage(results)}
/>,
{ theme$: theme.theme$ }
),
});
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { ToastNotificationText } from '../components';
import { useApi } from './use_api';

export const useStopTransforms = () => {
const deps = useAppDependencies();
const { overlays, theme } = useAppDependencies();
const toastNotifications = useToastNotifications();
const api = useApi();

Expand All @@ -39,7 +39,12 @@ export const useStopTransforms = () => {
}
),
text: toMountPoint(
<ToastNotificationText overlays={deps.overlays} text={getErrorMessage(results)} />
<ToastNotificationText
overlays={overlays}
theme={theme}
text={getErrorMessage(results)}
/>,
{ theme$: theme.theme$ }
),
});
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export async function mountManagementSection(
const { http, notifications, getStartServices } = coreSetup;
const startServices = await getStartServices();
const [core, plugins] = startServices;
const { application, chrome, docLinks, i18n, overlays, savedObjects, uiSettings } = core;
const { application, chrome, docLinks, i18n, overlays, theme, savedObjects, uiSettings } = core;
const { data, share, spaces, triggersActionsUi } = plugins;
const { docTitle } = chrome;

Expand All @@ -47,6 +47,7 @@ export async function mountManagementSection(
i18n,
notifications,
overlays,
theme,
savedObjects,
storage: localStorage,
uiSettings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export const StepCreateForm: FC<StepCreateFormProps> = React.memo(
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [created, started, indexPatternId]);

const { overlays, theme } = useAppDependencies();
const api = useApi();

async function createTransform() {
Expand All @@ -160,9 +161,11 @@ export const StepCreateForm: FC<StepCreateFormProps> = React.memo(
}),
text: toMountPoint(
<ToastNotificationText
overlays={deps.overlays}
overlays={overlays}
theme={theme}
text={getErrorMessage(isPutTransformsResponseSchema(resp) ? respErrors : resp)}
/>
/>,
{ theme$: theme.theme$ }
),
});
setCreated(false);
Expand Down Expand Up @@ -214,7 +217,12 @@ export const StepCreateForm: FC<StepCreateFormProps> = React.memo(
values: { transformId },
}),
text: toMountPoint(
<ToastNotificationText overlays={deps.overlays} text={getErrorMessage(errorMessage)} />
<ToastNotificationText
overlays={overlays}
theme={theme}
text={getErrorMessage(errorMessage)}
/>,
{ theme$: theme.theme$ }
),
});
setStarted(false);
Expand Down Expand Up @@ -275,7 +283,8 @@ export const StepCreateForm: FC<StepCreateFormProps> = React.memo(
values: { dataViewName },
}),
text: toMountPoint(
<ToastNotificationText overlays={deps.overlays} text={getErrorMessage(e)} />
<ToastNotificationText overlays={overlays} theme={theme} text={getErrorMessage(e)} />,
{ theme$: theme.theme$ }
),
});
setLoading(false);
Expand Down Expand Up @@ -321,7 +330,12 @@ export const StepCreateForm: FC<StepCreateFormProps> = React.memo(
defaultMessage: 'An error occurred getting the progress percentage:',
}),
text: toMountPoint(
<ToastNotificationText overlays={deps.overlays} text={getErrorMessage(stats)} />
<ToastNotificationText
overlays={overlays}
theme={theme}
text={getErrorMessage(stats)}
/>,
{ theme$: theme.theme$ }
),
});
clearInterval(interval);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export const StepDetailsForm: FC<StepDetailsFormProps> = React.memo(
[setIndexPatternTimeField, indexPatternAvailableTimeFields]
);

const { overlays, theme } = useAppDependencies();
const api = useApi();

// fetch existing transform IDs and indices once for form validation
Expand Down Expand Up @@ -150,9 +151,11 @@ export const StepDetailsForm: FC<StepDetailsFormProps> = React.memo(
}),
text: toMountPoint(
<ToastNotificationText
overlays={deps.overlays}
overlays={overlays}
theme={theme}
text={getErrorMessage(transformPreview)}
/>
/>,
{ theme$: theme.theme$ }
),
});
}
Expand All @@ -165,7 +168,12 @@ export const StepDetailsForm: FC<StepDetailsFormProps> = React.memo(
defaultMessage: 'An error occurred getting the existing transform IDs:',
}),
text: toMountPoint(
<ToastNotificationText overlays={deps.overlays} text={getErrorMessage(resp)} />
<ToastNotificationText
overlays={overlays}
theme={theme}
text={getErrorMessage(resp)}
/>,
{ theme$: theme.theme$ }
),
});
} else {
Expand All @@ -182,7 +190,12 @@ export const StepDetailsForm: FC<StepDetailsFormProps> = React.memo(
defaultMessage: 'An error occurred getting the existing index names:',
}),
text: toMountPoint(
<ToastNotificationText overlays={deps.overlays} text={getErrorMessage(indices)} />
<ToastNotificationText
overlays={overlays}
theme={theme}
text={getErrorMessage(indices)}
/>,
{ theme$: theme.theme$ }
),
});
}
Expand All @@ -195,7 +208,8 @@ export const StepDetailsForm: FC<StepDetailsFormProps> = React.memo(
defaultMessage: 'An error occurred getting the existing data view titles:',
}),
text: toMountPoint(
<ToastNotificationText overlays={deps.overlays} text={getErrorMessage(e)} />
<ToastNotificationText overlays={overlays} theme={theme} text={getErrorMessage(e)} />,
{ theme$: theme.theme$ }
),
});
}
Expand Down

0 comments on commit 1e159f2

Please sign in to comment.