mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
Merge pull request #11122 from overleaf/ii-rollback-some-ts-hooks-to-js
[web] Revert detach hooks from TS to JS GitOrigin-RevId: c108fe219d472f0c0b745dc8648fd15375f0e083
This commit is contained in:
parent
8ca4827834
commit
ea2af56f7b
4 changed files with 35 additions and 69 deletions
|
@ -1,27 +1,22 @@
|
|||
import { useCallback, useEffect } from 'react'
|
||||
import { useDetachContext } from '../context/detach-context'
|
||||
import getMeta from '../../utils/meta'
|
||||
import { DetachRole, DetachTargetRole, Message } from './use-detach-state'
|
||||
|
||||
const debugPdfDetach = getMeta('ol-debugPdfDetach')
|
||||
|
||||
function useDetachAction<
|
||||
A,
|
||||
S extends DetachRole,
|
||||
T extends DetachTargetRole<S>
|
||||
>(
|
||||
actionName: string,
|
||||
actionFunction: (...args: A[]) => void,
|
||||
senderRole: S,
|
||||
targetRole: T
|
||||
export default function useDetachAction(
|
||||
actionName,
|
||||
actionFunction,
|
||||
senderRole,
|
||||
targetRole
|
||||
) {
|
||||
const { role, broadcastEvent, addEventHandler, deleteEventHandler } =
|
||||
useDetachContext()
|
||||
|
||||
const eventName: Message['event'] = `action-${actionName}`
|
||||
const eventName = `action-${actionName}`
|
||||
|
||||
const triggerFn = useCallback(
|
||||
(...args: A[]) => {
|
||||
(...args) => {
|
||||
if (role === senderRole) {
|
||||
broadcastEvent(eventName, { args })
|
||||
} else {
|
||||
|
@ -32,7 +27,7 @@ function useDetachAction<
|
|||
)
|
||||
|
||||
const handleActionEvent = useCallback(
|
||||
(message: Message<A>) => {
|
||||
message => {
|
||||
if (message.event !== eventName) {
|
||||
return
|
||||
}
|
||||
|
@ -54,5 +49,3 @@ function useDetachAction<
|
|||
|
||||
return triggerFn
|
||||
}
|
||||
|
||||
export default useDetachAction
|
|
@ -0,0 +1,19 @@
|
|||
import { useEffect } from 'react'
|
||||
import useDetachState from './use-detach-state'
|
||||
|
||||
function useDetachStateWatcher(key, stateValue, senderRole, targetRole) {
|
||||
const [value, setValue] = useDetachState(
|
||||
key,
|
||||
stateValue,
|
||||
senderRole,
|
||||
targetRole
|
||||
)
|
||||
|
||||
useEffect(() => {
|
||||
setValue(stateValue)
|
||||
}, [setValue, stateValue])
|
||||
|
||||
return [value, setValue]
|
||||
}
|
||||
|
||||
export default useDetachStateWatcher
|
|
@ -1,32 +0,0 @@
|
|||
import { useEffect } from 'react'
|
||||
import useDetachState, {
|
||||
DetachRole,
|
||||
DetachTargetRole,
|
||||
} from './use-detach-state'
|
||||
|
||||
type UseDetachParams = Parameters<typeof useDetachState>
|
||||
|
||||
function useDetachStateWatcher<
|
||||
S extends DetachRole,
|
||||
T extends DetachTargetRole<S>
|
||||
>(
|
||||
key: UseDetachParams[0],
|
||||
stateValue: UseDetachParams[1],
|
||||
senderRole: S,
|
||||
targetRole: T
|
||||
) {
|
||||
const [value, setValue] = useDetachState(
|
||||
key,
|
||||
stateValue,
|
||||
senderRole,
|
||||
targetRole
|
||||
)
|
||||
|
||||
useEffect(() => {
|
||||
setValue(stateValue)
|
||||
}, [setValue, stateValue])
|
||||
|
||||
return [value, setValue]
|
||||
}
|
||||
|
||||
export default useDetachStateWatcher
|
|
@ -4,24 +4,12 @@ import getMeta from '../../utils/meta'
|
|||
|
||||
const debugPdfDetach = getMeta('ol-debugPdfDetach')
|
||||
|
||||
export type DetachRole = 'detacher' | 'detached'
|
||||
export type DetachTargetRole<T extends DetachRole> = T extends 'detacher'
|
||||
? 'detached'
|
||||
: 'detacher'
|
||||
export type Message<DataArgs = unknown> = {
|
||||
event: `${'action' | 'state'}-${string}`
|
||||
data: {
|
||||
args: DataArgs[]
|
||||
value: unknown
|
||||
}
|
||||
}
|
||||
|
||||
function useDetachState<S extends DetachRole, T extends DetachTargetRole<S>>(
|
||||
key: string,
|
||||
defaultValue: unknown,
|
||||
senderRole: S,
|
||||
targetRole: T
|
||||
): [unknown, React.Dispatch<unknown>] {
|
||||
export default function useDetachState(
|
||||
key,
|
||||
defaultValue,
|
||||
senderRole,
|
||||
targetRole
|
||||
) {
|
||||
const [value, setValue] = useState(defaultValue)
|
||||
|
||||
const {
|
||||
|
@ -32,7 +20,7 @@ function useDetachState<S extends DetachRole, T extends DetachTargetRole<S>>(
|
|||
deleteEventHandler,
|
||||
} = useDetachContext()
|
||||
|
||||
const eventName: Message['event'] = `state-${key}`
|
||||
const eventName = `state-${key}`
|
||||
|
||||
// lastDetachedConnectedAt is added as a dependency in order to re-broadcast
|
||||
// all states when a new detached tab connects
|
||||
|
@ -50,7 +38,7 @@ function useDetachState<S extends DetachRole, T extends DetachTargetRole<S>>(
|
|||
])
|
||||
|
||||
const handleStateEvent = useCallback(
|
||||
(message: Message) => {
|
||||
message => {
|
||||
if (message.event !== eventName) {
|
||||
return
|
||||
}
|
||||
|
@ -72,5 +60,3 @@ function useDetachState<S extends DetachRole, T extends DetachTargetRole<S>>(
|
|||
|
||||
return [value, setValue]
|
||||
}
|
||||
|
||||
export default useDetachState
|
Loading…
Reference in a new issue