projectBadge.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import {cloneElement} from 'react';
  2. import styled from '@emotion/styled';
  3. import BadgeDisplayName from 'sentry/components/idBadge/badgeDisplayName';
  4. import BaseBadge from 'sentry/components/idBadge/baseBadge';
  5. import Link, {LinkProps} from 'sentry/components/links/link';
  6. import {Organization} from 'sentry/types';
  7. import withOrganization from 'sentry/utils/withOrganization';
  8. type BaseBadgeProps = React.ComponentProps<typeof BaseBadge>;
  9. type Project = NonNullable<BaseBadgeProps['project']>;
  10. interface Props
  11. extends Partial<Omit<BaseBadgeProps, 'project' | 'organization' | 'team'>> {
  12. project: Project;
  13. className?: string;
  14. /**
  15. * If true, this component will not be a link to project details page
  16. */
  17. disableLink?: boolean;
  18. /**
  19. * If true, will use default max-width, or specify one as a string
  20. */
  21. hideOverflow?: boolean | string;
  22. organization?: Organization;
  23. /**
  24. * Overrides where the project badge links
  25. */
  26. to?: LinkProps['to'];
  27. }
  28. const ProjectBadge = ({
  29. project,
  30. organization,
  31. to,
  32. hideOverflow = true,
  33. disableLink = false,
  34. className,
  35. ...props
  36. }: Props) => {
  37. const {slug, id} = project;
  38. const badge = (
  39. <BaseBadge
  40. displayName={
  41. <BadgeDisplayName hideOverflow={hideOverflow}>{slug}</BadgeDisplayName>
  42. }
  43. project={project}
  44. {...props}
  45. />
  46. );
  47. if (!disableLink && organization?.slug) {
  48. const defaultTo = `/organizations/${organization.slug}/projects/${slug}/${
  49. id ? `?project=${id}` : ''
  50. }`;
  51. return (
  52. <StyledLink to={to ?? defaultTo} className={className}>
  53. {badge}
  54. </StyledLink>
  55. );
  56. }
  57. return cloneElement(badge, {className});
  58. };
  59. const StyledLink = styled(Link)`
  60. flex-shrink: 0;
  61. img:hover {
  62. cursor: pointer;
  63. }
  64. `;
  65. export default withOrganization(ProjectBadge);