activitySection.tsx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import {Fragment, useCallback, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  4. import {NoteBody} from 'sentry/components/activity/note/body';
  5. import {NoteInputWithStorage} from 'sentry/components/activity/note/inputWithStorage';
  6. import {Button} from 'sentry/components/button';
  7. import {Flex} from 'sentry/components/container/flex';
  8. import useMutateActivity from 'sentry/components/feedback/useMutateActivity';
  9. import Timeline from 'sentry/components/timeline';
  10. import TimeSince from 'sentry/components/timeSince';
  11. import {Tooltip} from 'sentry/components/tooltip';
  12. import {IconEllipsis} from 'sentry/icons';
  13. import {t, tct} from 'sentry/locale';
  14. import GroupStore from 'sentry/stores/groupStore';
  15. import {space} from 'sentry/styles/space';
  16. import textStyles from 'sentry/styles/text';
  17. import type {NoteType} from 'sentry/types/alerts';
  18. import type {Group, GroupActivity} from 'sentry/types/group';
  19. import {GroupActivityType} from 'sentry/types/group';
  20. import type {Team} from 'sentry/types/organization';
  21. import type {User} from 'sentry/types/user';
  22. import {uniqueId} from 'sentry/utils/guid';
  23. import useOrganization from 'sentry/utils/useOrganization';
  24. import {useTeamsById} from 'sentry/utils/useTeamsById';
  25. import {useUser} from 'sentry/utils/useUser';
  26. import {groupActivityTypeIconMapping} from 'sentry/views/issueDetails/streamline/groupActivityIcons';
  27. import getGroupActivityItem from 'sentry/views/issueDetails/streamline/groupActivityItem';
  28. import {NoteDropdown} from 'sentry/views/issueDetails/streamline/noteDropdown';
  29. import {SidebarSectionTitle} from 'sentry/views/issueDetails/streamline/sidebar';
  30. function TimelineItem({
  31. item,
  32. handleDelete,
  33. group,
  34. teams,
  35. }: {
  36. group: Group;
  37. handleDelete: (item: GroupActivity) => void;
  38. item: GroupActivity;
  39. teams: Team[];
  40. }) {
  41. const organization = useOrganization();
  42. const authorName = item.user ? item.user.name : 'Sentry';
  43. const {title, message} = getGroupActivityItem(
  44. item,
  45. organization,
  46. group.project.id,
  47. <strong>{authorName}</strong>,
  48. teams
  49. );
  50. const iconMapping = groupActivityTypeIconMapping[item.type];
  51. const Icon = iconMapping?.Component ?? null;
  52. return (
  53. <ActivityTimelineItem
  54. title={
  55. <Flex gap={space(0.5)} align="center" justify="flex-start">
  56. <TitleTooltip title={title} showOnlyOnOverflow skipWrapper>
  57. {title}
  58. </TitleTooltip>
  59. {item.type === GroupActivityType.NOTE && (
  60. <TitleDropdown onDelete={() => handleDelete(item)} user={item.user} />
  61. )}
  62. </Flex>
  63. }
  64. timestamp={<Timestamp date={item.dateCreated} tooltipProps={{skipWrapper: true}} />}
  65. icon={
  66. Icon && (
  67. <Icon
  68. {...iconMapping.defaultProps}
  69. {...iconMapping.propsFunction?.(item.data)}
  70. size="xs"
  71. />
  72. )
  73. }
  74. >
  75. {typeof message === 'string' ? (
  76. <NoteWrapper>
  77. <NoteBody text={message} />
  78. </NoteWrapper>
  79. ) : (
  80. message
  81. )}
  82. </ActivityTimelineItem>
  83. );
  84. }
  85. export default function StreamlinedActivitySection({group}: {group: Group}) {
  86. const organization = useOrganization();
  87. const {teams} = useTeamsById();
  88. const [showAll, setShowAll] = useState(false);
  89. const [inputId, setInputId] = useState(uniqueId());
  90. const activeUser = useUser();
  91. const projectSlugs = group?.project ? [group.project.slug] : [];
  92. const noteProps = {
  93. minHeight: 140,
  94. group,
  95. projectSlugs,
  96. placeholder: t('Add a comment...'),
  97. };
  98. const mutators = useMutateActivity({
  99. organization,
  100. group,
  101. });
  102. const handleDelete = useCallback(
  103. (item: GroupActivity) => {
  104. const restore = group.activity.find(activity => activity.id === item.id);
  105. const index = GroupStore.removeActivity(group.id, item.id);
  106. if (index === -1 || restore === undefined) {
  107. addErrorMessage(t('Failed to delete comment'));
  108. return;
  109. }
  110. mutators.handleDelete(
  111. item.id,
  112. group.activity.filter(a => a.id !== item.id),
  113. {
  114. onError: () => {
  115. addErrorMessage(t('Failed to delete comment'));
  116. },
  117. onSuccess: () => {
  118. addSuccessMessage(t('Comment removed'));
  119. },
  120. }
  121. );
  122. },
  123. [group.activity, mutators, group.id]
  124. );
  125. const handleCreate = useCallback(
  126. (n: NoteType, _me: User) => {
  127. mutators.handleCreate(n, group.activity, {
  128. onError: err => {
  129. const errMessage = err.responseJSON?.detail
  130. ? tct('Error: [msg]', {msg: err.responseJSON?.detail as string})
  131. : t('Unable to post comment');
  132. addErrorMessage(errMessage);
  133. },
  134. onSuccess: data => {
  135. GroupStore.addActivity(group.id, data);
  136. addSuccessMessage(t('Comment posted'));
  137. },
  138. });
  139. },
  140. [group.activity, mutators, group.id]
  141. );
  142. return (
  143. <div>
  144. <Flex justify="space-between" align="center">
  145. <SidebarSectionTitle>{t('Activity')}</SidebarSectionTitle>
  146. {showAll && (
  147. <TextButton borderless size="zero" onClick={() => setShowAll(false)}>
  148. {t('Collapse')}
  149. </TextButton>
  150. )}
  151. </Flex>
  152. <Timeline.Container>
  153. <NoteInputWithStorage
  154. key={inputId}
  155. storageKey="groupinput:latest"
  156. itemKey={group.id}
  157. onCreate={n => {
  158. handleCreate(n, activeUser);
  159. setInputId(uniqueId());
  160. }}
  161. source="issue-details"
  162. {...noteProps}
  163. />
  164. {(group.activity.length < 5 || showAll) &&
  165. group.activity.map(item => {
  166. return (
  167. <TimelineItem
  168. item={item}
  169. handleDelete={handleDelete}
  170. group={group}
  171. teams={teams}
  172. key={item.id}
  173. />
  174. );
  175. })}
  176. {!showAll && group.activity.length >= 5 && (
  177. <Fragment>
  178. {group.activity.slice(0, 2).map(item => {
  179. return (
  180. <TimelineItem
  181. item={item}
  182. handleDelete={handleDelete}
  183. group={group}
  184. teams={teams}
  185. key={item.id}
  186. />
  187. );
  188. })}
  189. <ActivityTimelineItem
  190. title={
  191. <TextButton
  192. aria-label={t('Show all activity')}
  193. onClick={() => setShowAll(true)}
  194. borderless
  195. size="zero"
  196. >
  197. {t('%s activities hidden', group.activity.length - 3)}
  198. </TextButton>
  199. }
  200. icon={<RotatedEllipsisIcon direction={'up'} />}
  201. />
  202. <TimelineItem
  203. item={group.activity[group.activity.length - 1]}
  204. handleDelete={handleDelete}
  205. group={group}
  206. teams={teams}
  207. key={group.activity[group.activity.length - 1].id}
  208. />
  209. </Fragment>
  210. )}
  211. </Timeline.Container>
  212. </div>
  213. );
  214. }
  215. const TitleTooltip = styled(Tooltip)`
  216. justify-self: start;
  217. overflow: hidden;
  218. text-overflow: ellipsis;
  219. white-space: nowrap;
  220. `;
  221. const TitleDropdown = styled(NoteDropdown)`
  222. font-weight: normal;
  223. `;
  224. const ActivityTimelineItem = styled(Timeline.Item)`
  225. align-items: center;
  226. grid-template-columns: 22px minmax(50px, 1fr) auto;
  227. `;
  228. const Timestamp = styled(TimeSince)`
  229. font-size: ${p => p.theme.fontSizeSmall};
  230. white-space: nowrap;
  231. `;
  232. const TextButton = styled(Button)`
  233. font-weight: ${p => p.theme.fontWeightNormal};
  234. font-size: ${p => p.theme.fontSizeSmall};
  235. color: ${p => p.theme.subText};
  236. `;
  237. const RotatedEllipsisIcon = styled(IconEllipsis)`
  238. transform: rotate(90deg) translateY(1px);
  239. `;
  240. const NoteWrapper = styled('div')`
  241. ${textStyles}
  242. `;