repositorySwitcher.tsx 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. label: <RepoLabel>{repo.name}</RepoLabel>,
  34. }))}
  35. onChange={opt => this.handleRepoFilterChange(opt?.value)}
  36. />
  37. );
  38. }
  39. }
  40. export default RepositorySwitcher;
  41. const StyledCompactSelect = styled(CompactSelect)`
  42. margin-bottom: ${space(1)};
  43. `;
  44. const RepoLabel = styled('div')`
  45. ${p => p.theme.overflowEllipsis}
  46. `;