lost-connection-alert.tsx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { useTranslation } from 'react-i18next'
  2. import { useEffect, useState } from 'react'
  3. import { secondsUntil } from '@/features/ide-react/connection/utils'
  4. import OLNotification from '@/shared/components/ol/ol-notification'
  5. import OLButton from '@/shared/components/ol/ol-button'
  6. type LostConnectionAlertProps = {
  7. reconnectAt: number
  8. tryReconnectNow: () => void
  9. }
  10. export function LostConnectionAlert({
  11. reconnectAt,
  12. tryReconnectNow,
  13. }: LostConnectionAlertProps) {
  14. const { t } = useTranslation()
  15. const [secondsUntilReconnect, setSecondsUntilReconnect] = useState(
  16. secondsUntil(reconnectAt)
  17. )
  18. useEffect(() => {
  19. const timer = window.setInterval(() => {
  20. setSecondsUntilReconnect(secondsUntil(reconnectAt))
  21. }, 1000)
  22. return () => window.clearInterval(timer)
  23. }, [reconnectAt])
  24. return (
  25. <OLNotification
  26. type="warning"
  27. content={
  28. <>
  29. <strong>{t('lost_connection')}</strong>{' '}
  30. {t('reconnecting_in_x_secs', { seconds: secondsUntilReconnect })}.
  31. </>
  32. }
  33. action={
  34. <OLButton
  35. id="try-reconnect-now-button"
  36. onClick={() => tryReconnectNow()}
  37. size="sm"
  38. variant="secondary"
  39. >
  40. {t('try_now')}
  41. </OLButton>
  42. }
  43. />
  44. )
  45. }