index.js.jinja2 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. {% extends "web/pages/base_page.js.jinja2" %}
  2. {% block declaration %}
  3. {% for custom_code in custom_codes %}
  4. {{custom_code}}
  5. {% endfor %}
  6. {% endblock %}
  7. {% block export %}
  8. export default function Component() {
  9. const [{{state_name}}, {{state_name|react_setter}}] = useState({{initial_state|json_dumps}})
  10. const [{{const.result}}, {{const.result|react_setter}}] = useState({{const.initial_result|json_dumps}})
  11. const [notConnected, setNotConnected] = useState(false)
  12. const {{const.router}} = useRouter()
  13. const {{const.socket}} = useRef(null)
  14. const { isReady } = {{const.router}}
  15. const { {{const.color_mode}}, {{const.toggle_color_mode}} } = {{const.use_color_mode}}()
  16. const focusRef = useRef();
  17. // Function to add new events to the event queue.
  18. const Event = (events, _e) => {
  19. preventDefault(_e);
  20. {{state_name|react_setter}}(state => ({
  21. ...state,
  22. events: [...state.events, ...events],
  23. }))
  24. }
  25. // Function to add new files to be uploaded.
  26. const File = files => {{state_name|react_setter}}(state => ({
  27. ...state,
  28. files,
  29. }))
  30. // Main event loop.
  31. useEffect(()=> {
  32. // Skip if the router is not ready.
  33. if (!isReady) {
  34. return;
  35. }
  36. // Initialize the websocket connection.
  37. if (!{{const.socket}}.current) {
  38. connect({{const.socket}}, {{state_name}}, {{state_name|react_setter}}, {{const.result}}, {{const.result|react_setter}}, {{const.router}}, {{transports}}, setNotConnected)
  39. }
  40. // If we are not processing an event, process the next event.
  41. if (!{{const.result}}.{{const.processing}}) {
  42. processEvent({{state_name}}, {{state_name|react_setter}}, {{const.result}}, {{const.result|react_setter}}, {{const.router}}, {{const.socket}}.current)
  43. }
  44. // If there is a new result, update the state.
  45. if ({{const.result}}.{{const.state}} != null) {
  46. // Apply the new result to the state and the new events to the queue.
  47. {{state_name|react_setter}}(state => ({
  48. ...{{const.result}}.{{const.state}},
  49. events: [...state.{{const.events}}, ...{{const.result}}.{{const.events}}],
  50. }))
  51. // Reset the result.
  52. {{const.result|react_setter}}(result => ({
  53. {{const.state}}: null,
  54. {{const.events}}: [],
  55. {{const.final}}: true,
  56. {{const.processing}}: !{{const.result}}.{{const.final}},
  57. }))
  58. // Process the next event.
  59. processEvent({{state_name}}, {{state_name|react_setter}}, {{const.result}}, {{const.result|react_setter}}, {{const.router}}, {{const.socket}}.current)
  60. }
  61. })
  62. // Set focus to the specified element.
  63. useEffect(() => {
  64. if (focusRef.current) {
  65. focusRef.current.focus();
  66. }
  67. })
  68. // Route after the initial page hydration.
  69. useEffect(() => {
  70. const change_complete = () => Event([E('{{state_name}}.{{const.hydrate}}', {})])
  71. {{const.router}}.events.on('routeChangeComplete', change_complete)
  72. return () => {
  73. {{const.router}}.events.off('routeChangeComplete', change_complete)
  74. }
  75. }, [{{const.router}}])
  76. {% for hook in hooks %}
  77. {{ hook }}
  78. {% endfor %}
  79. return (
  80. <Fragment>
  81. {%- if err_comp -%}
  82. {{ utils.render(err_comp, indent_width=1) }}
  83. {%- endif -%}
  84. {{utils.render(render, indent_width=0)}}
  85. </Fragment>
  86. )
  87. }
  88. {% endblock %}