Skip to content

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

11.6 KB
Raw Blame
import React, {
  createContext,
  useContext,
  useState,
  useCallback,
  useMemo,
} from "react";
import { useTranslation } from "react-i18next";
import { useFileHandler } from "@editor/hooks/useFileHandler";
import { useFileActions } from "@editor/contexts/FileContext";
import { useFileContext } from "@editor/contexts/file/fileHooks";
import {
  useNavigationActions,
  useNavigationState,
} from "@editor/contexts/NavigationContext";
import { StirlingFileStub } from "@editor/types/fileContext";
import type { FileId } from "@editor/types/file";
import { fileStorage } from "@editor/services/fileStorage";
import { importShareBundle } from "@editor/services/shareBundleImportService";
import apiClient from "@editor/services/apiClient";
import {
  extractLatestFilesFromBundle,
  isZipBundle,
  loadShareBundleEntries,
  parseContentDispositionFilename,
  readResponseHeader,
} from "@editor/services/shareBundleUtils";

type CustomFileHandler = (
  files: File[],
  insertAfterPage?: number,
) => void | Promise<void>;

interface FilesModalOptions {
  insertAfterPage?: number;
  customHandler?: CustomFileHandler;
  maxSelectable?: number | null;
  supportedFormats?: string[];
}

interface FilesModalContextType {
  isFilesModalOpen: boolean;
  openFilesModal: (options?: FilesModalOptions) => void;
  closeFilesModal: () => void;
  maxSelectable: number | null;
  /** A caller's format restriction takes precedence over the currently selected tool. */
  supportedFormats?: string[];
  /** Closes the picker before ingestion; rejects on import failure. */
  onFileUpload: (files: File[]) => Promise<void>;
  /** Imports readable selections before rejecting with unavailable filenames; leaves modal state unchanged. */
  onRecentFileSelect: (
    stirlingFileStubs: StirlingFileStub[],
    uploads?: File[],
  ) => Promise<void>;
  /** Reads inputs in selection order without changing the workspace; rejects if any file is unavailable. */
  loadFiles: (stubs: StirlingFileStub[]) => Promise<File[]>;
  onModalClose?: () => void;
  setOnModalClose: (callback: () => void) => void;
}

export const FilesModalContext = createContext<FilesModalContextType | null>(
  null,
);

export const FilesModalProvider: React.FC<{ children: React.ReactNode }> = ({
  children,
}) => {
  const { t } = useTranslation();
  const { addFiles } = useFileHandler();
  const { actions } = useFileActions();
  const fileCtx = useFileContext();
  const { actions: navActions } = useNavigationActions();
  const { workbench: currentWorkbench } = useNavigationState();
  // The page editor lays out every open file, so an added file belongs there.
  const staysOnAdd = currentWorkbench === "pageEditor";
  const [isFilesModalOpen, setIsFilesModalOpen] = useState(false);
  const [onModalClose, setOnModalClose] = useState<(() => void) | undefined>();
  const [insertAfterPage, setInsertAfterPage] = useState<number | undefined>();
  const [customHandler, setCustomHandler] = useState<CustomFileHandler>();
  const [maxSelectable, setMaxSelectable] = useState<number | null>(null);
  const [supportedFormats, setSupportedFormats] = useState<string[]>();

  const importBundleToWorkbench = useCallback(
    async (
      blob: Blob,
      filename: string,
      contentType: string,
      remoteStorageId?: number,
      remoteStorageUpdatedAt?: number,
      remoteOwnerUsername?: string,
      remoteOwnedByCurrentUser?: boolean,
      remoteSharedViaLink?: boolean,
      remoteShareToken?: string,
    ): Promise<FileId[]> => {
      const bundle = isZipBundle(contentType, filename)
        ? await loadShareBundleEntries(blob)
        : null;
      if (bundle) {
        const remoteUpdatedAt = remoteStorageUpdatedAt ?? Date.now();
        const currentFiles = await importShareBundle(
          bundle,
          {
            addFiles: (files) =>
              actions.addFilesWithOptions(files, {
                selectFiles: false,
                autoUnzip: false,
                skipAutoUnzip: false,
                allowDuplicates: true,
              }),
            updateStub: actions.updateStirlingFileStub,
          },
          {
            remoteStorageId,
            remoteStorageUpdatedAt: remoteUpdatedAt,
            remoteOwnerUsername,
            remoteOwnedByCurrentUser,
            remoteSharedViaLink,
            remoteShareToken,
          },
        );
        return currentFiles.map(({ file }) => file.fileId);
      }

      const file = new File([blob], filename, {
        type: contentType || blob.type,
      });
      const stirlingFiles = await actions.addFilesWithOptions([file], {
        selectFiles: false,
        autoUnzip: false,
        skipAutoUnzip: false,
        allowDuplicates: true,
      });
      const fileId = stirlingFiles[0]?.fileId as FileId | undefined;
      if (fileId && remoteStorageId) {
        const remoteUpdatedAt = remoteStorageUpdatedAt ?? Date.now();
        const updates = {
          remoteStorageId,
          remoteStorageUpdatedAt: remoteUpdatedAt,
          remoteOwnerUsername,
          remoteOwnedByCurrentUser,
          remoteSharedViaLink,
          remoteShareToken,
        };
        actions.updateStirlingFileStub(fileId, updates);
        await fileStorage.updateFileMetadata(fileId, updates);
      }
      return fileId ? [fileId] : [];
    },
    [actions, fileStorage],
  );

  const downloadRemoteFile = useCallback(async (stub: StirlingFileStub) => {
    const path = stub.remoteShareToken
      ? `/api/v1/storage/share-links/${stub.remoteShareToken}`
      : stub.remoteStorageId && stub.id.startsWith("server-")
        ? `/api/v1/storage/files/${stub.remoteStorageId}/download`
        : null;
    if (!path) return null;
    const response = await apiClient.get(path, {
      responseType: "blob",
      suppressErrorToast: true,
      skipAuthRedirect: true,
    });
    const contentType = readResponseHeader(response.headers, "content-type");
    const disposition = readResponseHeader(
      response.headers,
      "content-disposition",
    );
    const filename = parseContentDispositionFilename(disposition) || stub.name;
    const blob = response.data as Blob;
    const contentTypeValue = contentType || blob.type;
    return { blob, filename, contentType: contentTypeValue };
  }, []);

  const openFilesModal = useCallback((options?: FilesModalOptions) => {
    setInsertAfterPage(options?.insertAfterPage);
    setCustomHandler(() => options?.customHandler);
    setMaxSelectable(options?.maxSelectable ?? null);
    setSupportedFormats(options?.supportedFormats);
    setIsFilesModalOpen(true);
  }, []);

  const closeFilesModal = useCallback(() => {
    setIsFilesModalOpen(false);
    setInsertAfterPage(undefined);
    setCustomHandler(undefined);
    onModalClose?.();
  }, [onModalClose]);

  const loadFiles = useCallback(
    async (stirlingFileStubs: StirlingFileStub[]) => {
      const loadedFiles: File[] = [];
      for (const stub of stirlingFileStubs) {
        const remote = await downloadRemoteFile(stub);
        if (remote) {
          loadedFiles.push(
            ...(await extractLatestFilesFromBundle(
              remote.blob,
              remote.filename,
              remote.contentType,
            )),
          );
        } else {
          const file = await fileStorage.getStirlingFile(stub.id);
          if (!file) throw new Error(`${stub.name} is no longer available.`);
          loadedFiles.push(file);
        }
      }
      return loadedFiles;
    },
    [downloadRemoteFile],
  );

  const handleRecentFileSelect = useCallback(
    async (stirlingFileStubs: StirlingFileStub[], uploads: File[] = []) => {
      const failedNames: string[] = [];
      const reportUnavailable = () => {
        if (failedNames.length)
          throw new Error(
            t("filePicker.filesUnavailable", "Could not add: {{names}}", {
              names: failedNames.join(", "),
            }),
          );
      };
      if (customHandler) {
        const loadedFiles: File[] = [];
        for (const stub of stirlingFileStubs) {
          try {
            loadedFiles.push(...(await loadFiles([stub])));
          } catch (cause) {
            console.error("Could not load selected file", stub.name, cause);
            failedNames.push(stub.name);
          }
        }
        const files = [...loadedFiles, ...uploads];
        if (files.length) await customHandler(files, insertAfterPage);
        reportUnavailable();
        return;
      }
      const uploadedFiles = uploads.length > 0 ? await addFiles(uploads) : [];

      const localStubs: StirlingFileStub[] = [];
      const requestedIds: FileId[] = [];
      for (const stub of stirlingFileStubs) {
        try {
          const remote = await downloadRemoteFile(stub);
          if (!remote) {
            localStubs.push(stub);
            continue;
          }
          const importedIds = await importBundleToWorkbench(
            remote.blob,
            remote.filename,
            remote.contentType,
            stub.remoteStorageId,
            stub.remoteStorageUpdatedAt,
            stub.remoteOwnerUsername,
            stub.remoteOwnedByCurrentUser,
            stub.remoteShareToken ? true : stub.remoteSharedViaLink,
            stub.remoteShareToken,
          );
          requestedIds.push(...importedIds);
        } catch (cause) {
          console.error("Could not import selected file", stub.name, cause);
          failedNames.push(stub.name);
        }
      }

      if (localStubs.length) {
        const admitted = await actions.addStirlingFileStubs(localStubs, {
          selectFiles: false,
        });
        requestedIds.push(...admitted.map((file) => file.fileId));
      }
      // Adding an input must preserve a tool's existing selection.
      const uploadedIds = uploadedFiles.map((file) => file.fileId);
      const currentSelected = fileCtx.selectors
        .getSelectedStirlingFileStubs()
        .map((s) => s.id);
      actions.setSelectedFiles(
        Array.from(
          new Set([...currentSelected, ...requestedIds, ...uploadedIds]),
        ),
      );

      const totalAdded = requestedIds.length + uploadedIds.length;
      if (!staysOnAdd && totalAdded > 0) {
        navActions.setWorkbench(totalAdded === 1 ? "viewer" : "fileEditor");
      }
      reportUnavailable();
    },
    [
      addFiles,
      loadFiles,
      actions,
      customHandler,
      insertAfterPage,
      fileCtx,
      downloadRemoteFile,
      importBundleToWorkbench,
      navActions,
      staysOnAdd,
      t,
    ],
  );

  const handleFileUpload = useCallback(
    (files: File[]) => {
      closeFilesModal();
      return handleRecentFileSelect([], files);
    },
    [closeFilesModal, handleRecentFileSelect],
  );

  const setModalCloseCallback = useCallback((callback: () => void) => {
    setOnModalClose(() => callback);
  }, []);

  const contextValue: FilesModalContextType = useMemo(
    () => ({
      isFilesModalOpen,
      openFilesModal,
      closeFilesModal,
      onFileUpload: handleFileUpload,
      onRecentFileSelect: handleRecentFileSelect,
      loadFiles,
      onModalClose,
      setOnModalClose: setModalCloseCallback,
      maxSelectable,
      supportedFormats,
    }),
    [
      isFilesModalOpen,
      openFilesModal,
      closeFilesModal,
      handleFileUpload,
      handleRecentFileSelect,
      loadFiles,
      onModalClose,
      setModalCloseCallback,
      maxSelectable,
      supportedFormats,
    ],
  );

  return (
    <FilesModalContext.Provider value={contextValue}>
      {children}
    </FilesModalContext.Provider>
  );
};

export const useFilesModalContext = () => {
  const context = useContext(FilesModalContext);
  if (!context) {
    throw new Error(
      "useFilesModalContext must be used within FilesModalProvider",
    );
  }
  return context;
};