123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- import {useState} from 'react';
- import {browserHistory} from 'react-router';
- import styled from '@emotion/styled';
- import debounce from 'lodash/debounce';
- import {CompactSelect, SelectOption} from 'sentry/components/compactSelect';
- import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
- import {DEFAULT_DEBOUNCE_DURATION} from 'sentry/constants';
- import {t, tn} from 'sentry/locale';
- import {space} from 'sentry/styles/space';
- import {defined} from 'sentry/utils';
- import {getFormattedDate} from 'sentry/utils/dates';
- import {useLocation} from 'sentry/utils/useLocation';
- import {
- useReleases,
- useReleaseSelection,
- useReleaseStats,
- } from 'sentry/views/starfish/queries/useReleases';
- type Props = {
- selectorKey: string;
- selectorName?: string;
- selectorValue?: string;
- };
- export function ReleaseSelector({selectorName, selectorKey, selectorValue}: Props) {
- const [searchTerm, setSearchTerm] = useState<string | undefined>(undefined);
- const {data, isLoading} = useReleases(searchTerm);
- const {data: releaseStats} = useReleaseStats();
- const location = useLocation();
- const options: SelectOption<string>[] = [];
- if (defined(selectorValue)) {
- const index = data?.findIndex(({version}) => version === selectorValue);
- const selectedRelease = defined(index) ? data?.[index] : undefined;
- let selectedReleaseDate: string | undefined = undefined;
- if (defined(selectedRelease)) {
- selectedReleaseDate = selectedRelease.dateCreated;
- }
- options.push({
- value: selectorValue,
- label: selectorValue,
- details: (
- <LabelDetails
- sessionCount={releaseStats[selectorValue]?.['sum(session)']}
- dateCreated={selectedReleaseDate}
- />
- ),
- });
- }
- data
- ?.filter(({version}) => selectorValue !== version)
- .forEach(release => {
- const option = {
- value: release.version,
- label: release.version,
- details: (
- <LabelDetails
- sessionCount={releaseStats[release.version]?.['sum(session)']}
- dateCreated={release.dateCreated}
- />
- ),
- };
- options.push(option);
- });
- return (
- <StyledCompactSelect
- triggerProps={{
- prefix: selectorName,
- title: selectorValue,
- }}
- loading={isLoading}
- searchable
- value={selectorValue}
- options={[
- {
- value: '_releases',
- label: t('Sorted by date created'),
- options,
- },
- ]}
- onSearch={debounce(val => {
- setSearchTerm(val);
- }, DEFAULT_DEBOUNCE_DURATION)}
- onChange={newValue => {
- browserHistory.push({
- ...location,
- query: {
- ...location.query,
- [selectorKey]: newValue.value,
- },
- });
- }}
- onClose={() => {
- setSearchTerm(undefined);
- }}
- />
- );
- }
- type LabelDetailsProps = {
- dateCreated?: string;
- sessionCount?: number;
- };
- function LabelDetails(props: LabelDetailsProps) {
- return (
- <DetailsContainer>
- <div>
- {defined(props.sessionCount)
- ? tn('%s session', '%s sessions', props.sessionCount)
- : '-'}
- </div>
- <div>
- {defined(props.dateCreated)
- ? getFormattedDate(props.dateCreated, 'MMM D, YYYY')
- : null}
- </div>
- </DetailsContainer>
- );
- }
- export function ReleaseComparisonSelector() {
- const {primaryRelease, secondaryRelease} = useReleaseSelection();
- return (
- <PageFilterBar condensed>
- <ReleaseSelector
- selectorKey="primaryRelease"
- selectorValue={primaryRelease}
- selectorName={t('Release 1')}
- />
- <ReleaseSelector
- selectorKey="secondaryRelease"
- selectorName={t('Release 2')}
- selectorValue={secondaryRelease}
- />
- </PageFilterBar>
- );
- }
- const StyledCompactSelect = styled(CompactSelect)`
- @media (min-width: ${p => p.theme.breakpoints.medium}) {
- max-width: 275px;
- }
- `;
- const DetailsContainer = styled('div')`
- display: flex;
- flex-direction: row;
- justify-content: space-between;
- gap: ${space(1)};
- `;
|