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

[Fleet] Use KibanaThemeProvider #122385

Merged
merged 7 commits into from
Jan 11, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions x-pack/plugins/fleet/.storybook/context/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export const StorybookContext: React.FC<{ storyContext?: StoryContext }> = ({
} as unknown as FleetConfigType;

const extensions = {};
const theme$ = EMPTY;
const kibanaVersion = '1.2.3';
const setHeaderActionMenu = useCallback(() => {}, []);

Expand All @@ -129,6 +130,7 @@ export const StorybookContext: React.FC<{ storyContext?: StoryContext }> = ({
startServices,
extensions,
setHeaderActionMenu,
theme$,
}}
>
{storyChildren}
Expand Down
41 changes: 29 additions & 12 deletions x-pack/plugins/fleet/public/applications/fleet/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import useObservable from 'react-use/lib/useObservable';

import type { TopNavMenuData } from 'src/plugins/navigation/public';

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

import type { FleetConfigType, FleetStartServices } from '../../plugin';
import {
KibanaContextProvider,
Expand Down Expand Up @@ -209,10 +211,20 @@ export const FleetAppContext: React.FC<{
history: AppMountParameters['history'];
kibanaVersion: string;
extensions: UIExtensionsStorage;
theme$: AppMountParameters['theme$'];
/** For testing purposes only */
routerHistory?: History<any>;
}> = memo(
({ children, startServices, config, history, kibanaVersion, extensions, routerHistory }) => {
({
children,
startServices,
config,
history,
kibanaVersion,
extensions,
routerHistory,
theme$,
}) => {
const isDarkMode = useObservable<boolean>(startServices.uiSettings.get$('theme:darkMode'));

return (
Expand All @@ -222,17 +234,22 @@ export const FleetAppContext: React.FC<{
<EuiErrorBoundary>
<ConfigContext.Provider value={config}>
<KibanaVersionContext.Provider value={kibanaVersion}>
<EuiThemeProvider darkMode={isDarkMode}>
<UIExtensionsContext.Provider value={extensions}>
<FleetStatusProvider>
<Router history={history}>
<PackageInstallProvider notifications={startServices.notifications}>
{children}
</PackageInstallProvider>
</Router>
</FleetStatusProvider>
</UIExtensionsContext.Provider>
</EuiThemeProvider>
<KibanaThemeProvider theme$={theme$}>
<EuiThemeProvider darkMode={isDarkMode}>
<UIExtensionsContext.Provider value={extensions}>
<FleetStatusProvider>
<Router history={history}>
<PackageInstallProvider
notifications={startServices.notifications}
theme$={theme$}
>
{children}
</PackageInstallProvider>
</Router>
</FleetStatusProvider>
</UIExtensionsContext.Provider>
</EuiThemeProvider>
</KibanaThemeProvider>
</KibanaVersionContext.Provider>
</ConfigContext.Provider>
</EuiErrorBoundary>
Expand Down
6 changes: 5 additions & 1 deletion x-pack/plugins/fleet/public/applications/fleet/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ interface FleetAppProps {
kibanaVersion: string;
extensions: UIExtensionsStorage;
setHeaderActionMenu: AppMountParameters['setHeaderActionMenu'];
theme$: AppMountParameters['theme$'];
}
const FleetApp = ({
basepath,
Expand All @@ -47,6 +48,7 @@ const FleetApp = ({
kibanaVersion,
extensions,
setHeaderActionMenu,
theme$,
}: FleetAppProps) => {
return (
<FleetAppContext
Expand All @@ -56,6 +58,7 @@ const FleetApp = ({
history={history}
kibanaVersion={kibanaVersion}
extensions={extensions}
theme$={theme$}
>
<WithPermissionsAndSetup>
<AppRoutes setHeaderActionMenu={setHeaderActionMenu} />
Expand All @@ -66,7 +69,7 @@ const FleetApp = ({

export function renderApp(
startServices: FleetStartServices,
{ element, appBasePath, history, setHeaderActionMenu }: AppMountParameters,
{ element, appBasePath, history, setHeaderActionMenu, theme$ }: AppMountParameters,
config: FleetConfigType,
kibanaVersion: string,
extensions: UIExtensionsStorage
Expand All @@ -80,6 +83,7 @@ export function renderApp(
kibanaVersion={kibanaVersion}
extensions={extensions}
setHeaderActionMenu={setHeaderActionMenu}
theme$={theme$}
/>,
element
);
Expand Down
41 changes: 25 additions & 16 deletions x-pack/plugins/fleet/public/applications/integrations/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {
} from '../../../../../../src/plugins/kibana_react/public';
import { EuiThemeProvider } from '../../../../../../src/plugins/kibana_react/common';

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

import { AgentPolicyContextProvider } from './hooks';
import { INTEGRATIONS_ROUTING_PATHS, pagePathGetters } from './constants';

Expand All @@ -43,6 +45,7 @@ export const IntegrationsAppContext: React.FC<{
kibanaVersion: string;
extensions: UIExtensionsStorage;
setHeaderActionMenu: AppMountParameters['setHeaderActionMenu'];
theme$: AppMountParameters['theme$'];
/** For testing purposes only */
routerHistory?: History<any>; // TODO remove
}> = memo(
Expand All @@ -54,6 +57,7 @@ export const IntegrationsAppContext: React.FC<{
kibanaVersion,
extensions,
setHeaderActionMenu,
theme$,
}) => {
const isDarkMode = useObservable<boolean>(startServices.uiSettings.get$('theme:darkMode'));

Expand All @@ -64,22 +68,27 @@ export const IntegrationsAppContext: React.FC<{
<EuiErrorBoundary>
<ConfigContext.Provider value={config}>
<KibanaVersionContext.Provider value={kibanaVersion}>
<EuiThemeProvider darkMode={isDarkMode}>
<UIExtensionsContext.Provider value={extensions}>
<FleetStatusProvider>
<startServices.customIntegrations.ContextProvider>
<Router history={history}>
<AgentPolicyContextProvider>
<PackageInstallProvider notifications={startServices.notifications}>
<IntegrationsHeader {...{ setHeaderActionMenu }} />
{children}
</PackageInstallProvider>
</AgentPolicyContextProvider>
</Router>
</startServices.customIntegrations.ContextProvider>
</FleetStatusProvider>
</UIExtensionsContext.Provider>
</EuiThemeProvider>
<KibanaThemeProvider theme$={theme$}>
<EuiThemeProvider darkMode={isDarkMode}>
<UIExtensionsContext.Provider value={extensions}>
<FleetStatusProvider>
<startServices.customIntegrations.ContextProvider>
<Router history={history}>
<AgentPolicyContextProvider>
<PackageInstallProvider
notifications={startServices.notifications}
theme$={theme$}
>
<IntegrationsHeader {...{ setHeaderActionMenu, theme$ }} />
{children}
</PackageInstallProvider>
</AgentPolicyContextProvider>
</Router>
</startServices.customIntegrations.ContextProvider>
</FleetStatusProvider>
</UIExtensionsContext.Provider>
</EuiThemeProvider>
</KibanaThemeProvider>
</KibanaVersionContext.Provider>
</ConfigContext.Provider>
</EuiErrorBoundary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ import { DeploymentDetails } from './deployment_details';

export const IntegrationsHeader = ({
setHeaderActionMenu,
theme$,
}: {
setHeaderActionMenu: AppMountParameters['setHeaderActionMenu'];
theme$: AppMountParameters['theme$'];
}) => {
return (
<HeaderPortal {...{ setHeaderActionMenu }}>
<HeaderPortal {...{ setHeaderActionMenu, theme$ }}>
<EuiHeaderSection grow={false}>
<EuiHeaderSectionItem>
<EuiHeaderLinks>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,23 @@ import { toMountPoint } from '../../../../../../../../src/plugins/kibana_react/p

export interface Props {
setHeaderActionMenu: AppMountParameters['setHeaderActionMenu'];
theme$: AppMountParameters['theme$'];
}

export const HeaderPortal: FC<Props> = ({ children, setHeaderActionMenu }) => {
export const HeaderPortal: FC<Props> = ({ children, setHeaderActionMenu, theme$ }) => {
const portalNode = useMemo(() => createPortalNode(), []);

useEffect(() => {
setHeaderActionMenu((element) => {
const mount = toMountPoint(<OutPortal node={portalNode} />);
const mount = toMountPoint(<OutPortal node={portalNode} />, { theme$ });
Copy link
Contributor

Choose a reason for hiding this comment

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

non-blocking question: if we forget to do this in the future, do we get some kind of runtime or type check error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@joshdover Not currently, I guess once all existing cases have been moved over the core team may be able to add some more strict checking?

return mount(element);
});

return () => {
portalNode.unmount();
setHeaderActionMenu(undefined);
};
}, [portalNode, setHeaderActionMenu]);
}, [portalNode, setHeaderActionMenu, theme$]);

return <InPortal node={portalNode}>{children}</InPortal>;
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import React, { useCallback, useState } from 'react';
import { useHistory } from 'react-router-dom';
import { FormattedMessage } from '@kbn/i18n-react';
import type { NotificationsStart } from 'src/core/public';
import type { Observable } from 'rxjs';
import type { CoreTheme } from 'kibana/public';

import { toMountPoint } from '../../../../../../../src/plugins/kibana_react/public';
import type { PackageInfo } from '../../../types';
Expand All @@ -30,7 +32,13 @@ type InstallPackageProps = Pick<PackageInfo, 'name' | 'version' | 'title'> & {
};
type SetPackageInstallStatusProps = Pick<PackageInfo, 'name'> & PackageInstallItem;

function usePackageInstall({ notifications }: { notifications: NotificationsStart }) {
function usePackageInstall({
notifications,
theme$,
}: {
notifications: NotificationsStart;
theme$: Observable<CoreTheme>;
}) {
const history = useHistory();
const { getPath } = useLink();
const [packages, setPackage] = useState<PackagesInstall>({});
Expand Down Expand Up @@ -77,13 +85,15 @@ function usePackageInstall({ notifications }: { notifications: NotificationsStar
id="xpack.fleet.integrations.packageInstallErrorTitle"
defaultMessage="Failed to install {title} package"
values={{ title }}
/>
/>,
{ theme$ }
),
text: toMountPoint(
<FormattedMessage
id="xpack.fleet.integrations.packageInstallErrorDescription"
defaultMessage="Something went wrong while trying to install this package. Please try again later."
/>
/>,
{ theme$ }
),
iconType: 'alert',
});
Expand All @@ -102,19 +112,28 @@ function usePackageInstall({ notifications }: { notifications: NotificationsStar
id="xpack.fleet.integrations.packageInstallSuccessTitle"
defaultMessage="Installed {title}"
values={{ title }}
/>
/>,
{ theme$ }
),
text: toMountPoint(
<FormattedMessage
id="xpack.fleet.integrations.packageInstallSuccessDescription"
defaultMessage="Successfully installed {title}"
values={{ title }}
/>
/>,
{ theme$ }
),
});
}
},
[getPackageInstallStatus, notifications.toasts, setPackageInstallStatus, getPath, history]
[
getPackageInstallStatus,
notifications.toasts,
setPackageInstallStatus,
getPath,
history,
theme$,
]
);

const uninstallPackage = useCallback(
Expand All @@ -135,13 +154,15 @@ function usePackageInstall({ notifications }: { notifications: NotificationsStar
id="xpack.fleet.integrations.packageUninstallErrorTitle"
defaultMessage="Failed to uninstall {title} package"
values={{ title }}
/>
/>,
{ theme$ }
),
text: toMountPoint(
<FormattedMessage
id="xpack.fleet.integrations.packageUninstallErrorDescription"
defaultMessage="Something went wrong while trying to uninstall this package. Please try again later."
/>
/>,
{ theme$ }
),
iconType: 'alert',
});
Expand All @@ -154,14 +175,16 @@ function usePackageInstall({ notifications }: { notifications: NotificationsStar
id="xpack.fleet.integrations.packageUninstallSuccessTitle"
defaultMessage="Uninstalled {title}"
values={{ title }}
/>
/>,
{ theme$ }
),
text: toMountPoint(
<FormattedMessage
id="xpack.fleet.integrations.packageUninstallSuccessDescription"
defaultMessage="Successfully uninstalled {title}"
values={{ title }}
/>
/>,
{ theme$ }
),
});
if (redirectToVersion !== version) {
Expand All @@ -172,7 +195,7 @@ function usePackageInstall({ notifications }: { notifications: NotificationsStar
}
}
},
[notifications.toasts, setPackageInstallStatus, getPath, history]
[notifications.toasts, setPackageInstallStatus, getPath, history, theme$]
);

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ interface IntegrationsAppProps {
kibanaVersion: string;
extensions: UIExtensionsStorage;
setHeaderActionMenu: AppMountParameters['setHeaderActionMenu'];
theme$: AppMountParameters['theme$'];
}
const IntegrationsApp = ({
basepath,
Expand All @@ -47,6 +48,7 @@ const IntegrationsApp = ({
kibanaVersion,
extensions,
setHeaderActionMenu,
theme$,
}: IntegrationsAppProps) => {
return (
<IntegrationsAppContext
Expand All @@ -57,6 +59,7 @@ const IntegrationsApp = ({
kibanaVersion={kibanaVersion}
extensions={extensions}
setHeaderActionMenu={setHeaderActionMenu}
theme$={theme$}
>
<AppRoutes />
</IntegrationsAppContext>
Expand All @@ -65,7 +68,7 @@ const IntegrationsApp = ({

export function renderApp(
startServices: FleetStartServices,
{ element, appBasePath, history, setHeaderActionMenu }: AppMountParameters,
{ element, appBasePath, history, setHeaderActionMenu, theme$ }: AppMountParameters,
config: FleetConfigType,
kibanaVersion: string,
extensions: UIExtensionsStorage,
Expand All @@ -81,6 +84,7 @@ export function renderApp(
kibanaVersion={kibanaVersion}
extensions={extensions}
setHeaderActionMenu={setHeaderActionMenu}
theme$={theme$}
/>
</UsageTracker>,
element
Expand Down
Loading