index.tsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import styled from '@emotion/styled';
  2. import Link from 'sentry/components/links/link';
  3. import {t} from 'sentry/locale';
  4. import {space} from 'sentry/styles/space';
  5. import getRouteStringFromRoutes from 'sentry/utils/getRouteStringFromRoutes';
  6. import recreateRoute from 'sentry/utils/recreateRoute';
  7. import {useBreadcrumbsPathmap} from './context';
  8. import Crumb from './crumb';
  9. import Divider from './divider';
  10. import {OrganizationCrumb} from './organizationCrumb';
  11. import ProjectCrumb from './projectCrumb';
  12. import TeamCrumb from './teamCrumb';
  13. import type {RouteWithName} from './types';
  14. const MENUS = {
  15. Organization: OrganizationCrumb,
  16. Project: ProjectCrumb,
  17. Team: TeamCrumb,
  18. } as const;
  19. type Props = {
  20. params: {[param: string]: string | undefined};
  21. route: any;
  22. routes: RouteWithName[];
  23. className?: string;
  24. };
  25. function SettingsBreadcrumb({className, routes, params}: Props) {
  26. const pathMap = useBreadcrumbsPathmap();
  27. const lastRouteIndex = routes.map(r => !!r.name).lastIndexOf(true);
  28. return (
  29. <Breadcrumbs aria-label={t('Settings Breadcrumbs')} className={className}>
  30. {routes.map((route, i) => {
  31. if (!route.name) {
  32. return null;
  33. }
  34. const pathTitle = pathMap[getRouteStringFromRoutes(routes.slice(0, i + 1))];
  35. const isLast = i === lastRouteIndex;
  36. const createMenu = MENUS[route.name];
  37. const Menu = typeof createMenu === 'function' && createMenu;
  38. const hasMenu = !!Menu;
  39. if (hasMenu) {
  40. return (
  41. <Menu
  42. key={`${route.name}:${route.path}`}
  43. routes={routes}
  44. params={params}
  45. route={route}
  46. isLast={isLast}
  47. />
  48. );
  49. }
  50. return (
  51. <Crumb key={`${route.name}:${route.path}`}>
  52. <CrumbLink to={recreateRoute(route, {routes, params})}>
  53. {pathTitle || route.name}
  54. </CrumbLink>
  55. <Divider isLast={isLast} />
  56. </Crumb>
  57. );
  58. })}
  59. </Breadcrumbs>
  60. );
  61. }
  62. const CrumbLink = styled(Link)`
  63. display: block;
  64. color: ${p => p.theme.subText};
  65. &:hover {
  66. color: ${p => p.theme.textColor};
  67. }
  68. `;
  69. const Breadcrumbs = styled('nav')`
  70. display: flex;
  71. gap: ${space(0.75)};
  72. align-items: center;
  73. `;
  74. export {CrumbLink};
  75. export default SettingsBreadcrumb;