repositorySwitcher.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import {PureComponent} from 'react';
  2. import {InjectedRouter} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {Location} from 'history';
  5. import {CompactSelect} from 'sentry/components/compactSelect';
  6. import {t} from 'sentry/locale';
  7. import {space} from 'sentry/styles/space';
  8. import {Repository} from 'sentry/types';
  9. type Props = {
  10. location: Location;
  11. repositories: Array<Repository>;
  12. router: InjectedRouter;
  13. activeRepository?: Repository;
  14. };
  15. class RepositorySwitcher extends PureComponent<Props> {
  16. handleRepoFilterChange = (activeRepo: string) => {
  17. const {router, location} = this.props;
  18. router.push({
  19. ...location,
  20. query: {...location.query, cursor: undefined, activeRepo},
  21. });
  22. };
  23. render() {
  24. const {activeRepository, repositories} = this.props;
  25. const activeRepo = activeRepository?.name;
  26. return (
  27. <StyledCompactSelect
  28. triggerLabel={activeRepo}
  29. triggerProps={{prefix: t('Filter')}}
  30. value={activeRepo}
  31. options={repositories.map(repo => ({
  32. value: repo.name,
  33. textValue: repo.name,
  34. label: <RepoLabel>{repo.name}</RepoLabel>,
  35. }))}
  36. onChange={opt => this.handleRepoFilterChange(String(opt?.value))}
  37. />
  38. );
  39. }
  40. }
  41. export default RepositorySwitcher;
  42. const StyledCompactSelect = styled(CompactSelect)`
  43. margin-bottom: ${space(1)};
  44. `;
  45. const RepoLabel = styled('div')`
  46. ${p => p.theme.overflowEllipsis}
  47. `;