groupActivity.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import {Fragment, useCallback, useMemo} from 'react';
  2. import {RouteComponentProps} from 'react-router';
  3. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  4. import useMutateActivity, {
  5. TContext,
  6. TData,
  7. TError,
  8. TVariables,
  9. } from 'sentry/components/feedback/useMutateActivity';
  10. import * as Layout from 'sentry/components/layouts/thirds';
  11. import ReprocessedBox from 'sentry/components/reprocessedBox';
  12. import {t} from 'sentry/locale';
  13. import GroupStore from 'sentry/stores/groupStore';
  14. import {
  15. Group,
  16. GroupActivity as GroupActivityType,
  17. GroupActivityNote,
  18. GroupActivityReprocess,
  19. User,
  20. } from 'sentry/types';
  21. import {NoteType} from 'sentry/types/alerts';
  22. import {MutateOptions} from 'sentry/utils/queryClient';
  23. import useOrganization from 'sentry/utils/useOrganization';
  24. import ActivitySection from 'sentry/views/issueDetails/activitySection';
  25. import {
  26. getGroupMostRecentActivity,
  27. getGroupReprocessingStatus,
  28. ReprocessingStatus,
  29. } from 'sentry/views/issueDetails/utils';
  30. type Props = {
  31. group: Group;
  32. } & RouteComponentProps<{}, {}>;
  33. function GroupActivity({group}: Props) {
  34. const organization = useOrganization();
  35. const {activity: activities, count, id: groupId} = group;
  36. const groupCount = Number(count);
  37. const mostRecentActivity = getGroupMostRecentActivity(activities);
  38. const reprocessingStatus = getGroupReprocessingStatus(group, mostRecentActivity);
  39. const mutators = useMutateActivity({
  40. organization,
  41. group,
  42. });
  43. const deleteOptions: MutateOptions<TData, TError, TVariables, TContext> =
  44. useMemo(() => {
  45. return {
  46. onError: () => {
  47. addErrorMessage(t('Failed to delete comment'));
  48. },
  49. onSuccess: () => {
  50. addSuccessMessage(t('Comment removed'));
  51. },
  52. };
  53. }, []);
  54. const createOptions: MutateOptions<TData, TError, TVariables, TContext> =
  55. useMemo(() => {
  56. return {
  57. onError: () => {
  58. addErrorMessage(t('Unable to post comment'));
  59. },
  60. onSuccess: data => {
  61. GroupStore.addActivity(group.id, data);
  62. addSuccessMessage(t('Comment posted'));
  63. },
  64. };
  65. }, [group.id]);
  66. const updateOptions: MutateOptions<TData, TError, TVariables, TContext> =
  67. useMemo(() => {
  68. return {
  69. onError: () => {
  70. addErrorMessage(t('Unable to update comment'));
  71. },
  72. onSuccess: data => {
  73. const d = data as GroupActivityNote;
  74. GroupStore.updateActivity(group.id, data.id, {text: d.data.text});
  75. addSuccessMessage(t('Comment updated'));
  76. },
  77. };
  78. }, [group.id]);
  79. const handleDelete = useCallback(
  80. (item: GroupActivityType) => {
  81. const restore = group.activity.find(activity => activity.id === item.id);
  82. const index = GroupStore.removeActivity(group.id, item.id);
  83. if (index === -1 || restore === undefined) {
  84. addErrorMessage(t('Failed to delete comment'));
  85. return;
  86. }
  87. mutators.handleDelete(
  88. item.id,
  89. group.activity.filter(a => a.id !== item.id),
  90. deleteOptions
  91. );
  92. },
  93. [deleteOptions, group.activity, mutators, group.id]
  94. );
  95. const handleCreate = useCallback(
  96. (n: NoteType, _me: User) => {
  97. mutators.handleCreate(n, group.activity, createOptions);
  98. },
  99. [createOptions, group.activity, mutators]
  100. );
  101. const handleUpdate = useCallback(
  102. (item: GroupActivityType, n: NoteType) => {
  103. mutators.handleUpdate(n, item.id, group.activity, updateOptions);
  104. },
  105. [updateOptions, group.activity, mutators]
  106. );
  107. return (
  108. <Fragment>
  109. {(reprocessingStatus === ReprocessingStatus.REPROCESSED_AND_HASNT_EVENT ||
  110. reprocessingStatus === ReprocessingStatus.REPROCESSED_AND_HAS_EVENT) && (
  111. <ReprocessedBox
  112. reprocessActivity={mostRecentActivity as GroupActivityReprocess}
  113. groupCount={groupCount}
  114. orgSlug={organization.slug}
  115. groupId={groupId}
  116. />
  117. )}
  118. <Layout.Body>
  119. <Layout.Main>
  120. <ActivitySection
  121. group={group}
  122. onDelete={handleDelete}
  123. onCreate={handleCreate}
  124. onUpdate={handleUpdate}
  125. placeholderText={t(
  126. 'Add details or updates to this event. \nTag users with @, or teams with #'
  127. )}
  128. />
  129. </Layout.Main>
  130. </Layout.Body>
  131. </Fragment>
  132. );
  133. }
  134. export default GroupActivity;