/* * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 */ import React, { useCallback, useMemo, useState } from 'react'; import { EuiSuperSelect, EuiSuperSelectOption, EuiIcon, IconType, EuiConfirmModal, } from '@elastic/eui'; import { i18n } from '@osd/i18n'; import { FormattedMessage } from '@osd/i18n/react'; import { useVisualizationType } from '../utils/use'; import './side_nav.scss'; import { useOpenSearchDashboards } from '../../../../opensearch_dashboards_react/public'; import { VisBuilderServices } from '../../types'; import { ActiveVisPayload, setActiveVisualization, useTypedDispatch, useTypedSelector, } from '../utils/state_management'; import { getPersistedAggParams } from '../utils/get_persisted_agg_params'; export const RightNavUI = () => { const { ui, name: activeVisName } = useVisualizationType(); const [confirmAggs, setConfirmAggs] = useState(); const { services: { types }, } = useOpenSearchDashboards(); const dispatch = useTypedDispatch(); const StyleSection = ui.containerConfig.style.render; const { activeVisualization } = useTypedSelector((state) => state.visualization); const aggConfigParams = useMemo(() => activeVisualization?.aggConfigParams ?? [], [ activeVisualization, ]); const handleVisTypeChange = useCallback( (newVisName) => { const currentVisSchemas = types.get(activeVisName)?.ui.containerConfig.data.schemas.all ?? []; const newVisSchemas = types.get(newVisName)?.ui.containerConfig.data.schemas.all ?? []; const persistedAggParams = getPersistedAggParams( aggConfigParams, currentVisSchemas, newVisSchemas ); const newVis = { name: newVisName, aggConfigParams: persistedAggParams, style: types.get(newVisName)?.ui.containerConfig.style.defaults, }; if (persistedAggParams.length < aggConfigParams.length) return setConfirmAggs(newVis); dispatch(setActiveVisualization(newVis)); }, [activeVisName, aggConfigParams, dispatch, types] ); const options: Array> = types.all().map(({ name, icon, title }) => ({ value: name, inputDisplay: , dropdownDisplay: , 'data-test-subj': `visType-${name}`, })); return (
{confirmAggs && ( setConfirmAggs(undefined)} onConfirm={() => { dispatch(setActiveVisualization(confirmAggs)); setConfirmAggs(undefined); }} maxWidth="300px" data-test-subj="confirmVisChangeModal" >

)}
); }; const OptionItem = ({ icon, title }: { icon: IconType; title: string }) => ( <> {title} ); // The app uses EuiResizableContainer that triggers a rerender for every mouseover action. // To prevent this child component from unnecessarily rerendering in that instance, it needs to be memoized export const RightNav = React.memo(RightNavUI);