alerts.tsx 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. <OLNotification
  28. type="error"
  29. content={<strong>{t('disconnected')}</strong>}
  30. />
  31. ) : null}
  32. {connectionState.reconnectAt ? (
  33. <LostConnectionAlert
  34. reconnectAt={connectionState.reconnectAt}
  35. tryReconnectNow={tryReconnectNow}
  36. />
  37. ) : null}
  38. {isStillReconnecting ? (
  39. <OLNotification
  40. type="warning"
  41. content={<strong>{t('reconnecting')}…</strong>}
  42. />
  43. ) : null}
  44. {synctexError ? (
  45. <OLNotification
  46. type="warning"
  47. content={<strong>{t('synctex_failed')}</strong>}
  48. action={
  49. <OLButton
  50. href="/learn/how-to/SyncTeX_Errors"
  51. target="_blank"
  52. id="synctex-more-info-button"
  53. variant="secondary"
  54. size="sm"
  55. bs3Props={{ className: 'alert-link-as-btn pull-right' }}
  56. >
  57. {t('more_info')}
  58. </OLButton>
  59. }
  60. />
  61. ) : null}
  62. {connectionState.inactiveDisconnect ||
  63. (connectionState.readyState === WebSocket.CLOSED &&
  64. (connectionState.error === 'rate-limited' ||
  65. connectionState.error === 'unable-to-connect') &&
  66. !secondsUntilReconnect()) ? (
  67. <OLNotification
  68. type="warning"
  69. content={
  70. <strong>{t('editor_disconected_click_to_reconnect')}</strong>
  71. }
  72. />
  73. ) : null}
  74. {debugging ? (
  75. <OLNotification
  76. type="warning"
  77. content={<strong>Connected: {isConnected.toString()}</strong>}
  78. />
  79. ) : null}
  80. </>,
  81. globalAlertsContainer
  82. )
  83. }