2021-11-15 11:33:57 -05:00
|
|
|
import {
|
|
|
|
createContext,
|
|
|
|
useContext,
|
|
|
|
useCallback,
|
|
|
|
useMemo,
|
|
|
|
useEffect,
|
|
|
|
useState,
|
|
|
|
} from 'react'
|
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
import sysend from 'sysend'
|
|
|
|
import getMeta from '../../utils/meta'
|
|
|
|
import { buildUrlWithDetachRole } from '../utils/url-helper'
|
|
|
|
import useCallbackHandlers from '../hooks/use-callback-handlers'
|
|
|
|
|
|
|
|
export const DetachContext = createContext()
|
|
|
|
|
|
|
|
DetachContext.Provider.propTypes = {
|
|
|
|
value: PropTypes.shape({
|
|
|
|
role: PropTypes.oneOf(['detacher', 'detached', null]),
|
|
|
|
setRole: PropTypes.func.isRequired,
|
|
|
|
broadcastEvent: PropTypes.func.isRequired,
|
|
|
|
addEventHandler: PropTypes.func.isRequired,
|
|
|
|
deleteEventHandler: PropTypes.func.isRequired,
|
|
|
|
}).isRequired,
|
|
|
|
}
|
|
|
|
|
|
|
|
const debugPdfDetach = getMeta('ol-debugPdfDetach')
|
|
|
|
|
|
|
|
const SYSEND_CHANNEL = `detach-${getMeta('ol-project_id')}`
|
|
|
|
|
|
|
|
export function DetachProvider({ children }) {
|
2022-03-31 07:22:36 -04:00
|
|
|
const [lastDetachedConnectedAt, setLastDetachedConnectedAt] = useState()
|
2021-11-15 11:33:57 -05:00
|
|
|
const [role, setRole] = useState(() => getMeta('ol-detachRole') || null)
|
|
|
|
const {
|
|
|
|
addHandler: addEventHandler,
|
|
|
|
deleteHandler: deleteEventHandler,
|
|
|
|
callHandlers: callEventHandlers,
|
|
|
|
} = useCallbackHandlers()
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (debugPdfDetach) {
|
|
|
|
console.log('Effect', { role })
|
|
|
|
}
|
2021-12-14 08:24:53 -05:00
|
|
|
window.history.replaceState({}, '', buildUrlWithDetachRole(role).toString())
|
2021-11-15 11:33:57 -05:00
|
|
|
}, [role])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
sysend.on(SYSEND_CHANNEL, message => {
|
|
|
|
if (debugPdfDetach) {
|
|
|
|
console.log(`Receiving:`, message)
|
|
|
|
}
|
|
|
|
callEventHandlers(message)
|
|
|
|
})
|
|
|
|
return () => sysend.off(SYSEND_CHANNEL)
|
|
|
|
}, [callEventHandlers])
|
|
|
|
|
|
|
|
const broadcastEvent = useCallback(
|
|
|
|
(event, data) => {
|
|
|
|
if (!role) {
|
|
|
|
if (debugPdfDetach) {
|
|
|
|
console.log('Not Broadcasting (no role)', {
|
|
|
|
role,
|
|
|
|
event,
|
|
|
|
data,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (debugPdfDetach) {
|
|
|
|
console.log('Broadcasting', {
|
|
|
|
role,
|
|
|
|
event,
|
|
|
|
data,
|
|
|
|
})
|
|
|
|
}
|
2021-12-14 08:24:53 -05:00
|
|
|
const message = {
|
2021-11-15 11:33:57 -05:00
|
|
|
role,
|
|
|
|
event,
|
2021-12-14 08:24:53 -05:00
|
|
|
}
|
|
|
|
if (data) {
|
|
|
|
message.data = data
|
|
|
|
}
|
|
|
|
sysend.broadcast(SYSEND_CHANNEL, message)
|
2021-11-15 11:33:57 -05:00
|
|
|
},
|
|
|
|
[role]
|
|
|
|
)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
broadcastEvent('connected')
|
|
|
|
}, [broadcastEvent])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const onBeforeUnload = () => broadcastEvent('closed')
|
|
|
|
window.addEventListener('beforeunload', onBeforeUnload)
|
|
|
|
return () => window.removeEventListener('beforeunload', onBeforeUnload)
|
|
|
|
}, [broadcastEvent])
|
|
|
|
|
2022-03-31 07:22:36 -04:00
|
|
|
useEffect(() => {
|
|
|
|
const updateLastDetachedConnectedAt = message => {
|
|
|
|
if (message.role === 'detached' && message.event === 'connected') {
|
|
|
|
setLastDetachedConnectedAt(new Date())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
addEventHandler(updateLastDetachedConnectedAt)
|
|
|
|
return () => deleteEventHandler(updateLastDetachedConnectedAt)
|
|
|
|
}, [addEventHandler, deleteEventHandler])
|
|
|
|
|
2021-11-15 11:33:57 -05:00
|
|
|
const value = useMemo(
|
|
|
|
() => ({
|
|
|
|
role,
|
|
|
|
setRole,
|
|
|
|
broadcastEvent,
|
2022-03-31 07:22:36 -04:00
|
|
|
lastDetachedConnectedAt,
|
2021-11-15 11:33:57 -05:00
|
|
|
addEventHandler,
|
|
|
|
deleteEventHandler,
|
|
|
|
}),
|
2022-03-31 07:22:36 -04:00
|
|
|
[
|
|
|
|
role,
|
|
|
|
setRole,
|
|
|
|
broadcastEvent,
|
|
|
|
lastDetachedConnectedAt,
|
|
|
|
addEventHandler,
|
|
|
|
deleteEventHandler,
|
|
|
|
]
|
2021-11-15 11:33:57 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<DetachContext.Provider value={value}>{children}</DetachContext.Provider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
DetachProvider.propTypes = {
|
|
|
|
children: PropTypes.any,
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useDetachContext(propTypes) {
|
|
|
|
const data = useContext(DetachContext)
|
|
|
|
PropTypes.checkPropTypes(propTypes, data, 'data', 'DetachContext.Provider')
|
|
|
|
return data
|
|
|
|
}
|