Skip to content

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

1.6 KB
Raw Blame
import { useCallback } from "react";
import { useFileActions } from "@editor/contexts/FileContext";
import type { StirlingFile, StirlingFileStub } from "@editor/types/fileContext";

export const useFileHandler = () => {
  const { actions } = useFileActions();

  const addFiles = useCallback(
    async (
      files: File[],
      options: {
        insertAfterPageId?: string;
        selectFiles?: boolean;
        /** Persist to IDB without dispatching to workspace state. */
        skipWorkspaceDispatch?: boolean;
        /** Defaults to true; false keeps an archive intact (e.g. duplicating one). */
        autoUnzip?: boolean;
        /** Skip the upload metric - the file isn't new to the system (e.g. a copy). */
        skipUploadTracking?: boolean;
        /** Folder every added file is born into (see AddFileOptions). */
        folderId?: string;
        /** Classification computed outside the policy system (see AddFileOptions). */
        presetClassification?: {
          labels: string[];
          confidence: StirlingFileStub["classificationConfidence"];
        };
        /** Bytes and stub only, no thumbnail parse (see AddFileOptions). */
        skipMetadataHydration?: boolean;
      } = {},
    ): Promise<StirlingFile[]> => {
      // Merge default options with passed options - passed options take precedence
      const mergedOptions = { selectFiles: true, ...options };
      // Let FileContext handle deduplication with quickKey logic
      const result = await actions.addFiles(files, mergedOptions);
      return result;
    },
    [actions.addFiles],
  );

  return {
    addFiles,
  };
};