alerts.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. >
  58. {t('more_info')}
  59. </OLButton>
  60. }
  61. />
  62. ) : null}
  63. {connectionState.inactiveDisconnect ||
  64. (connectionState.readyState === WebSocket.CLOSED &&
  65. (connectionState.error === 'rate-limited' ||
  66. connectionState.error === 'unable-to-connect') &&
  67. !secondsUntilReconnect()) ? (
  68. <OLNotification
  69. type="warning"
  70. content={
  71. <strong>{t('editor_disconected_click_to_reconnect')}</strong>
  72. }
  73. />
  74. ) : null}
  75. {debugging ? (
  76. <OLNotification
  77. type="warning"
  78. content={<strong>Connected: {isConnected.toString()}</strong>}
  79. />
  80. ) : null}
  81. </>,
  82. globalAlertsContainer
  83. )
  84. }