Skip to content

Commit

Permalink
feat: enable data source info in new left navigation
Browse files Browse the repository at this point in the history
Signed-off-by: SuZhou-Joe <[email protected]>
  • Loading branch information
SuZhou-Joe committed Jul 5, 2024
1 parent 967dbf5 commit d686d75
Show file tree
Hide file tree
Showing 4 changed files with 41 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
3 changes: 2 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,7 @@ 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 +44,7 @@ export const SecurityPluginTopNavMenu = React.memo(
const wrapSetDataSourceWithUpdateUrl = (dataSources: DataSourceOption[]) => {
setDataSourceInUrl(dataSources[0]);
setDataSource(dataSources[0]);
setDataSourceInSubscription(dataSources[0]);
};

return dataSourceEnabled ? (
Expand Down
23 changes: 23 additions & 0 deletions public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ 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, setDataSourceInUrl } from './utils/datasource-utils';

async function hasApiPermission(core: CoreSetup): Promise<boolean | undefined> {
try {
Expand Down Expand Up @@ -90,6 +91,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 +171,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 +181,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 +191,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 +201,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 +211,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 +221,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 +231,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 +280,12 @@ export class SecurityPlugin
}),
});
}

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

core.application.register({
Expand Down
17 changes: 15 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,20 @@ 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 d686d75

Please sign in to comment.