Skip to content

Commit

Permalink
Merge pull request #3 from SuZhou-Joe/fix/mds-in-new-navigation
Browse files Browse the repository at this point in the history
feat: enable data source info in new left navigation
  • Loading branch information
derek-ho authored Jul 8, 2024
2 parents 967dbf5 + a7a70d1 commit 9bfefef
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
4 changes: 1 addition & 3 deletions public/apps/configuration/app-router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import { Action, RouteItem, SubAction } from './types';
import { ResourceType } from '../../../common';
import { buildHashUrl, buildUrl } from './utils/url-builder';
import { CrossPageToast } from './cross-page-toast';
import { getDataSourceFromUrl } from '../../utils/datasource-utils';
import { getDataSourceFromUrl, LocalCluster } from '../../utils/datasource-utils';

const LANDING_PAGE_URL = '/getstarted';

Expand Down Expand Up @@ -151,8 +151,6 @@ export interface DataSourceContextType {
setDataSource: React.Dispatch<React.SetStateAction<DataSourceOption>>;
}

export const LocalCluster = { label: 'Local cluster', id: '' };

export const DataSourceContext = createContext<DataSourceContextType | null>(null);

export function AppRouter(props: AppDependencies) {
Expand Down
6 changes: 5 additions & 1 deletion public/apps/configuration/top-nav-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import React from 'react';
import { DataSourceSelectableConfig } from 'src/plugins/data_source_management/public';
import { DataSourceOption } from 'src/plugins/data_source_management/public/components/data_source_menu/types';
import { AppDependencies } from '../types';
import { setDataSourceInUrl } from '../../utils/datasource-utils';
import {
setDataSourceInUrl,
setDataSource as setDataSourceInSubscription,
} from '../../utils/datasource-utils';

export interface TopNavMenuProps extends AppDependencies {
dataSourcePickerReadOnly: boolean;
Expand All @@ -44,6 +47,7 @@ export const SecurityPluginTopNavMenu = React.memo(
const wrapSetDataSourceWithUpdateUrl = (dataSources: DataSourceOption[]) => {
setDataSourceInUrl(dataSources[0]);
setDataSource(dataSources[0]);
setDataSourceInSubscription(dataSources[0]);
};

return dataSourceEnabled ? (
Expand Down
27 changes: 27 additions & 0 deletions public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ import { addTenantToShareURL } from './services/shared-link';
import { interceptError } from './utils/logout-utils';
import { tenantColumn, getNamespacesToRegister } from './apps/configuration/utils/tenant-utils';
import { getDashboardsInfoSafe } from './utils/dashboards-info-utils';
import {
dataSource$,
getDataSourceEnabledUrl,
getDataSourceFromUrl,
} from './utils/datasource-utils';

async function hasApiPermission(core: CoreSetup): Promise<boolean | undefined> {
try {
Expand Down Expand Up @@ -90,6 +95,15 @@ export class SecurityPlugin
// @ts-ignore : initializerContext not used
constructor(private readonly initializerContext: PluginInitializerContext) {}

private updateDefaultRouteOfSecurityApplications: AppUpdater = () => {
const url = getDataSourceEnabledUrl(getDataSourceFromUrl());
return {
defaultPath: `?${url.searchParams.toString()}`,
};
};

private appStateUpdater = new BehaviorSubject(this.updateDefaultRouteOfSecurityApplications);

public async setup(
core: CoreSetup,
deps: SecurityPluginSetupDependencies
Expand Down Expand Up @@ -161,6 +175,7 @@ export class SecurityPlugin
title: 'Get Started',
order: 8040,
workspaceAvailability: WorkspaceAvailability.outsideWorkspace,
updater$: this.appStateUpdater,
mount: async (params: AppMountParameters) => {
return mountWrapper(params, '/getstarted');
},
Expand All @@ -170,6 +185,7 @@ export class SecurityPlugin
title: 'Authentication',
order: 8040,
workspaceAvailability: WorkspaceAvailability.outsideWorkspace,
updater$: this.appStateUpdater,
mount: async (params: AppMountParameters) => {
return mountWrapper(params, '/auth');
},
Expand All @@ -179,6 +195,7 @@ export class SecurityPlugin
title: 'Roles',
order: 8040,
workspaceAvailability: WorkspaceAvailability.outsideWorkspace,
updater$: this.appStateUpdater,
mount: async (params: AppMountParameters) => {
return mountWrapper(params, '/roles');
},
Expand All @@ -188,6 +205,7 @@ export class SecurityPlugin
title: 'Internal users',
order: 8040,
workspaceAvailability: WorkspaceAvailability.outsideWorkspace,
updater$: this.appStateUpdater,
mount: async (params: AppMountParameters) => {
return mountWrapper(params, '/users');
},
Expand All @@ -197,6 +215,7 @@ export class SecurityPlugin
title: 'Permissions',
order: 8040,
workspaceAvailability: WorkspaceAvailability.outsideWorkspace,
updater$: this.appStateUpdater,
mount: async (params: AppMountParameters) => {
return mountWrapper(params, '/permissions');
},
Expand All @@ -206,6 +225,7 @@ export class SecurityPlugin
title: 'Tenants',
order: 8040,
workspaceAvailability: WorkspaceAvailability.outsideWorkspace,
updater$: this.appStateUpdater,
mount: async (params: AppMountParameters) => {
return mountWrapper(params, '/tenants');
},
Expand All @@ -215,6 +235,7 @@ export class SecurityPlugin
title: 'Audit logs',
order: 8040,
workspaceAvailability: WorkspaceAvailability.outsideWorkspace,
updater$: this.appStateUpdater,
mount: async (params: AppMountParameters) => {
return mountWrapper(params, '/auditLogging');
},
Expand Down Expand Up @@ -263,6 +284,12 @@ export class SecurityPlugin
}),
});
}

dataSource$.subscribe((dataSourceOption) => {
if (dataSourceOption) {
this.appStateUpdater.next(this.updateDefaultRouteOfSecurityApplications);
}
});
}

core.application.register({
Expand Down
19 changes: 17 additions & 2 deletions public/utils/datasource-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* permissions and limitations under the License.
*/

import { BehaviorSubject } from 'rxjs';
import { DataSourceOption } from 'src/plugins/data_source_management/public/components/data_source_menu/types';

const DATASOURCEURLKEY = 'dataSource';
Expand All @@ -35,8 +36,22 @@ export function getDataSourceFromUrl(): DataSourceOption {
}
}

export function setDataSourceInUrl(dataSource: DataSourceOption) {
export function getDataSourceEnabledUrl(dataSource: DataSourceOption) {
const url = new URL(window.location.href);
url.searchParams.set(DATASOURCEURLKEY, JSON.stringify(dataSource));
window.history.replaceState({}, '', url.toString());
return url;
}

export function setDataSourceInUrl(dataSource: DataSourceOption) {
window.history.replaceState({}, '', getDataSourceEnabledUrl(dataSource).toString());
}

export const LocalCluster = { label: 'Local cluster', id: '' };

export const dataSource$ = new BehaviorSubject<DataSourceOption>(
getDataSourceFromUrl() || LocalCluster
);

export function setDataSource(dataSource: DataSourceOption) {
dataSource$.next(dataSource);
}

0 comments on commit 9bfefef

Please sign in to comment.