groupActivity.tsx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 author={{type: 'user', user: me}}>
  142. {() => (
  143. <NoteInputWithStorage
  144. key={this.state.inputId}
  145. storageKey="groupinput:latest"
  146. itemKey={group.id}
  147. onCreate={this.handleNoteCreate}
  148. busy={this.state.createBusy}
  149. error={this.state.error}
  150. errorJSON={this.state.errorJSON}
  151. {...noteProps}
  152. />
  153. )}
  154. </ActivityItem>
  155. <Teams
  156. ids={uniq(
  157. group.activity
  158. .filter(
  159. (item): item is GroupActivityAssigned =>
  160. item.type === GroupActivityType.ASSIGNED &&
  161. item.data.assigneeType === 'team' &&
  162. item.data.assignee?.length > 0
  163. )
  164. .map(item => item.data.assignee)
  165. )}
  166. >
  167. {({initiallyLoaded}) =>
  168. initiallyLoaded ? (
  169. group.activity.map(item => {
  170. const authorName = item.user ? item.user.name : 'Sentry';
  171. if (item.type === GroupActivityType.NOTE) {
  172. return (
  173. <ErrorBoundary mini key={`note-${item.id}`}>
  174. <Note
  175. showTime={false}
  176. text={item.data.text}
  177. noteId={item.id}
  178. user={item.user as User}
  179. dateCreated={item.dateCreated}
  180. authorName={authorName}
  181. onDelete={this.handleNoteDelete}
  182. onUpdate={this.handleNoteUpdate}
  183. {...noteProps}
  184. />
  185. </ErrorBoundary>
  186. );
  187. }
  188. return (
  189. <ErrorBoundary mini key={`item-${item.id}`}>
  190. <ActivityItem
  191. author={{
  192. type: item.user ? 'user' : 'system',
  193. user: item.user ?? undefined,
  194. }}
  195. date={item.dateCreated}
  196. header={
  197. <GroupActivityItem
  198. author={<ActivityAuthor>{authorName}</ActivityAuthor>}
  199. activity={item}
  200. organization={organization}
  201. projectId={group.project.id}
  202. />
  203. }
  204. />
  205. </ErrorBoundary>
  206. );
  207. })
  208. ) : (
  209. <LoadingIndicator />
  210. )
  211. }
  212. </Teams>
  213. </Layout.Main>
  214. </Layout.Body>
  215. </Fragment>
  216. );
  217. }
  218. }
  219. export {GroupActivity};
  220. export default withApi(withOrganization(GroupActivity));