teamCrumb.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import type {RouteComponentProps} from 'react-router';
  2. import debounce from 'lodash/debounce';
  3. import IdBadge from 'sentry/components/idBadge';
  4. import {DEFAULT_DEBOUNCE_DURATION} from 'sentry/constants';
  5. import {browserHistory} from 'sentry/utils/browserHistory';
  6. import recreateRoute from 'sentry/utils/recreateRoute';
  7. import {useParams} from 'sentry/utils/useParams';
  8. import {useTeams} from 'sentry/utils/useTeams';
  9. import BreadcrumbDropdown from './breadcrumbDropdown';
  10. import MenuItem from './menuItem';
  11. import {CrumbLink} from '.';
  12. type Props = RouteComponentProps<{teamId: string}, {}>;
  13. function TeamCrumb({routes, route, ...props}: Props) {
  14. const {teams, onSearch, fetching} = useTeams();
  15. const params = useParams();
  16. const team = teams.find(({slug}) => slug === params.teamId);
  17. const hasMenu = teams.length > 1;
  18. const handleSearchChange = (e: React.ChangeEvent<HTMLInputElement>) => {
  19. onSearch(e.target.value);
  20. };
  21. const debouncedHandleSearch = debounce(handleSearchChange, DEFAULT_DEBOUNCE_DURATION);
  22. if (!team) {
  23. return null;
  24. }
  25. const teamUrl = `/settings/${params.orgId}/teams/${team.slug}/`;
  26. return (
  27. <BreadcrumbDropdown
  28. name={
  29. <CrumbLink to={teamUrl}>
  30. <IdBadge avatarSize={18} team={team} />
  31. </CrumbLink>
  32. }
  33. onSelect={item => {
  34. browserHistory.push(
  35. recreateRoute('', {
  36. routes,
  37. params: {...params, teamId: item.value},
  38. })
  39. );
  40. }}
  41. hasMenu={hasMenu}
  42. route={route}
  43. items={teams.map((teamItem, index) => ({
  44. index,
  45. value: teamItem.slug,
  46. label: (
  47. <MenuItem>
  48. <IdBadge team={teamItem} />
  49. </MenuItem>
  50. ),
  51. }))}
  52. onChange={debouncedHandleSearch}
  53. busyItemsStillVisible={fetching}
  54. {...props}
  55. />
  56. );
  57. }
  58. export default TeamCrumb;