groupActivity.tsx 4.5 KB

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