groupActivity.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import {Fragment, useCallback, useMemo} from 'react';
  2. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  3. import type {
  4. TContext,
  5. TData,
  6. TError,
  7. TVariables,
  8. } from 'sentry/components/feedback/useMutateActivity';
  9. import useMutateActivity 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 type {NoteType} from 'sentry/types/alerts';
  15. import type {
  16. Group,
  17. GroupActivity as GroupActivityType,
  18. GroupActivityNote,
  19. GroupActivityReprocess,
  20. } from 'sentry/types/group';
  21. import type {RouteComponentProps} from 'sentry/types/legacyReactRouter';
  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. export type MutateActivityOptions = MutateOptions<TData, TError, TVariables, TContext>;
  35. function GroupActivity({group}: Props) {
  36. const organization = useOrganization();
  37. const {activity: activities, count, id: groupId} = group;
  38. const groupCount = Number(count);
  39. const mostRecentActivity = getGroupMostRecentActivity(activities);
  40. const reprocessingStatus = getGroupReprocessingStatus(group, mostRecentActivity);
  41. const mutators = useMutateActivity({
  42. organization,
  43. group,
  44. });
  45. const deleteOptions: MutateActivityOptions = 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: MutateActivityOptions = 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: MutateActivityOptions = useMemo(() => {
  67. return {
  68. onError: () => {
  69. addErrorMessage(t('Unable to update comment'));
  70. },
  71. onSuccess: data => {
  72. const d = data as GroupActivityNote;
  73. GroupStore.updateActivity(group.id, data.id, {text: d.data.text});
  74. addSuccessMessage(t('Comment updated'));
  75. },
  76. };
  77. }, [group.id]);
  78. const handleDelete = useCallback(
  79. (item: GroupActivityType) => {
  80. const restore = group.activity.find(activity => activity.id === item.id);
  81. const index = GroupStore.removeActivity(group.id, item.id);
  82. if (index === -1 || restore === undefined) {
  83. addErrorMessage(t('Failed to delete comment'));
  84. return;
  85. }
  86. mutators.handleDelete(
  87. item.id,
  88. group.activity.filter(a => a.id !== item.id),
  89. deleteOptions
  90. );
  91. },
  92. [deleteOptions, group.activity, mutators, group.id]
  93. );
  94. const handleCreate = useCallback(
  95. (n: NoteType, _me: User) => {
  96. mutators.handleCreate(n, group.activity, createOptions);
  97. },
  98. [createOptions, group.activity, mutators]
  99. );
  100. const handleUpdate = useCallback(
  101. (item: GroupActivityType, n: NoteType) => {
  102. mutators.handleUpdate(n, item.id, group.activity, updateOptions);
  103. },
  104. [updateOptions, group.activity, mutators]
  105. );
  106. return (
  107. <Fragment>
  108. {(reprocessingStatus === ReprocessingStatus.REPROCESSED_AND_HASNT_EVENT ||
  109. reprocessingStatus === ReprocessingStatus.REPROCESSED_AND_HAS_EVENT) && (
  110. <ReprocessedBox
  111. reprocessActivity={mostRecentActivity as GroupActivityReprocess}
  112. groupCount={groupCount}
  113. orgSlug={organization.slug}
  114. groupId={groupId}
  115. />
  116. )}
  117. <Layout.Body>
  118. <Layout.Main>
  119. <ActivitySection
  120. group={group}
  121. onDelete={handleDelete}
  122. onCreate={handleCreate}
  123. onUpdate={handleUpdate}
  124. placeholderText={t(
  125. 'Add details or updates to this event. \nTag users with @, or teams with #'
  126. )}
  127. />
  128. </Layout.Main>
  129. </Layout.Body>
  130. </Fragment>
  131. );
  132. }
  133. export default GroupActivity;