import type { FileCollection } from 'file-collection'; import type { Ref, RefObject } from 'react'; import { useImperativeHandle } from 'react'; import { useCreateNmriumZip } from '../hooks/useCreateNmriumZip.ts'; import type { SaveIncludeOptions } from '../hooks/useExport.tsx'; import { useLoadFiles } from '../loader/useLoadFiles.js'; import type { BlobObject } from '../utility/export.js'; import { getBlob } from '../utility/export.js'; export interface NMRiumRefAPI { getSpectraViewerAsBlob: () => BlobObject | null; getNMRiumFile: (option: SaveIncludeOptions) => Promise; loadFiles: (files: File[]) => void; loadFileCollection: (fileCollection: FileCollection) => void; } export function useNMRiumRefAPI( ref: Ref | undefined, rootRef: RefObject, ) { const loadFiles = useLoadFiles(); const createNmriumZip = useCreateNmriumZip(); useImperativeHandle( ref, () => ({ getSpectraViewerAsBlob: () => { return rootRef.current ? getBlob('nmrSVG', { rootElement: rootRef.current }) : null; }, loadFiles, loadFileCollection: (fileCollection: FileCollection) => loadFiles(fileCollection), getNMRiumFile: (options) => createNmriumZip(options), }), [loadFiles, rootRef, createNmriumZip], ); }