@@ -22,61 +22,37 @@ import { useGist } from "./features/editor/hooks/useGist";
22
22
import { useSearchParams } from "react-router-dom" ;
23
23
import Copyable from "./components/Copyable" ;
24
24
import useTheme from "./context/theme" ;
25
+ import { AIGenerationDialog } from "./features/ai/components/AIGenerationDialog" ;
26
+ import { AI_FEATURES_ENABLED } from "./constants" ;
25
27
26
28
const DRAWER_WIDTH = "40vw" ;
27
29
28
30
function App ( ) {
29
- // The current sway code in the editor.
30
31
const [ swayCode , setSwayCode ] = useState < string > ( loadSwayCode ( ) ) ;
31
-
32
- // The current solidity code in the editor.
33
32
const [ solidityCode , setSolidityCode ] = useState < string > ( loadSolidityCode ( ) ) ;
34
-
35
- // An error message to display to the user.
36
33
const [ showSolidity , setShowSolidity ] = useState ( false ) ;
37
-
38
- // The most recent code that the user has requested to compile.
39
34
const [ codeToCompile , setCodeToCompile ] = useState < string | undefined > (
40
35
undefined ,
41
36
) ;
42
-
43
- // The most recent code that the user has requested to transpile.
44
37
const [ codeToTranspile , setCodeToTranspile ] = useState < string | undefined > (
45
38
undefined ,
46
39
) ;
47
-
48
- // Whether or not the current code in the editor has been compiled.
49
40
const [ isCompiled , setIsCompiled ] = useState ( false ) ;
50
-
51
- // The toolchain to use for compilation.
52
41
const [ toolchain , setToolchain ] = useState < Toolchain > ( "testnet" ) ;
53
-
54
- // The deployment state
55
42
const [ deployState , setDeployState ] = useState ( DeployState . NOT_DEPLOYED ) ;
56
-
57
- // Functions for reading and writing to the log output.
58
43
const [ log , updateLog ] = useLog ( ) ;
59
-
60
- // The contract ID of the deployed contract.
61
44
const [ contractId , setContractId ] = useState ( "" ) ;
62
-
63
- // An error message to display to the user.
64
45
const [ drawerOpen , setDrawerOpen ] = useState ( false ) ;
65
-
66
- // The query parameters for the current URL.
67
46
const [ searchParams ] = useSearchParams ( ) ;
68
-
69
- // The theme color for the app.
70
47
const { themeColor } = useTheme ( ) ;
48
+ const [ aiDialogOpen , setAiDialogOpen ] = useState ( false ) ;
71
49
72
- // If showSolidity is toggled on, reset the compiled state.
73
50
useEffect ( ( ) => {
74
51
if ( showSolidity ) {
75
52
setIsCompiled ( false ) ;
76
53
}
77
54
} , [ showSolidity ] ) ;
78
55
79
- // Load the query parameters from the URL and set the state accordingly. Gists are loaded in useGist.
80
56
useEffect ( ( ) => {
81
57
if ( searchParams . get ( "transpile" ) === "true" ) {
82
58
setShowSolidity ( true ) ;
@@ -106,7 +82,6 @@ function App() {
106
82
[ setSolidityCode ] ,
107
83
) ;
108
84
109
- // Loading shared code by query parameter and get a function for creating sharable permalinks.
110
85
const { newGist } = useGist ( onSwayCodeChange , onSolidityCodeChange ) ;
111
86
112
87
const setError = useCallback (
@@ -144,7 +119,6 @@ function App() {
144
119
const onCompileClick = useCallback ( ( ) => {
145
120
track ( "Compile Click" , { toolchain } ) ;
146
121
if ( showSolidity ) {
147
- // Transpile the Solidity code before compiling.
148
122
track ( "Transpile" ) ;
149
123
setCodeToTranspile ( solidityCode ) ;
150
124
} else {
@@ -159,14 +133,30 @@ function App() {
159
133
toolchain ,
160
134
] ) ;
161
135
136
+ const onAIAssistClick = useCallback ( ( ) => {
137
+ track ( "AI Assist Click" ) ;
138
+ setAiDialogOpen ( true ) ;
139
+ } , [ ] ) ;
140
+
141
+ const onAICodeGenerated = useCallback ( ( code : string ) => {
142
+ track ( "AI Code Generated" ) ;
143
+ onSwayCodeChange ( code ) ;
144
+ setAiDialogOpen ( false ) ;
145
+ } , [ onSwayCodeChange ] ) ;
146
+
147
+ const onAICodeFixed = useCallback ( ( fixedCode : string ) => {
148
+ track ( "AI Code Fixed" ) ;
149
+ onSwayCodeChange ( fixedCode ) ;
150
+ } , [ onSwayCodeChange ] ) ;
151
+
162
152
useTranspile (
163
153
codeToTranspile ,
164
154
setCodeToCompile ,
165
155
onSwayCodeChange ,
166
156
setError ,
167
157
updateLog ,
168
158
) ;
169
- useCompile ( codeToCompile , setError , setIsCompiled , updateLog , toolchain ) ;
159
+ useCompile ( codeToCompile , setError , setIsCompiled , updateLog , toolchain , onAICodeFixed ) ;
170
160
171
161
return (
172
162
< div
@@ -188,6 +178,7 @@ function App() {
188
178
showSolidity = { showSolidity }
189
179
setShowSolidity = { setShowSolidity }
190
180
updateLog = { updateLog }
181
+ onAIAssistClick = { AI_FEATURES_ENABLED ? onAIAssistClick : undefined }
191
182
/>
192
183
< div
193
184
style = { {
@@ -215,6 +206,13 @@ function App() {
215
206
contractId = { contractId }
216
207
updateLog = { updateLog }
217
208
/>
209
+ { AI_FEATURES_ENABLED && (
210
+ < AIGenerationDialog
211
+ open = { aiDialogOpen }
212
+ onClose = { ( ) => setAiDialogOpen ( false ) }
213
+ onCodeGenerated = { onAICodeGenerated }
214
+ />
215
+ ) }
218
216
< Analytics />
219
217
</ div >
220
218
) ;
0 commit comments