/* * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 */ import { EuiComboBoxOptionOption } from "@elastic/eui"; import { _EuiComboBoxProps } from "@elastic/eui/src/components/combo_box/combo_box"; import { CoreStart } from "opensearch-dashboards/public"; import ComboBoxWithoutWarning from "../../../../components/ComboBoxWithoutWarning"; import React, { useContext, useEffect, useState } from "react"; import { CoreServicesContext } from "../../../../components/core_services"; import { IndexSelectItem } from "../../models/interfaces"; import { filterOverlaps } from "../../utils/helper"; import { filterByMinimatch } from "../../../../../utils/helper"; import { SYSTEM_ALIAS, SYSTEM_INDEX } from "../../../../../utils/constants"; interface IndexSelectProps extends Pick<_EuiComboBoxProps, "data-test-subj" | "placeholder"> { getIndexOptions: (searchValue: string, excludeDataStreamIndex?: boolean) => Promise[]>; onSelectedOptions: (options: EuiComboBoxOptionOption[]) => void; singleSelect: boolean; selectedOption: EuiComboBoxOptionOption[]; excludeDataStreamIndex?: boolean; excludeList?: EuiComboBoxOptionOption[]; excludeSystemIndex?: boolean; } export default function IndexSelect(props: IndexSelectProps) { const [indexOptions, setIndexOptions] = useState([] as EuiComboBoxOptionOption[]); const coreServices = useContext(CoreServicesContext) as CoreStart; const searchIndex = (searchValue?: string) => { props .getIndexOptions(searchValue ? searchValue : "", props.excludeDataStreamIndex) .then((options) => { props.excludeSystemIndex && filterSystemIndices(options); setIndexOptions(filterOverlaps(options, props.excludeList)); }) .catch((err) => { coreServices.notifications.toasts.addDanger(`fetch indices error ${err}`); }); }; useEffect(() => { searchIndex(); }, [props.getIndexOptions, props.excludeList, props.excludeDataStreamIndex, props.excludeSystemIndex]); const onSearchChange = (searchValue: string) => { searchIndex(searchValue); }; const filterSystemIndices = (list: EuiComboBoxOptionOption[]) => { list.map((it) => { it.options = it.options?.filter((item) => !filterByMinimatch(item.label, SYSTEM_ALIAS)); it.options = it.options?.filter((item) => !filterByMinimatch(item.label, SYSTEM_INDEX)); }); }; return (
); }