alerts.tsx 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 useScopeValue from '@/shared/hooks/use-scope-value'
  6. import { createPortal } from 'react-dom'
  7. import { useGlobalAlertsContainer } from '@/features/ide-react/context/global-alerts-context'
  8. import OLNotification from '@/features/ui/components/ol/ol-notification'
  9. import OLButton from '@/features/ui/components/ol/ol-button'
  10. export function Alerts() {
  11. const { t } = useTranslation()
  12. const {
  13. connectionState,
  14. isConnected,
  15. isStillReconnecting,
  16. tryReconnectNow,
  17. secondsUntilReconnect,
  18. } = useConnectionContext()
  19. const globalAlertsContainer = useGlobalAlertsContainer()
  20. const [synctexError] = useScopeValue('sync_tex_error')
  21. if (!globalAlertsContainer) {
  22. return null
  23. }
  24. return createPortal(
  25. <>
  26. {connectionState.forceDisconnected &&
  27. // hide "disconnected" banner when displaying out of sync modal
  28. connectionState.error !== 'out-of-sync' ? (
  29. <OLNotification
  30. type="error"
  31. content={<strong>{t('disconnected')}</strong>}
  32. />
  33. ) : null}
  34. {connectionState.reconnectAt ? (
  35. <LostConnectionAlert
  36. reconnectAt={connectionState.reconnectAt}
  37. tryReconnectNow={tryReconnectNow}
  38. />
  39. ) : null}
  40. {isStillReconnecting ? (
  41. <OLNotification
  42. type="warning"
  43. content={<strong>{t('reconnecting')}…</strong>}
  44. />
  45. ) : null}
  46. {synctexError ? (
  47. <OLNotification
  48. type="warning"
  49. content={<strong>{t('synctex_failed')}</strong>}
  50. action={
  51. <OLButton
  52. href="/learn/how-to/SyncTeX_Errors"
  53. target="_blank"
  54. id="synctex-more-info-button"
  55. variant="secondary"
  56. size="sm"
  57. bs3Props={{ className: 'alert-link-as-btn pull-right' }}
  58. >
  59. {t('more_info')}
  60. </OLButton>
  61. }
  62. />
  63. ) : null}
  64. {connectionState.inactiveDisconnect ||
  65. (connectionState.readyState === WebSocket.CLOSED &&
  66. (connectionState.error === 'rate-limited' ||
  67. connectionState.error === 'unable-to-connect') &&
  68. !secondsUntilReconnect()) ? (
  69. <OLNotification
  70. type="warning"
  71. content={
  72. <strong>{t('editor_disconected_click_to_reconnect')}</strong>
  73. }
  74. />
  75. ) : null}
  76. {debugging ? (
  77. <OLNotification
  78. type="warning"
  79. content={<strong>Connected: {isConnected.toString()}</strong>}
  80. />
  81. ) : null}
  82. </>,
  83. globalAlertsContainer
  84. )
  85. }