mirror of
https://github.com/overleaf/overleaf.git
synced 2024-10-24 21:12:38 -04:00
aa480a2663
[web] migrate from window attributes to getMeta GitOrigin-RevId: 3dcf1ab6b01155e5e4abeb3e78d0fa9053e055bc
50 lines
1.4 KiB
TypeScript
50 lines
1.4 KiB
TypeScript
import { useEffect } from 'react'
|
|
import SystemMessage from './system-message'
|
|
import TranslationMessage from './translation-message'
|
|
import useAsync from '../hooks/use-async'
|
|
import { getJSON } from '@/infrastructure/fetch-json'
|
|
import getMeta from '../../utils/meta'
|
|
import { SystemMessage as TSystemMessage } from '../../../../types/system-message'
|
|
import { debugConsole } from '@/utils/debugging'
|
|
|
|
const MESSAGE_POLL_INTERVAL = 15 * 60 * 1000
|
|
|
|
function SystemMessages() {
|
|
const { data: messages, runAsync } = useAsync<TSystemMessage[]>()
|
|
const suggestedLanguage = getMeta('ol-suggestedLanguage')
|
|
|
|
useEffect(() => {
|
|
const pollMessages = () => {
|
|
// Ignore polling if tab is hidden or browser is offline
|
|
if (document.hidden || !navigator.onLine) {
|
|
return
|
|
}
|
|
|
|
runAsync(getJSON('/system/messages')).catch(debugConsole.error)
|
|
}
|
|
pollMessages()
|
|
|
|
const interval = setInterval(pollMessages, MESSAGE_POLL_INTERVAL)
|
|
|
|
return () => {
|
|
clearInterval(interval)
|
|
}
|
|
}, [runAsync])
|
|
|
|
if (!messages?.length && !suggestedLanguage) {
|
|
return null
|
|
}
|
|
|
|
return (
|
|
<ul className="system-messages">
|
|
{messages?.map((message, idx) => (
|
|
<SystemMessage key={idx} id={message._id}>
|
|
{message.content}
|
|
</SystemMessage>
|
|
))}
|
|
{suggestedLanguage ? <TranslationMessage /> : null}
|
|
</ul>
|
|
)
|
|
}
|
|
|
|
export default SystemMessages
|