ownedBy.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import styled from '@emotion/styled';
  2. import {openCreateOwnershipRule} from 'sentry/actionCreators/modal';
  3. import Access from 'sentry/components/acl/access';
  4. import ActorAvatar from 'sentry/components/avatar/actorAvatar';
  5. import Button from 'sentry/components/button';
  6. import Link from 'sentry/components/links/link';
  7. import QuestionTooltip from 'sentry/components/questionTooltip';
  8. import * as SidebarSection from 'sentry/components/sidebarSection';
  9. import {IconAdd, IconSettings, IconUser} from 'sentry/icons';
  10. import {t} from 'sentry/locale';
  11. import MemberListStore from 'sentry/stores/memberListStore';
  12. import {useLegacyStore} from 'sentry/stores/useLegacyStore';
  13. import space from 'sentry/styles/space';
  14. import type {Actor, Group, Organization, Project} from 'sentry/types';
  15. import {buildTeamId} from 'sentry/utils';
  16. interface OwnedByProps {
  17. group: Group;
  18. organization: Organization;
  19. project: Project;
  20. }
  21. function OwnedBy({group, project, organization}: OwnedByProps) {
  22. const memberList = useLegacyStore(MemberListStore);
  23. const owner = group.owners?.find(({type}) =>
  24. ['codeowners', 'ownershipRule'].includes(type)
  25. );
  26. let currentOwner: Actor | undefined;
  27. // converts a backend suggested owner to a suggested assignee
  28. if (owner) {
  29. const [ownerType, id] = owner.owner.split(':');
  30. if (ownerType === 'user') {
  31. const member = memberList.find(user => user.id === id);
  32. if (member) {
  33. currentOwner = {
  34. type: 'user',
  35. id,
  36. name: member.name,
  37. };
  38. }
  39. } else if (ownerType === 'team') {
  40. const teams = project?.teams ?? [];
  41. const team = teams.find(({id: teamId}) => buildTeamId(teamId) === owner.owner);
  42. if (team) {
  43. currentOwner = {
  44. type: 'team',
  45. id,
  46. name: team.slug,
  47. };
  48. }
  49. }
  50. }
  51. return (
  52. <SidebarSection.Wrap>
  53. <StyledSidebarTitle>
  54. <TitleWrapper>
  55. {t('Owned By')}
  56. <QuestionTooltip
  57. position="top"
  58. title={t(
  59. 'Set rules on which user or team owns an issue based on path, module, tag, or URL'
  60. )}
  61. size="sm"
  62. color="gray200"
  63. />
  64. </TitleWrapper>
  65. <ActionsWrapper>
  66. <Access access={['project:write']}>
  67. <Button
  68. type="button"
  69. onClick={() => {
  70. openCreateOwnershipRule({project, organization, issueId: group.id});
  71. }}
  72. aria-label={t('Create Ownership Rule')}
  73. icon={<IconAdd />}
  74. size="zero"
  75. borderless
  76. />
  77. </Access>
  78. <StyledLink
  79. to={`/settings/${organization.slug}/projects/${project.slug}/ownership/`}
  80. aria-label={t('Issue Owners Settings')}
  81. >
  82. <IconSettings />
  83. </StyledLink>
  84. </ActionsWrapper>
  85. </StyledSidebarTitle>
  86. <SidebarSection.Content>
  87. <ActorWrapper>
  88. {currentOwner ? (
  89. <ActorAvatar
  90. data-test-id="owner-avatar"
  91. actor={currentOwner}
  92. hasTooltip={false}
  93. size={24}
  94. />
  95. ) : (
  96. <IconWrapper>
  97. <IconUser size="md" />
  98. </IconWrapper>
  99. )}
  100. <ActorName>
  101. {currentOwner?.type === 'team'
  102. ? `#${currentOwner?.name}`
  103. : currentOwner?.name ?? t('No-one')}
  104. </ActorName>
  105. </ActorWrapper>
  106. </SidebarSection.Content>
  107. </SidebarSection.Wrap>
  108. );
  109. }
  110. export default OwnedBy;
  111. const ActorWrapper = styled('div')`
  112. display: flex;
  113. align-items: center;
  114. gap: ${space(1)};
  115. max-width: 85%;
  116. line-height: 1;
  117. `;
  118. const ActorName = styled('div')`
  119. line-height: 1.2;
  120. ${p => p.theme.overflowEllipsis}
  121. `;
  122. const IconWrapper = styled('div')`
  123. display: flex;
  124. padding: ${space(0.25)};
  125. `;
  126. const StyledLink = styled(Link)`
  127. display: flex;
  128. align-items: center;
  129. color: ${p => p.theme.textColor};
  130. `;
  131. const StyledSidebarTitle = styled(SidebarSection.Title)`
  132. display: flex;
  133. justify-content: space-between;
  134. align-items: center;
  135. `;
  136. const TitleWrapper = styled('div')`
  137. display: flex;
  138. align-items: center;
  139. gap: ${space(0.5)};
  140. `;
  141. const ActionsWrapper = styled('div')`
  142. display: flex;
  143. align-items: center;
  144. gap: ${space(1)};
  145. `;