import React from 'react'; import type {SelectOption} from '@gravity-ui/uikit'; import {Select} from '@gravity-ui/uikit'; import escapeRegExp from 'lodash/escapeRegExp'; import {Search} from '../../../../../components/Search/Search'; import {b} from '../Partitions'; import i18n from '../i18n'; import type {PreparedPartitionDataWithHosts} from '../utils/types'; interface PartitionsControlsProps { consumers: string[] | undefined; selectedConsumer?: string; onSelectedConsumerChange: (consumer?: string) => void; selectDisabled: boolean; partitions: PreparedPartitionDataWithHosts[] | undefined; onSearchChange: (filteredPartitions: PreparedPartitionDataWithHosts[]) => void; } export const PartitionsControls = ({ consumers, selectedConsumer, onSelectedConsumerChange, selectDisabled, partitions, onSearchChange, }: PartitionsControlsProps) => { const [generalSearchValue, setGeneralSearchValue] = React.useState(''); const [partitionIdSearchValue, setPartitionIdSearchValue] = React.useState(''); React.useEffect(() => { if (!partitions) { return; } const partitionIdRe = new RegExp(escapeRegExp(partitionIdSearchValue.trim()), 'i'); const generalRe = new RegExp(escapeRegExp(generalSearchValue.trim()), 'i'); const filteredPartitions = partitions.filter((partition) => { const { partitionId, readerName, readSessionId, partitionNodeId, connectionNodeId, partitionHost, connectionHost, } = partition; const isPartitionIdMatch = partitionIdRe.test(String(partitionId)); const otherValues = [ readerName, readSessionId, partitionNodeId, connectionNodeId, partitionHost, connectionHost, ] .filter(Boolean) .map(String); const isOtherValuesMatch = otherValues.length === 0 || otherValues.some((value) => generalRe.test(value)); return isPartitionIdMatch && isOtherValuesMatch; }); onSearchChange(filteredPartitions); }, [partitionIdSearchValue, generalSearchValue, partitions, onSearchChange]); const consumersToSelect = React.useMemo(() => { const options = consumers && consumers.length ? consumers.map((consumer) => ({ value: consumer, content: consumer, })) : []; return [{value: '', content: i18n('controls.consumerSelector.emptyOption')}, ...options]; }, [consumers]); const handleConsumerSelectChange = (value: string[]) => { // Do not set empty string to state onSelectedConsumerChange(value[0] || undefined); }; const handlePartitionIdSearchChange = (value: string) => { setPartitionIdSearchValue(value); }; const handleGeneralSearchChange = (value: string) => { setGeneralSearchValue(value); }; const renderOption = (option: SelectOption) => { return (