|
| 1 | +import {useMemo} from 'react'; |
| 2 | +import {useSearchParams} from 'react-router-dom'; |
| 3 | +import styled from '@emotion/styled'; |
| 4 | +import debounce from 'lodash/debounce'; |
| 5 | + |
| 6 | +import BaseSearchBar from 'sentry/components/searchBar'; |
| 7 | +import {t} from 'sentry/locale'; |
| 8 | +import {space} from 'sentry/styles/space'; |
| 9 | + |
| 10 | +const FILTER_TO_NAME = { |
| 11 | + slowestTests: 'Slowest Tests', |
| 12 | + flakyTests: 'Flaky Tests', |
| 13 | + failedTests: 'Failed Tests', |
| 14 | + skippedTests: 'Skipped Tests', |
| 15 | +}; |
| 16 | + |
| 17 | +type TestSearchBarProps = { |
| 18 | + testCount: number; |
| 19 | +}; |
| 20 | + |
| 21 | +export function TestSearchBar({testCount}: TestSearchBarProps) { |
| 22 | + const [searchParams, setSearchParams] = useSearchParams(); |
| 23 | + const term = searchParams.get('term') || ''; |
| 24 | + |
| 25 | + const filterBy = searchParams.get('filterBy') || ''; |
| 26 | + const testTitle = |
| 27 | + filterBy in FILTER_TO_NAME |
| 28 | + ? FILTER_TO_NAME[filterBy as keyof typeof FILTER_TO_NAME] |
| 29 | + : 'Tests'; |
| 30 | + const count = testCount > 999 ? `${(testCount / 1000).toFixed(1)}K` : testCount; |
| 31 | + const searchTitle = `${testTitle} (${count})`; |
| 32 | + |
| 33 | + const handleSearchChange = useMemo( |
| 34 | + () => |
| 35 | + debounce((newValue: string) => { |
| 36 | + const currentParams = Object.fromEntries(searchParams.entries()); |
| 37 | + |
| 38 | + if (newValue) { |
| 39 | + currentParams.term = newValue; |
| 40 | + } else { |
| 41 | + delete currentParams.term; |
| 42 | + } |
| 43 | + |
| 44 | + setSearchParams(currentParams); |
| 45 | + }, 500), |
| 46 | + [setSearchParams, searchParams] |
| 47 | + ); |
| 48 | + |
| 49 | + return ( |
| 50 | + <Container> |
| 51 | + <Title>{searchTitle}</Title> |
| 52 | + <StyledSearchBar |
| 53 | + placeholder={t('Search by test name')} |
| 54 | + onChange={handleSearchChange} |
| 55 | + query={term} |
| 56 | + /> |
| 57 | + </Container> |
| 58 | + ); |
| 59 | +} |
| 60 | + |
| 61 | +const StyledSearchBar = styled(BaseSearchBar)` |
| 62 | + flex: 1 1 auto; |
| 63 | + min-width: 0; |
| 64 | +`; |
| 65 | + |
| 66 | +const Title = styled('h2')` |
| 67 | + white-space: nowrap; |
| 68 | + flex-shrink: 0; |
| 69 | + margin: 0; |
| 70 | + font-size: ${p => p.theme.fontSize.xl}; |
| 71 | +`; |
| 72 | + |
| 73 | +const Container = styled('div')` |
| 74 | + display: flex; |
| 75 | + align-items: center; |
| 76 | + gap: ${space(1.5)}; |
| 77 | + width: 100%; |
| 78 | +`; |
0 commit comments