kibana/x-pack/legacy/plugins/spaces/public/lib/spaces_manager.ts
Larry Gregory 9c2fa892ca
[7.x] Spaces - Client NP Migration, Phase 1 (#40856) (#53113)
* Spaces - Client NP Migration, Phase 1 (#40856)

* shimming NP for spaces client-side plugin

* refresh active space in nav control when updated

* fix advanced settings screen

* allow npStart from unauthed routes

* use NP for deriving space management url

* remove security's usage of SpacesManager

* remove usages of ui/capabilities

* fix tests

* implement NP plugin interface

* remove hack in favor of convention in migration guide

* shim feature catalogue registration

* streamline nav control, and handle async loading more gracefully

* adding opaqueId

* fixes from merge

* fix merge from master

* fixing merge from master

* move _active_space route to NP

* moving to the NP feature catalogue registry

* moving setup to setup phase

* optimizing active space retrieval

* reverting test isolation change

* Apply suggestions from code review

Co-Authored-By: Aleh Zasypkin <aleh.zasypkin@gmail.com>

* removing unnecessary PluginInitializerContext

* updating advanced settings subtitle

* using NP anonymousPaths service

* additional nav_control_popover cleanup

* additional cleanup

* testing out onActiveSpaceChange$ property

* make the linter happy

* make the type checker happy

* fixing types

* fix merge from master

* spaces LP init should run on all pages, not just the kibana app

* address nits

* fix infra/logs, and the spaces disabled scenario

* fix typescript errors

* revert changes to infra plugin

* reintroducing activeSpace injected var for legacy plugins

* fixing react deprecation warning and unhandled promise rejection

* restore activeSpace default var

* spaces does not need to check its own enabled status

* fix from merge

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>

* fix backport merge
2019-12-16 12:13:05 -05:00

111 lines
3.6 KiB
TypeScript

/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { Observable, BehaviorSubject } from 'rxjs';
import { skipWhile } from 'rxjs/operators';
import { HttpSetup } from 'src/core/public';
import { SavedObjectsManagementRecord } from '../../../../../../src/legacy/core_plugins/management/public';
import { Space } from '../../common/model/space';
import { GetSpacePurpose } from '../../common/model/types';
import { CopySavedObjectsToSpaceResponse } from './copy_saved_objects_to_space/types';
import { ENTER_SPACE_PATH } from '../../common/constants';
import { addSpaceIdToPath } from '../../../../../plugins/spaces/common';
export class SpacesManager {
private activeSpace$: BehaviorSubject<Space | null> = new BehaviorSubject<Space | null>(null);
public readonly onActiveSpaceChange$: Observable<Space>;
constructor(private readonly serverBasePath: string, private readonly http: HttpSetup) {
this.onActiveSpaceChange$ = this.activeSpace$
.asObservable()
.pipe(skipWhile((v: Space | null) => v == null)) as Observable<Space>;
this.refreshActiveSpace();
}
public async getSpaces(purpose?: GetSpacePurpose): Promise<Space[]> {
return await this.http.get('/api/spaces/space', { query: { purpose } });
}
public async getSpace(id: string): Promise<Space> {
return await this.http.get(`/api/spaces/space/${encodeURIComponent(id)}`);
}
public getActiveSpace({ forceRefresh = false } = {}) {
if (!forceRefresh && this.activeSpace$.value) {
return Promise.resolve(this.activeSpace$.value);
}
return this.http.get('/internal/spaces/_active_space') as Promise<Space>;
}
public async createSpace(space: Space) {
await this.http.post(`/api/spaces/space`, {
body: JSON.stringify(space),
});
}
public async updateSpace(space: Space) {
await this.http.put(`/api/spaces/space/${encodeURIComponent(space.id)}`, {
query: {
overwrite: true,
},
body: JSON.stringify(space),
});
const activeSpaceId = (await this.getActiveSpace()).id;
if (space.id === activeSpaceId) {
this.refreshActiveSpace();
}
}
public async deleteSpace(space: Space) {
await this.http.delete(`/api/spaces/space/${encodeURIComponent(space.id)}`);
}
public async copySavedObjects(
objects: Array<Pick<SavedObjectsManagementRecord, 'type' | 'id'>>,
spaces: string[],
includeReferences: boolean,
overwrite: boolean
): Promise<CopySavedObjectsToSpaceResponse> {
return this.http.post('/api/spaces/_copy_saved_objects', {
body: JSON.stringify({
objects,
spaces,
includeReferences,
overwrite,
}),
});
}
public async resolveCopySavedObjectsErrors(
objects: Array<Pick<SavedObjectsManagementRecord, 'type' | 'id'>>,
retries: unknown,
includeReferences: boolean
): Promise<CopySavedObjectsToSpaceResponse> {
return this.http.post(`/api/spaces/_resolve_copy_saved_objects_errors`, {
body: JSON.stringify({
objects,
includeReferences,
retries,
}),
});
}
public async changeSelectedSpace(space: Space) {
window.location.href = addSpaceIdToPath(this.serverBasePath, space.id, ENTER_SPACE_PATH);
}
public redirectToSpaceSelector() {
window.location.href = `${this.serverBasePath}/spaces/space_selector`;
}
private async refreshActiveSpace() {
const activeSpace = await this.getActiveSpace({ forceRefresh: true });
this.activeSpace$.next(activeSpace);
}
}