[canvas] Create Nav Link service; remove legacy service (#107346)

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Clint Andrew Hall 2021-08-02 11:49:49 -04:00 committed by GitHub
parent 3bd9ff361d
commit 2d17071c9f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 59 additions and 52 deletions

View file

@ -10,7 +10,7 @@ import PropTypes from 'prop-types';
import { History } from 'history';
// @ts-expect-error
import createHashStateHistory from 'history-extra/dist/createHashStateHistory';
import { useServices } from '../../services';
import { useNavLinkService } from '../../services';
// @ts-expect-error
import { shortcutManager } from '../../lib/shortcut_manager';
import { CanvasRouter } from '../../routes';
@ -31,11 +31,11 @@ class ShortcutManagerContextWrapper extends React.Component {
export const App: FC = () => {
const historyRef = useRef<History>(createHashStateHistory() as History);
const services = useServices();
const { updatePath } = useNavLinkService();
useEffect(() => {
return historyRef.current.listen(({ pathname }) => {
services.navLink.updatePath(pathname);
updatePath(pathname);
});
});

View file

@ -9,6 +9,7 @@ export * from './legacy';
import { PluginServices } from '../../../../../src/plugins/presentation_util/public';
import { CanvasWorkpadService } from './workpad';
import { CanvasNavLinkService } from './nav_link';
import { CanvasNotifyService } from './notify';
import { CanvasPlatformService } from './platform';
@ -16,10 +17,12 @@ export interface CanvasPluginServices {
workpad: CanvasWorkpadService;
notify: CanvasNotifyService;
platform: CanvasPlatformService;
navLink: CanvasNavLinkService;
}
export const pluginServices = new PluginServices<CanvasPluginServices>();
export const useWorkpadService = () => (() => pluginServices.getHooks().workpad.useService())();
export const useNavLinkService = () => (() => pluginServices.getHooks().navLink.useService())();
export const useNotifyService = () => (() => pluginServices.getHooks().notify.useService())();
export const usePlatformService = () => (() => pluginServices.getHooks().platform.useService())();

View file

@ -13,6 +13,7 @@ import {
} from '../../../../../../src/plugins/presentation_util/public';
import { workpadServiceFactory } from './workpad';
import { navLinkServiceFactory } from './nav_link';
import { notifyServiceFactory } from './notify';
import { platformServiceFactory } from './platform';
import { CanvasPluginServices } from '..';
@ -27,6 +28,7 @@ export const pluginServiceProviders: PluginServiceProviders<
KibanaPluginServiceParams<CanvasStartDeps>
> = {
workpad: new PluginServiceProvider(workpadServiceFactory),
navLink: new PluginServiceProvider(navLinkServiceFactory),
notify: new PluginServiceProvider(notifyServiceFactory),
platform: new PluginServiceProvider(platformServiceFactory),
};

View file

@ -0,0 +1,28 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { KibanaPluginServiceFactory } from '../../../../../../src/plugins/presentation_util/public';
import { SESSIONSTORAGE_LASTPATH } from '../../../common/lib/constants';
import { getSessionStorage } from '../../lib/storage';
import { CanvasStartDeps } from '../../plugin';
import { CanvasNavLinkService } from '../nav_link';
export type CanvasNavLinkServiceFactory = KibanaPluginServiceFactory<
CanvasNavLinkService,
CanvasStartDeps
>;
export const navLinkServiceFactory: CanvasNavLinkServiceFactory = ({ coreStart, appUpdater }) => ({
updatePath: (path: string) => {
appUpdater?.next(() => ({
defaultPath: `#${path}`,
}));
getSessionStorage().set(`${SESSIONSTORAGE_LASTPATH}:${coreStart.http.basePath.get()}`, path);
},
});

View file

@ -22,7 +22,6 @@ export interface WithServicesProps {
const defaultContextValue = {
embeddables: {},
expressions: {},
navLink: {},
search: {},
};
@ -31,7 +30,6 @@ export const ServicesContext = createContext<CanvasServices>(defaultContextValue
export const useServices = () => useContext(ServicesContext);
export const useEmbeddablesService = () => useServices().embeddables;
export const useExpressionsService = () => useServices().expressions;
export const useNavLinkService = () => useServices().navLink;
export const useLabsService = () => useServices().labs;
export const useReportingService = () => useServices().reporting;
@ -49,7 +47,6 @@ export const LegacyServicesProvider: FC<{
const value = {
embeddables: specifiedProviders.embeddables.getService(),
expressions: specifiedProviders.expressions.getService(),
navLink: specifiedProviders.navLink.getService(),
search: specifiedProviders.search.getService(),
reporting: specifiedProviders.reporting.getService(),
labs: specifiedProviders.labs.getService(),

View file

@ -8,7 +8,6 @@
import { BehaviorSubject } from 'rxjs';
import { CoreSetup, CoreStart, AppUpdater } from '../../../../../../src/core/public';
import { CanvasSetupDeps, CanvasStartDeps } from '../../plugin';
import { navLinkServiceFactory } from './nav_link';
import { embeddablesServiceFactory } from './embeddables';
import { expressionsServiceFactory } from './expressions';
import { searchServiceFactory } from './search';
@ -16,7 +15,6 @@ import { labsServiceFactory } from './labs';
import { reportingServiceFactory } from './reporting';
export { SearchService } from './search';
export { NavLinkService } from './nav_link';
export { EmbeddablesService } from './embeddables';
export { ExpressionsService } from '../../../../../../src/plugins/expressions/common';
export * from './context';
@ -75,7 +73,6 @@ export type ServiceFromProvider<P> = P extends CanvasServiceProvider<infer T> ?
export const services = {
embeddables: new CanvasServiceProvider(embeddablesServiceFactory),
expressions: new CanvasServiceProvider(expressionsServiceFactory),
navLink: new CanvasServiceProvider(navLinkServiceFactory),
search: new CanvasServiceProvider(searchServiceFactory),
reporting: new CanvasServiceProvider(reportingServiceFactory),
labs: new CanvasServiceProvider(labsServiceFactory),
@ -86,7 +83,6 @@ export type CanvasServiceProviders = typeof services;
export interface CanvasServices {
embeddables: ServiceFromProvider<typeof services.embeddables>;
expressions: ServiceFromProvider<typeof services.expressions>;
navLink: ServiceFromProvider<typeof services.navLink>;
search: ServiceFromProvider<typeof services.search>;
reporting: ServiceFromProvider<typeof services.reporting>;
labs: ServiceFromProvider<typeof services.labs>;
@ -112,7 +108,6 @@ export const stopServices = () => {
export const {
embeddables: embeddableService,
navLink: navLinkService,
expressions: expressionsService,
search: searchService,
reporting: reportingService,

View file

@ -1,32 +0,0 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { CanvasServiceFactory } from '.';
import { SESSIONSTORAGE_LASTPATH } from '../../../common/lib/constants';
import { getSessionStorage } from '../../lib/storage';
export interface NavLinkService {
updatePath: (path: string) => void;
}
export const navLinkServiceFactory: CanvasServiceFactory<NavLinkService> = (
coreSetup,
_coreStart,
_setupPlugins,
_startPlugins,
appUpdater
) => {
return {
updatePath: (path: string) => {
appUpdater.next(() => ({
defaultPath: `#${path}`,
}));
getSessionStorage().set(`${SESSIONSTORAGE_LASTPATH}:${coreSetup.http.basePath.get()}`, path);
},
};
};

View file

@ -9,7 +9,6 @@ import { CanvasServices, services } from '../';
import { embeddablesService } from './embeddables';
import { expressionsService } from './expressions';
import { reportingService } from './reporting';
import { navLinkService } from './nav_link';
import { labsService } from './labs';
import { searchService } from './search';
@ -17,7 +16,6 @@ export const stubs: CanvasServices = {
embeddables: embeddablesService,
expressions: expressionsService,
reporting: reportingService,
navLink: navLinkService,
search: searchService,
labs: labsService,
};

View file

@ -5,10 +5,6 @@
* 2.0.
*/
import { NavLinkService } from '../nav_link';
const noop = (..._args: any[]): any => {};
export const navLinkService: NavLinkService = {
updatePath: noop,
};
export interface CanvasNavLinkService {
updatePath: (path: string) => void;
}

View file

@ -15,15 +15,18 @@ import {
import { CanvasPluginServices } from '..';
import { workpadServiceFactory } from './workpad';
import { navLinkServiceFactory } from './nav_link';
import { notifyServiceFactory } from './notify';
import { platformServiceFactory } from './platform';
export { workpadServiceFactory } from './workpad';
export { navLinkServiceFactory } from './nav_link';
export { notifyServiceFactory } from './notify';
export { platformServiceFactory } from './platform';
export const pluginServiceProviders: PluginServiceProviders<CanvasPluginServices> = {
workpad: new PluginServiceProvider(workpadServiceFactory),
navLink: new PluginServiceProvider(navLinkServiceFactory),
notify: new PluginServiceProvider(notifyServiceFactory),
platform: new PluginServiceProvider(platformServiceFactory),
};

View file

@ -0,0 +1,17 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { PluginServiceFactory } from '../../../../../../src/plugins/presentation_util/public';
import { CanvasNavLinkService } from '../nav_link';
type CanvasNavLinkServiceFactory = PluginServiceFactory<CanvasNavLinkService>;
const noop = (..._args: any[]): any => {};
export const navLinkServiceFactory: CanvasNavLinkServiceFactory = () => ({
updatePath: noop,
});