Skip to content

Automate browser based workflows with AI

1.8 KB
Raw Blame
import { useMutation, useQueryClient } from "@tanstack/react-query";

import { getClient } from "@/api/AxiosClient";
import { toast } from "@/components/ui/use-toast";
import { useCredentialGetter } from "@/hooks/useCredentialGetter";

const useCloseBrowserSessionMutation = ({
  browserSessionId,
  external = false,
  onSuccess,
}: {
  browserSessionId?: string;
  external?: boolean;
  onSuccess?: () => void;
}) => {
  const credentialGetter = useCredentialGetter();
  const queryClient = useQueryClient();

  const closeBrowserSessionMutation = useMutation({
    mutationFn: async () => {
      if (!browserSessionId) {
        console.warn("No browserSessionId provided for close mutation");
        return;
      }

      const client = await getClient(credentialGetter, "sans-api-v1");

      return client
        .post(`/browser_sessions/${browserSessionId}/close`)
        .then((response) => response.data);
    },
    onSuccess: () => {
      queryClient.invalidateQueries({
        queryKey: ["browser_sessions"],
      });
      queryClient.invalidateQueries({
        queryKey: ["browserSession", browserSessionId],
      });
      toast(
        external
          ? {
              variant: "success",
              title: "Disconnected",
              description:
                "Skyvern disconnected from the browser. The browser itself keeps running.",
            }
          : {
              variant: "success",
              title: "Browser Session Closed",
              description: "The browser session has been successfully closed.",
            },
      );
      onSuccess?.();
    },
    onError: (error) => {
      toast({
        variant: "destructive",
        title: "Error",
        description: error.message,
      });
    },
  });

  return closeBrowserSessionMutation;
};

export { useCloseBrowserSessionMutation };