Skip to content

The open alternative to Salesforce, designed for AI.

3.0 KB
Raw Blame
import { useApolloAdminClient } from '@/settings/admin-panel/apollo/hooks/useApolloAdminClient';
import { CombinedGraphQLErrors } from '@apollo/client/errors';
import { useMutation } from '@apollo/client/react';
import { plural } from '@lingui/core/macro';
import { useState } from 'react';
import { isDefined } from 'twenty-shared/utils';
import { useToast } from 'twenty-ui/components/feedback';
import { DeleteJobsDocument } from '~/generated-admin/graphql';
import { getErrorMessageFromApolloError } from '~/utils/get-error-message-from-apollo-error.util';
import { useLingui } from '@lingui/react/macro';

export const useDeleteJobs = ({
  queueName,
  onSuccess,
}: {
  queueName: string;
  onSuccess?: () => void;
}) => {
  const { t } = useLingui();
  const apolloAdminClient = useApolloAdminClient();
  const { enqueueToast } = useToast();
  const [isDeleting, setIsDeleting] = useState(false);
  const [deleteJobsMutation] = useMutation(DeleteJobsDocument, {
    client: apolloAdminClient,
  });

  const deleteJobs = async (jobIds: string[]) => {
    setIsDeleting(true);

    try {
      const result = await deleteJobsMutation({
        variables: {
          queueName,
          jobIds,
        },
      });

      const response = result.data?.deleteJobs;

      if (isDefined(response)) {
        const { deletedCount, results } = response;
        const failedResults = results.filter((r) => !r.success);

        if (deletedCount > 0) {
          if (failedResults.length > 0) {
            enqueueToast({
              status: 'success',
              children: plural(deletedCount, {
                one: `Successfully deleted ${deletedCount} job`,
                other: `Successfully deleted ${deletedCount} jobs`,
              }),
            });
            enqueueToast({
              status: 'error',
              children: plural(failedResults.length, {
                one: `# job could not be deleted`,
                other: `# jobs could not be deleted`,
              }),
            });
          } else {
            enqueueToast({
              status: 'success',
              children: plural(deletedCount, {
                one: `Successfully deleted ${deletedCount} job`,
                other: `Successfully deleted ${deletedCount} jobs`,
              }),
            });
          }

          onSuccess?.();
        } else {
          const errorMessages = failedResults
            .map((r) => r.error)
            .filter(Boolean);
          const errorDetails =
            errorMessages.length > 0 ? `: ${errorMessages[0]}` : '';

          enqueueToast({
            status: 'error',
            children: t`No jobs were deleted${errorDetails}`,
          });
        }
      }
    } catch (error) {
      enqueueToast({
        status: 'error',
        children: CombinedGraphQLErrors.is(error)
          ? getErrorMessageFromApolloError(error)
          : t`Failed to delete jobs. Please try again later.`,
      });
    } finally {
      setIsDeleting(false);
    }
  };

  return {
    deleteJobs,
    isDeleting,
  };
};