import {useCallback, useMemo} from 'react'; import styled from '@emotion/styled'; import type {SelectOption} from 'sentry/components/compactSelect'; import {CompactSelect} from 'sentry/components/compactSelect'; import {t} from 'sentry/locale'; import type {Sort} from 'sentry/utils/discover/fields'; import type {Field} from 'sentry/views/explore/hooks/useSampleFields'; import {ToolbarHeading, ToolbarSection} from './styles'; interface ToolbarSortByProps { fields: Field[]; setSorts: (newSorts: Sort[]) => void; sorts: Sort[]; } export function ToolbarSortBy({fields, setSorts, sorts}: ToolbarSortByProps) { const fieldOptions: SelectOption[] = useMemo(() => { return fields.map(field => { return { label: field, value: field, }; }); }, [fields]); const setSortField = useCallback( (i: number, {value}: SelectOption) => { if (sorts[i]) { setSorts([ { field: value, kind: sorts[i].kind, }, ]); } }, [setSorts, sorts] ); const kindOptions: SelectOption[] = useMemo(() => { return [ { label: 'Descending', value: 'desc', }, { label: 'Ascending', value: 'asc', }, ]; }, []); const setSortKind = useCallback( (i: number, {value}: SelectOption) => { if (sorts[i]) { setSorts([ { field: sorts[i].field, kind: value, }, ]); } }, [setSorts, sorts] ); return ( {t('Sort By')} setSortField(0, newSortField)} /> setSortKind(0, newSortKind)} /> ); } const ToolbarContent = styled('div')` display: flex; `;