UNPKG

@pdftron/webviewer-react-toolkit

Version:

A React component library for integrating with PDFTron WebViewer API.

31 lines (30 loc) 1.44 kB
import { useMemo } from 'react'; import { useFileSubscribe } from './useFileSubscribe'; /** * This hook converts a file class with async values into a React-friendly hook * with async values set to undefined until they are fetched. * @param file The file to convert to react observable values. */ export function useFile(file) { var _a = useFileSubscribe(file, function (f) { return f.name; }, 'onnamechange'), name = _a[0], nameErr = _a[1]; var _b = useFileSubscribe(file, function (f) { return f.thumbnail; }, 'onthumbnailchange'), thumbnail = _b[0], thumbnailErr = _b[1]; var _c = useFileSubscribe(file, function (f) { return f.fileObj; }, 'onfileobjchange'), fileObj = _c[0], fileObjErr = _c[1]; var _d = useFileSubscribe(file, function (f) { return f.documentObj; }, 'ondocumentobjchange'), documentObj = _d[0], documentObjErr = _d[1]; var fileValue = useMemo(function () { return ({ file: file, id: file.id, originalName: file.originalName, extension: file.extension, name: name, thumbnail: thumbnail, fileObj: fileObj, documentObj: documentObj, errors: { name: nameErr, thumbnail: thumbnailErr, fileObj: fileObjErr, documentObj: documentObjErr, }, }); }, [documentObj, documentObjErr, file, fileObj, fileObjErr, name, nameErr, thumbnail, thumbnailErr]); return fileValue; }