-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEdcHelp.tsx
52 lines (47 loc) · 1.66 KB
/
EdcHelp.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import React, { useEffect, useRef, useState } from 'react'
import '@fortawesome/fontawesome-free/css/all.min.css'
import './EdcHelp.scss'
import { PopoverConfigContext } from '../config/PopoverConfigProvider'
import { EdcHelpProps, PopoverData } from '../data/EdcHelpData'
import { buildData, getIcon } from '../handlers/EdcHelpHandler'
import { EdcPopover } from './EdcPopover'
import { PopoverContent } from 'edc-popover-utils'
const defaultProps: EdcHelpProps = {
pluginId: undefined,
mainKey: '',
subKey: '',
lang: undefined
}
export function EdcHelp(props: EdcHelpProps): JSX.Element {
const config = React.useContext(PopoverConfigContext)
// Mixing options by overriding global with local ones
config.options = { ...config.options, ...props.options }
const finalProps = { ...defaultProps, ...props }
// setData can be used to rerender the Component with new data (useful for async task)
const [data, setData] = useState<PopoverData>({
triggerError: false,
content: new PopoverContent('Loading...', 'Loading...'),
labels: {},
failBehaviorData: {
displayIcon: getIcon(config, props) || '',
errorIcon: ''
}
})
const isDataFetched = useRef(false)
// Make async tasks cancellable if component is unmounted (effect cleanup)
useEffect(() => {
let isMounted = true
if (!isDataFetched.current) {
buildData(config, finalProps, setData, isMounted)
}
isDataFetched.current = !isDataFetched.current
return (): void => {
isMounted = false
}
}, [config, finalProps])
return (
<div className='help-container'>
<EdcPopover edcHelp={finalProps} config={config} data={data} />
</div>
)
}