actorAvatar.tsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 MemberListStore from 'sentry/stores/memberListStore';
  6. import type {Actor} from 'sentry/types';
  7. import {useTeamsById} from 'sentry/utils/useTeamsById';
  8. import type {BaseAvatarProps} from './baseAvatar';
  9. interface Props extends BaseAvatarProps {
  10. actor: Actor;
  11. }
  12. /**
  13. * Wrapper to assist loading the team from api or store
  14. */
  15. function LoadTeamAvatar({
  16. teamId,
  17. ...props
  18. }: {teamId: string} & Omit<React.ComponentProps<typeof TeamAvatar>, 'team'>) {
  19. const {teams, isLoading} = useTeamsById({ids: [teamId]});
  20. const team = teams.find(t => t.id === teamId);
  21. if (isLoading) {
  22. return <LoadingIndicator mini />;
  23. }
  24. return <TeamAvatar team={team} {...props} />;
  25. }
  26. function ActorAvatar({size = 24, hasTooltip = true, actor, ...props}: Props) {
  27. const otherProps = {
  28. size,
  29. hasTooltip,
  30. ...props,
  31. };
  32. if (actor.type === 'user') {
  33. const user = actor.id ? MemberListStore.getById(actor.id) ?? actor : actor;
  34. return <UserAvatar user={user} {...otherProps} />;
  35. }
  36. if (actor.type === 'team') {
  37. return <LoadTeamAvatar teamId={actor.id} {...otherProps} />;
  38. }
  39. Sentry.withScope(scope => {
  40. scope.setExtra('actor', actor);
  41. Sentry.captureException(new Error('Unknown avatar type'));
  42. });
  43. return null;
  44. }
  45. export default ActorAvatar;