teamCrumb.tsx 1.9 KB

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