Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement plugin wrappers #1332

Merged
merged 11 commits into from
Mar 13, 2023
Merged
63 changes: 63 additions & 0 deletions docs/components/Plugin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Plugin Component

A wrapper that creates an iframe for a specified plugin and establishes a two-way communication channel with said plugin, allowing you to pass props (including callbacks between an app and a plugin). Note that the plugin must be built using the app-platform with entryPoints.plugin specified in the d2.config.js file.

## Basic Usage (Defining a plugin within an app)

Within an app you can specify a plugin (either by providing its short name `pluginShortName`, or by specifying a URL directly (`pluginSource`). If you have provided `pluginSource`, this will take precedence (Note: lookup logic is TBD? Should we allow a URL only in development mode, for example?).

```jsx
import { Plugin } from '@dhis2/app-runtime'

// within the app
const MyApp = () => (
<Plugin
pluginShortName={mutation}
onError={(err) => {
console.error(err)
}}
showAlertsInPlugin={true}
numberToPass={'42'}
callbackToPass={({ name }) => {
console.log(`Hi ${name}!`)
}}
/>
)
```

## Basic Usage (Using properties from the parent app)

You must build your plugin with the app-platform. If you have done this, your entry component will be passed the props from the parent app. From the example above, the properties `numberToPass` and `callbackToPass` will be available in the build plugin (when it is rendered with a <Plugin> component).

```jsx
// your plugin entry point (the plugin itself)

const MyPlugin = (propsFromParent) => {
const { numberToPass, callbackToPass: sayHi } = propsFromParent
return (
<>
<p>{`The meaning of life is: ${numberToPass}`}</p>
<button onClick={() => sayHi({ name: 'Arthur Dent' })}>
Say hi
</button>
</>
)
}
```

## Plugin Props (reserved props)

| Name | Type | Required | Description |
| :--------------------: | :------------: | :---------------------------------------------: | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **pluginShortName** | _string_ | _required_ if `pluginSource` is not provided | The shortName of the app/plugin you wish to load (matching the result from api/apps). Used to look up the plugin entry point. If this is not provided, `pluginSource` must be provided. `pluginSource` will take precedence if provided. |
| **pluginSource** | _string_ (url) | _required_ if `pluginShortName` is not provided | The URL of the plugin. If this is not provided, `pluginShortName` must be provided. |
| **onError** | _Function_ | _optional_ | Callback function to be called when an error in the plugin triggers an error boundary. You can use this to pass an error back up to the app and create a custom handling/UX if errors occur in the plugin. In general, it is recommended that you use the plugin's built-in error boundaries |
| **showAlertsInPlugin** | _boolean_ | _optional_ | If `true`, any alerts within the plugin (defined with the `useAlert` hook) will be rendered within the iframe. By default, this is `false`. It is recommended, in general, that you do not override this and allow alerts to be hoisted up to the app level |

## Plugin Props (custom props)

You can specify pass any other props on the <Plugin> component and these will be passed down to the plugin (provided it was built with app-platform). When props are updated, they will be passed back down to the plugin. This mimics the behaviour of a normal React component, and hence you should provide stable references as needed to prevent rerendering.

## Extended example

See these links for an extended example of how <Plugin> component can be used within an [app](https://github.com/tomzemp/workingplugin/blob/plugin-wrapper-in-platform/src/App.js) and consumed within the [plugin](https://github.com/tomzemp/workingplugin/blob/plugin-wrapper-in-platform/src/Plugin.js).
3 changes: 2 additions & 1 deletion examples/cra/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"@dhis2/app-service-alerts": "file:../../services/alerts",
"@dhis2/app-service-config": "file:../../services/config",
"@dhis2/app-service-data": "file:../../services/data",
"@dhis2/app-service-offline": "file:../../services/offline"
"@dhis2/app-service-offline": "file:../../services/offline",
"@dhis2/app-service-plugin": "file:../../services/plugin"
},
"scripts": {
"start": "react-scripts start",
Expand Down
49 changes: 36 additions & 13 deletions examples/cra/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1054,29 +1054,42 @@
integrity sha512-ij4wRiunFfaJxjB0BdrYHIH8FxBJpOwNPhhAcunlmPdXudL1WQV1qoP9un6JsEBAgQH+7UXyyjh0g7jTxXK6tg==

"@dhis2/app-runtime@file:../../runtime":
version "3.5.0"
version "3.7.0"
dependencies:
"@dhis2/app-service-alerts" "3.5.0"
"@dhis2/app-service-config" "3.5.0"
"@dhis2/app-service-data" "3.5.0"
"@dhis2/app-service-offline" "3.5.0"
"@dhis2/app-service-alerts" "3.7.0"
"@dhis2/app-service-config" "3.7.0"
"@dhis2/app-service-data" "3.7.0"
"@dhis2/app-service-offline" "3.7.0"
"@dhis2/app-service-plugin" "3.7.0"
"@dhis2/d2-i18n" "^1.1.0"

"@dhis2/app-service-alerts@3.5.0", "@dhis2/app-service-alerts@file:../../services/alerts":
version "3.5.0"
"@dhis2/app-service-alerts@3.7.0", "@dhis2/app-service-alerts@file:../../services/alerts":
version "3.7.0"

"@dhis2/app-service-config@3.5.0", "@dhis2/app-service-config@file:../../services/config":
version "3.5.0"
"@dhis2/app-service-config@3.7.0", "@dhis2/app-service-config@file:../../services/config":
version "3.7.0"

"@dhis2/app-service-data@3.5.0", "@dhis2/app-service-data@file:../../services/data":
version "3.5.0"
"@dhis2/app-service-data@3.7.0", "@dhis2/app-service-data@file:../../services/data":
version "3.7.0"
dependencies:
react-query "^3.13.11"

"@dhis2/app-service-offline@3.5.0", "@dhis2/app-service-offline@file:../../services/offline":
version "3.5.0"
"@dhis2/app-service-offline@3.7.0", "@dhis2/app-service-offline@file:../../services/offline":
version "3.7.0"
dependencies:
lodash "^4.17.21"

"@dhis2/app-service-plugin@3.7.0", "@dhis2/app-service-plugin@file:../../services/plugin":
version "3.7.0"

"@dhis2/d2-i18n@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@dhis2/d2-i18n/-/d2-i18n-1.1.0.tgz#ec777c5091f747e4c5aa4f9801c62ba4d1ef3d16"
integrity sha512-x3u58goDQsMfBzy50koxNrJjofJTtjRZOfz6f6Py/wMMJfp/T6vZjWMQgcfWH0JrV6d04K1RTt6bI05wqsVQvg==
dependencies:
i18next "^10.3"
moment "^2.24.0"

"@hapi/address@2.x.x":
version "2.1.4"
resolved "https://registry.yarnpkg.com/@hapi/address/-/address-2.1.4.tgz#5d67ed43f3fd41a69d4b9ff7b56e7c0d1d0a81e5"
Expand Down Expand Up @@ -5149,6 +5162,11 @@ https-browserify@^1.0.0:
resolved "https://registry.yarnpkg.com/https-browserify/-/https-browserify-1.0.0.tgz#ec06c10e0a34c0f2faf199f7fd7fc78fffd03c73"
integrity sha1-7AbBDgo0wPL68Zn3/X/Hj//QPHM=

i18next@^10.3:
version "10.6.0"
resolved "https://registry.yarnpkg.com/i18next/-/i18next-10.6.0.tgz#90ffd9f9bc617f34b9a12e037260f524445f7684"
integrity sha512-ycRlN145kQf8EsyDAzMfjqv1ZT1Jwp7P2H/07bP8JLWm+7cSLD4XqlJOvq4mKVS2y2mMIy10lX9ZeYUdQ0qSRw==

iconv-lite@0.4.24, iconv-lite@^0.4.24:
version "0.4.24"
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.24.tgz#2022b4b25fbddc21d2f524974a474aafe733908b"
Expand Down Expand Up @@ -6878,6 +6896,11 @@ mkdirp@^0.5.1, mkdirp@^0.5.3, mkdirp@~0.5.1:
dependencies:
minimist "^1.2.5"

moment@^2.24.0:
version "2.29.4"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.29.4.tgz#3dbe052889fe7c1b2ed966fcb3a77328964ef108"
integrity sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w==

move-concurrently@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/move-concurrently/-/move-concurrently-1.0.1.tgz#be2c005fda32e0b29af1f05d7c4b33214c701f92"
Expand Down
3 changes: 2 additions & 1 deletion examples/query-playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@dhis2/app-service-alerts": "file:../../services/alerts",
"@dhis2/app-service-config": "file:../../services/config",
"@dhis2/app-service-data": "file:../../services/data",
"@dhis2/app-service-offline": "file:../../services/offline"
"@dhis2/app-service-offline": "file:../../services/offline",
"@dhis2/app-service-plugin": "file:../../services/plugin"
}
}
51 changes: 37 additions & 14 deletions examples/query-playground/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1789,30 +1789,45 @@
dependencies:
moment "^2.24.0"

"@dhis2/app-runtime@*", "@dhis2/app-runtime@^2.2.2", "@dhis2/app-runtime@file:../../runtime":
version "3.5.0"
"@dhis2/app-runtime@*":
version "3.8.0"
resolved "https://registry.yarnpkg.com/@dhis2/app-runtime/-/app-runtime-3.8.0.tgz#4ec7fc4ec6647dc8428e3c0d2e14b2d188a993b9"
integrity sha512-f5M1RfUJb4yZaPDywTfogVXjzWcuYGJ7JQzny6iWXrJu1+qrRKYbfFutYNhB+4bXD4bB59DelHWqVaHtrGvbVA==
dependencies:
"@dhis2/app-service-alerts" "3.5.0"
"@dhis2/app-service-config" "3.5.0"
"@dhis2/app-service-data" "3.5.0"
"@dhis2/app-service-offline" "3.5.0"
"@dhis2/app-service-alerts" "3.8.0"
"@dhis2/app-service-config" "3.8.0"
"@dhis2/app-service-data" "3.8.0"
"@dhis2/app-service-offline" "3.8.0"

"@dhis2/app-service-alerts@3.5.0", "@dhis2/app-service-alerts@file:../../services/alerts":
version "3.5.0"
"@dhis2/app-runtime@^2.2.2", "@dhis2/app-runtime@file:../../runtime":
version "3.7.0"
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-offline" "3.7.0"
"@dhis2/app-service-plugin" "3.7.0"
"@dhis2/d2-i18n" "^1.1.0"

"@dhis2/app-service-config@3.5.0", "@dhis2/app-service-config@file:../../services/config":
version "3.5.0"
"@dhis2/app-service-alerts@3.7.0", "@dhis2/app-service-alerts@3.8.0", "@dhis2/app-service-alerts@file:../../services/alerts":
version "3.7.0"

"@dhis2/app-service-data@3.5.0", "@dhis2/app-service-data@file:../../services/data":
version "3.5.0"
"@dhis2/app-service-config@3.7.0", "@dhis2/app-service-config@3.8.0", "@dhis2/app-service-config@file:../../services/config":
version "3.7.0"

"@dhis2/app-service-data@3.7.0", "@dhis2/app-service-data@3.8.0", "@dhis2/app-service-data@file:../../services/data":
version "3.7.0"
dependencies:
react-query "^3.13.11"

"@dhis2/app-service-offline@3.5.0", "@dhis2/app-service-offline@file:../../services/offline":
version "3.5.0"
"@dhis2/app-service-offline@3.7.0", "@dhis2/app-service-offline@3.8.0", "@dhis2/app-service-offline@file:../../services/offline":
version "3.7.0"
dependencies:
lodash "^4.17.21"

"@dhis2/app-service-plugin@3.7.0", "@dhis2/app-service-plugin@file:../../services/plugin":
version "3.7.0"

"@dhis2/app-shell@5.2.0":
version "5.2.0"
resolved "https://registry.yarnpkg.com/@dhis2/app-shell/-/app-shell-5.2.0.tgz#19fc3c6b18ea18048d3cdd1680ce535417edb6b3"
Expand Down Expand Up @@ -1900,6 +1915,14 @@
i18next "^10.3"
moment "^2.24.0"

"@dhis2/d2-i18n@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@dhis2/d2-i18n/-/d2-i18n-1.1.0.tgz#ec777c5091f747e4c5aa4f9801c62ba4d1ef3d16"
integrity sha512-x3u58goDQsMfBzy50koxNrJjofJTtjRZOfz6f6Py/wMMJfp/T6vZjWMQgcfWH0JrV6d04K1RTt6bI05wqsVQvg==
dependencies:
i18next "^10.3"
moment "^2.24.0"

"@dhis2/prop-types@^1.6.4":
version "1.6.4"
resolved "https://registry.yarnpkg.com/@dhis2/prop-types/-/prop-types-1.6.4.tgz#ec4d256c9440d4d00071524422a727c61ddaa6f6"
Expand Down
6 changes: 4 additions & 2 deletions runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
"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",
"@dhis2/app-service-plugin": "3.7.0",
"@dhis2/d2-i18n": "^1.1.0"
},
"peerDependencies": {
"prop-types": "^15.7.2",
Expand Down
12 changes: 11 additions & 1 deletion runtime/src/Provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,24 @@ type ProviderInput = {
config: Config
children: React.ReactNode
offlineInterface?: any // temporary until offline service has types
plugin: boolean
parentAlertsAdd: any
showAlertsInPlugin: boolean
}
export const Provider = ({
config,
children,
offlineInterface,
plugin,
parentAlertsAdd,
showAlertsInPlugin,
}: ProviderInput) => (
<ConfigProvider config={config}>
<AlertsProvider>
<AlertsProvider
plugin={plugin}
parentAlertsAdd={parentAlertsAdd}
showAlertsInPlugin={showAlertsInPlugin}
>
<DataProvider>
<OfflineProvider offlineInterface={offlineInterface}>
{children}
Expand Down
2 changes: 2 additions & 0 deletions runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ export {
clearSensitiveCaches,
} from '@dhis2/app-service-offline'

export { Plugin } from '@dhis2/app-service-plugin'

export { Provider } from './Provider'
3 changes: 3 additions & 0 deletions services/alerts/src/AlertsManagerContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const placeholder = () => {

const defaultAlertsManager: AlertsManager = {
add: placeholder,
plugin: false,
parentAlertsAdd: null,
showAlertsInPlugin: false,
}

export const AlertsManagerContext =
Expand Down
12 changes: 10 additions & 2 deletions services/alerts/src/AlertsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,25 @@ import { makeAlertsManager } from './makeAlertsManager'
import type { Alert, AlertsManager } from './types'

export const AlertsProvider = ({
plugin,
parentAlertsAdd,
showAlertsInPlugin,
children,
}: {
plugin: boolean
parentAlertsAdd: any
showAlertsInPlugin: boolean
children: React.ReactNode
}): ReactElement => {
const [alerts, setAlerts] = useState<Alert[]>([])
const [alertsManager] = useState<AlertsManager>(() =>
makeAlertsManager(setAlerts)
makeAlertsManager(setAlerts, plugin)
)

return (
<AlertsManagerContext.Provider value={alertsManager}>
<AlertsManagerContext.Provider
value={{ ...alertsManager, parentAlertsAdd, showAlertsInPlugin }}
>
<AlertsContext.Provider value={alerts}>
{children}
</AlertsContext.Provider>
Expand Down
1 change: 1 addition & 0 deletions services/alerts/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { AlertsProvider } from './AlertsProvider'
export { useAlerts } from './useAlerts'
export { useAlert } from './useAlert'
export { AlertsManagerContext } from './AlertsManagerContext'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is now being exported so that the PluginSender can access add from the parent (app) and pass it to the plugin, so that the alerts can then be added to the app's AlertsManager rather than the plugin's

4 changes: 3 additions & 1 deletion services/alerts/src/makeAlertsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const toVisibleAlertsArray = (alertsMap: AlertsMap) =>
Array.from(alertsMap.values())

export const makeAlertsManager = (
setAlerts: React.Dispatch<React.SetStateAction<Alert[]>>
setAlerts: React.Dispatch<React.SetStateAction<Alert[]>>,
plugin: boolean
): AlertsManager => {
const alertsMap: AlertsMap = new Map()
let id = 0
Expand All @@ -29,5 +30,6 @@ export const makeAlertsManager = (

return {
add,
plugin,
}
}
3 changes: 3 additions & 0 deletions services/alerts/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ export type AlertsMap = Map<number, Alert>

export type AlertsManager = {
add: (alert: Alert, alertRef: AlertRef) => Alert
plugin: boolean
parentAlertsAdd?: any
showAlertsInPlugin?: boolean
}
29 changes: 20 additions & 9 deletions services/alerts/src/useAlert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export const useAlert = (
message: string | ((props: any) => string),
options: AlertOptions | ((props: any) => AlertOptions) = {}
): { show: (props?: any) => void; hide: () => void } => {
const { add }: AlertsManager = useContext(AlertsManagerContext)
const { add, plugin, parentAlertsAdd, showAlertsInPlugin }: AlertsManager =
useContext(AlertsManagerContext)
const alertRef = useRef(<Alert | null>null)

const show = useCallback(
Expand All @@ -17,15 +18,25 @@ export const useAlert = (
const resolvedOptions =
typeof options === 'function' ? options(props) : options

alertRef.current = add(
{
message: resolvedMessage,
options: resolvedOptions,
},
alertRef
)
if (plugin && parentAlertsAdd && !showAlertsInPlugin) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I passed this showAlertsInPlugin down to useAlert as I thought it made the logic clearer here, but it could be incorporated into logic further up and then not passed down.

alertRef.current = parentAlertsAdd(
{
message: resolvedMessage,
options: resolvedOptions,
},
alertRef
)
} else {
alertRef.current = add(
{
message: resolvedMessage,
options: resolvedOptions,
},
alertRef
)
}
},
[add, message, options]
[add, parentAlertsAdd, message, options, plugin, showAlertsInPlugin]
)

const hide = useCallback(() => {
Expand Down
Loading