userBadge.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import styled from '@emotion/styled';
  2. import UserAvatar from 'sentry/components/avatar/userAvatar';
  3. import space from 'sentry/styles/space';
  4. import {AvatarUser} from 'sentry/types';
  5. type Props = {
  6. avatarSize?: React.ComponentProps<typeof UserAvatar>['size'];
  7. className?: string;
  8. displayEmail?: React.ReactNode | string;
  9. displayName?: React.ReactNode;
  10. hideEmail?: boolean;
  11. user?: AvatarUser;
  12. };
  13. const UserBadge = ({
  14. avatarSize = 24,
  15. hideEmail = false,
  16. displayName,
  17. displayEmail,
  18. user,
  19. className,
  20. }: Props) => {
  21. const title =
  22. displayName ||
  23. (user &&
  24. (user.name ||
  25. user.email ||
  26. user.username ||
  27. user.ipAddress ||
  28. // Because this can be used to render EventUser models, or User *interface*
  29. // objects from serialized Event models. we try both ipAddress and ip_address.
  30. user.ip_address ||
  31. user.ip ||
  32. user.id));
  33. return (
  34. <StyledUserBadge className={className}>
  35. <StyledAvatar user={user} size={avatarSize} />
  36. <StyledNameAndEmail>
  37. <StyledName hideEmail={!!hideEmail}>{title}</StyledName>
  38. {!hideEmail && <StyledEmail>{displayEmail || (user && user.email)}</StyledEmail>}
  39. </StyledNameAndEmail>
  40. </StyledUserBadge>
  41. );
  42. };
  43. const StyledUserBadge = styled('div')`
  44. display: flex;
  45. align-items: center;
  46. `;
  47. const StyledNameAndEmail = styled('div')`
  48. flex-shrink: 1;
  49. min-width: 0;
  50. line-height: normal;
  51. `;
  52. const StyledEmail = styled('div')`
  53. font-size: 0.875em;
  54. margin-top: ${space(0.25)};
  55. color: ${p => p.theme.gray300};
  56. ${p => p.theme.overflowEllipsis};
  57. `;
  58. export const StyledName = styled('span')<{hideEmail: boolean}>`
  59. font-weight: ${p => (p.hideEmail ? 'inherit' : 'bold')};
  60. line-height: 1.15em;
  61. ${p => p.theme.overflowEllipsis};
  62. `;
  63. const StyledAvatar = styled(UserAvatar)`
  64. min-width: ${space(3)};
  65. min-height: ${space(3)};
  66. margin-right: ${space(1)};
  67. `;
  68. export default UserBadge;