Skip to content

Commit

Permalink
Fix components
Browse files Browse the repository at this point in the history
  • Loading branch information
kertal committed Feb 5, 2021
1 parent e47cfec commit 83816fb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 41 deletions.
25 changes: 6 additions & 19 deletions src/plugins/discover/public/application/components/discover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import { DocViewFilterFn } from '../doc_views/doc_views_types';
import { DiscoverGrid } from './discover_grid/discover_grid';
import { DiscoverTopNav } from './discover_topnav';
import { ElasticSearchHit } from '../doc_views/doc_views_types';
import { getTopNavLinks } from './top_nav/get_top_nav_links';

const DocTableLegacyMemoized = React.memo(DocTableLegacy);
const SidebarMemoized = React.memo(DiscoverSidebarResponsive);
Expand Down Expand Up @@ -82,23 +81,6 @@ export function Discover({
const { savedSearch, indexPatternList, config, services, data, setAppState } = opts;
const { trackUiMetric, capabilities, indexPatterns } = services;
const [isSidebarClosed, setIsSidebarClosed] = useState(false);
const topNavMenu = useMemo(
() =>
getTopNavLinks({
getFieldCounts: opts.getFieldCounts,
indexPattern,
inspectorAdapters: opts.inspectorAdapters,
navigateTo: opts.navigateTo,
savedSearch: opts.savedSearch,
services,
state: opts.stateContainer,
onOpenInspector: () => {
// prevent overlapping
setExpandedDoc(undefined);
},
}),
[indexPattern, opts, services]
);
const bucketAggConfig = opts.chartAggConfigs?.aggs[1];
const bucketInterval =
bucketAggConfig && search.aggs.isDateHistogramBucketAggConfig(bucketAggConfig)
Expand All @@ -121,6 +103,11 @@ export function Discover({
[capabilities, indexPattern, indexPatterns, setAppState, state, useNewFieldsApi]
);

const onOpenInspector = useCallback(() => {
// prevent overlapping
setExpandedDoc(undefined);
}, [setExpandedDoc]);

const onSort = useCallback(
(sort: string[][]) => {
setAppState({ sort });
Expand Down Expand Up @@ -204,8 +191,8 @@ export function Discover({
<TopNavMemoized
indexPattern={indexPattern}
opts={opts}
onOpenInspector={onOpenInspector}
state={state}
topNavMenu={topNavMenu}
updateQuery={updateQuery}
/>
<EuiPageBody className="dscPageBody" aria-describedby="savedSearchTitle">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,21 @@

import React from 'react';
import { shallowWithIntl } from '@kbn/test/jest';
import { inspectorPluginMock } from '../../../../inspector/public/mocks';
import { indexPatternMock } from '../../__mocks__/index_pattern';
import { getTopNavLinks } from './top_nav/get_top_nav_links';
import { DiscoverServices } from '../../build_services';
import { GetStateReturn } from '../angular/discover_state';
import { AppState, GetStateReturn } from '../angular/discover_state';
import { savedSearchMock } from '../../__mocks__/saved_search';
import { dataPluginMock } from '../../../../data/public/mocks';
import { createFilterManagerMock } from '../../../../data/public/query/filter_manager/filter_manager.mock';
import { uiSettingsMock as mockUiSettings } from '../../__mocks__/ui_settings';
import { IndexPatternAttributes } from '../../../../data/common/index_patterns';
import { SavedObject } from '../../../../../core/types';
import { navigationPluginMock } from '../../../../navigation/public/mocks';
import { DiscoverTopNav } from './discover_topnav';
import { DiscoverTopNav, DiscoverTopNavProps } from './discover_topnav';
import { RequestAdapter } from '../../../../inspector/common/adapters/request';

function getProps() {
const state = ({} as unknown) as GetStateReturn;
function getProps(): DiscoverTopNavProps {
const state = ({} as unknown) as AppState;
const services = ({
navigation: navigationPluginMock.createStartContract(),
capabilities: {
Expand All @@ -40,26 +39,21 @@ function getProps() {
config: mockUiSettings,
data: dataPluginMock.createStartContract(),
filterManager: createFilterManagerMock(),
getFieldCounts: jest.fn(),
indexPatternList: (indexPattern as unknown) as Array<SavedObject<IndexPatternAttributes>>,
inspectorAdapters: { requests: {} as RequestAdapter },
navigateTo: jest.fn(),
sampleSize: 10,
savedSearch: savedSearchMock,
setHeaderActionMenu: jest.fn(),
timefield: indexPattern.timeFieldName || '',
setAppState: jest.fn(),
services,
setAppState: jest.fn(),
setHeaderActionMenu: jest.fn(),
stateContainer: {} as GetStateReturn,
timefield: indexPattern.timeFieldName || '',
},
topNavMenu: getTopNavLinks({
getFieldCounts: jest.fn(),
indexPattern,
inspectorAdapters: inspectorPluginMock,
navigateTo: jest.fn(),
savedSearch: savedSearchMock,
services,
state,
}),
state,
updateQuery: jest.fn(),
onOpenInspector: jest.fn(),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,36 @@
*/
import React, { useMemo } from 'react';
import { DiscoverProps } from './types';
import { getTopNavLinks } from './top_nav/get_top_nav_links';

export type DiscoverTopNavProps = Pick<
DiscoverProps,
'indexPattern' | 'updateQuery' | 'state' | 'opts'
> & { onOpenInspector: () => void };

export const DiscoverTopNav = ({
topNavMenu,
indexPattern,
updateQuery,
state,
opts,
}: Pick<DiscoverProps, 'topNavMenu' | 'indexPattern' | 'updateQuery' | 'state' | 'opts'>) => {
onOpenInspector,
state,
updateQuery,
}: DiscoverTopNavProps) => {
const showDatePicker = useMemo(() => indexPattern.isTimeBased(), [indexPattern]);
const { TopNavMenu } = opts.services.navigation.ui;
const topNavMenu = useMemo(
() =>
getTopNavLinks({
getFieldCounts: opts.getFieldCounts,
indexPattern,
inspectorAdapters: opts.inspectorAdapters,
navigateTo: opts.navigateTo,
savedSearch: opts.savedSearch,
services: opts.services,
state: opts.stateContainer,
onOpenInspector,
}),
[indexPattern, opts, onOpenInspector]
);

const updateSavedQueryId = (newSavedQueryId: string | undefined) => {
const { appStateContainer, setAppState } = opts.stateContainer;
Expand Down

0 comments on commit 83816fb

Please sign in to comment.