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

feat: optimize code for stateful url #40

Merged
merged 1 commit into from
Jul 11, 2023
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
16 changes: 4 additions & 12 deletions src/core/public/http/base_path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,12 @@ export class BasePath {
return this.basePath;
};

public prepend = (path: string): string => {
if (!this.get()) return path;
public prepend = (path: string, withoutWorkspace: boolean = false): string => {
const basePath = withoutWorkspace ? this.basePath : this.get();
if (!basePath) return path;
return modifyUrl(path, (parts) => {
if (!parts.hostname && parts.pathname && parts.pathname.startsWith('/')) {
parts.pathname = `${this.get()}${parts.pathname}`;
}
});
};

public prependWithoutWorkspacePath = (path: string): string => {
if (!this.basePath) return path;
return modifyUrl(path, (parts) => {
if (!parts.hostname && parts.pathname && parts.pathname.startsWith('/')) {
parts.pathname = `${this.basePath}${parts.pathname}`;
parts.pathname = `${basePath}${parts.pathname}`;
}
});
};
Expand Down
8 changes: 7 additions & 1 deletion src/core/public/http/http_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { AnonymousPathsService } from './anonymous_paths_service';
import { LoadingCountService } from './loading_count_service';
import { Fetch } from './fetch';
import { CoreService } from '../../types';
import { getWorkspaceIdFromUrl } from '../utils';

interface HttpDeps {
injectedMetadata: InjectedMetadataSetup;
Expand All @@ -50,10 +51,15 @@ export class HttpService implements CoreService<HttpSetup, HttpStart> {

public setup({ injectedMetadata, fatalErrors }: HttpDeps): HttpSetup {
const opensearchDashboardsVersion = injectedMetadata.getOpenSearchDashboardsVersion();
let workspaceBasePath = '';
const workspaceId = getWorkspaceIdFromUrl(window.location.href);
if (workspaceId) {
workspaceBasePath = `/w/${workspaceId}`;
}
const basePath = new BasePath(
injectedMetadata.getBasePath(),
injectedMetadata.getServerBasePath(),
injectedMetadata.getWorkspaceBasePath()
workspaceBasePath
);
const fetchService = new Fetch({ basePath, opensearchDashboardsVersion });
const loadingCount = this.loadingCount.setup({ fatalErrors });
Expand Down
5 changes: 0 additions & 5 deletions src/core/public/http/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,6 @@ export interface IBasePath {
* See {@link BasePath.get} for getting the basePath value for a specific request
*/
readonly serverBasePath: string;

/**
* Prepends `path` with the basePath.
*/
prependWithoutWorkspacePath: (url: string) => string;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,6 @@ export class InjectedMetadataService {
getSurvey: () => {
return this.state.survey;
},

getWorkspaceBasePath: () => {
const workspaceId = getWorkspaceIdFromUrl(window.location.href);
if (workspaceId) {
return `/w/${workspaceId}`;
}

return '';
},
};
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/workspace/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import {
AppMountParameters,
AppNavLinkStatus,
} from '../../../core/public';
import { WORKSPACE_APP_ID, PATHS } from '../common/constants';
import { WORKSPACE_APP_ID } from '../common/constants';
import { mountDropdownList } from './mount';
import { getWorkspaceIdFromUrl } from '../../../core/public/utils';

export class WorkspacesPlugin implements Plugin<{}, {}> {
private core?: CoreSetup;
private getWorkpsaceIdFromURL(): string | null {
private getWorkspaceIdFromURL(): string | null {
return getWorkspaceIdFromUrl(window.location.href);
}
private getPatchedUrl = (url: string, workspaceId: string) => {
Expand All @@ -40,9 +40,9 @@ export class WorkspacesPlugin implements Plugin<{}, {}> {
this.core = core;
this.core?.workspaces.setFormatUrlWithWorkspaceId((url, id) => this.getPatchedUrl(url, id));
/**
* Retrive workspace id from url
* Retrieve workspace id from url
*/
const workspaceId = this.getWorkpsaceIdFromURL();
const workspaceId = this.getWorkspaceIdFromURL();

if (workspaceId) {
const result = await core.workspaces.client.enterWorkspace(workspaceId);
Expand Down
Loading