groupActivity.tsx 7.2 KB

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