|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +import { |
| 9 | + EuiFilterButton, |
| 10 | + EuiFilterGroup, |
| 11 | + EuiFlexGroup, |
| 12 | + EuiFlexItem, |
| 13 | + EuiIconTip, |
| 14 | + useEuiTheme, |
| 15 | +} from '@elastic/eui'; |
| 16 | +import { i18n } from '@kbn/i18n'; |
| 17 | +import React, { useCallback, useEffect, useMemo, useState } from 'react'; |
| 18 | +import { css } from '@emotion/react'; |
| 19 | +import { |
| 20 | + useStreamsRoutingSelector, |
| 21 | + type DocumentMatchFilterOptions, |
| 22 | +} from './state_management/stream_routing_state_machine'; |
| 23 | + |
| 24 | +export interface DocumentMatchFilterControlsProps { |
| 25 | + initialFilter: DocumentMatchFilterOptions; |
| 26 | + onFilterChange: (filter: DocumentMatchFilterOptions) => void; |
| 27 | + matchedDocumentPercentage: number; |
| 28 | + isDisabled?: boolean; |
| 29 | +} |
| 30 | + |
| 31 | +export const DocumentMatchFilterControls = ({ |
| 32 | + initialFilter, |
| 33 | + onFilterChange, |
| 34 | + matchedDocumentPercentage, |
| 35 | + isDisabled = false, |
| 36 | +}: DocumentMatchFilterControlsProps) => { |
| 37 | + const { euiTheme } = useEuiTheme(); |
| 38 | + |
| 39 | + const [selectedFilter, setSelectedFilter] = useState<DocumentMatchFilterOptions>(initialFilter); |
| 40 | + |
| 41 | + const isIdleState = useStreamsRoutingSelector((snapshot) => snapshot).matches({ |
| 42 | + ready: 'idle', |
| 43 | + }); |
| 44 | + |
| 45 | + const handleFilterChanged = useCallback( |
| 46 | + (value: DocumentMatchFilterOptions) => { |
| 47 | + if (value === selectedFilter) return; |
| 48 | + |
| 49 | + const newFilter = selectedFilter === 'matched' ? 'unmatched' : 'matched'; |
| 50 | + onFilterChange(newFilter); |
| 51 | + setSelectedFilter(newFilter); |
| 52 | + }, |
| 53 | + [selectedFilter, onFilterChange] |
| 54 | + ); |
| 55 | + |
| 56 | + useEffect(() => { |
| 57 | + if (isIdleState) { |
| 58 | + handleFilterChanged('matched'); |
| 59 | + } |
| 60 | + }, [isIdleState, handleFilterChanged]); |
| 61 | + |
| 62 | + const filterButtonCss = useMemo( |
| 63 | + () => css` |
| 64 | + background-color: transparent !important; |
| 65 | + border: 0px !important; |
| 66 | +
|
| 67 | + &[aria-pressed='true']:not(:disabled) { |
| 68 | + color: ${euiTheme.colors.textParagraph} !important; |
| 69 | + } |
| 70 | + `, |
| 71 | + [euiTheme] |
| 72 | + ); |
| 73 | + |
| 74 | + return ( |
| 75 | + <EuiFlexItem grow={false} data-test-subj="routingPreviewFilterControls"> |
| 76 | + <EuiFlexGroup gutterSize="s" alignItems="center"> |
| 77 | + <EuiFlexItem grow={false}> |
| 78 | + <EuiFilterGroup compressed fullWidth> |
| 79 | + <EuiFilterButton |
| 80 | + aria-label={i18n.translate( |
| 81 | + 'xpack.streams.streamDetail.preview.filter.matchedAriaLabel', |
| 82 | + { defaultMessage: 'Filter for matched documents.' } |
| 83 | + )} |
| 84 | + data-test-subj="routingPreviewMatchedFilterButton" |
| 85 | + hasActiveFilters={selectedFilter === 'matched'} |
| 86 | + onClick={() => handleFilterChanged('matched')} |
| 87 | + isDisabled={isDisabled || isNaN(matchedDocumentPercentage)} |
| 88 | + isSelected={selectedFilter === 'matched'} |
| 89 | + badgeColor="success" |
| 90 | + grow={false} |
| 91 | + isToggle |
| 92 | + numActiveFilters={ |
| 93 | + isNaN(matchedDocumentPercentage) ? '' : `${matchedDocumentPercentage}%` |
| 94 | + } |
| 95 | + css={filterButtonCss} |
| 96 | + > |
| 97 | + {i18n.translate('xpack.streams.streamDetail.preview.filter.matched', { |
| 98 | + defaultMessage: 'Matched', |
| 99 | + })} |
| 100 | + </EuiFilterButton> |
| 101 | + <EuiFilterButton |
| 102 | + aria-label={i18n.translate( |
| 103 | + 'xpack.streams.streamDetail.preview.filter.unmatchedAriaLabel', |
| 104 | + { defaultMessage: 'Filter for unmatched documents.' } |
| 105 | + )} |
| 106 | + data-test-subj="routingPreviewUnmatchedFilterButton" |
| 107 | + hasActiveFilters={selectedFilter === 'unmatched'} |
| 108 | + onClick={() => handleFilterChanged('unmatched')} |
| 109 | + isDisabled={isDisabled || isNaN(matchedDocumentPercentage)} |
| 110 | + isSelected={selectedFilter === 'unmatched'} |
| 111 | + badgeColor="accent" |
| 112 | + grow={false} |
| 113 | + isToggle |
| 114 | + numActiveFilters={ |
| 115 | + isNaN(matchedDocumentPercentage) ? '' : `${100 - matchedDocumentPercentage}%` |
| 116 | + } |
| 117 | + css={filterButtonCss} |
| 118 | + > |
| 119 | + {i18n.translate('xpack.streams.streamDetail.preview.filter.unmatched', { |
| 120 | + defaultMessage: 'Unmatched', |
| 121 | + })} |
| 122 | + </EuiFilterButton> |
| 123 | + </EuiFilterGroup> |
| 124 | + </EuiFlexItem> |
| 125 | + <EuiFlexItem grow={false} data-test-subj="routingPreviewFilterControlsTooltip"> |
| 126 | + <EuiIconTip |
| 127 | + aria-label={i18n.translate( |
| 128 | + 'xpack.streams.streamRouting.previewMatchesTooltipAriaLabel', |
| 129 | + { |
| 130 | + defaultMessage: 'Additional information', |
| 131 | + } |
| 132 | + )} |
| 133 | + type={'question'} |
| 134 | + content={i18n.translate('xpack.streams.streamDetail.previewMatchesTooltipText', { |
| 135 | + defaultMessage: |
| 136 | + 'Approximate percentage of documents matching/unmatching the condition over a random sample of documents.', |
| 137 | + })} |
| 138 | + iconProps={{ style: { verticalAlign: 'text-bottom', marginLeft: 2 } }} |
| 139 | + /> |
| 140 | + </EuiFlexItem> |
| 141 | + </EuiFlexGroup> |
| 142 | + </EuiFlexItem> |
| 143 | + ); |
| 144 | +}; |
0 commit comments