useMutateActivity.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import {useCallback} from 'react';
  2. import type {Group, GroupActivity, Organization} from 'sentry/types';
  3. import {NoteType} from 'sentry/types/alerts';
  4. import {fetchMutation, MutateOptions, useMutation} from 'sentry/utils/queryClient';
  5. import useApi from 'sentry/utils/useApi';
  6. type TPayload = {activity: GroupActivity[]; note?: NoteType; noteId?: string};
  7. type TMethod = 'PUT' | 'POST' | 'DELETE';
  8. export type TData = GroupActivity;
  9. export type TError = unknown;
  10. export type TVariables = [TPayload, TMethod];
  11. export type TContext = unknown;
  12. export type DeleteCommentCallback = (
  13. noteId: string,
  14. activity: GroupActivity[],
  15. options?: MutateOptions<TData, TError, TVariables, TContext>
  16. ) => void;
  17. export type CreateCommentCallback = (
  18. note: NoteType,
  19. activity: GroupActivity[],
  20. options?: MutateOptions<TData, TError, TVariables, TContext>
  21. ) => void;
  22. export type UpdateCommentCallback = (
  23. note: NoteType,
  24. noteId: string,
  25. activity: GroupActivity[],
  26. options?: MutateOptions<TData, TError, TVariables, TContext>
  27. ) => void;
  28. interface Props {
  29. group: Group;
  30. organization: Organization;
  31. onMutate?: (variables: TVariables) => unknown | undefined;
  32. onSettled?:
  33. | ((
  34. data: unknown,
  35. error: unknown,
  36. variables: TVariables,
  37. context: unknown
  38. ) => unknown)
  39. | undefined;
  40. }
  41. export default function useMutateActivity({
  42. organization,
  43. group,
  44. onMutate,
  45. onSettled,
  46. }: Props) {
  47. const api = useApi({
  48. persistInFlight: false,
  49. });
  50. const mutation = useMutation<TData, TError, TVariables, TContext>({
  51. onMutate: onMutate ?? undefined,
  52. mutationFn: ([{note, noteId}, method]) => {
  53. const url =
  54. method === 'PUT' || method === 'DELETE'
  55. ? `/organizations/${organization.slug}/issues/${group.id}/comments/${noteId}/`
  56. : `/organizations/${organization.slug}/issues/${group.id}/comments/`;
  57. return fetchMutation(api)([
  58. method,
  59. url,
  60. {},
  61. {text: note?.text, mentions: note?.mentions},
  62. ]);
  63. },
  64. onSettled: onSettled ?? undefined,
  65. cacheTime: 0,
  66. });
  67. const handleUpdate = useCallback<UpdateCommentCallback>(
  68. (note, noteId, activity, options) => {
  69. mutation.mutate([{note, noteId, activity}, 'PUT'], options);
  70. },
  71. [mutation]
  72. );
  73. const handleCreate = useCallback<CreateCommentCallback>(
  74. (note, activity, options) => {
  75. mutation.mutate([{note, activity}, 'POST'], options);
  76. },
  77. [mutation]
  78. );
  79. const handleDelete = useCallback<DeleteCommentCallback>(
  80. (noteId, activity, options) => {
  81. mutation.mutate([{noteId, activity}, 'DELETE'], options);
  82. },
  83. [mutation]
  84. );
  85. return {
  86. handleUpdate,
  87. handleCreate,
  88. handleDelete,
  89. };
  90. }