diff --git a/src/plugins/discover/public/application/components/chart/chart.tsx b/src/plugins/discover/public/application/components/chart/chart.tsx index 5f5de6b98545..8ba188af3dd9 100644 --- a/src/plugins/discover/public/application/components/chart/chart.tsx +++ b/src/plugins/discover/public/application/components/chart/chart.tsx @@ -28,7 +28,7 @@ interface DiscoverChartProps { hits: number; resetQuery: () => void; showResetButton?: boolean; - timeField?: string; + isTimeBased?: boolean; services: DiscoverServices; } @@ -39,7 +39,7 @@ export const DiscoverChart = ({ data, hits, resetQuery, - timeField, + isTimeBased, services, showResetButton = false, }: DiscoverChartProps) => { @@ -75,7 +75,7 @@ export const DiscoverChart = ({ onResetQuery={resetQuery} /> - {timeField && ( + {isTimeBased && ( )} - {timeField && chartData && ( + {isTimeBased && chartData && (
!uiSettings.get(DOC_HIDE_TIME_COLUMN_SETTING, false) && indexPattern?.isTimeBased(), + [indexPattern, uiSettings] + ); + return ( {}} sort={sort} rows={rows} - displayTimeColumn={true} + displayTimeColumn={displayTimeColumn} services={services} isToolbarVisible={false} isContextView={true} diff --git a/src/plugins/discover/public/application/view_components/canvas/discover_chart_container.tsx b/src/plugins/discover/public/application/view_components/canvas/discover_chart_container.tsx index db3ad027c9c2..cae81b6604c0 100644 --- a/src/plugins/discover/public/application/view_components/canvas/discover_chart_container.tsx +++ b/src/plugins/discover/public/application/view_components/canvas/discover_chart_container.tsx @@ -4,7 +4,7 @@ */ import './discover_chart_container.scss'; -import React from 'react'; +import React, { useMemo } from 'react'; import { DiscoverViewServices } from '../../../build_services'; import { useOpenSearchDashboards } from '../../../../../opensearch_dashboards_react/public'; import { useDiscoverContext } from '../context'; @@ -16,27 +16,26 @@ export const DiscoverChartContainer = ({ hits, bucketInterval, chartData }: Sear const { uiSettings, data } = services; const { indexPattern, savedSearch } = useDiscoverContext(); - const timeField = indexPattern?.timeFieldName; - - if (!hits || !bucketInterval || !chartData) { - // TODO: handle better - return null; - } + const isTimeBased = useMemo(() => (indexPattern ? indexPattern.isTimeBased() : false), [ + indexPattern, + ]); return ( - { - window.location.href = `#/view/${savedSearch?.id}`; - window.location.reload(); - }} - services={services} - showResetButton={!!savedSearch && !!savedSearch.id} - /> + hits && ( + { + window.location.href = `#/view/${savedSearch?.id}`; + window.location.reload(); + }} + services={services} + showResetButton={!!savedSearch && !!savedSearch.id} + isTimeBased={isTimeBased} + /> + ) ); }; diff --git a/src/plugins/discover/public/application/view_components/canvas/discover_table.tsx b/src/plugins/discover/public/application/view_components/canvas/discover_table.tsx index b228110ec9fc..c4e05cd6bdf3 100644 --- a/src/plugins/discover/public/application/view_components/canvas/discover_table.tsx +++ b/src/plugins/discover/public/application/view_components/canvas/discover_table.tsx @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -import React, { useState, useEffect, useCallback } from 'react'; +import React, { useState, useEffect, useCallback, useMemo } from 'react'; import { History } from 'history'; import { DiscoverViewServices } from '../../../build_services'; import { useOpenSearchDashboards } from '../../../../../opensearch_dashboards_react/public'; @@ -21,6 +21,7 @@ import { ResultStatus, SearchData } from '../utils/use_search'; import { IndexPatternField, opensearchFilters } from '../../../../../data/public'; import { DocViewFilterFn } from '../../doc_views/doc_views_types'; import { SortOrder } from '../../../saved_searches/types'; +import { DOC_HIDE_TIME_COLUMN_SETTING } from '../../../../common'; interface Props { history: History; @@ -28,7 +29,12 @@ interface Props { export const DiscoverTable = ({ history }: Props) => { const { services } = useOpenSearchDashboards(); - const { filterManager } = services.data.query; + const { + uiSettings, + data: { + query: { filterManager }, + }, + } = services; const { data$, refetch$, indexPattern } = useDiscoverContext(); const [fetchState, setFetchState] = useState({ status: data$.getValue().status, @@ -57,6 +63,10 @@ export const DiscoverTable = ({ history }: Props) => { }, [filterManager, indexPattern] ); + const displayTimeColumn = useMemo( + () => !!(!uiSettings.get(DOC_HIDE_TIME_COLUMN_SETTING, false) && indexPattern?.isTimeBased()), + [indexPattern, uiSettings] + ); const { rows } = fetchState || {}; @@ -93,7 +103,7 @@ export const DiscoverTable = ({ history }: Props) => { onSort={onSetSort} sort={sort} rows={rows} - displayTimeColumn={true} + displayTimeColumn={displayTimeColumn} services={services} /> ); diff --git a/src/plugins/discover/public/application/view_components/canvas/top_nav.tsx b/src/plugins/discover/public/application/view_components/canvas/top_nav.tsx index a7c7c5ef473d..13a96707c21f 100644 --- a/src/plugins/discover/public/application/view_components/canvas/top_nav.tsx +++ b/src/plugins/discover/public/application/view_components/canvas/top_nav.tsx @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -import React, { useEffect, useState } from 'react'; +import React, { useEffect, useMemo, useState } from 'react'; import { AppMountParameters } from '../../../../../../core/public'; import { NEW_DISCOVER_APP, PLUGIN_ID } from '../../../../common'; import { useOpenSearchDashboards } from '../../../../../opensearch_dashboards_react/public'; @@ -21,7 +21,7 @@ export interface TopNavProps { export const TopNav = ({ opts }: TopNavProps) => { const { services } = useOpenSearchDashboards(); - const { inspectorAdapters, savedSearch } = useDiscoverContext(); + const { inspectorAdapters, savedSearch, indexPattern } = useDiscoverContext(); const [indexPatterns, setIndexPatterns] = useState(undefined); const { @@ -54,11 +54,11 @@ export const TopNav = ({ opts }: TopNavProps) => { let isMounted = true; const getDefaultIndexPattern = async () => { await data.indexPatterns.ensureDefaultIndexPattern(); - const indexPattern = await data.indexPatterns.getDefault(); + const defaultIndexPattern = await data.indexPatterns.getDefault(); if (!isMounted) return; - setIndexPatterns(indexPattern ? [indexPattern] : undefined); + setIndexPatterns(defaultIndexPattern ? [defaultIndexPattern] : undefined); }; getDefaultIndexPattern(); @@ -82,11 +82,16 @@ export const TopNav = ({ opts }: TopNavProps) => { } }, [chrome, getUrlForApp, savedSearch?.id, savedSearch?.title]); + const showDatePicker = useMemo(() => (indexPattern ? indexPattern.isTimeBased() : false), [ + indexPattern, + ]); + return (