File size: 1,401 Bytes
246d201
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import React from "react";
import toast from "react-hot-toast";
import { useDispatch, useSelector } from "react-redux";
import { setImportedProjectZip } from "#/state/initial-query-slice";
import { RootState } from "#/store";
import { base64ToBlob } from "#/utils/base64-to-blob";
import { useUploadFiles } from "../../../hooks/mutation/use-upload-files";

import { RUNTIME_INACTIVE_STATES } from "#/types/agent-state";

export const useHandleRuntimeActive = () => {
  const dispatch = useDispatch();

  const { mutate: uploadFiles } = useUploadFiles();
  const { curAgentState } = useSelector((state: RootState) => state.agent);

  const runtimeActive = !RUNTIME_INACTIVE_STATES.includes(curAgentState);

  const { importedProjectZip } = useSelector(
    (state: RootState) => state.initialQuery,
  );

  const handleUploadFiles = (zip: string) => {
    const blob = base64ToBlob(zip);
    const file = new File([blob], "imported-project.zip", {
      type: blob.type,
    });
    uploadFiles(
      { files: [file] },
      {
        onError: () => {
          toast.error("Failed to upload project files.");
        },
      },
    );
    dispatch(setImportedProjectZip(null));
  };

  React.useEffect(() => {
    if (runtimeActive && importedProjectZip) {
      handleUploadFiles(importedProjectZip);
    }
  }, [runtimeActive, importedProjectZip]);
};