import React from 'react';
import type {Value} from '@gravity-ui/date-components';
import {RelativeDatePicker} from '@gravity-ui/date-components';
import {dateTimeParse} from '@gravity-ui/date-utils';
import {CircleChevronDownFill} from '@gravity-ui/icons';
import {
ActionTooltip,
Button,
Flex,
Icon,
SegmentedRadioGroup,
Select,
Text,
} from '@gravity-ui/uikit';
import {isNil} from 'lodash';
import {DebouncedNumberInput} from '../../../../../components/DebouncedInput/DebouncedNumerInput';
import type {PreparedPartitionData} from '../../../../../store/reducers/partitions/types';
import {formatNumber} from '../../../../../utils/dataFormatters/dataFormatters';
import {prepareErrorMessage} from '../../../../../utils/prepareErrorMessage';
import {safeParseNumber} from '../../../../../utils/utils';
import i18n from '../i18n';
import {useTopicDataQueryParams} from '../useTopicDataQueryParams';
import {b} from '../utils/constants';
import {TopicDataFilterValues} from '../utils/types';
import type {TopicDataFilterValue} from '../utils/types';
interface TopicDataControlsProps {
partitions?: PreparedPartitionData[];
partitionsLoading: boolean;
partitionsError: unknown;
startOffset?: number;
endOffset?: number;
scrollToOffset: (offset: number) => void;
handlePartitionChange?: (value: string[]) => void;
}
export function TopicDataControls({
handlePartitionChange,
startOffset,
endOffset,
partitions,
partitionsLoading,
partitionsError,
scrollToOffset,
}: TopicDataControlsProps) {
const {selectedPartition} = useTopicDataQueryParams();
const partitionsToSelect = partitions?.map(({partitionId}) => ({
content: String(partitionId),
value: String(partitionId),
}));
return (
);
}
interface TopicDataStartControlsProps {
scrollToOffset: (offset: number) => void;
}
function TopicDataStartControls({scrollToOffset}: TopicDataStartControlsProps) {
const {
selectedOffset,
startTimestamp,
topicDataFilter,
activeOffset,
handleSelectedOffsetChange,
handleStartTimestampChange,
handleTopicDataFilterChange,
} = useTopicDataQueryParams();
const inputRef = React.useRef(null);
const isDrawerVisible = !isNil(activeOffset);
React.useEffect(() => {
if (isDrawerVisible) {
inputRef.current?.blur();
}
}, [isDrawerVisible]);
const onFilterChange = React.useCallback(
(value: TopicDataFilterValue) => {
if (value === 'TIMESTAMP') {
handleSelectedOffsetChange(undefined);
} else {
handleStartTimestampChange(undefined);
}
handleTopicDataFilterChange(value);
},
[handleTopicDataFilterChange, handleSelectedOffsetChange, handleStartTimestampChange],
);
const onStartOffsetChange = React.useCallback(
(value: number | null) => {
handleSelectedOffsetChange(value);
},
[handleSelectedOffsetChange],
);
const handleFromTimestampChange = React.useCallback(
(data: Value | null) => {
let newValue: number | undefined;
if (data) {
const {value, type} = data;
if (type === 'absolute') {
newValue = value.valueOf();
} else if (type === 'relative') {
const absoluteValue = dateTimeParse(value);
newValue = absoluteValue ? absoluteValue.valueOf() : undefined;
}
}
handleStartTimestampChange(newValue);
},
[handleStartTimestampChange],
);
const startDateTime = dateTimeParse(Number(startTimestamp));
return (
value={topicDataFilter}
onUpdate={onFilterChange}
>
{TopicDataFilterValues.TIMESTAMP}
{TopicDataFilterValues.OFFSET}
{topicDataFilter === 'OFFSET' && (
}
autoFocus
/>
)}
{topicDataFilter === 'TIMESTAMP' && (
value.isAfter(dateTimeParse())}
label={i18n('label_from')}
onUpdate={handleFromTimestampChange}
defaultValue={startDateTime ? {type: 'absolute', value: startDateTime} : null}
className={b('date-picker')}
/>
)}
);
}