Skip to content

Commit

Permalink
Translate dashboard state to URL conditionally
Browse files Browse the repository at this point in the history
  • Loading branch information
Heenawter committed Feb 7, 2022
1 parent 3302843 commit 550ce12
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/plugins/dashboard/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ export type {

export type { DashboardUrlGeneratorState } from './url_generator';
export { DASHBOARD_APP_URL_GENERATOR, createDashboardUrlGenerator } from './url_generator';
export type { DashboardAppLocator, DashboardAppLocatorParams } from './locator';
export {
type DashboardAppLocator,
type DashboardAppLocatorParams,
cleanEmptyKeys,
} from './locator';

export type { DashboardSavedObject } from './saved_dashboards';
export type { SavedDashboardPanel, DashboardContainerInput } from './types';
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/dashboard/public/locator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { DashboardConstants } from './dashboard_constants';
*/
const getSerializableRecord: <O>(o: O) => O & SerializableRecord = flow(JSON.stringify, JSON.parse);

const cleanEmptyKeys = (stateObj: Record<string, unknown>) => {
export const cleanEmptyKeys = (stateObj: Record<string, unknown>) => {
Object.keys(stateObj).forEach((key) => {
if (stateObj[key] === undefined) {
delete stateObj[key];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ export abstract class AbstractDashboardDrilldown<Context extends object = object

public abstract readonly supportedTriggers: () => string[];

protected abstract getLocation(config: Config, context: Context): Promise<KibanaLocation>;
protected abstract getLocation(
config: Config,
context: Context,
useUrl: boolean
): Promise<KibanaLocation>;

public readonly order = 100;

Expand Down Expand Up @@ -73,7 +77,7 @@ export abstract class AbstractDashboardDrilldown<Context extends object = object
};

public readonly getHref = async (config: Config, context: Context): Promise<string> => {
const { app, path } = await this.getLocation(config, context);
const { app, path } = await this.getLocation(config, context, true);
const url = await this.params.start().core.application.getUrlForApp(app, {
path,
absolute: true,
Expand All @@ -82,7 +86,7 @@ export abstract class AbstractDashboardDrilldown<Context extends object = object
};

public readonly execute = async (config: Config, context: Context) => {
const { app, path, state } = await this.getLocation(config, context);
const { app, path, state } = await this.getLocation(config, context, false);
await this.params.start().core.application.navigateToApp(app, {
path,
state,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
*/

import type { KibanaLocation } from 'src/plugins/share/public';
import { DashboardAppLocatorParams } from '../../../../../../../src/plugins/dashboard/public';
import {
DashboardAppLocatorParams,
cleanEmptyKeys,
} from '../../../../../../../src/plugins/dashboard/public';
import { setStateToKbnUrl } from '../../../../../../../src/plugins/kibana_utils/public';
import {
ApplyGlobalFilterActionContext,
APPLY_FILTER_TRIGGER,
Expand Down Expand Up @@ -49,7 +53,11 @@ export class EmbeddableToDashboardDrilldown extends AbstractDashboardDrilldown<C

public readonly supportedTriggers = () => [APPLY_FILTER_TRIGGER];

protected async getLocation(config: Config, context: Context): Promise<KibanaLocation> {
protected async getLocation(
config: Config,
context: Context,
useUrl: boolean
): Promise<KibanaLocation> {
const params: DashboardAppLocatorParams = {
dashboardId: config.dashboardId,
};
Expand Down Expand Up @@ -85,10 +93,27 @@ export class EmbeddableToDashboardDrilldown extends AbstractDashboardDrilldown<C
}

const location = await this.locator.getLocation(params);
if (useUrl) {
this.useUrlForState(location);
}

return location;
}

private useUrlForState(location: KibanaLocation<DashboardAppLocatorParams>) {
const state = location.state;
location.path = setStateToKbnUrl(
'_a',
cleanEmptyKeys({
query: state.query,
filters: state.filters?.filter((f) => !esFilters.isFilterPinned(f)),
savedQuery: state.savedQuery,
}),
{ useHash: false, storeInHashQuery: true },
location.path
);
}

public readonly inject = createInject({ drilldownId: this.id });

public readonly extract = createExtract({ drilldownId: this.id });
Expand Down

0 comments on commit 550ce12

Please sign in to comment.