mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-30 11:04:12 -05:00
eb3e5037f8
[ide-react] Improve handling of lost connection GitOrigin-RevId: 89b641b2beca4f9de65551e6873b3c8c11bb1695
36 lines
950 B
TypeScript
36 lines
950 B
TypeScript
import { createContext, FC, useCallback, useContext, useState } from 'react'
|
|
|
|
const GlobalAlertsContext = createContext<HTMLDivElement | null | undefined>(
|
|
undefined
|
|
)
|
|
|
|
export const GlobalAlertsProvider: FC = ({ children }) => {
|
|
const [globalAlertsContainer, setGlobalAlertsContainer] =
|
|
useState<HTMLDivElement | null>(null)
|
|
|
|
const handleGlobalAlertsContainer = useCallback(
|
|
(node: HTMLDivElement | null) => {
|
|
setGlobalAlertsContainer(node)
|
|
},
|
|
[]
|
|
)
|
|
|
|
return (
|
|
<GlobalAlertsContext.Provider value={globalAlertsContainer}>
|
|
<div className="global-alerts" ref={handleGlobalAlertsContainer} />
|
|
{children}
|
|
</GlobalAlertsContext.Provider>
|
|
)
|
|
}
|
|
|
|
export const useGlobalAlertsContainer = () => {
|
|
const context = useContext(GlobalAlertsContext)
|
|
|
|
if (context === undefined) {
|
|
throw new Error(
|
|
'useGlobalAlertsContainer is only available inside GlobalAlertsProvider'
|
|
)
|
|
}
|
|
|
|
return context
|
|
}
|