Skip to content

Commit

Permalink
feat: ideas for plugin wrappers [LIBS-397]
Browse files Browse the repository at this point in the history
  • Loading branch information
tomzemp committed Dec 21, 2022
1 parent 2aea6e2 commit be38607
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 2 deletions.
8 changes: 6 additions & 2 deletions runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
"build/**"
],
"dependencies": {
"@dhis2/app-service-alerts": "3.7.0",
"@dhis2/app-service-config": "3.7.0",
"@dhis2/app-service-data": "3.7.0",
"@dhis2/app-service-alerts": "3.7.0",
"@dhis2/app-service-offline": "3.7.0"
"@dhis2/app-service-offline": "3.7.0",
"post-robot": "^10.0.46"
},
"peerDependencies": {
"prop-types": "^15.7.2",
Expand All @@ -38,5 +39,8 @@
"build:package": "d2-app-scripts build",
"build": "concurrently -n build,types \"yarn build:package\" \"yarn build:types\"",
"test": "echo \"No tests yet!\""
},
"devDependencies": {
"@types/post-robot": "^10.0.3"
}
}
53 changes: 53 additions & 0 deletions runtime/src/PluginSender.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import postRobot from 'post-robot'
import React, { useEffect, useRef, useState } from 'react'

export const PluginSender = ({
pluginSource,
...propsToPass
}: {
pluginSource: string
propsToPass: any
}): JSX.Element => {
const iframeRef = useRef<any>()

const [missingProps, setMissingProps] = useState<any[] | null>(null)

useEffect(() => {
if (iframeRef?.current) {
const iframeProps = { ...propsToPass, setMissingProps }
const listener = postRobot.on(
'getPropsFromParent',
// listen for messages coming only from the iframe rendered by this component
{ window: iframeRef.current.contentWindow },
(): any => {
return iframeProps
}
)

return () => listener.cancel()
}
}, [propsToPass])

if (missingProps && missingProps?.length > 0) {
return (
<p>{`Plugin could not load because required props are missing: ${missingProps.join()}`}</p>
)
}

return (
<div>
{pluginSource ? (
<iframe
ref={iframeRef}
src={pluginSource}
style={{
width: '100%',
height: '100%',
border: 'none',
}}
hidden={missingProps === null}
></iframe>
) : null}
</div>
)
}
43 changes: 43 additions & 0 deletions runtime/src/PluginWrapper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import postRobot from 'post-robot'
import { useEffect, useState } from 'react'

export const PluginWrapper = ({
requiredProps,
children,
}: {
requiredProps: [string]
children: any
}): any => {
const [propsFromParent, setPropsFromParent] = useState<any>()

const receivePropsFromParent = (event: any) => {
const { data: receivedProps } = event
const { setMissingProps, ...explictlyPassedProps } = receivedProps

setPropsFromParent(explictlyPassedProps)

// check for required props
const missingProps = requiredProps?.filter(
(prop) => !receivedProps[prop]
)
if (missingProps && missingProps.length > 0) {
setMissingProps(missingProps)
console.error(
`The following required props were not provided: ${missingProps.join(
','
)}`
)
} else {
setMissingProps([])
}
}

useEffect(() => {
postRobot
.send(window.top, 'getPropsFromParent')
.then(receivePropsFromParent)
.catch((err: Error) => console.error(err))
})

return children({ ...propsFromParent })
}
4 changes: 4 additions & 0 deletions runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ export {
clearSensitiveCaches,
} from '@dhis2/app-service-offline'

export { PluginSender } from './PluginSender'

export { PluginWrapper } from './PluginWrapper'

export { Provider } from './Provider'
49 changes: 49 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2440,6 +2440,11 @@
resolved "https://registry.yarnpkg.com/@types/parse-json/-/parse-json-4.0.0.tgz#2f8bb441434d163b35fb8ffdccd7138927ffb8c0"
integrity sha512-//oorEZjL6sbPcKUaCdIGlIUeH26mgzimjBB77G6XRgnDl/L5wOnpyBGRe/Mmf5CVW3PwEBE1NjiMZ/ssFh4wA==

"@types/post-robot@^10.0.3":
version "10.0.3"
resolved "https://registry.yarnpkg.com/@types/post-robot/-/post-robot-10.0.3.tgz#d1429085f2faf4c87f841dab4e51472457edbf31"
integrity sha512-y8ysuxddaG8V/oA1Ay6Err7nSADRa9Bv1rl0ZQpJ0qgdIQ7ks3CHcOsYL4qE8w75+/XYDS94dBeXDs0xexm3tA==

"@types/prettier@^2.0.0":
version "2.2.1"
resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-2.2.1.tgz#374e31645d58cb18a07b3ecd8e9dede4deb2cccd"
Expand Down Expand Up @@ -3805,6 +3810,15 @@ bcryptjs@^2.3.0:
resolved "https://registry.yarnpkg.com/bcryptjs/-/bcryptjs-2.4.3.tgz#9ab5627b93e60621ff7cdac5da9733027df1d0cb"
integrity sha1-mrVie5PmBiH/fNrF2pczAn3x0Ms=

belter@^1.0.41:
version "1.0.190"
resolved "https://registry.yarnpkg.com/belter/-/belter-1.0.190.tgz#491857550ef240d9c66b56fc637991f5c3089966"
integrity sha512-jz05FHrO+bwitdI6JxV5ESyRdVhTcwMWQ7L4o+q/R4LNJFQrG58sp9EiwsSjhbihhiyYFcmmCMRRagxte6igtw==
dependencies:
cross-domain-safe-weakmap "^1"
cross-domain-utils "^2"
zalgo-promise "^1"

bfj@^7.0.2:
version "7.0.2"
resolved "https://registry.yarnpkg.com/bfj/-/bfj-7.0.2.tgz#1988ce76f3add9ac2913fd8ba47aad9e651bfbb2"
Expand Down Expand Up @@ -5011,6 +5025,20 @@ create-hmac@^1.1.0, create-hmac@^1.1.4, create-hmac@^1.1.7:
safe-buffer "^5.0.1"
sha.js "^2.4.8"

cross-domain-safe-weakmap@^1, cross-domain-safe-weakmap@^1.0.1:
version "1.0.29"
resolved "https://registry.yarnpkg.com/cross-domain-safe-weakmap/-/cross-domain-safe-weakmap-1.0.29.tgz#0847975c27d9e1cc840f24c1745311958df98022"
integrity sha512-VLoUgf2SXnf3+na8NfeUFV59TRZkIJqCIATaMdbhccgtnTlSnHXkyTRwokngEGYdQXx8JbHT9GDYitgR2sdjuA==
dependencies:
cross-domain-utils "^2.0.0"

cross-domain-utils@^2, cross-domain-utils@^2.0.0:
version "2.0.38"
resolved "https://registry.yarnpkg.com/cross-domain-utils/-/cross-domain-utils-2.0.38.tgz#2eaf321c4dfdb61596805ca4233fde4400cb6377"
integrity sha512-zZfi3+2EIR9l4chrEiXI2xFleyacsJf8YMLR1eJ0Veb5FTMXeJ3DpxDjZkto2FhL/g717WSELqbptNSo85UJDw==
dependencies:
zalgo-promise "^1.0.11"

cross-spawn@7.0.3, cross-spawn@^7.0.0, cross-spawn@^7.0.2, cross-spawn@^7.0.3:
version "7.0.3"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"
Expand Down Expand Up @@ -11503,6 +11531,17 @@ posix-character-classes@^0.1.0:
resolved "https://registry.yarnpkg.com/posix-character-classes/-/posix-character-classes-0.1.1.tgz#01eac0fe3b5af71a2a6c02feabb8c1fef7e00eab"
integrity sha1-AerA/jta9xoqbAL+q7jB/vfgDqs=

post-robot@^10.0.46:
version "10.0.46"
resolved "https://registry.yarnpkg.com/post-robot/-/post-robot-10.0.46.tgz#39cea5b51033729390fc7c90be3285cd285f0377"
integrity sha512-EgVJiuvI4iRWDZvzObWes0X/n8olWBEJWxlSw79zmhpgkigX8UsVL4VOBhVtoJKwf0Y9qP9g2zOONw1rv80QbA==
dependencies:
belter "^1.0.41"
cross-domain-safe-weakmap "^1.0.1"
cross-domain-utils "^2.0.0"
universal-serialize "^1.0.4"
zalgo-promise "^1.0.3"

postcss-attribute-case-insensitive@^4.0.1:
version "4.0.2"
resolved "https://registry.yarnpkg.com/postcss-attribute-case-insensitive/-/postcss-attribute-case-insensitive-4.0.2.tgz#d93e46b504589e94ac7277b0463226c68041a880"
Expand Down Expand Up @@ -15086,6 +15125,11 @@ unique-string@^1.0.0:
dependencies:
crypto-random-string "^1.0.0"

universal-serialize@^1.0.4:
version "1.0.10"
resolved "https://registry.yarnpkg.com/universal-serialize/-/universal-serialize-1.0.10.tgz#3279bb30f47290ea479f45135620f98fa9d3f3a6"
integrity sha512-FdouA4xSFa0fudk1+z5vLWtxZCoC0Q9lKYV3uUdFl7DttNfolmiw2ASr5ddY+/Yz6Isr68u3IqC9XMSwMP+Pow==

universalify@^0.1.0:
version "0.1.2"
resolved "https://registry.yarnpkg.com/universalify/-/universalify-0.1.2.tgz#b646f69be3942dabcecc9d6639c80dc105efaa66"
Expand Down Expand Up @@ -16065,6 +16109,11 @@ yocto-queue@^0.1.0:
resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b"
integrity sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==

zalgo-promise@^1, zalgo-promise@^1.0.11, zalgo-promise@^1.0.3:
version "1.0.48"
resolved "https://registry.yarnpkg.com/zalgo-promise/-/zalgo-promise-1.0.48.tgz#9e33eef502d5ed9f5a09fc5728c833c3e87afa2e"
integrity sha512-LLHANmdm53+MucY9aOFIggzYtUdkSBFxUsy4glTTQYNyK6B3uCPWTbfiGvSrEvLojw0mSzyFJ1/RRLv+QMNdzQ==

zip-stream@^2.1.2:
version "2.1.3"
resolved "https://registry.yarnpkg.com/zip-stream/-/zip-stream-2.1.3.tgz#26cc4bdb93641a8590dd07112e1f77af1758865b"
Expand Down

0 comments on commit be38607

Please sign in to comment.