banner.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. """Banner components."""
  2. from __future__ import annotations
  3. from typing import Optional
  4. from reflex.components.base.bare import Bare
  5. from reflex.components.component import Component
  6. from reflex.components.layout import Box, Cond
  7. from reflex.components.overlay.modal import Modal
  8. from reflex.components.typography import Text
  9. from reflex.utils import imports
  10. from reflex.vars import ImportVar, Var
  11. connection_error: Var = Var.create_safe(
  12. value="(connectError !== null) ? connectError.message : ''",
  13. _var_is_local=False,
  14. _var_is_string=False,
  15. )
  16. has_connection_error: Var = Var.create_safe(
  17. value="connectError !== null",
  18. _var_is_string=False,
  19. )
  20. has_connection_error._var_type = bool
  21. class WebsocketTargetURL(Bare):
  22. """A component that renders the websocket target URL."""
  23. def _get_imports(self) -> imports.ImportDict:
  24. return {
  25. "/utils/state.js": [ImportVar(tag="getEventURL")],
  26. }
  27. @classmethod
  28. def create(cls) -> Component:
  29. """Create a websocket target URL component.
  30. Returns:
  31. The websocket target URL component.
  32. """
  33. return super().create(contents="{getEventURL().href}")
  34. def default_connection_error() -> list[str | Var | Component]:
  35. """Get the default connection error message.
  36. Returns:
  37. The default connection error message.
  38. """
  39. return [
  40. "Cannot connect to server: ",
  41. connection_error,
  42. ". Check if server is reachable at ",
  43. WebsocketTargetURL.create(),
  44. ]
  45. class ConnectionBanner(Component):
  46. """A connection banner component."""
  47. @classmethod
  48. def create(cls, comp: Optional[Component] = None) -> Component:
  49. """Create a connection banner component.
  50. Args:
  51. comp: The component to render when there's a server connection error.
  52. Returns:
  53. The connection banner component.
  54. """
  55. if not comp:
  56. comp = Box.create(
  57. Text.create(
  58. *default_connection_error(),
  59. bg="red",
  60. color="white",
  61. ),
  62. textAlign="center",
  63. )
  64. return Cond.create(has_connection_error, comp)
  65. class ConnectionModal(Component):
  66. """A connection status modal window."""
  67. @classmethod
  68. def create(cls, comp: Optional[Component] = None) -> Component:
  69. """Create a connection banner component.
  70. Args:
  71. comp: The component to render when there's a server connection error.
  72. Returns:
  73. The connection banner component.
  74. """
  75. if not comp:
  76. comp = Text.create(*default_connection_error())
  77. return Cond.create(
  78. has_connection_error,
  79. Modal.create(
  80. header="Connection Error",
  81. body=comp,
  82. is_open=has_connection_error,
  83. ),
  84. )