2022-10-12 10:52:53 -04:00
|
|
|
import { useEffect } from 'react'
|
|
|
|
import SystemMessage from './system-message'
|
|
|
|
import TranslationMessage from './translation-message'
|
2023-12-14 06:06:36 -05:00
|
|
|
import useAsync from '../hooks/use-async'
|
|
|
|
import { getJSON } from '@/infrastructure/fetch-json'
|
|
|
|
import getMeta from '../../utils/meta'
|
2022-10-12 10:52:53 -04:00
|
|
|
import {
|
|
|
|
SystemMessage as TSystemMessage,
|
|
|
|
SuggestedLanguage,
|
2023-12-14 06:06:36 -05:00
|
|
|
} from '../../../../types/system-message'
|
2023-09-27 05:45:49 -04:00
|
|
|
import { debugConsole } from '@/utils/debugging'
|
2022-10-12 10:52:53 -04:00
|
|
|
|
|
|
|
const MESSAGE_POLL_INTERVAL = 15 * 60 * 1000
|
|
|
|
|
|
|
|
function SystemMessages() {
|
|
|
|
const { data: messages, runAsync } = useAsync<TSystemMessage[]>()
|
|
|
|
const suggestedLanguage = getMeta('ol-suggestedLanguage', undefined) as
|
|
|
|
| SuggestedLanguage
|
|
|
|
| undefined
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const pollMessages = () => {
|
|
|
|
// Ignore polling if tab is hidden or browser is offline
|
|
|
|
if (document.hidden || !navigator.onLine) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-27 05:45:49 -04:00
|
|
|
runAsync(getJSON('/system/messages')).catch(debugConsole.error)
|
2022-10-12 10:52:53 -04:00
|
|
|
}
|
|
|
|
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
|