Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

2.4 KB
Raw Blame
import { useMemo } from "react";
import { useTranslation } from "react-i18next";
import type { IconName } from "@editor/ui/Icon";
import { useGoogleDrivePicker } from "@editor/hooks/useGoogleDrivePicker";
import { useOneDrivePicker } from "@editor/hooks/useOneDrivePicker";

export interface CloudPickOptions {
  multiple?: boolean;
  accept?: string;
}

export interface CloudFilePicker {
  id: "googledrive" | "onedrive";
  icon: IconName;
  name: string;
  actionLabel: string;
  failedTitle: string;
  testId: string;
  isLoading: boolean;
  error: string | null;
  reconnectNeeded: boolean;
  clearError: () => void;
  openPicker: (options?: CloudPickOptions) => Promise<File[]>;
}

export function useCloudFilePickers(): CloudFilePicker[] {
  const { t } = useTranslation();
  const {
    isEnabled: googleEnabled,
    isLoading: googleLoading,
    error: googleError,
    clearError: clearGoogleError,
    openPicker: openGooglePicker,
  } = useGoogleDrivePicker();
  const oneDrive = useOneDrivePicker();

  return useMemo(() => {
    const pickers: CloudFilePicker[] = [];
    if (googleEnabled) {
      pickers.push({
        id: "googledrive",
        icon: "googledrive",
        name: "Google Drive",
        actionLabel: t("fileSidebar.googleDrive", "Open from Google Drive"),
        failedTitle: t(
          "provider.googledrive.errors.pickerFailed",
          "Failed to open Google Drive picker",
        ),
        testId: "google-drive-button",
        isLoading: googleLoading,
        error: googleError,
        reconnectNeeded: false,
        clearError: clearGoogleError,
        openPicker: (options = {}) =>
          openGooglePicker({
            multiple: options.multiple,
            mimeTypes: options.accept,
          }),
      });
    }
    if (oneDrive.isEnabled) {
      pickers.push({
        id: "onedrive",
        icon: "onedrive",
        name: "OneDrive",
        actionLabel: t("fileSidebar.oneDrive", "Open from OneDrive"),
        failedTitle: t("fileSidebar.oneDriveFailed", "Couldn't open OneDrive"),
        testId: "onedrive-button",
        isLoading: oneDrive.isLoading,
        error: oneDrive.error,
        reconnectNeeded: oneDrive.reconnectNeeded,
        clearError: oneDrive.clearError,
        openPicker: oneDrive.openPicker,
      });
    }
    return pickers;
  }, [
    googleEnabled,
    googleLoading,
    googleError,
    clearGoogleError,
    openGooglePicker,
    oneDrive,
    t,
  ]);
}