index.tsx 2.5 KB

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