index.tsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  37. const createMenu = MENUS[route.name];
  38. const Menu = typeof createMenu === 'function' && createMenu;
  39. const hasMenu = !!Menu;
  40. if (hasMenu) {
  41. return (
  42. <Menu
  43. key={`${route.name}:${route.path}`}
  44. routes={routes}
  45. params={params}
  46. route={route}
  47. isLast={isLast}
  48. />
  49. );
  50. }
  51. return (
  52. <Crumb key={`${route.name}:${route.path}`}>
  53. <CrumbLink to={recreateRoute(route, {routes, params})}>
  54. {pathTitle || route.name}
  55. </CrumbLink>
  56. <Divider isLast={isLast} />
  57. </Crumb>
  58. );
  59. })}
  60. </Breadcrumbs>
  61. );
  62. }
  63. const CrumbLink = styled(Link)`
  64. display: block;
  65. color: ${p => p.theme.subText};
  66. &:hover {
  67. color: ${p => p.theme.textColor};
  68. }
  69. `;
  70. const Breadcrumbs = styled('nav')`
  71. display: flex;
  72. gap: ${space(0.75)};
  73. align-items: center;
  74. `;
  75. export {CrumbLink};
  76. export default SettingsBreadcrumb;