async function upload()

in gemini/autocal/frontend/components/upload/UploadForm.tsx [60:119]


  async function upload() {
    try {
      setLoading(true);

      // Clear the current file selection so the same item can be re-uploaded
      setFileKey(new Date());

      if (!user || !user.email) {
        setError(
          "You must be logged in with a valid email address to upload a screenshot"
        );
        return;
      }
      if (!screenshot) {
        console.error("Screenshot is null");
        return;
      }

      const filePath = `screenshots/${user.email}/${id}-${screenshot.name}`;
      const docId = `${user.email}-${id}`;

      // Upload to Google Cloud Storage
      const storageRef = ref(firebaseStorage, filePath);
      await uploadString(storageRef, screenshot.base64, "base64");

      // Payload to save
      const screenshotUpload: ScreenshotUpload = {
        ID: docId,
        image: `gs://${firebaseStorage.app.options.storageBucket}/${filePath}`,
        type: screenshot.type,
      };
      status.active = true;
      const s: ProcessedScreenshot = {
        ID: docId,
        image: filePath,
        ...status,
      };

      const screenshotDocRef = doc(
        clientFirestore,
        "screenshots",
        docId
      ).withConverter(screenshotUploadConverter);
      const uploadDocRef = doc(clientFirestore, "state", docId).withConverter(
        processedScreenshotConverter
      );
      // Run as a transaction to update both collections at the same time
      await runTransaction(clientFirestore, async (transaction) => {
        transaction.set(screenshotDocRef, screenshotUpload);
        transaction.set(uploadDocRef, s);
      });
      setShowPreview(false);
    } catch (error) {
      console.error("Error uploading image:", error);
      setError(`${error}`);
    } finally {
      setLoading(false);
      setShowPreview(false);
    }
  }