Files
papercrate/frontend/src/documents/documentActions.js
T
2025-10-30 18:17:01 +01:00

76 lines
1.8 KiB
JavaScript

import { openOcrTextInNewTab } from '../utils/ocr';
const asyncFalse = async () => false;
const resolveDocumentDownloadHref = (document, resolveApiPath) => {
if (!document || typeof resolveApiPath !== 'function') {
return null;
}
const downloadPath = document.current_version?.download_path;
if (!downloadPath) {
return null;
}
return resolveApiPath(downloadPath);
};
const hasDocumentOcrAsset = (document, getDocumentAsset) => {
if (!document || typeof getDocumentAsset !== 'function') {
return false;
}
return Boolean(getDocumentAsset(document, 'ocr-text'));
};
export const createDocumentActionState = ({
document,
resolveApiPath,
ensurePreviewData,
ensureAssetUrl,
getDocumentAsset,
notifyApiError,
ocrErrorMessage = 'Unable to open OCR text.',
}) => {
if (!document) {
return {
downloadHref: null,
hasOcr: false,
openOcr: asyncFalse,
};
}
const downloadHref = resolveDocumentDownloadHref(document, resolveApiPath);
const hasOcr = hasDocumentOcrAsset(document, getDocumentAsset);
const openOcr = hasOcr
? async () => {
try {
const success = await openOcrTextInNewTab({
document,
ensurePreviewData,
getDocumentAsset,
ensureAssetUrl,
});
if (!success && typeof notifyApiError === 'function') {
notifyApiError(new Error('OCR text URL unavailable.'), ocrErrorMessage);
}
return success;
} catch (error) {
if (typeof notifyApiError === 'function') {
notifyApiError(error, ocrErrorMessage);
}
throw error;
}
}
: asyncFalse;
return {
downloadHref,
hasOcr,
openOcr,
};
};
export const documentActionsTestExports = {
resolveDocumentDownloadHref,
hasDocumentOcrAsset,
};