11import React , { useState , useEffect , useRef } from 'react'
22import { ViewPort , Top , Fill , Bottom , BottomResizable , Right } from 'react-spaces'
33import { useRouter } from 'next/router'
4- import { LOG_LEVEL } from '@cryptostats/sdk'
54import { useWeb3React } from '@web3-react/core'
65import styled from 'styled-components'
76import { useENSName } from 'use-ens-name'
87import CodeEditor from 'components/CodeEditor'
98import ConnectionButton from 'components/ConnectionButton'
10- import { useSubgraph , newModule , getStorageItem , FileData } from 'hooks/local-subgraphs'
11- import { useCompiler } from 'hooks/compiler'
12- import { useConsole } from 'hooks/console'
13- import { emptyMapping , emptySchema } from 'resources/templates'
9+ import { useLocalSubgraph , newSubgraph , DEFAULT_MAPPING } from 'hooks/local-subgraphs'
1410import PrimaryFooter from './PrimaryFooter'
1511import { Tabs , TabState } from './Tabs'
1612import EditorModal from './EditorModal'
1713import NewAdapterForm from './NewAdapterForm'
1814import { MarkerSeverity } from './types'
1915import ErrorPanel from './ErrorPanel'
2016import { usePlausible } from 'next-plausible'
21- import { useEditorState } from '../../hooks/editor-state'
2217import EditorControls from './EditorControls'
2318import Console from './Console'
2419import BottomTitleBar , { BottomView } from './BottomTitleBar'
2520import SaveMessage from './SaveMessage'
2621import ImageLibrary from './ImageLibrary/ImageLibrary'
22+ import { useEditorState } from 'hooks/editor-state'
2723
2824const Header = styled ( Top ) `
2925 background-image: url('/editor_logo.png');
@@ -130,49 +126,38 @@ const PrimaryFill = styled(FillWithStyledResize)`
130126 }
131127`
132128
133- const formatLog = ( val : any ) => {
134- if ( val . toString ( ) === '[object Object]' ) {
135- return JSON . stringify ( val , null , 2 )
136- } else if ( typeof val === 'string' ) {
137- return `"${ val } "`
138- }
139- return val . toString ( )
140- }
129+ const SCHEMA_FILE_NAME = 'schema.graphql'
141130
142131const Editor : React . FC = ( ) => {
143132 const router = useRouter ( )
144133 const plausible = usePlausible ( )
145- const [ subgraphFiles , setSubgraphFiles ] = useEditorState < TabState [ ] | [ ] > (
146- 'test' ,
147- [
148- { type : 'schema' , fileId : null , open : true , focused : true } ,
149- { type : 'mapping' , fileId : null , open : true , focused : false } ,
150- ] ,
151- 'subgraph-editor-state'
152- )
153- const { save } = useSubgraph ( )
154-
155- const openTabs = ( subgraphFiles || [ ] )
156- . filter ( sgf => sgf . open )
157- . map ( ot => ( { ...ot , ...( ot . fileId && { fileData : getStorageItem ( ot . fileId ) as FileData } ) } ) )
158- const focusedTab = openTabs ?. find ( sgf => sgf . focused ) !
159-
160- useEffect ( ( ) => {
161- setSubgraphFiles ( ( prev : TabState [ ] ) =>
162- prev . map ( pi =>
163- pi . open && ! pi . fileId
164- ? {
165- ...pi ,
166- fileId : newModule (
167- pi ?. type === 'schema'
168- ? { code : emptySchema , name : 'New Schema' }
169- : { code : emptyMapping , name : 'New Mapping' }
170- ) ,
171- }
172- : pi
173- )
174- )
175- } , [ ] )
134+ const [ file , setFile ] = useEditorState < string | null > ( 'subgraph-file' )
135+ const [ tab , setTab ] = useState ( SCHEMA_FILE_NAME )
136+
137+ const { saveSchema, saveMapping, subgraph } = useLocalSubgraph ( file )
138+
139+ const subgraphFiles : ( TabState & { value : string } ) [ ] = subgraph
140+ ? [
141+ {
142+ type : 'schema' ,
143+ name : 'Schema' ,
144+ fileId : SCHEMA_FILE_NAME ,
145+ open : true ,
146+ focused : tab === SCHEMA_FILE_NAME ,
147+ value : subgraph . schema ,
148+ } ,
149+ {
150+ type : 'mapping' ,
151+ name : 'Mapping' ,
152+ fileId : DEFAULT_MAPPING ,
153+ open : true ,
154+ focused : tab !== SCHEMA_FILE_NAME ,
155+ value : subgraph . mappings [ DEFAULT_MAPPING ] ,
156+ } ,
157+ ]
158+ : [ ]
159+
160+ const focusedTab = subgraphFiles . find ( sgf => sgf . focused ) !
176161
177162 const [ started , setStarted ] = useState ( false )
178163 const [ newAdapterModalOpen , setNewAdapterModalOpen ] = useState ( false )
@@ -181,8 +166,6 @@ const Editor: React.FC = () => {
181166 const [ bottomView , setBottomView ] = useState ( BottomView . NONE )
182167 const editorRef = useRef < any > ( null )
183168
184- const { evaluate } = useCompiler ( )
185- const { addLine } = useConsole ( )
186169 const { account } = useWeb3React ( )
187170 const name = useENSName ( account )
188171
@@ -207,9 +190,6 @@ const Editor: React.FC = () => {
207190 return null
208191 }
209192
210- const saveCode = ( code : string ) =>
211- save ( focusedTab . fileId ! , code , focusedTab . fileData ! . name , focusedTab . fileData ! . version )
212-
213193 return (
214194 < ViewPort style = { { background : '#0f1011' } } >
215195 < Header size = { 64 } order = { 1 } >
@@ -231,13 +211,9 @@ const Editor: React.FC = () => {
231211 < TabContainer size = { 50 } >
232212 < Fill >
233213 < Tabs
234- openTabs = { openTabs }
235- current = { focusedTab . fileData ?. name }
236- onSelect = { fileId =>
237- setSubgraphFiles ( prev =>
238- prev . map ( p => ( { ...p , focused : p . fileId === fileId } ) )
239- )
240- }
214+ openTabs = { subgraphFiles }
215+ current = { tab }
216+ onSelect = { fileId => setTab ( fileId || SCHEMA_FILE_NAME ) }
241217 />
242218 </ Fill >
243219 < Right size = { 100 } >
@@ -246,36 +222,30 @@ const Editor: React.FC = () => {
246222 </ TabContainer >
247223
248224 < Fill >
249- { focusedTab ?. fileData ? (
225+ { subgraph ? (
250226 < CodeEditor
251227 defaultLanguage = { focusedTab . type === 'schema' ? 'graphql' : 'typescript' }
252- fileId = { focusedTab . fileId ! }
253- defaultValue = { focusedTab ?. fileData ?. code }
228+ fileId = { tab }
229+ defaultValue = { subgraph . schema }
254230 onMount = { ( editor : any ) => {
255231 editorRef . current = editor
256232 } }
257- onChange = { saveCode }
258- onValidated = { ( code : string , markers : any [ ] ) => {
233+ onChange = { ( code : string ) =>
234+ tab === SCHEMA_FILE_NAME ? saveSchema ( code ) : saveMapping ( tab , code )
235+ }
236+ onValidated = { ( _code : string , markers : any [ ] ) => {
259237 setMarkers ( markers )
260238
261239 if (
262240 markers . filter ( ( marker : any ) => marker . severity === MarkerSeverity . Error )
263241 . length === 0
264242 ) {
265- evaluate ( {
266- code,
267- isTS : true ,
268- onLog : ( level : LOG_LEVEL , ...args : any [ ] ) =>
269- addLine ( {
270- level : level . toString ( ) ,
271- value : args . map ( formatLog ) . join ( ' ' ) ,
272- } ) ,
273- } )
243+ // Evaluate code
274244 }
275245 } }
276246 />
277247 ) : (
278- < div > test </ div >
248+ < div style = { { color : 'white' } } > Empty state </ div >
279249 ) }
280250 </ Fill >
281251
@@ -304,9 +274,9 @@ const Editor: React.FC = () => {
304274 </ FillWithStyledResize >
305275
306276 < PrimaryFooterContainer size = { 55 } >
307- { focusedTab ?. fileData ? (
277+ { subgraph ? (
308278 < PrimaryFooter
309- fileName = { focusedTab ?. fileData . name }
279+ fileName = { tab }
310280 markers = { markers }
311281 onMarkerClick = { ( ) => setBottomView ( BottomView . ERRORS ) }
312282 onConsoleClick = { ( ) => setBottomView ( BottomView . CONSOLE ) }
@@ -333,19 +303,20 @@ const Editor: React.FC = () => {
333303 onClick : ( ) => setNewAdapterModalOpen ( false ) ,
334304 } ,
335305 {
336- label : 'Create Blank Adapter ' ,
306+ label : 'Create Blank Subgraph ' ,
337307 onClick : ( ) => {
338- plausible ( 'new-adapter ' , {
308+ plausible ( 'new-subgraph ' , {
339309 props : {
340310 template : 'blank' ,
341311 } ,
342312 } )
343313
344- // setMappingFileName(newModule(emptyMapping ))
314+ setFile ( newSubgraph ( ) )
345315 setNewAdapterModalOpen ( false )
346316 } ,
347317 } ,
348- ] } >
318+ ] }
319+ >
349320 < NewAdapterForm
350321 onAdapterSelection = { ( _fileName : string ) => {
351322 // setMappingFileName(fileName)
0 commit comments