mirror of
https://github.com/overleaf/overleaf.git
synced 2024-12-25 22:00:51 +00:00
50df230846
GitOrigin-RevId: 02f97af1b9704782eee77a0b7dfc477ada23e34d
48 lines
1.2 KiB
JavaScript
48 lines
1.2 KiB
JavaScript
import { useEffect, useState, useCallback } from 'react'
|
|
import { useDetachContext } from '../context/detach-context'
|
|
import getMeta from '../../utils/meta'
|
|
|
|
const debugPdfDetach = getMeta('ol-debugPdfDetach')
|
|
|
|
export default function useDetachState(
|
|
key,
|
|
defaultValue,
|
|
senderRole,
|
|
targetRole
|
|
) {
|
|
const [value, setValue] = useState(defaultValue)
|
|
|
|
const { role, broadcastEvent, addEventHandler, deleteEventHandler } =
|
|
useDetachContext()
|
|
|
|
const eventName = `state-${key}`
|
|
|
|
useEffect(() => {
|
|
if (role === senderRole) {
|
|
broadcastEvent(eventName, { value })
|
|
}
|
|
}, [role, senderRole, eventName, value, broadcastEvent])
|
|
|
|
const handleStateEvent = useCallback(
|
|
message => {
|
|
if (message.event !== eventName) {
|
|
return
|
|
}
|
|
if (role !== targetRole) {
|
|
return
|
|
}
|
|
if (debugPdfDetach) {
|
|
console.log(`Set ${message.data.value} for ${eventName}`)
|
|
}
|
|
setValue(message.data.value)
|
|
},
|
|
[role, targetRole, eventName, setValue]
|
|
)
|
|
|
|
useEffect(() => {
|
|
addEventHandler(handleStateEvent)
|
|
return () => deleteEventHandler(handleStateEvent)
|
|
}, [addEventHandler, deleteEventHandler, handleStateEvent])
|
|
|
|
return [value, setValue]
|
|
}
|