useBulkEditFeedbacks.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import {useCallback} from 'react';
  2. import {
  3. addErrorMessage,
  4. addLoadingMessage,
  5. addSuccessMessage,
  6. } from 'sentry/actionCreators/indicator';
  7. import {openConfirmModal} from 'sentry/components/confirm';
  8. import type useListItemCheckboxState from 'sentry/components/feedback/list/useListItemCheckboxState';
  9. import useMutateFeedback from 'sentry/components/feedback/useMutateFeedback';
  10. import {t, tct} from 'sentry/locale';
  11. import {GroupStatus} from 'sentry/types/group';
  12. import {trackAnalytics} from 'sentry/utils/analytics';
  13. import {decodeList} from 'sentry/utils/queryString';
  14. import useLocationQuery from 'sentry/utils/url/useLocationQuery';
  15. import useOrganization from 'sentry/utils/useOrganization';
  16. const statusToButtonLabel: Record<string, string> = {
  17. resolved: t('Resolve'),
  18. unresolved: t('Unresolve'),
  19. ignored: t('Mark as Spam'),
  20. };
  21. const statusToText: Record<string, string> = {
  22. resolved: 'resolved',
  23. unresolved: 'unresolved',
  24. ignored: 'spam',
  25. };
  26. interface Props
  27. extends Pick<
  28. ReturnType<typeof useListItemCheckboxState>,
  29. 'deselectAll' | 'selectedIds'
  30. > {}
  31. export default function useBulkEditFeedbacks({deselectAll, selectedIds}: Props) {
  32. const organization = useOrganization();
  33. const queryView = useLocationQuery({
  34. fields: {
  35. project: decodeList,
  36. },
  37. });
  38. const {markAsRead, resolve} = useMutateFeedback({
  39. feedbackIds: selectedIds,
  40. organization,
  41. projectIds: queryView.project,
  42. });
  43. const onToggleResovled = useCallback(
  44. ({newMailbox, moveToInbox}: {newMailbox: GroupStatus; moveToInbox?: boolean}) => {
  45. openConfirmModal({
  46. bypass: Array.isArray(selectedIds) && selectedIds.length === 1,
  47. onConfirm: () => {
  48. if (newMailbox === GroupStatus.IGNORED) {
  49. // target action is marking as spam aka ignored
  50. trackAnalytics('feedback.mark-spam-clicked', {
  51. organization,
  52. type: 'bulk',
  53. });
  54. }
  55. addLoadingMessage(t('Updating feedbacks...'));
  56. resolve(newMailbox, {
  57. onError: () => {
  58. addErrorMessage(t('An error occurred while updating the feedbacks.'));
  59. },
  60. onSuccess: () => {
  61. addSuccessMessage(t('Updated feedbacks'));
  62. deselectAll();
  63. },
  64. });
  65. },
  66. message: moveToInbox
  67. ? t('Are you sure you want to move these feedbacks to the inbox?')
  68. : tct('Are you sure you want to mark these feedbacks as [status]?', {
  69. status: statusToText[newMailbox],
  70. }),
  71. confirmText: moveToInbox ? t('Move to Inbox') : statusToButtonLabel[newMailbox],
  72. withoutBold: true,
  73. });
  74. },
  75. [deselectAll, resolve, selectedIds, organization]
  76. );
  77. const onMarkAsRead = useCallback(
  78. () =>
  79. openConfirmModal({
  80. bypass: Array.isArray(selectedIds) && selectedIds.length === 1,
  81. onConfirm: () => {
  82. addLoadingMessage(t('Updating feedbacks...'));
  83. markAsRead(true, {
  84. onError: () => {
  85. addErrorMessage(t('An error occurred while updating the feedbacks.'));
  86. },
  87. onSuccess: () => {
  88. addSuccessMessage(t('Updated feedbacks'));
  89. deselectAll();
  90. },
  91. });
  92. },
  93. message: t('Are you sure you want to mark these feedbacks as read?'),
  94. confirmText: 'Mark read',
  95. }),
  96. [deselectAll, markAsRead, selectedIds]
  97. );
  98. const onMarkUnread = useCallback(
  99. () =>
  100. openConfirmModal({
  101. bypass: Array.isArray(selectedIds) && selectedIds.length === 1,
  102. onConfirm: () => {
  103. addLoadingMessage(t('Updating feedbacks...'));
  104. markAsRead(false, {
  105. onError: () => {
  106. addErrorMessage(t('An error occurred while updating the feedbacks.'));
  107. },
  108. onSuccess: () => {
  109. addSuccessMessage(t('Updated feedbacks'));
  110. deselectAll();
  111. },
  112. });
  113. },
  114. message: t('Are you sure you want to mark these feedbacks as unread?'),
  115. confirmText: 'Mark unread',
  116. }),
  117. [deselectAll, markAsRead, selectedIds]
  118. );
  119. return {
  120. onToggleResovled,
  121. onMarkAsRead,
  122. onMarkUnread,
  123. };
  124. }