groupActivity.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import {Component, Fragment} from 'react';
  2. import {RouteComponentProps} from 'react-router';
  3. import uniq from 'lodash/uniq';
  4. import {createNote, deleteNote, updateNote} from 'sentry/actionCreators/group';
  5. import {
  6. addErrorMessage,
  7. addLoadingMessage,
  8. clearIndicators,
  9. } from 'sentry/actionCreators/indicator';
  10. import {Client} from 'sentry/api';
  11. import {ActivityAuthor} from 'sentry/components/activity/author';
  12. import {ActivityItem} from 'sentry/components/activity/item';
  13. import {Note} from 'sentry/components/activity/note';
  14. import {NoteInputWithStorage} from 'sentry/components/activity/note/inputWithStorage';
  15. import {CreateError} from 'sentry/components/activity/note/types';
  16. import ErrorBoundary from 'sentry/components/errorBoundary';
  17. import * as Layout from 'sentry/components/layouts/thirds';
  18. import LoadingIndicator from 'sentry/components/loadingIndicator';
  19. import ReprocessedBox from 'sentry/components/reprocessedBox';
  20. import {DEFAULT_ERROR_JSON} from 'sentry/constants';
  21. import {t} from 'sentry/locale';
  22. import ConfigStore from 'sentry/stores/configStore';
  23. import {
  24. Group,
  25. GroupActivityAssigned,
  26. GroupActivityReprocess,
  27. GroupActivityType,
  28. Organization,
  29. User,
  30. } from 'sentry/types';
  31. import {uniqueId} from 'sentry/utils/guid';
  32. import Teams from 'sentry/utils/teams';
  33. import withApi from 'sentry/utils/withApi';
  34. import withOrganization from 'sentry/utils/withOrganization';
  35. import GroupActivityItem from './groupActivityItem';
  36. import {
  37. getGroupMostRecentActivity,
  38. getGroupReprocessingStatus,
  39. ReprocessingStatus,
  40. } from './utils';
  41. type Props = {
  42. api: Client;
  43. group: Group;
  44. organization: Organization;
  45. } & RouteComponentProps<{}, {}>;
  46. type State = {
  47. createBusy: boolean;
  48. error: boolean;
  49. errorJSON: CreateError | null;
  50. inputId: string;
  51. };
  52. class GroupActivity extends Component<Props, State> {
  53. // TODO(dcramer): only re-render on group/activity change
  54. state: State = {
  55. createBusy: false,
  56. error: false,
  57. errorJSON: null,
  58. inputId: uniqueId(),
  59. };
  60. handleNoteDelete = async ({noteId, text: oldText}) => {
  61. const {api, group} = this.props;
  62. addLoadingMessage(t('Removing comment...'));
  63. try {
  64. await deleteNote(api, group, noteId, oldText);
  65. clearIndicators();
  66. } catch (_err) {
  67. addErrorMessage(t('Failed to delete comment'));
  68. }
  69. };
  70. /**
  71. * Note: This is nearly the same logic as `app/views/alerts/details/activity`
  72. * This can be abstracted a bit if we create more objects that can have activities
  73. */
  74. handleNoteCreate = async note => {
  75. const {api, group} = this.props;
  76. this.setState({
  77. createBusy: true,
  78. });
  79. addLoadingMessage(t('Posting comment...'));
  80. try {
  81. await createNote(api, group, note);
  82. this.setState({
  83. createBusy: false,
  84. // This is used as a `key` to Note Input so that after successful post
  85. // we reset the value of the input
  86. inputId: uniqueId(),
  87. });
  88. clearIndicators();
  89. } catch (error) {
  90. this.setState({
  91. createBusy: false,
  92. error: true,
  93. errorJSON: error.responseJSON || DEFAULT_ERROR_JSON,
  94. });
  95. addErrorMessage(t('Unable to post comment'));
  96. }
  97. };
  98. handleNoteUpdate = async (note, {noteId, text: oldText}) => {
  99. const {api, group} = this.props;
  100. addLoadingMessage(t('Updating comment...'));
  101. try {
  102. await updateNote(api, group, note, noteId, oldText);
  103. clearIndicators();
  104. } catch (error) {
  105. this.setState({
  106. error: true,
  107. errorJSON: error.responseJSON || DEFAULT_ERROR_JSON,
  108. });
  109. addErrorMessage(t('Unable to update comment'));
  110. }
  111. };
  112. render() {
  113. const {group, organization} = this.props;
  114. const {activity: activities, count, id: groupId} = group;
  115. const groupCount = Number(count);
  116. const mostRecentActivity = getGroupMostRecentActivity(activities);
  117. const reprocessingStatus = getGroupReprocessingStatus(group, mostRecentActivity);
  118. const me = ConfigStore.get('user');
  119. const projectSlugs = group && group.project ? [group.project.slug] : [];
  120. const noteProps = {
  121. minHeight: 140,
  122. group,
  123. projectSlugs,
  124. placeholder: t(
  125. 'Add details or updates to this event. \nTag users with @, or teams with #'
  126. ),
  127. };
  128. return (
  129. <Fragment>
  130. {(reprocessingStatus === ReprocessingStatus.REPROCESSED_AND_HASNT_EVENT ||
  131. reprocessingStatus === ReprocessingStatus.REPROCESSED_AND_HAS_EVENT) && (
  132. <ReprocessedBox
  133. reprocessActivity={mostRecentActivity as GroupActivityReprocess}
  134. groupCount={groupCount}
  135. orgSlug={organization.slug}
  136. groupId={groupId}
  137. />
  138. )}
  139. <Layout.Body>
  140. <Layout.Main>
  141. <ActivityItem noPadding author={{type: 'user', user: me}}>
  142. <NoteInputWithStorage
  143. key={this.state.inputId}
  144. storageKey="groupinput:latest"
  145. itemKey={group.id}
  146. onCreate={this.handleNoteCreate}
  147. busy={this.state.createBusy}
  148. error={this.state.error}
  149. errorJSON={this.state.errorJSON}
  150. {...noteProps}
  151. />
  152. </ActivityItem>
  153. <Teams
  154. ids={uniq(
  155. group.activity
  156. .filter(
  157. (item): item is GroupActivityAssigned =>
  158. item.type === GroupActivityType.ASSIGNED &&
  159. item.data.assigneeType === 'team' &&
  160. item.data.assignee?.length > 0
  161. )
  162. .map(item => item.data.assignee)
  163. )}
  164. >
  165. {({initiallyLoaded}) =>
  166. initiallyLoaded ? (
  167. group.activity.map(item => {
  168. const authorName = item.user ? item.user.name : 'Sentry';
  169. if (item.type === GroupActivityType.NOTE) {
  170. return (
  171. <ErrorBoundary mini key={`note-${item.id}`}>
  172. <Note
  173. showTime={false}
  174. text={item.data.text}
  175. noteId={item.id}
  176. user={item.user as User}
  177. dateCreated={item.dateCreated}
  178. authorName={authorName}
  179. onDelete={this.handleNoteDelete}
  180. onUpdate={this.handleNoteUpdate}
  181. {...noteProps}
  182. />
  183. </ErrorBoundary>
  184. );
  185. }
  186. return (
  187. <ErrorBoundary mini key={`item-${item.id}`}>
  188. <ActivityItem
  189. author={{
  190. type: item.user ? 'user' : 'system',
  191. user: item.user ?? undefined,
  192. }}
  193. date={item.dateCreated}
  194. header={
  195. <GroupActivityItem
  196. author={<ActivityAuthor>{authorName}</ActivityAuthor>}
  197. activity={item}
  198. organization={organization}
  199. projectId={group.project.id}
  200. />
  201. }
  202. />
  203. </ErrorBoundary>
  204. );
  205. })
  206. ) : (
  207. <LoadingIndicator />
  208. )
  209. }
  210. </Teams>
  211. </Layout.Main>
  212. </Layout.Body>
  213. </Fragment>
  214. );
  215. }
  216. }
  217. export {GroupActivity};
  218. export default withApi(withOrganization(GroupActivity));