repositorySwitcher.tsx 1.3 KB

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