|
| 1 | +import {useEffect, useMemo, useRef, useState} from 'react'; |
| 2 | +import cn from 'bem-cn-lite'; |
| 3 | + |
| 4 | +import {Select, SelectOption} from '@yandex-cloud/uikit'; |
| 5 | + |
| 6 | +import EntityStatus from "../../../components/EntityStatus/EntityStatus"; |
| 7 | + |
| 8 | +import {getUsageSeverityForEntityStatus} from '../utils'; |
| 9 | + |
| 10 | +import i18n from './i18n'; |
| 11 | +import './UsageFilter.scss'; |
| 12 | + |
| 13 | +interface UsageFilterItem { |
| 14 | + threshold: number; |
| 15 | + count: number; |
| 16 | +} |
| 17 | + |
| 18 | +interface UsageFilterProps { |
| 19 | + className?: string; |
| 20 | + value?: string[]; |
| 21 | + groups?: UsageFilterItem[]; |
| 22 | + onChange?: (value: string[]) => void; |
| 23 | + debounce?: number; |
| 24 | + disabled?: boolean; |
| 25 | +} |
| 26 | + |
| 27 | +const b = cn('usage-filter'); |
| 28 | + |
| 29 | +export const UsageFilter = (props: UsageFilterProps) => { |
| 30 | + const { |
| 31 | + className, |
| 32 | + value = [], |
| 33 | + groups = [], |
| 34 | + onChange, |
| 35 | + debounce = 200, |
| 36 | + disabled, |
| 37 | + } = props; |
| 38 | + |
| 39 | + const [filterValue, setFilterValue] = useState(value); |
| 40 | + const timer = useRef<number>(); |
| 41 | + |
| 42 | + useEffect(() => { |
| 43 | + // sync inner state with external value |
| 44 | + setFilterValue((prevValue) => { |
| 45 | + if (prevValue.join(',') !== value.join(',')) { |
| 46 | + return value; |
| 47 | + } |
| 48 | + |
| 49 | + return prevValue; |
| 50 | + }); |
| 51 | + }, [value]); |
| 52 | + |
| 53 | + const options = useMemo(() => groups.map(({threshold, count}) => ({ |
| 54 | + value: String(threshold), |
| 55 | + text: `${threshold}%`, |
| 56 | + data: {count} |
| 57 | + })), [groups]); |
| 58 | + |
| 59 | + const handleUpdate = (newValue: string[]) => { |
| 60 | + setFilterValue(newValue); |
| 61 | + |
| 62 | + window.clearTimeout(timer.current); |
| 63 | + timer.current = window.setTimeout(() => { |
| 64 | + onChange?.(newValue); |
| 65 | + }, debounce); |
| 66 | + }; |
| 67 | + |
| 68 | + const maxWidth = Math.max(...groups.map(({count}) => count)); |
| 69 | + |
| 70 | + const renderOption = ({value, data, text}: SelectOption) => ( |
| 71 | + <div className={b('option')}> |
| 72 | + <EntityStatus |
| 73 | + className={b('option-title')} |
| 74 | + status={getUsageSeverityForEntityStatus(Number(value))} |
| 75 | + name={text} |
| 76 | + size="xs" |
| 77 | + /> |
| 78 | + <div className={b('option-meta')}> |
| 79 | + {i18n('groups_count', {count: data.count})} |
| 80 | + <div className={b('option-bar')} style={{width: `${data.count / maxWidth * 100}%`}} /> |
| 81 | + </div> |
| 82 | + </div> |
| 83 | + ); |
| 84 | + |
| 85 | + return ( |
| 86 | + <Select |
| 87 | + className={b(null, className)} |
| 88 | + label={i18n('label')} |
| 89 | + value={filterValue} |
| 90 | + placeholder={i18n('default_value')} |
| 91 | + options={options} |
| 92 | + multiple |
| 93 | + onUpdate={handleUpdate} |
| 94 | + renderOption={renderOption} |
| 95 | + getOptionHeight={() => 50} |
| 96 | + popupWidth={280} |
| 97 | + disabled={disabled} |
| 98 | + /> |
| 99 | + ); |
| 100 | +}; |
0 commit comments