|
| 1 | +import * as React from "react"; |
| 2 | +import { css } from "@emotion/react"; |
| 3 | +import { Project } from "../../model/Project/Project"; |
| 4 | +import ImdiGenerator, { IMDIMode } from "../../export/ImdiGenerator"; |
| 5 | +import ImdiBundler from "../../export/ImdiBundler"; |
| 6 | +import { mainProcessApi } from "../../mainProcess/MainProcessApiAccess"; |
| 7 | +import { XMLValidationResult } from "xmllint-wasm"; |
| 8 | +import Alert from "@mui/material/Alert"; |
| 9 | +import { Trans } from "@lingui/macro"; |
| 10 | +import { GetOtherConfigurationSettings } from "../../model/Project/OtherConfigurationSettings"; |
| 11 | +import { SearchableCodeViewer } from "../SearchableCodeViewer"; |
| 12 | + |
| 13 | +interface IProps { |
| 14 | + project: Project; |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * Shows the IMDI that would be generated for the ConsentDocuments bundle. |
| 19 | + * |
| 20 | + * This mirrors what ImdiBundler.generateConsentBundleData does during export. |
| 21 | + * The consent bundle gathers all consent files from persons who contributed |
| 22 | + * to sessions in the project. |
| 23 | + */ |
| 24 | +export const ConsentImdiView: React.FunctionComponent<IProps> = (props) => { |
| 25 | + const [imdi, setImdi] = React.useState<string>(""); |
| 26 | + const [validationResult, setValidationResult] = |
| 27 | + React.useState<XMLValidationResult | undefined>(); |
| 28 | + const [schemaName, setSchemaName] = React.useState<string>("IMDI_3.0.xsd"); |
| 29 | + |
| 30 | + // Generate the IMDI for consent documents using the shared helper |
| 31 | + React.useEffect(() => { |
| 32 | + const { session, tempDir, hasConsentFiles } = |
| 33 | + ImdiBundler.createConsentBundleSession(props.project); |
| 34 | + |
| 35 | + if (!hasConsentFiles) { |
| 36 | + setImdi(""); |
| 37 | + ImdiBundler.cleanupConsentBundleTempDir(tempDir); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + // Generate the IMDI using the same logic as export |
| 42 | + const xml = ImdiGenerator.generateSession( |
| 43 | + IMDIMode.RAW_IMDI, |
| 44 | + session, |
| 45 | + props.project |
| 46 | + ); |
| 47 | + setImdi(xml); |
| 48 | + |
| 49 | + ImdiBundler.cleanupConsentBundleTempDir(tempDir); |
| 50 | + }, [props.project, props.project.sessions.items.length]); |
| 51 | + |
| 52 | + // Validate the IMDI when it changes |
| 53 | + React.useEffect(() => { |
| 54 | + if (imdi) { |
| 55 | + const imdiSchema = |
| 56 | + GetOtherConfigurationSettings().imdiSchema || "IMDI_3.0.xsd"; |
| 57 | + setSchemaName(imdiSchema); |
| 58 | + mainProcessApi.validateImdiAsync(imdi, imdiSchema).then((r) => { |
| 59 | + setValidationResult(r); |
| 60 | + }); |
| 61 | + } else { |
| 62 | + setValidationResult(undefined); |
| 63 | + } |
| 64 | + }, [imdi]); |
| 65 | + |
| 66 | + return ( |
| 67 | + <div |
| 68 | + css={css` |
| 69 | + height: 500px; |
| 70 | + width: 100%; |
| 71 | + display: flex; |
| 72 | + flex-direction: column; |
| 73 | + flex-grow: 1; |
| 74 | + overflow: hidden; |
| 75 | + `} |
| 76 | + > |
| 77 | + {validationResult?.valid && ( |
| 78 | + <Alert severity="success"> |
| 79 | + <Trans>This XML conforms to the IMDI schema.</Trans> ({schemaName}) |
| 80 | + </Alert> |
| 81 | + )} |
| 82 | + {validationResult && !validationResult.valid && ( |
| 83 | + <Alert severity="error"> |
| 84 | + <div |
| 85 | + css={css` |
| 86 | + font-weight: bold; |
| 87 | + margin-bottom: 8px; |
| 88 | + `} |
| 89 | + > |
| 90 | + Validation failed using schema: {schemaName} |
| 91 | + </div> |
| 92 | + {validationResult?.errors.map((e, i) => { |
| 93 | + return ( |
| 94 | + <div |
| 95 | + key={i} |
| 96 | + css={css` |
| 97 | + margin: 0; |
| 98 | + font-weight: 500; |
| 99 | + white-space: pre-wrap; |
| 100 | + `} |
| 101 | + > |
| 102 | + {e.message} |
| 103 | + </div> |
| 104 | + ); |
| 105 | + })} |
| 106 | + </Alert> |
| 107 | + )} |
| 108 | + <SearchableCodeViewer |
| 109 | + content={imdi} |
| 110 | + language="xml" |
| 111 | + autoFocusSearch={true} |
| 112 | + /> |
| 113 | + </div> |
| 114 | + ); |
| 115 | +}; |
0 commit comments