alerts.tsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 { Alert } from 'react-bootstrap'
  6. import useScopeValue from '@/shared/hooks/use-scope-value'
  7. import { createPortal } from 'react-dom'
  8. import { useGlobalAlertsContainer } from '@/features/ide-react/context/global-alerts-context'
  9. export function Alerts() {
  10. const { t } = useTranslation()
  11. const {
  12. connectionState,
  13. isConnected,
  14. isStillReconnecting,
  15. tryReconnectNow,
  16. secondsUntilReconnect,
  17. } = useConnectionContext()
  18. const globalAlertsContainer = useGlobalAlertsContainer()
  19. const [synctexError] = useScopeValue('sync_tex_error')
  20. if (!globalAlertsContainer) {
  21. return null
  22. }
  23. return createPortal(
  24. <>
  25. {connectionState.forceDisconnected ? (
  26. <Alert bsStyle="danger" className="small">
  27. <strong>{t('disconnected')}</strong>
  28. </Alert>
  29. ) : null}
  30. {connectionState.reconnectAt ? (
  31. <LostConnectionAlert
  32. reconnectAt={connectionState.reconnectAt}
  33. tryReconnectNow={tryReconnectNow}
  34. />
  35. ) : null}
  36. {isStillReconnecting ? (
  37. <Alert bsStyle="warning" className="small">
  38. <strong>{t('reconnecting')}…</strong>
  39. </Alert>
  40. ) : null}
  41. {synctexError ? (
  42. <Alert bsStyle="warning" className="small">
  43. <strong>{t('synctex_failed')}</strong>
  44. <a
  45. href="/learn/how-to/SyncTeX_Errors"
  46. target="_blank"
  47. id="synctex-more-info-button"
  48. className="alert-link-as-btn pull-right"
  49. >
  50. {t('more_info')}
  51. </a>
  52. </Alert>
  53. ) : null}
  54. {connectionState.inactiveDisconnect ||
  55. (connectionState.readyState === WebSocket.CLOSED &&
  56. (connectionState.error === 'rate-limited' ||
  57. connectionState.error === 'unable-to-connect') &&
  58. !secondsUntilReconnect()) ? (
  59. <Alert bsStyle="warning" className="small">
  60. <strong>{t('editor_disconected_click_to_reconnect')}</strong>
  61. </Alert>
  62. ) : null}
  63. {debugging ? (
  64. <Alert bsStyle="warning" className="small">
  65. <strong>Connected: {isConnected.toString()}</strong>
  66. </Alert>
  67. ) : null}
  68. </>,
  69. globalAlertsContainer
  70. )
  71. }