mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
Close pop-up window and update UI after reference provider linking (#15106)
GitOrigin-RevId: 3c93491041170cd78c66bc0ab5db516749f9eded
This commit is contained in:
parent
a51259e48f
commit
23e2d9bf99
4 changed files with 45 additions and 0 deletions
|
@ -5,8 +5,10 @@ import importOverleafModules from '../../../../macros/import-overleaf-module.mac
|
|||
import { useSSOContext, SSOSubscription } from '../context/sso-context'
|
||||
import { SSOLinkingWidget } from './linking/sso-widget'
|
||||
import getMeta from '../../../utils/meta'
|
||||
import { useBroadcastUser } from '@/shared/hooks/user-channel/use-broadcast-user'
|
||||
|
||||
function LinkingSection() {
|
||||
useBroadcastUser()
|
||||
const { t } = useTranslation()
|
||||
const { subscriptions } = useSSOContext()
|
||||
const ssoErrorMessage = getMeta('ol-ssoErrorMessage') as string
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
import { useEffect } from 'react'
|
||||
import { useUserContext } from '@/shared/context/user-context'
|
||||
import { useUserChannel } from './use-user-channel'
|
||||
|
||||
export const useBroadcastUser = () => {
|
||||
const user = useUserContext()
|
||||
const channel = useUserChannel()
|
||||
|
||||
useEffect(() => {
|
||||
channel?.postMessage(user)
|
||||
}, [channel, user])
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
import { useEffect } from 'react'
|
||||
import { useUserChannel } from './use-user-channel'
|
||||
|
||||
export const useReceiveUser = (
|
||||
handleData: (data: Record<string, any>) => void
|
||||
) => {
|
||||
const channel = useUserChannel()
|
||||
|
||||
useEffect(() => {
|
||||
const abortController = new AbortController()
|
||||
channel?.addEventListener('message', ({ data }) => handleData(data), {
|
||||
signal: abortController.signal,
|
||||
})
|
||||
return () => abortController.abort()
|
||||
}, [channel, handleData])
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
import { useEffect, useRef } from 'react'
|
||||
|
||||
export const useUserChannel = (): BroadcastChannel | null => {
|
||||
const channelRef = useRef<BroadcastChannel | null>(null)
|
||||
|
||||
if (channelRef.current === null && 'BroadcastChannel' in window) {
|
||||
channelRef.current = new BroadcastChannel('user')
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
return () => channelRef.current?.close()
|
||||
}, [])
|
||||
|
||||
return channelRef.current
|
||||
}
|
Loading…
Reference in a new issue