1
- import React , { useEffect , useState } from "react" ;
1
+ import React , { useCallback , useEffect , useRef , useState } from "react" ;
2
2
import styled from "@emotion/styled" ;
3
3
import { useRouter } from "next/router" ;
4
4
import { DefaultEditorWorkspaceLayout } from "layouts/default-editor-workspace-layout" ;
@@ -67,6 +67,10 @@ export function Editor() {
67
67
const targetted =
68
68
find_node_by_id_under_entry ( targetId , root ?. entry ) ?? root ?. entry ;
69
69
70
+ const targetStateRef = useRef ( ) ;
71
+ //@ts -ignore
72
+ targetStateRef . current = targetted ;
73
+
70
74
useEffect ( ( ) => {
71
75
// ------------------------------------------------------------
72
76
// other platforms are not supported yet
@@ -88,6 +92,20 @@ export function Editor() {
88
92
// ------------------------------------------------------------
89
93
} , [ state . design ?. key , fat . accessToken ] ) ;
90
94
95
+ const on_result = ( result : Result ) => {
96
+ //@ts -ignore
97
+ if ( result . id == targetStateRef ?. current ?. id ) {
98
+ setResult ( result ) ;
99
+ }
100
+ } ;
101
+
102
+ const on_preview_result = ( result : Result ) => {
103
+ //@ts -ignore
104
+ if ( result . id == targetStateRef ?. current ?. id ) {
105
+ setPreview ( result ) ;
106
+ }
107
+ } ;
108
+
91
109
useEffect ( ( ) => {
92
110
const __target = targetted ;
93
111
if ( __target && framework_config ) {
@@ -102,12 +120,6 @@ export function Editor() {
102
120
disable_components : ! enable_components ,
103
121
} ;
104
122
105
- const on_result = ( result : Result ) => {
106
- if ( result . id == targetted . id ) {
107
- setResult ( result ) ;
108
- }
109
- } ;
110
-
111
123
// build code without assets fetch
112
124
designToCode ( {
113
125
input : _input ,
@@ -140,12 +152,6 @@ export function Editor() {
140
152
disable_components : true ,
141
153
} ;
142
154
143
- const on_preview_result = ( result : Result ) => {
144
- if ( result . id == targetId ) {
145
- setPreview ( result ) ;
146
- }
147
- } ;
148
-
149
155
// ----- for preview -----
150
156
designToCode ( {
151
157
input : _input ,
0 commit comments