groupActivity.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import {Fragment, useCallback, useEffect, 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 {useNavigate} from 'sentry/utils/useNavigate';
  25. import useOrganization from 'sentry/utils/useOrganization';
  26. import ActivitySection from 'sentry/views/issueDetails/activitySection';
  27. import {useGroupDetailsRoute} from 'sentry/views/issueDetails/useGroupDetailsRoute';
  28. import {
  29. getGroupMostRecentActivity,
  30. getGroupReprocessingStatus,
  31. ReprocessingStatus,
  32. useHasStreamlinedUI,
  33. } from 'sentry/views/issueDetails/utils';
  34. type Props = {
  35. group: Group;
  36. } & RouteComponentProps<{groupId: string; orgId: string}, {}>;
  37. export type MutateActivityOptions = MutateOptions<TData, TError, TVariables, TContext>;
  38. function GroupActivity({group}: Props) {
  39. const organization = useOrganization();
  40. const {activity: activities, count, id: groupId} = group;
  41. const groupCount = Number(count);
  42. const mostRecentActivity = getGroupMostRecentActivity(activities);
  43. const reprocessingStatus = getGroupReprocessingStatus(group, mostRecentActivity);
  44. const mutators = useMutateActivity({
  45. organization,
  46. group,
  47. });
  48. const deleteOptions: MutateActivityOptions = useMemo(() => {
  49. return {
  50. onError: () => {
  51. addErrorMessage(t('Failed to delete comment'));
  52. },
  53. onSuccess: () => {
  54. addSuccessMessage(t('Comment removed'));
  55. },
  56. };
  57. }, []);
  58. const createOptions: MutateActivityOptions = useMemo(() => {
  59. return {
  60. onError: () => {
  61. addErrorMessage(t('Unable to post comment'));
  62. },
  63. onSuccess: data => {
  64. GroupStore.addActivity(group.id, data);
  65. addSuccessMessage(t('Comment posted'));
  66. },
  67. };
  68. }, [group.id]);
  69. const updateOptions: MutateActivityOptions = useMemo(() => {
  70. return {
  71. onError: () => {
  72. addErrorMessage(t('Unable to update comment'));
  73. },
  74. onSuccess: data => {
  75. const d = data as GroupActivityNote;
  76. GroupStore.updateActivity(group.id, data.id, {text: d.data.text});
  77. addSuccessMessage(t('Comment updated'));
  78. },
  79. };
  80. }, [group.id]);
  81. const handleDelete = useCallback(
  82. (item: GroupActivityType) => {
  83. const restore = group.activity.find(activity => activity.id === item.id);
  84. const index = GroupStore.removeActivity(group.id, item.id);
  85. if (index === -1 || restore === undefined) {
  86. addErrorMessage(t('Failed to delete comment'));
  87. return;
  88. }
  89. mutators.handleDelete(
  90. item.id,
  91. group.activity.filter(a => a.id !== item.id),
  92. deleteOptions
  93. );
  94. },
  95. [deleteOptions, group.activity, mutators, group.id]
  96. );
  97. const handleCreate = useCallback(
  98. (n: NoteType, _me: User) => {
  99. mutators.handleCreate(n, group.activity, createOptions);
  100. },
  101. [createOptions, group.activity, mutators]
  102. );
  103. const handleUpdate = useCallback(
  104. (item: GroupActivityType, n: NoteType) => {
  105. mutators.handleUpdate(n, item.id, group.activity, updateOptions);
  106. },
  107. [updateOptions, group.activity, mutators]
  108. );
  109. return (
  110. <Fragment>
  111. {(reprocessingStatus === ReprocessingStatus.REPROCESSED_AND_HASNT_EVENT ||
  112. reprocessingStatus === ReprocessingStatus.REPROCESSED_AND_HAS_EVENT) && (
  113. <ReprocessedBox
  114. reprocessActivity={mostRecentActivity as GroupActivityReprocess}
  115. groupCount={groupCount}
  116. orgSlug={organization.slug}
  117. groupId={groupId}
  118. />
  119. )}
  120. <Layout.Body>
  121. <Layout.Main>
  122. <ActivitySection
  123. group={group}
  124. onDelete={handleDelete}
  125. onCreate={handleCreate}
  126. onUpdate={handleUpdate}
  127. placeholderText={t(
  128. 'Add details or updates to this event. \nTag users with @, or teams with #'
  129. )}
  130. />
  131. </Layout.Main>
  132. </Layout.Body>
  133. </Fragment>
  134. );
  135. }
  136. function GroupActivityRoute(props: Props) {
  137. const hasStreamlinedUI = useHasStreamlinedUI();
  138. const navigate = useNavigate();
  139. const {baseUrl} = useGroupDetailsRoute();
  140. // TODO(streamlined-ui): Activity will become a router redirect to the event details page
  141. useEffect(() => {
  142. if (hasStreamlinedUI) {
  143. navigate({
  144. ...props.location,
  145. pathname: baseUrl,
  146. });
  147. }
  148. }, [hasStreamlinedUI, navigate, baseUrl, props.location]);
  149. return <GroupActivity {...props} />;
  150. }
  151. export default GroupActivityRoute;