-
Notifications
You must be signed in to change notification settings - Fork 893
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Data Explorer] State management + enhancements (#4580)
* Adds toggle between legacy and new discover Signed-off-by: Ashwin P Chandran <[email protected]> * Fixes header offset Signed-off-by: Ashwin P Chandran <[email protected]> * adds basic state management Signed-off-by: Ashwin P Chandran <[email protected]> * attempt 1 at dynamic state management Signed-off-by: Ashwin P Chandran <[email protected]> * Working multi view state management Signed-off-by: Ashwin P Chandran <[email protected]> * Adds global state persistence to data explorer Signed-off-by: Ashwin P Chandran <[email protected]> --------- Signed-off-by: Ashwin P Chandran <[email protected]>
- Loading branch information
Showing
27 changed files
with
722 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
99 changes: 99 additions & 0 deletions
99
src/plugins/data_explorer/public/components/sidebar/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React, { useMemo, FC, useEffect, useState } from 'react'; | ||
import { i18n } from '@osd/i18n'; | ||
import { EuiPanel, EuiComboBox, EuiSelect, EuiComboBoxOptionOption } from '@elastic/eui'; | ||
import { useOpenSearchDashboards } from '../../../../opensearch_dashboards_react/public'; | ||
import { useView } from '../../utils/use'; | ||
import { DataExplorerServices } from '../../types'; | ||
import { useTypedDispatch, useTypedSelector, setIndexPattern } from '../../utils/state_management'; | ||
import { setView } from '../../utils/state_management/metadata_slice'; | ||
|
||
export const Sidebar: FC = ({ children }) => { | ||
const { indexPattern: indexPatternId } = useTypedSelector((state) => state.metadata); | ||
const dispatch = useTypedDispatch(); | ||
const [options, setOptions] = useState<Array<EuiComboBoxOptionOption<string>>>([]); | ||
const [selectedOption, setSelectedOption] = useState<EuiComboBoxOptionOption<string>>(); | ||
const { view, viewRegistry } = useView(); | ||
const views = viewRegistry.all(); | ||
const viewOptions = useMemo( | ||
() => | ||
views.map(({ id, title }) => ({ | ||
value: id, | ||
text: title, | ||
})), | ||
[views] | ||
); | ||
|
||
const { | ||
services: { | ||
data: { indexPatterns }, | ||
notifications: { toasts }, | ||
}, | ||
} = useOpenSearchDashboards<DataExplorerServices>(); | ||
|
||
useEffect(() => { | ||
const fetchIndexPatterns = async () => { | ||
await indexPatterns.ensureDefaultIndexPattern(); | ||
const cache = await indexPatterns.getCache(); | ||
const currentOptions = (cache || []).map((indexPattern) => ({ | ||
label: indexPattern.attributes.title, | ||
value: indexPattern.id, | ||
})); | ||
setOptions(currentOptions); | ||
}; | ||
fetchIndexPatterns(); | ||
}, [indexPatterns]); | ||
|
||
// Set option to the current index pattern | ||
useEffect(() => { | ||
if (indexPatternId) { | ||
const option = options.find((o) => o.value === indexPatternId); | ||
setSelectedOption(option); | ||
} | ||
}, [indexPatternId, options]); | ||
|
||
return ( | ||
<> | ||
<EuiPanel borderRadius="none" hasShadow={false}> | ||
<EuiComboBox | ||
placeholder="Select a datasource" | ||
singleSelection={{ asPlainText: true }} | ||
options={options} | ||
selectedOptions={selectedOption ? [selectedOption] : []} | ||
onChange={(selected) => { | ||
// TODO: There are many issues with this approach, but it's a start | ||
// 1. Combo box can delete a selected index pattern. This should not be possible | ||
// 2. Combo box is severely truncated. This should be fixed in the EUI component | ||
// 3. The onchange can fire with a option that is not valid. discuss where to handle this. | ||
// 4. value is optional. If the combobox needs to act as a slecet, this should be required. | ||
const { value } = selected[0] || {}; | ||
|
||
if (!value) { | ||
toasts.addWarning({ | ||
id: 'index-pattern-not-found', | ||
title: i18n.translate('dataExplorer.indexPatternError', { | ||
defaultMessage: 'Index pattern not found', | ||
}), | ||
}); | ||
return; | ||
} | ||
|
||
dispatch(setIndexPattern(value)); | ||
}} | ||
/> | ||
<EuiSelect | ||
options={viewOptions} | ||
value={view?.id} | ||
onChange={(e) => { | ||
dispatch(setView(e.target.value)); | ||
}} | ||
/> | ||
</EuiPanel> | ||
{children} | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.