actorAvatar.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import * as Sentry from '@sentry/react';
  2. import TeamAvatar from 'sentry/components/avatar/teamAvatar';
  3. import UserAvatar from 'sentry/components/avatar/userAvatar';
  4. import LoadingIndicator from 'sentry/components/loadingIndicator';
  5. import type {TooltipProps} from 'sentry/components/tooltip';
  6. import MemberListStore from 'sentry/stores/memberListStore';
  7. import type {Actor} from 'sentry/types';
  8. import {useTeamsById} from 'sentry/utils/useTeamsById';
  9. interface ActorAvatarProps {
  10. actor: Actor;
  11. className?: string;
  12. default?: string;
  13. gravatar?: boolean;
  14. hasTooltip?: boolean;
  15. onClick?: () => void;
  16. round?: boolean;
  17. size?: number;
  18. suggested?: boolean;
  19. title?: string;
  20. tooltip?: React.ReactNode;
  21. tooltipOptions?: Omit<TooltipProps, 'children' | 'title'>;
  22. }
  23. /**
  24. * Wrapper to assist loading the team from api or store
  25. */
  26. function LoadTeamAvatar({
  27. teamId,
  28. ...props
  29. }: {teamId: string} & Omit<React.ComponentProps<typeof TeamAvatar>, 'team'>) {
  30. const {teams, isLoading} = useTeamsById({ids: [teamId]});
  31. const team = teams.find(t => t.id === teamId);
  32. if (isLoading) {
  33. return <LoadingIndicator mini />;
  34. }
  35. return <TeamAvatar team={team} {...props} />;
  36. }
  37. function ActorAvatar({size = 24, hasTooltip = true, actor, ...props}: ActorAvatarProps) {
  38. const otherProps = {
  39. size,
  40. hasTooltip,
  41. ...props,
  42. };
  43. if (actor.type === 'user') {
  44. const user = actor.id ? MemberListStore.getById(actor.id) ?? actor : actor;
  45. return <UserAvatar user={user} {...otherProps} />;
  46. }
  47. if (actor.type === 'team') {
  48. return <LoadTeamAvatar teamId={actor.id} {...otherProps} />;
  49. }
  50. Sentry.withScope(scope => {
  51. scope.setExtra('actor', actor);
  52. Sentry.captureException(new Error('Unknown avatar type'));
  53. });
  54. return null;
  55. }
  56. export default ActorAvatar;