Skip to content

Commit

Permalink
[canvas] Create Nav Link service; remove legacy service (#107346)
Browse files Browse the repository at this point in the history
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
clintandrewhall and kibanamachine authored Aug 2, 2021
1 parent 3bd9ff3 commit 2d17071
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 52 deletions.
6 changes: 3 additions & 3 deletions x-pack/plugins/canvas/public/components/app/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
});
});

Expand Down
3 changes: 3 additions & 0 deletions x-pack/plugins/canvas/public/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@ 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';

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())();
2 changes: 2 additions & 0 deletions x-pack/plugins/canvas/public/services/kibana/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 '..';
Expand All @@ -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),
};
Expand Down
28 changes: 28 additions & 0 deletions x-pack/plugins/canvas/public/services/kibana/nav_link.ts
Original file line number Diff line number Diff line change
@@ -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);
},
});
3 changes: 0 additions & 3 deletions x-pack/plugins/canvas/public/services/legacy/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export interface WithServicesProps {
const defaultContextValue = {
embeddables: {},
expressions: {},
navLink: {},
search: {},
};

Expand All @@ -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;

Expand All @@ -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(),
Expand Down
5 changes: 0 additions & 5 deletions x-pack/plugins/canvas/public/services/legacy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@
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';
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';
Expand Down Expand Up @@ -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),
Expand All @@ -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>;
Expand All @@ -112,7 +108,6 @@ export const stopServices = () => {

export const {
embeddables: embeddableService,
navLink: navLinkService,
expressions: expressionsService,
search: searchService,
reporting: reportingService,
Expand Down
32 changes: 0 additions & 32 deletions x-pack/plugins/canvas/public/services/legacy/nav_link.ts

This file was deleted.

2 changes: 0 additions & 2 deletions x-pack/plugins/canvas/public/services/legacy/stubs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ 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';

export const stubs: CanvasServices = {
embeddables: embeddablesService,
expressions: expressionsService,
reporting: reportingService,
navLink: navLinkService,
search: searchService,
labs: labsService,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
3 changes: 3 additions & 0 deletions x-pack/plugins/canvas/public/services/stubs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};
Expand Down
17 changes: 17 additions & 0 deletions x-pack/plugins/canvas/public/services/stubs/nav_link.ts
Original file line number Diff line number Diff line change
@@ -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,
});

0 comments on commit 2d17071

Please sign in to comment.