Skip to content

Commit

Permalink
[Discover 2.0] fixes add filter and dismissible callout (#5029)
Browse files Browse the repository at this point in the history
* fixes add filter and dismissible callout

Signed-off-by: Ashwin P Chandran <[email protected]>

* fixes filters applying when loading a saved search

Signed-off-by: Ashwin P Chandran <[email protected]>

---------

Signed-off-by: Ashwin P Chandran <[email protected]>
  • Loading branch information
ashwin-pc authored Sep 15, 2023
1 parent 195417b commit b9b2b79
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ export const DiscoverChartContainer = ({ hits, bucketInterval, chartData }: Sear
indexPattern,
]);

if (!hits || !chartData || !bucketInterval) return null;

return (
hits && (
<DiscoverChart
bucketInterval={bucketInterval}
chartData={chartData}
config={uiSettings}
data={data}
hits={hits}
resetQuery={() => {
window.location.href = `#/view/${savedSearch?.id}`;
window.location.reload();
}}
services={services}
showResetButton={!!savedSearch && !!savedSearch.id}
isTimeBased={isTimeBased}
/>
)
<DiscoverChart
bucketInterval={bucketInterval}
chartData={chartData}
config={uiSettings}
data={data}
hits={hits}
resetQuery={() => {
window.location.href = `#/view/${savedSearch?.id}`;
window.location.reload();
}}
services={services}
showResetButton={!!savedSearch && !!savedSearch.id}
isTimeBased={isTimeBased}
/>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ export const DiscoverTable = ({ history }: Props) => {
refetch$.next();
};
const onAddFilter = useCallback(
(field: IndexPatternField, values: string, operation: '+' | '-') => {
(field: string | IndexPatternField, values: string, operation: '+' | '-') => {
if (!indexPattern) return;

const newFilters = opensearchFilters.generateFilters(
filterManager,
field,
values,
operation,
indexPattern.id
indexPattern.id ?? ''
);
return filterManager.addFilters(newFilters);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import { useOpenSearchDashboards } from '../../../../../opensearch_dashboards_re
import { filterColumns } from '../utils/filter_columns';
import { DEFAULT_COLUMNS_SETTING } from '../../../../common';

const KEY_SHOW_NOTICE = 'discover:deprecation-notice:show';

// eslint-disable-next-line import/no-default-export
export default function DiscoverCanvas({ setHeaderActionMenu, history }: ViewProps) {
const { data$, refetch$, indexPattern } = useDiscoverContext();
Expand All @@ -41,8 +43,13 @@ export default function DiscoverCanvas({ setHeaderActionMenu, history }: ViewPro
bucketInterval: {},
});

const [isCallOutVisible, setIsCallOutVisible] = useState(true);
const closeCallOut = () => setIsCallOutVisible(false);
const [isCallOutVisible, setIsCallOutVisible] = useState(
localStorage.getItem(KEY_SHOW_NOTICE) !== 'false'
);
const closeCallOut = () => {
localStorage.setItem(KEY_SHOW_NOTICE, 'false');
setIsCallOutVisible(false);
};

let callOut;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import React, { useEffect, useState } from 'react';
import React, { useCallback, useEffect, useState } from 'react';
import { ViewProps } from '../../../../../data_explorer/public';
import {
addColumn,
Expand All @@ -15,9 +15,18 @@ import {
import { DiscoverSidebar } from '../../components/sidebar';
import { useDiscoverContext } from '../context';
import { ResultStatus, SearchData } from '../utils/use_search';
import { IndexPatternField, opensearchFilters } from '../../../../../data/public';
import { useOpenSearchDashboards } from '../../../../../opensearch_dashboards_react/public';
import { DiscoverViewServices } from '../../../build_services';

// eslint-disable-next-line import/no-default-export
export default function DiscoverPanel(props: ViewProps) {
const { services } = useOpenSearchDashboards<DiscoverViewServices>();
const {
data: {
query: { filterManager },
},
} = services;
const { data$, indexPattern } = useDiscoverContext();
const [fetchState, setFetchState] = useState<SearchData>(data$.getValue());

Expand All @@ -36,6 +45,22 @@ export default function DiscoverPanel(props: ViewProps) {
};
}, [data$, fetchState]);

const onAddFilter = useCallback(
(field: string | IndexPatternField, values: string, operation: '+' | '-') => {
if (!indexPattern) return;

const newFilters = opensearchFilters.generateFilters(
filterManager,
field,
values,
operation,
indexPattern.id ?? ''
);
return filterManager.addFilters(newFilters);
},
[filterManager, indexPattern]
);

return (
<DiscoverSidebar
columns={columns || []}
Expand All @@ -61,7 +86,7 @@ export default function DiscoverPanel(props: ViewProps) {
);
}}
selectedIndexPattern={indexPattern}
onAddFilter={() => {}}
onAddFilter={onAddFilter}
/>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { IndexPattern } from '../../../opensearch_dashboards_services';
*/
export function filterColumns(
columns: string[],
indexPattern: IndexPattern,
indexPattern: IndexPattern | undefined,
defaultColumns: string[]
) {
const fieldsName = indexPattern?.fields.getAll().map((fld) => fld.name) || [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { BehaviorSubject, Subject, merge } from 'rxjs';
import { debounceTime } from 'rxjs/operators';
import { i18n } from '@osd/i18n';
import { useEffect } from 'react';
import { cloneDeep } from 'lodash';
import { RequestAdapter } from '../../../../../inspector/public';
import { DiscoverServices } from '../../../build_services';
import { search } from '../../../../../data/public';
Expand All @@ -30,7 +31,6 @@ import {
getResponseInspectorStats,
} from '../../../opensearch_dashboards_services';
import { SEARCH_ON_PAGE_LOAD_SETTING } from '../../../../common';
import { SortOrder } from '../../../saved_searches/types';

export enum ResultStatus {
UNINITIALIZED = 'uninitialized',
Expand Down Expand Up @@ -253,9 +253,29 @@ export const useSearch = (services: DiscoverServices) => {
(async () => {
const savedSearchInstance = await getSavedSearchById(savedSearchId || '');
setSavedSearch(savedSearchInstance);

// sync initial app filters from savedObject to filterManager
const filters = cloneDeep(savedSearchInstance.searchSource.getOwnField('filter'));
const query =
savedSearchInstance.searchSource.getField('query') ||
data.query.queryString.getDefaultQuery();
const actualFilters = [];

if (filters !== undefined) {
const result = typeof filters === 'function' ? filters() : filters;
if (result !== undefined) {
actualFilters.push(...(Array.isArray(result) ? result : [result]));
}
}

filterManager.setAppFilters(actualFilters);
data.query.queryString.setQuery(query);
})();

return () => {};
// This effect will only run when getSavedSearchById is called, which is
// only called when the component is first mounted.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [getSavedSearchById, savedSearchId]);

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ export interface DiscoverLegacyProps {
vis?: Vis;
}

const KEY_SHOW_NOTICE = 'discover:deprecation-notice:show';

export function DiscoverLegacy({
addColumn,
fetch,
Expand Down Expand Up @@ -132,7 +134,9 @@ export function DiscoverLegacy({
vis,
}: DiscoverLegacyProps) {
const [isSidebarClosed, setIsSidebarClosed] = useState(false);
const [isCallOutVisible, setIsCallOutVisible] = useState(true);
const [isCallOutVisible, setIsCallOutVisible] = useState(
localStorage.getItem(KEY_SHOW_NOTICE) !== 'false'
);
const { TopNavMenu } = getServices().navigation.ui;
const { savedSearch, indexPatternList } = opts;
const bucketAggConfig = vis?.data?.aggs?.aggs[1];
Expand All @@ -142,7 +146,10 @@ export function DiscoverLegacy({
: undefined;
const [fixedScrollEl, setFixedScrollEl] = useState<HTMLElement | undefined>();

const closeCallOut = () => setIsCallOutVisible(false);
const closeCallOut = () => {
localStorage.setItem(KEY_SHOW_NOTICE, 'false');
setIsCallOutVisible(false);
};

let callOut;

Expand Down

0 comments on commit b9b2b79

Please sign in to comment.