2021-11-15 11:33:57 -05:00
|
|
|
import {
|
|
|
|
createContext,
|
|
|
|
useContext,
|
|
|
|
useCallback,
|
|
|
|
useMemo,
|
|
|
|
useEffect,
|
|
|
|
useState,
|
2024-01-26 04:23:48 -05:00
|
|
|
FC,
|
2021-11-15 11:33:57 -05:00
|
|
|
} from 'react'
|
|
|
|
import getMeta from '../../utils/meta'
|
|
|
|
import { buildUrlWithDetachRole } from '../utils/url-helper'
|
|
|
|
import useCallbackHandlers from '../hooks/use-callback-handlers'
|
2023-09-27 05:45:49 -04:00
|
|
|
import { debugConsole } from '@/utils/debugging'
|
2021-11-15 11:33:57 -05:00
|
|
|
|
2024-01-26 04:23:48 -05:00
|
|
|
export type DetachRole = 'detacher' | 'detached' | null
|
2021-11-15 11:33:57 -05:00
|
|
|
|
2024-01-26 04:23:48 -05:00
|
|
|
type Message = {
|
|
|
|
role: DetachRole
|
|
|
|
event: string
|
|
|
|
data?: any
|
2021-11-15 11:33:57 -05:00
|
|
|
}
|
|
|
|
|
2024-01-26 04:23:48 -05:00
|
|
|
export const DetachContext = createContext<
|
|
|
|
| {
|
|
|
|
role: DetachRole
|
|
|
|
setRole: (role: DetachRole) => void
|
|
|
|
broadcastEvent: (event: string, data?: any) => void
|
|
|
|
addEventHandler: (handler: (...args: any[]) => void) => void
|
|
|
|
deleteEventHandler: (handler: (...args: any[]) => void) => void
|
|
|
|
}
|
|
|
|
| undefined
|
|
|
|
>(undefined)
|
|
|
|
|
2021-11-15 11:33:57 -05:00
|
|
|
const debugPdfDetach = getMeta('ol-debugPdfDetach')
|
|
|
|
|
2023-01-09 07:52:11 -05:00
|
|
|
const projectId = getMeta('ol-project_id')
|
|
|
|
export const detachChannelId = `detach-${projectId}`
|
|
|
|
export const detachChannel =
|
|
|
|
'BroadcastChannel' in window
|
|
|
|
? new BroadcastChannel(detachChannelId)
|
|
|
|
: undefined
|
2021-11-15 11:33:57 -05:00
|
|
|
|
2024-01-26 04:23:48 -05:00
|
|
|
export const DetachProvider: FC = ({ children }) => {
|
|
|
|
const [lastDetachedConnectedAt, setLastDetachedConnectedAt] = useState<Date>()
|
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) {
|
2023-09-27 05:45:49 -04:00
|
|
|
debugConsole.warn('Effect', { role })
|
2021-11-15 11:33:57 -05:00
|
|
|
}
|
2021-12-14 08:24:53 -05:00
|
|
|
window.history.replaceState({}, '', buildUrlWithDetachRole(role).toString())
|
2021-11-15 11:33:57 -05:00
|
|
|
}, [role])
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-01-09 07:52:11 -05:00
|
|
|
if (detachChannel) {
|
2024-01-26 04:23:48 -05:00
|
|
|
const listener = (event: MessageEvent) => {
|
2023-01-09 07:52:11 -05:00
|
|
|
if (debugPdfDetach) {
|
2023-09-27 05:45:49 -04:00
|
|
|
debugConsole.warn(`Receiving:`, event.data)
|
2023-01-09 07:52:11 -05:00
|
|
|
}
|
|
|
|
callEventHandlers(event.data)
|
|
|
|
}
|
|
|
|
|
|
|
|
detachChannel.addEventListener('message', listener)
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
detachChannel.removeEventListener('message', listener)
|
2021-11-15 11:33:57 -05:00
|
|
|
}
|
2023-01-09 07:52:11 -05:00
|
|
|
}
|
2021-11-15 11:33:57 -05:00
|
|
|
}, [callEventHandlers])
|
|
|
|
|
|
|
|
const broadcastEvent = useCallback(
|
2024-01-26 04:23:48 -05:00
|
|
|
(event: string, data?: any) => {
|
2021-11-15 11:33:57 -05:00
|
|
|
if (!role) {
|
|
|
|
if (debugPdfDetach) {
|
2023-09-27 05:45:49 -04:00
|
|
|
debugConsole.warn('Not Broadcasting (no role)', {
|
2021-11-15 11:33:57 -05:00
|
|
|
role,
|
|
|
|
event,
|
|
|
|
data,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (debugPdfDetach) {
|
2023-09-27 05:45:49 -04:00
|
|
|
debugConsole.warn('Broadcasting', {
|
2021-11-15 11:33:57 -05:00
|
|
|
role,
|
|
|
|
event,
|
|
|
|
data,
|
|
|
|
})
|
|
|
|
}
|
2024-01-26 04:23:48 -05:00
|
|
|
const message: Message = { role, event }
|
2021-12-14 08:24:53 -05:00
|
|
|
if (data) {
|
|
|
|
message.data = data
|
|
|
|
}
|
2023-01-09 07:52:11 -05:00
|
|
|
|
|
|
|
detachChannel?.postMessage(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(() => {
|
2024-01-26 04:23:48 -05:00
|
|
|
const updateLastDetachedConnectedAt = (message: Message) => {
|
2022-03-31 07:22:36 -04:00
|
|
|
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>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-01-26 04:23:48 -05:00
|
|
|
export function useDetachContext() {
|
2021-11-15 11:33:57 -05:00
|
|
|
const data = useContext(DetachContext)
|
2022-05-20 04:15:49 -04:00
|
|
|
if (!data) {
|
|
|
|
throw new Error('useDetachContext is only available inside DetachProvider')
|
|
|
|
}
|
2021-11-15 11:33:57 -05:00
|
|
|
return data
|
|
|
|
}
|