alerts.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { useTranslation } from 'react-i18next'
  2. import { LostConnectionAlert } from './lost-connection-alert'
  3. import { useConnectionContext } from '@/features/ide-react/context/connection-context'
  4. import { debugging } from '@/utils/debugging'
  5. import { ElementType } from 'react'
  6. import { createPortal } from 'react-dom'
  7. import { useGlobalAlertsContainer } from '@/features/ide-react/context/global-alerts-context'
  8. import OLNotification from '@/shared/components/ol/ol-notification'
  9. import importOverleafModules from '../../../../../macros/import-overleaf-module.macro'
  10. const rollingBuildsUpdatedAlert: Array<{
  11. import: { default: ElementType }
  12. path: string
  13. }> = importOverleafModules('rollingBuildsUpdatedAlert')
  14. export function Alerts() {
  15. const { t } = useTranslation()
  16. const {
  17. connectionState,
  18. isConnected,
  19. isStillReconnecting,
  20. tryReconnectNow,
  21. secondsUntilReconnect,
  22. } = useConnectionContext()
  23. const globalAlertsContainer = useGlobalAlertsContainer()
  24. if (!globalAlertsContainer) {
  25. return null
  26. }
  27. return createPortal(
  28. <>
  29. {rollingBuildsUpdatedAlert.map(
  30. ({ import: { default: Component }, path }) => (
  31. <Component key={path} />
  32. )
  33. )}
  34. {connectionState.forceDisconnected &&
  35. // hide "disconnected" banner when displaying out of sync modal
  36. connectionState.error !== 'out-of-sync' ? (
  37. <OLNotification
  38. type="error"
  39. content={<strong>{t('disconnected')}</strong>}
  40. />
  41. ) : null}
  42. {connectionState.reconnectAt ? (
  43. <LostConnectionAlert
  44. reconnectAt={connectionState.reconnectAt}
  45. tryReconnectNow={tryReconnectNow}
  46. />
  47. ) : null}
  48. {isStillReconnecting ? (
  49. <OLNotification
  50. type="warning"
  51. content={<strong>{t('reconnecting')}…</strong>}
  52. />
  53. ) : null}
  54. {connectionState.inactiveDisconnect ||
  55. (connectionState.readyState === WebSocket.CLOSED &&
  56. (connectionState.error === 'rate-limited' ||
  57. connectionState.error === 'unable-to-connect') &&
  58. !secondsUntilReconnect()) ? (
  59. <OLNotification
  60. type="warning"
  61. content={
  62. <strong>{t('editor_disconected_click_to_reconnect')}</strong>
  63. }
  64. />
  65. ) : null}
  66. {debugging ? (
  67. <OLNotification
  68. type="warning"
  69. content={<strong>Connected: {isConnected.toString()}</strong>}
  70. />
  71. ) : null}
  72. </>,
  73. globalAlertsContainer
  74. )
  75. }