Skip to content

Commit

Permalink
feat: Inject content hash into document preview URL
Browse files Browse the repository at this point in the history
  • Loading branch information
vsgoulart committed Dec 19, 2024
1 parent 910f8e2 commit 4e9b7da
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/form-js-playground/test/spec/Playground.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ describe('playground', function () {
metadata: {
fileName: 'My document.pdf',
contentType: 'application/pdf',
customProperties: {
contentHash: '1234567890',
},
},
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const type = 'documentPreview';
* @property {Object} metadata
* @property {string|undefined} [metadata.contentType]
* @property {string} metadata.fileName
* @property {Object} [metadata.customProperties]
* @property {string|undefined} [metadata.customProperties.contentHash]
*
* @typedef Field
* @property {string} id
Expand Down Expand Up @@ -170,7 +172,11 @@ function DocumentRenderer(props) {
const [hasError, setHasError] = useState(false);
const ref = useRef(null);
const isInViewport = useInViewport(ref);
const fullUrl = endpoint.replace(DOCUMENT_ID_PLACEHOLDER, documentMetadata.documentId);
const fullUrl = buildUrl({
baseUrl: endpoint,
documentId: documentMetadata.documentId,
customProperties: metadata.customProperties,
});
const singleDocumentContainerClassName = `fjs-${type}-single-document-container`;
const errorMessageId = `${domId}-error-message`;
const errorMessage = 'Unable to download document';
Expand Down Expand Up @@ -303,3 +309,25 @@ function useInViewport(ref) {

return isInViewport;
}

/**
* This solution should be a temporary fix, we should try to remove it via: https://github.com/bpmn-io/form-js/issues/1341
*
* @param {Object} options
* @param {string} options.baseUrl
* @param {string} options.documentId
* @param {Object} [options.customProperties]
* @param {string|undefined} [options.customProperties.contentHash]
*
* @returns {string}
*/
function buildUrl(options) {
const { baseUrl, documentId, customProperties } = options;
const finalUrl = new URL(baseUrl.replace(DOCUMENT_ID_PLACEHOLDER, documentId));

if (customProperties !== undefined && customProperties.contentHash !== undefined) {
finalUrl.searchParams.set('contentHash', customProperties.contentHash);
}

return decodeURI(finalUrl.toString());
}

0 comments on commit 4e9b7da

Please sign in to comment.