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

[Discover] Add container for internal state #144149

Merged
merged 52 commits into from
Dec 30, 2022
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
5ab45e2
Initial commit
kertal Oct 27, 2022
2557d20
[CI] Auto-commit changed files from 'node scripts/precommit_hook.js -…
kibanamachine Oct 27, 2022
67c1d23
Remove dataViews from internal state since it is not used
kertal Oct 28, 2022
838b95f
Merge branch 'discover-internal-state' of github.com:kertal/kibana in…
kertal Oct 28, 2022
d74ab22
Fix tests
kertal Oct 29, 2022
43c06f9
Fix more tests
kertal Oct 29, 2022
c2fe3f5
Merge remote-tracking branch 'upstream/main' into discover-internal-s…
kertal Nov 2, 2022
9404801
Refactor AppState type location
kertal Nov 2, 2022
5e784c3
[Discover] make adhoc data view logic more explicit
dimaanj Nov 3, 2022
badaf79
[Discover] remove log
dimaanj Nov 3, 2022
0fd1b90
Merge pull request #14 from dimaanj/make-adhoc-logic-more-explicit
kertal Nov 3, 2022
a01df77
Rename dataViewsAdHoc to dataViewAdHocList
kertal Nov 4, 2022
2f482d7
fix conflicts
kertal Nov 4, 2022
ad88bff
Fix context_app.tsx
kertal Nov 4, 2022
95483dd
Merge branch 'main' into discover-internal-state
kibanamachine Nov 7, 2022
5843385
Merge branch 'main' into discover-internal-state
kibanamachine Nov 8, 2022
e8a8d9e
Merge branch 'main' of https://github.com/elastic/kibana into discove…
dimaanj Nov 17, 2022
fc45043
[Discover] fix linting
dimaanj Nov 17, 2022
16c1d01
Merge branch 'main' of https://github.com/elastic/kibana into discove…
dimaanj Dec 12, 2022
9914d6f
[Discover] remove redundant comment
dimaanj Dec 12, 2022
80bb56c
Fix functional
kertal Dec 12, 2022
9ddf6be
[Discover] return savedDataViews list
dimaanj Dec 13, 2022
2fb845b
migrate dataViewList to internal state container
kertal Dec 14, 2022
16f92b3
undo change_dataview.tsx
kertal Dec 14, 2022
f49e221
remove unnecessary refetch function usage
kertal Dec 14, 2022
9b8e176
address review comments
kertal Dec 15, 2022
4bbb236
Fix change data view issue
kertal Dec 15, 2022
ca718fc
Merge remote-tracking branch 'upstream/main' into discover-internal-s…
kertal Dec 15, 2022
27377bb
Fix unit tests
kertal Dec 16, 2022
383ac1d
Fix functional tests
kertal Dec 16, 2022
9d7a73e
Merge remote-tracking branch 'upstream/main' into discover-internal-s…
kertal Dec 19, 2022
5cab758
Fix functionals
kertal Dec 19, 2022
43bb325
[Discover] fix two adhoc data views diplayed issue on click use witho…
dimaanj Dec 19, 2022
ad6169b
Merge branch 'discover-internal-state' of github.com:kertal/kibana in…
kertal Dec 19, 2022
c209394
Prevent duplicate ad-hocs in the data view select
kertal Dec 19, 2022
4c929b8
Fix loading state change
kertal Dec 19, 2022
7027786
Update src/plugins/discover/public/hooks/use_data_grid_columns.ts
kertal Dec 20, 2022
857afed
Update src/plugins/discover/public/hooks/use_data_grid_columns.ts
kertal Dec 20, 2022
74f5058
Fix storybook state name
kertal Dec 20, 2022
5091f46
Improve saved/adhoc data views naming
kertal Dec 20, 2022
8b75e85
Merge branch 'main' into discover-internal-state
kertal Dec 21, 2022
6fe5cd2
fix conflicts
kertal Dec 21, 2022
06c6bb6
Fix type errors
kertal Dec 21, 2022
475033b
Fix jest test
kertal Dec 21, 2022
8c8e92e
fix conflicts
kertal Dec 29, 2022
177e1b3
apply review suggestion
kertal Dec 29, 2022
ce9cc2a
Improve documentation
kertal Dec 29, 2022
fb71953
Merge main, fix conflicts
kertal Dec 29, 2022
1ab6046
Add functional test
kertal Dec 29, 2022
d0bc3d4
Add missing await to functional test
kertal Dec 29, 2022
ca6926a
Fix functional test
kertal Dec 30, 2022
1c73ea9
Merge remote-tracking branch 'upstream/main' into discover-internal-s…
kertal Dec 30, 2022
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
1 change: 1 addition & 0 deletions src/plugins/discover/public/__mocks__/data_view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export const buildDataViewMock = ({
name,
metaFields: ['_index', '_score'],
fields: dataViewFields,
type: 'default',
getName: () => name,
getComputedFields: () => ({ docvalueFields: [], scriptFields: {}, storedFields: ['*'] }),
getSourceFiltering: () => ({}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
* Side Public License, v 1.
*/
import { createBrowserHistory } from 'history';
import { getState } from '../application/main/services/discover_state';
import { getDiscoverStateContainer } from '../application/main/services/discover_state';
import { savedSearchMockWithTimeField, savedSearchMock } from './saved_search';
import { discoverServiceMock } from './services';

export function getDiscoverStateMock({ isTimeBased = true }) {
const history = createBrowserHistory();
history.push('/');
return getState({
return getDiscoverStateContainer({
savedSearch: isTimeBased ? savedSearchMockWithTimeField : savedSearchMock,
services: discoverServiceMock,
history,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ export const ContextApp = ({ dataView, anchorId, referrer }: ContextAppProps) =>
config: uiSettings,
dataView,
dataViews,
state: appState,
useNewFieldsApi,
setAppState: stateContainer.setAppState,
columns: appState.columns,
sort: appState.sort,
});

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { EuiFlexItem } from '@elastic/eui';
import { css } from '@emotion/react';
import { useDiscoverServices } from '../../../../hooks/use_discover_services';
import { FIELD_STATISTICS_LOADED } from './constants';
import type { GetStateReturn } from '../../services/discover_state';
import type { DiscoverStateContainer } from '../../services/discover_state';
import { AvailableFields$, DataRefetch$, DataTotalHits$ } from '../../hooks/use_saved_search';
export interface RandomSamplingOption {
mode: 'random_sampling';
Expand Down Expand Up @@ -95,7 +95,7 @@ export interface FieldStatisticsTableProps {
/**
* State container with persisted settings
*/
stateContainer?: GetStateReturn;
stateContainer?: DiscoverStateContainer;
/**
* Callback to add a filter to filter bar
*/
Expand Down Expand Up @@ -136,8 +136,7 @@ export const FieldStatisticsTable = (props: FieldStatisticsTableProps) => {
const embeddableRoot: React.RefObject<HTMLDivElement> = useRef<HTMLDivElement>(null);

const showPreviewByDefault = useMemo(
() =>
stateContainer ? !stateContainer.appStateContainer.getState().hideAggregatedPreview : true,
() => (stateContainer ? !stateContainer.appState.getState().hideAggregatedPreview : true),
[stateContainer]
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
import React, { useState } from 'react';
import { storiesOf } from '@storybook/react';
import { __IntlProvider as IntlProvider } from '@kbn/i18n-react';
import { AppState } from '../../../services/discover_app_state_container';
import { getDataViewMock } from '../../../../../__mocks__/__storybook_mocks__/get_data_view_mock';
import { withDiscoverServices } from '../../../../../__mocks__/__storybook_mocks__/with_discover_services';
import { getDocumentsLayoutProps, getPlainRecordLayoutProps } from './get_layout_props';
import { DiscoverLayout } from '../discover_layout';
import { setHeaderActionMenuMounter } from '../../../../../kibana_services';
import { AppState } from '../../../services/discover_state';
import { DiscoverLayoutProps } from '../types';

setHeaderActionMenuMounter(() => void 0);

const DiscoverLayoutStory = (layoutProps: DiscoverLayoutProps) => {
const [state, setState] = useState(layoutProps.state);
const [state, setState] = useState({});

const setAppState = (newState: Partial<AppState>) => {
setState((prevState) => ({ ...prevState, ...newState }));
Expand All @@ -31,10 +31,9 @@ const DiscoverLayoutStory = (layoutProps: DiscoverLayoutProps) => {
return (
<DiscoverLayout
{...layoutProps}
state={state}
stateContainer={{
...layoutProps.stateContainer,
appStateContainer: { ...layoutProps.stateContainer.appStateContainer, getState },
appState: { ...layoutProps.stateContainer.appState, getState },
setAppState,
}}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { buildDataTableRecordList } from '../../../../../utils/build_data_record
import { esHits } from '../../../../../__mocks__/es_hits';
import { SavedSearch } from '../../../../..';
import { DiscoverLayoutProps } from '../types';
import { GetStateReturn } from '../../../services/discover_state';
import { DiscoverStateContainer } from '../../../services/discover_state';

const documentObservables = {
main$: new BehaviorSubject({
Expand Down Expand Up @@ -100,7 +100,7 @@ const getCommonProps = (dataView: DataView) => {
}),
setState: action('Set app state'),
},
} as unknown as GetStateReturn,
} as unknown as DiscoverStateContainer,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is appStateContainer in this definition. Is it correct?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's wrong, thx and changed, however it doesn't change the current broken state of storybook, it needs some ❤️ to unbreak it

setExpandedDoc: action('opening an expanded doc'),
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { mountWithIntl } from '@kbn/test-jest-helpers';
import { setHeaderActionMenuMounter } from '../../../../kibana_services';
import { esHits } from '../../../../__mocks__/es_hits';
import { savedSearchMock } from '../../../../__mocks__/saved_search';
import { AppState, GetStateReturn } from '../../services/discover_state';
import { DiscoverStateContainer } from '../../services/discover_state';
import { DataDocuments$ } from '../../hooks/use_saved_search';
import { discoverServiceMock } from '../../../../__mocks__/services';
import { FetchStatus } from '../../../types';
Expand All @@ -21,6 +21,9 @@ import { dataViewMock } from '../../../../__mocks__/data_view';
import { KibanaContextProvider } from '@kbn/kibana-react-plugin/public';
import { buildDataTableRecord } from '../../../../utils/build_data_record';
import { EsHitRecord } from '../../../../types';
import { DiscoverMainProvider } from '../../services/discover_state_provider';
import { getDiscoverStateMock } from '../../../../__mocks__/discover_state.mock';
import { AppState } from '../../services/discover_app_state_container';

setHeaderActionMenuMounter(jest.fn());

Expand All @@ -34,6 +37,7 @@ function mountComponent(fetchStatus: FetchStatus, hits: EsHitRecord[]) {
fetchStatus,
result: hits.map((hit) => buildDataTableRecord(hit, dataViewMock)),
}) as DataDocuments$;
const stateContainer = getDiscoverStateMock({});

const props = {
expandedDoc: undefined,
Expand All @@ -44,14 +48,16 @@ function mountComponent(fetchStatus: FetchStatus, hits: EsHitRecord[]) {
searchSource: documents$,
setExpandedDoc: jest.fn(),
state: { columns: [] },
stateContainer: { setAppState: () => {} } as unknown as GetStateReturn,
stateContainer,
navigateTo: jest.fn(),
onFieldEdited: jest.fn(),
};

return mountWithIntl(
<KibanaContextProvider services={services}>
<DiscoverDocuments {...props} />
<DiscoverMainProvider value={stateContainer}>
<DiscoverDocuments {...props} />
</DiscoverMainProvider>
</KibanaContextProvider>
);
}
Expand Down Expand Up @@ -83,15 +89,17 @@ describe('Discover documents layout', () => {
setAppState: (newState: Partial<AppState>) => {
state = { ...state, ...newState };
},
} as unknown as GetStateReturn;
appState: {
getState: () => state,
},
} as unknown as DiscoverStateContainer;

onResize(
{
columnId: 'someField',
width: 205.5435345534,
},
stateContainer,
state
stateContainer
);

expect(state.grid?.columns?.someField.width).toEqual(206);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import { FormattedMessage } from '@kbn/i18n-react';
import { DataView } from '@kbn/data-views-plugin/public';
import { SavedSearch, SortOrder } from '@kbn/saved-search-plugin/public';
import { useAppStateSelector } from '../../services/discover_app_state_container';
import { useDiscoverServices } from '../../../../hooks/use_discover_services';
import { DocViewFilterFn } from '../../../../services/doc_views/doc_views_types';
import { DiscoverGrid } from '../../../../components/discover_grid/discover_grid';
Expand All @@ -29,7 +30,7 @@ import {
} from '../../../../../common';
import { useColumns } from '../../../../hooks/use_data_grid_columns';
import { DataDocuments$, RecordRawType } from '../../hooks/use_saved_search';
import { AppState, GetStateReturn } from '../../services/discover_state';
import { DiscoverStateContainer } from '../../services/discover_state';
import { useDataState } from '../../hooks/use_data_state';
import { DocTableInfinite } from '../../../../components/doc_table/doc_table_infinite';
import { DocumentExplorerCallout } from '../document_explorer_callout';
Expand All @@ -44,9 +45,9 @@ const DataGridMemoized = React.memo(DiscoverGrid);
// export needs for testing
export const onResize = (
colSettings: { columnId: string; width: number },
stateContainer: GetStateReturn,
state: AppState
stateContainer: DiscoverStateContainer
) => {
const state = stateContainer.appState.getState();
const grid = { ...(state.grid || {}) };
const newColumns = { ...(grid.columns || {}) };
newColumns[colSettings.columnId] = {
Expand All @@ -63,7 +64,6 @@ function DiscoverDocumentsComponent({
onAddFilter,
savedSearch,
setExpandedDoc,
state,
stateContainer,
onFieldEdited,
}: {
Expand All @@ -74,49 +74,56 @@ function DiscoverDocumentsComponent({
onAddFilter?: DocViewFilterFn;
savedSearch: SavedSearch;
setExpandedDoc: (doc?: DataTableRecord) => void;
state: AppState;
stateContainer: GetStateReturn;
stateContainer: DiscoverStateContainer;
onFieldEdited?: () => void;
}) {
const { capabilities, dataViews, uiSettings } = useDiscoverServices();
const [query, sort, rowHeight, rowsPerPage, grid, columns] = useAppStateSelector((state) => {
return [state.query, state.sort, state.rowHeight, state.rowsPerPage, state.grid, state.columns];
});

const useNewFieldsApi = useMemo(() => !uiSettings.get(SEARCH_FIELDS_FROM_SOURCE), [uiSettings]);
const hideAnnouncements = useMemo(() => uiSettings.get(HIDE_ANNOUNCEMENTS), [uiSettings]);
const isLegacy = useMemo(() => uiSettings.get(DOC_TABLE_LEGACY), [uiSettings]);
const sampleSize = useMemo(() => uiSettings.get(SAMPLE_SIZE_SETTING), [uiSettings]);

const documentState = useDataState(documents$);
const isLoading = documentState.fetchStatus === FetchStatus.LOADING;
const isPlainRecord = useMemo(
() => getRawRecordType(state.query) === RecordRawType.PLAIN,
[state.query]
);
const isPlainRecord = useMemo(() => getRawRecordType(query) === RecordRawType.PLAIN, [query]);
const rows = useMemo(() => documentState.result || [], [documentState.result]);

const { columns, onAddColumn, onRemoveColumn, onMoveColumn, onSetColumns } = useColumns({
const {
columns: currentColumns,
onAddColumn,
onRemoveColumn,
onMoveColumn,
onSetColumns,
} = useColumns({
capabilities,
config: uiSettings,
dataView,
dataViews,
setAppState: stateContainer.setAppState,
state,
useNewFieldsApi,
columns,
sort,
});

const onResizeDataGrid = useCallback(
(colSettings) => onResize(colSettings, stateContainer, state),
[stateContainer, state]
(colSettings) => onResize(colSettings, stateContainer),
[stateContainer]
);

const onUpdateRowsPerPage = useCallback(
(rowsPerPage: number) => {
stateContainer.setAppState({ rowsPerPage });
(nextRowsPerPage: number) => {
stateContainer.setAppState({ rowsPerPage: nextRowsPerPage });
},
[stateContainer]
);

const onSort = useCallback(
(sort: string[][]) => {
stateContainer.setAppState({ sort });
(nextSort: string[][]) => {
stateContainer.setAppState({ sort: nextSort });
},
[stateContainer]
);
Expand Down Expand Up @@ -162,10 +169,10 @@ function DiscoverDocumentsComponent({
<>
{!hideAnnouncements && <DocumentExplorerCallout />}
<DocTableInfiniteMemoized
columns={columns}
columns={currentColumns}
dataView={dataView}
rows={rows}
sort={state.sort || []}
sort={sort || []}
isLoading={isLoading}
searchDescription={savedSearch.description}
sharedItemTitle={savedSearch.title}
Expand All @@ -189,30 +196,30 @@ function DiscoverDocumentsComponent({
<div className="dscDiscoverGrid">
<DataGridMemoized
ariaLabelledBy="documentsAriaLabel"
columns={columns}
columns={currentColumns}
expandedDoc={expandedDoc}
dataView={dataView}
isLoading={isLoading}
rows={rows}
sort={(state.sort as SortOrder[]) || []}
sort={(sort as SortOrder[]) || []}
sampleSize={sampleSize}
searchDescription={savedSearch.description}
searchTitle={savedSearch.title}
setExpandedDoc={!isPlainRecord ? setExpandedDoc : undefined}
showTimeCol={showTimeCol}
settings={state.grid}
settings={grid}
onAddColumn={onAddColumn}
onFilter={onAddFilter as DocViewFilterFn}
onRemoveColumn={onRemoveColumn}
onSetColumns={onSetColumns}
onSort={!isPlainRecord ? onSort : undefined}
onResize={onResizeDataGrid}
useNewFieldsApi={useNewFieldsApi}
rowHeightState={state.rowHeight}
rowHeightState={rowHeight}
onUpdateRowHeight={onUpdateRowHeight}
isSortEnabled={!isPlainRecord}
isPlainRecord={isPlainRecord}
rowsPerPageState={state.rowsPerPage}
rowsPerPageState={rowsPerPage}
onUpdateRowsPerPage={onUpdateRowsPerPage}
onFieldEdited={onFieldEdited}
savedSearchId={savedSearch.id}
Expand Down
Loading