alerts.tsx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 { createPortal } from 'react-dom'
  6. import { useGlobalAlertsContainer } from '@/features/ide-react/context/global-alerts-context'
  7. import OLNotification from '@/features/ui/components/ol/ol-notification'
  8. export function Alerts() {
  9. const { t } = useTranslation()
  10. const {
  11. connectionState,
  12. isConnected,
  13. isStillReconnecting,
  14. tryReconnectNow,
  15. secondsUntilReconnect,
  16. } = useConnectionContext()
  17. const globalAlertsContainer = useGlobalAlertsContainer()
  18. if (!globalAlertsContainer) {
  19. return null
  20. }
  21. return createPortal(
  22. <>
  23. {connectionState.forceDisconnected &&
  24. // hide "disconnected" banner when displaying out of sync modal
  25. connectionState.error !== 'out-of-sync' ? (
  26. <OLNotification
  27. type="error"
  28. content={<strong>{t('disconnected')}</strong>}
  29. />
  30. ) : null}
  31. {connectionState.reconnectAt ? (
  32. <LostConnectionAlert
  33. reconnectAt={connectionState.reconnectAt}
  34. tryReconnectNow={tryReconnectNow}
  35. />
  36. ) : null}
  37. {isStillReconnecting ? (
  38. <OLNotification
  39. type="warning"
  40. content={<strong>{t('reconnecting')}…</strong>}
  41. />
  42. ) : null}
  43. {connectionState.inactiveDisconnect ||
  44. (connectionState.readyState === WebSocket.CLOSED &&
  45. (connectionState.error === 'rate-limited' ||
  46. connectionState.error === 'unable-to-connect') &&
  47. !secondsUntilReconnect()) ? (
  48. <OLNotification
  49. type="warning"
  50. content={
  51. <strong>{t('editor_disconected_click_to_reconnect')}</strong>
  52. }
  53. />
  54. ) : null}
  55. {debugging ? (
  56. <OLNotification
  57. type="warning"
  58. content={<strong>Connected: {isConnected.toString()}</strong>}
  59. />
  60. ) : null}
  61. </>,
  62. globalAlertsContainer
  63. )
  64. }