overleaf/services/web/frontend/js/features/ide-react/components/alerts/lost-connection-alert.tsx
Tim Down 87199c80fe React IDE page: add connection and SyncTex alerts (#15273)
Add connection and SyncTex alerts

GitOrigin-RevId: 5004a0d356d0a0355d125516a18db1f57e617a7f
2023-10-19 08:03:04 +00:00

40 lines
1.1 KiB
TypeScript

import { useTranslation } from 'react-i18next'
import { useEffect, useState } from 'react'
import { secondsUntil } from '@/features/ide-react/connection/utils'
import { Alert } from 'react-bootstrap'
type LostConnectionAlertProps = {
reconnectAt: number
tryReconnectNow: () => void
}
export function LostConnectionAlert({
reconnectAt,
tryReconnectNow,
}: LostConnectionAlertProps) {
const { t } = useTranslation()
const [secondsUntilReconnect, setSecondsUntilReconnect] = useState(
secondsUntil(reconnectAt)
)
useEffect(() => {
const timer = window.setInterval(() => {
setSecondsUntilReconnect(secondsUntil(reconnectAt))
}, 1000)
return () => window.clearInterval(timer)
}, [reconnectAt])
return (
<Alert bsStyle="warning" className="small">
<strong>{t('lost_connection')}</strong>{' '}
{t('reconnecting_in_x_secs', { seconds: secondsUntilReconnect })}.
<button
id="try-reconnect-now-button"
className="pull-right"
onClick={() => tryReconnectNow()}
>
{t('try_now')}
</button>
</Alert>
)
}