mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
260b878b7d
* Added toggle chat button to navigation header * new `useBrowserWindow` hook to work with browser title and focus * react2angular chat toggle button plumbing GitOrigin-RevId: 4380f1db9c7cc9a25bfb8d7a33e18d61b1d32993
60 lines
1.3 KiB
JavaScript
60 lines
1.3 KiB
JavaScript
import { useEffect, useState } from 'react'
|
|
|
|
let titleIsFlashing = false
|
|
let originalTitle
|
|
let flashIntervalHandle
|
|
|
|
export function flashTitle(message) {
|
|
if (document.hasFocus() || titleIsFlashing) {
|
|
return
|
|
}
|
|
|
|
function swapTitle() {
|
|
if (window.document.title === originalTitle) {
|
|
window.document.title = message
|
|
} else {
|
|
window.document.title = originalTitle
|
|
}
|
|
}
|
|
|
|
originalTitle = window.document.title
|
|
window.document.title = message
|
|
titleIsFlashing = true
|
|
flashIntervalHandle = setInterval(swapTitle, 800)
|
|
}
|
|
|
|
export function stopFlashingTitle() {
|
|
if (!titleIsFlashing) {
|
|
return
|
|
}
|
|
|
|
clearInterval(flashIntervalHandle)
|
|
window.document.title = originalTitle
|
|
originalTitle = undefined
|
|
titleIsFlashing = false
|
|
}
|
|
|
|
function useBrowserWindow() {
|
|
const [hasFocus, setHasFocus] = useState(document.hasFocus())
|
|
|
|
useEffect(() => {
|
|
function handleFocusEvent() {
|
|
setHasFocus(true)
|
|
}
|
|
|
|
function handleBlurEvent() {
|
|
setHasFocus(false)
|
|
}
|
|
|
|
window.addEventListener('focus', handleFocusEvent)
|
|
window.addEventListener('blur', handleBlurEvent)
|
|
return () => {
|
|
window.removeEventListener('focus', handleFocusEvent)
|
|
window.removeEventListener('blur', handleBlurEvent)
|
|
}
|
|
}, [])
|
|
|
|
return { hasFocus, flashTitle, stopFlashingTitle }
|
|
}
|
|
|
|
export default useBrowserWindow
|