forked from opensearch-project/OpenSearch-Dashboards
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add source selector for vega visualization
Signed-off-by: Yulong Ruan <ruanyl@amazon.com>
- Loading branch information
Showing
4 changed files
with
129 additions
and
15 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
99 changes: 99 additions & 0 deletions
99
src/plugins/visualize/public/application/components/source_selector.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 @@ | ||
import React, { useCallback, useMemo, useState, useEffect } from 'react'; | ||
import { i18n } from '@osd/i18n'; | ||
|
||
import { useOpenSearchDashboards } from '../../../../opensearch_dashboards_react/public'; | ||
import { | ||
DataSource, | ||
DataSourceGroup, | ||
DataSourceSelectable, | ||
DataSourceOption, | ||
} from '../../../../data/public'; | ||
import { VisualizeServices } from '../types'; | ||
|
||
export const SourceSelector = ({ | ||
selectedSourceId, | ||
onChange, | ||
}: { | ||
selectedSourceId: string; | ||
onChange: (ds: DataSourceOption) => void; | ||
}) => { | ||
const { | ||
services: { | ||
data: { dataSources }, | ||
notifications: { toasts }, | ||
}, | ||
} = useOpenSearchDashboards<VisualizeServices>(); | ||
const [currentDataSources, setCurrentDataSources] = useState<DataSource[]>([]); | ||
const [dataSourceOptions, setDataSourceOptions] = useState<DataSourceGroup[]>([]); | ||
|
||
const selectedSources = useMemo(() => { | ||
if (selectedSourceId) { | ||
for (const group of dataSourceOptions) { | ||
for (const item of group.options) { | ||
if (item.value === selectedSourceId) { | ||
return [item]; | ||
} | ||
} | ||
} | ||
} | ||
return []; | ||
}, [selectedSourceId, dataSourceOptions]); | ||
|
||
useEffect(() => { | ||
if ( | ||
!selectedSourceId && | ||
dataSourceOptions.length > 0 && | ||
dataSourceOptions[0].options.length > 0 | ||
) { | ||
onChange(dataSourceOptions[0].options[0]); | ||
} | ||
}, [selectedSourceId, dataSourceOptions]); | ||
Check failure on line 50 in src/plugins/visualize/public/application/components/source_selector.tsx GitHub Actions / Build and Verify on Linux (ciGroup1)
|
||
|
||
useEffect(() => { | ||
const subscription = dataSources.dataSourceService | ||
.getDataSources$() | ||
.subscribe((currentDataSources) => { | ||
setCurrentDataSources(Object.values(currentDataSources)); | ||
}); | ||
|
||
return () => { | ||
subscription.unsubscribe(); | ||
}; | ||
}, [dataSources]); | ||
|
||
const onDataSourceSelect = useCallback( | ||
(selectedDataSources: DataSourceOption[]) => { | ||
onChange(selectedDataSources[0]); | ||
}, | ||
[onChange] | ||
); | ||
|
||
const handleGetDataSetError = useCallback( | ||
() => (error: Error) => { | ||
toasts.addError(error, { | ||
title: | ||
i18n.translate('visualize.vega.failedToGetDataSetErrorDescription', { | ||
defaultMessage: 'Failed to get data set: ', | ||
}) + (error.message || error.name), | ||
}); | ||
}, | ||
[toasts] | ||
); | ||
|
||
const memorizedReload = useCallback(() => { | ||
dataSources.dataSourceService.reload(); | ||
}, [dataSources.dataSourceService]); | ||
|
||
return ( | ||
<DataSourceSelectable | ||
dataSources={currentDataSources} | ||
dataSourceOptionList={dataSourceOptions} | ||
setDataSourceOptionList={setDataSourceOptions} | ||
onDataSourceSelect={onDataSourceSelect} | ||
selectedSources={selectedSources} | ||
onGetDataSetError={handleGetDataSetError} | ||
onRefresh={memorizedReload} | ||
fullWidth | ||
/> | ||
); | ||
}; |
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