Skip to content

Commit

Permalink
remove redux tools from presentation_utils plugin (elastic#197891)
Browse files Browse the repository at this point in the history
Co-authored-by: kibanamachine <[email protected]>
Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
3 people authored and CAWilson94 committed Dec 12, 2024
1 parent f9a6be1 commit e7ed964
Show file tree
Hide file tree
Showing 17 changed files with 59 additions and 694 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ import {
connectToQueryState,
syncGlobalQueryStateWithUrl,
} from '@kbn/data-plugin/public';
import { cleanFiltersForSerialize } from '@kbn/presentation-util-plugin/public';
import moment, { Moment } from 'moment';
import { cleanFiltersForSerialize } from '../utils/clean_filters_for_serialize';
import { dataService } from '../services/kibana_services';
import { DashboardCreationOptions, DashboardState } from './types';
import { DEFAULT_DASHBOARD_INPUT, GLOBAL_STATE_STORAGE_KEY } from '../dashboard_constants';
Expand Down
6 changes: 0 additions & 6 deletions src/plugins/dashboard/public/dashboard_container/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/

import type { ContainerOutput } from '@kbn/embeddable-plugin/public';
import type { ReduxEmbeddableState } from '@kbn/presentation-util-plugin/public';
import { SerializableRecord } from '@kbn/utility-types';

import { ControlGroupRuntimeState } from '@kbn/controls-plugin/public';
Expand All @@ -19,11 +18,6 @@ export interface UnsavedPanelState {
[key: string]: object | undefined;
}

export type DashboardReduxState = ReduxEmbeddableState<
DashboardContainerInput,
DashboardContainerOutput
>;

export type DashboardRedirect = (props: RedirectToProps) => void;
export type RedirectToProps =
| { destination: 'dashboard'; id?: string; useReplace?: boolean; editMode?: boolean }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import { injectSearchSourceReferences } from '@kbn/data-plugin/public';
import { ViewMode } from '@kbn/embeddable-plugin/public';
import { Filter, Query } from '@kbn/es-query';
import { SavedObjectNotFound } from '@kbn/kibana-utils-plugin/public';
import { cleanFiltersForSerialize } from '@kbn/presentation-util-plugin/public';

import { cleanFiltersForSerialize } from '../../../utils/clean_filters_for_serialize';
import { getDashboardContentManagementCache } from '..';
import { convertPanelsArrayToPanelMap, injectReferences } from '../../../../common';
import type { DashboardGetIn, DashboardGetOut } from '../../../../server/content_management';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { Filter } from '@kbn/es-query';
import { cleanFiltersForSerialize } from './clean_filters_for_serialize';

describe('cleanFiltersForSerialize', () => {
test('should return an empty array if filters is not provided', () => {
expect(cleanFiltersForSerialize()).toEqual([]);
});

test('should remove "meta.value" property from each filter', () => {
const filters: Filter[] = [
{ query: { a: 'a' }, meta: { value: 'value1' } },
{ query: { b: 'b' }, meta: { value: 'value2' } },
];

const cleanedFilters = cleanFiltersForSerialize(filters);

expect(cleanedFilters[0]).toEqual({ query: { a: 'a' }, meta: {} });
expect(cleanedFilters[1]).toEqual({ query: { b: 'b' }, meta: {} });
});

test('should not fail if meta is missing from filters', () => {
const filters: Filter[] = [{ query: { a: 'a' } }, { query: { b: 'b' } }] as unknown as Filter[];

const cleanedFilters = cleanFiltersForSerialize(filters as unknown as Filter[]);

expect(cleanedFilters[0]).toEqual({ query: { a: 'a' } });
expect(cleanedFilters[1]).toEqual({ query: { b: 'b' } });
});
});
18 changes: 18 additions & 0 deletions src/plugins/dashboard/public/utils/clean_filters_for_serialize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { Filter } from '@kbn/es-query';

export function cleanFiltersForSerialize(filters?: Filter[]): Filter[] {
if (!filters) return [];
return filters.map((filter) => {
if (filter.meta?.value) delete filter.meta.value;
return filter;
});
}
47 changes: 0 additions & 47 deletions src/plugins/presentation_util/README.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,4 @@ tags: ['kibana', 'presentation', 'services']
related: []
---

## Introduction

The Presentation Utility Plugin is a set of common, shared components and toolkits for solutions within the Presentation space, (e.g. Dashboards, Canvas).

## Redux Embeddables

The Redux Embeddables system allows embeddable authors to interact with their embeddables in a standardized way using Redux toolkit. This wrapper abstracts away store and slice creation, and embeddable input sync. To use this system, a developer can use CreateReduxEmbeddableTools in the constructor of their embeddable, supplying a collection of reducers.

### Reducers

The reducer object expected by the ReduxEmbeddableWrapper is the same type as the reducers expected by [Redux Toolkit's CreateSlice](https://redux-toolkit.js.org/api/createslice).

<DocAccordion buttonContent="Reducers Example" initialIsOpen>
```ts
// my_embeddable_reducers.ts
import { MyEmbeddableInput } from './my_embeddable';

export const myEmbeddableReducers = {
setSpecialBoolean: (
state: WritableDraft<MyEmbeddableInput>,
action: PayloadAction<MyEmbeddableInput['specialBoolean']>
) => {
state.specialBoolean = action.payload;
}
}

```
</DocAccordion>

### Accessing Actions and State

From components under the embeddable, actions, containerActions, and the current state of the redux store are accessed via the embeddable instance. You can pass the embeddable instance down as a prop, or use a context.

<DocAccordion buttonContent="Accessing Redux Embeddable Context" initialIsOpen>
```ts
// my_embeddable_component.tsx
const MyEmbeddableComponent = ({ embeddableInstance }: { embeddableInstance: IEmbeddable }) => {
// current state
const specialBoolean = embeddableInstance.select((state) => state.specialBoolean);

// change specialBoolean after 5 seconds
setTimeout(() => embeddableInstance.dispatch.setSpecialBoolean(false), 5000);

}

```
</DocAccordion>
```
9 changes: 0 additions & 9 deletions src/plugins/presentation_util/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,6 @@ export {
DEFAULT_DASHBOARD_DRILLDOWN_OPTIONS,
} from './components';

export {
lazyLoadReduxToolsPackage,
cleanFiltersForSerialize,
type ReduxEmbeddableState,
type ReduxEmbeddableTools,
type ReduxTools,
type ReduxToolsPackage,
} from './redux_tools';

export type {
ExpressionInputEditorRef,
ExpressionInputProps,
Expand Down
12 changes: 1 addition & 11 deletions src/plugins/presentation_util/public/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
*/

import { PresentationUtilPluginStart } from './types';
import { ReduxToolsPackage, registerExpressionsLanguage } from '.';
import { createReduxEmbeddableTools } from './redux_tools/redux_embeddables/create_redux_embeddable_tools';
import { createReduxTools } from './redux_tools/create_redux_tools';
import { registerExpressionsLanguage } from '.';
import { setStubKibanaServices } from './services/mocks';

const createStartContract = (): PresentationUtilPluginStart => {
Expand All @@ -31,14 +29,6 @@ export const presentationUtilPluginMock = {
createStartContract,
};

/**
* A non async-imported version of the real redux embeddable tools package for mocking purposes.
*/
export const mockedReduxEmbeddablePackage: ReduxToolsPackage = {
createReduxEmbeddableTools,
createReduxTools,
};

export * from './__stories__/fixtures/flights';
export const setMockedPresentationUtilServices = () => {
setStubKibanaServices();
Expand Down

This file was deleted.

25 changes: 0 additions & 25 deletions src/plugins/presentation_util/public/redux_tools/index.ts

This file was deleted.

Loading

0 comments on commit e7ed964

Please sign in to comment.