[labs] Fix reset overreach; add method for testing project (#99672)
This commit is contained in:
parent
59f42ec148
commit
e0c57e08fd
|
@ -8,8 +8,9 @@
|
|||
|
||||
import { i18n } from '@kbn/i18n';
|
||||
|
||||
export const USE_DATA_SERVICE = 'labs:canvas:useDataService';
|
||||
export const TIME_TO_PRESENT = 'labs:presentation:timeToPresent';
|
||||
export const LABS_PROJECT_PREFIX = 'labs:';
|
||||
export const USE_DATA_SERVICE = `${LABS_PROJECT_PREFIX}canvas:useDataService` as const;
|
||||
export const TIME_TO_PRESENT = `${LABS_PROJECT_PREFIX}presentation:timeToPresent` as const;
|
||||
|
||||
export const projectIDs = [TIME_TO_PRESENT, USE_DATA_SERVICE] as const;
|
||||
export const environmentNames = ['kibana', 'browser', 'session'] as const;
|
||||
|
|
|
@ -7,7 +7,6 @@
|
|||
*/
|
||||
|
||||
import {
|
||||
environmentNames,
|
||||
EnvironmentName,
|
||||
projectIDs,
|
||||
projects,
|
||||
|
@ -15,6 +14,7 @@ import {
|
|||
Project,
|
||||
getProjectIDs,
|
||||
SolutionName,
|
||||
LABS_PROJECT_PREFIX,
|
||||
} from '../../../common';
|
||||
import { PresentationUtilPluginStartDeps } from '../../types';
|
||||
import { KibanaPluginServiceFactory } from '../create';
|
||||
|
@ -31,6 +31,16 @@ export type LabsServiceFactory = KibanaPluginServiceFactory<
|
|||
PresentationUtilPluginStartDeps
|
||||
>;
|
||||
|
||||
const clearLabsFromStorage = (storage: Storage) => {
|
||||
projectIDs.forEach((projectID) => storage.removeItem(projectID));
|
||||
|
||||
// This is a redundancy, to catch any labs that may have been removed above.
|
||||
// We could consider gathering telemetry to see how often this happens, or this may be unnecessary.
|
||||
Object.keys(storage)
|
||||
.filter((key) => key.startsWith(LABS_PROJECT_PREFIX))
|
||||
.forEach((key) => storage.removeItem(key));
|
||||
};
|
||||
|
||||
export const labsServiceFactory: LabsServiceFactory = ({ coreStart }) => {
|
||||
const { uiSettings } = coreStart;
|
||||
const localStorage = window.localStorage;
|
||||
|
@ -75,17 +85,18 @@ export const labsServiceFactory: LabsServiceFactory = ({ coreStart }) => {
|
|||
};
|
||||
|
||||
const reset = () => {
|
||||
localStorage.clear();
|
||||
sessionStorage.clear();
|
||||
environmentNames.forEach((env) =>
|
||||
projectIDs.forEach((id) => setProjectStatus(id, env, projects[id].isActive))
|
||||
);
|
||||
clearLabsFromStorage(localStorage);
|
||||
clearLabsFromStorage(sessionStorage);
|
||||
projectIDs.forEach((id) => setProjectStatus(id, 'kibana', projects[id].isActive));
|
||||
};
|
||||
|
||||
const isProjectEnabled = (id: ProjectID) => getProject(id).status.isEnabled;
|
||||
|
||||
return {
|
||||
getProjectIDs,
|
||||
getProjects,
|
||||
getProject,
|
||||
isProjectEnabled,
|
||||
reset,
|
||||
setProjectStatus,
|
||||
};
|
||||
|
|
|
@ -20,6 +20,7 @@ import {
|
|||
} from '../../common';
|
||||
|
||||
export interface PresentationLabsService {
|
||||
isProjectEnabled: (id: ProjectID) => boolean;
|
||||
getProjectIDs: () => typeof projectIDs;
|
||||
getProject: (id: ProjectID) => Project;
|
||||
getProjects: (solutions?: SolutionName[]) => Record<ProjectID, Project>;
|
||||
|
|
|
@ -46,13 +46,17 @@ export const labsServiceFactory: LabsServiceFactory = () => {
|
|||
};
|
||||
|
||||
const reset = () => {
|
||||
// This is normally not ok, but it's our isolated Storybook instance.
|
||||
storage.clear();
|
||||
};
|
||||
|
||||
const isProjectEnabled = (id: ProjectID) => getProject(id).status.isEnabled;
|
||||
|
||||
return {
|
||||
getProjectIDs,
|
||||
getProjects,
|
||||
getProject,
|
||||
isProjectEnabled,
|
||||
reset,
|
||||
setProjectStatus,
|
||||
};
|
||||
|
|
|
@ -64,11 +64,13 @@ export const labsServiceFactory: LabsServiceFactory = () => {
|
|||
const setProjectStatus = (id: ProjectID, env: EnvironmentName, value: boolean) => {
|
||||
statuses[id] = { ...statuses[id], [env]: value };
|
||||
};
|
||||
const isProjectEnabled = (id: ProjectID) => getProject(id).status.isEnabled;
|
||||
|
||||
return {
|
||||
getProjectIDs,
|
||||
getProject,
|
||||
getProjects,
|
||||
isProjectEnabled,
|
||||
setProjectStatus,
|
||||
reset: () => {
|
||||
statuses = reset();
|
||||
|
|
|
@ -25,8 +25,7 @@ export const expressionsServiceFactory: CanvasServiceFactory<ExpressionsService>
|
|||
if (!cached) {
|
||||
cached = (async () => {
|
||||
const labService = startPlugins.presentationUtil.labsService;
|
||||
const useDataSearchProject = labService.getProject('labs:canvas:useDataService');
|
||||
const hasDataSearch = useDataSearchProject.status.isEnabled;
|
||||
const hasDataSearch = labService.isProjectEnabled('labs:canvas:useDataService');
|
||||
const dataSearchFns = ['essql', 'esdocs', 'escount'];
|
||||
|
||||
const serverFunctionList = await coreSetup.http.get(API_ROUTE_FUNCTIONS);
|
||||
|
|
|
@ -14,6 +14,7 @@ export const labsService: CanvasLabsService = {
|
|||
getProject: noop,
|
||||
getProjects: noop,
|
||||
getProjectIDs: () => projectIDs,
|
||||
isProjectEnabled: () => false,
|
||||
isLabsEnabled: () => true,
|
||||
projectIDs,
|
||||
reset: noop,
|
||||
|
|
Loading…
Reference in a new issue