Skip to content

Commit 9186418

Browse files
committed
Add structure from local-subgraphs
1 parent b12307a commit 9186418

File tree

5 files changed

+105
-185
lines changed

5 files changed

+105
-185
lines changed

components/SubgraphEditor/Editor.tsx

Lines changed: 49 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
11
import React, { useState, useEffect, useRef } from 'react'
22
import { ViewPort, Top, Fill, Bottom, BottomResizable, Right } from 'react-spaces'
33
import { useRouter } from 'next/router'
4-
import { LOG_LEVEL } from '@cryptostats/sdk'
54
import { useWeb3React } from '@web3-react/core'
65
import styled from 'styled-components'
76
import { useENSName } from 'use-ens-name'
87
import CodeEditor from 'components/CodeEditor'
98
import 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'
1410
import PrimaryFooter from './PrimaryFooter'
1511
import { Tabs, TabState } from './Tabs'
1612
import EditorModal from './EditorModal'
1713
import NewAdapterForm from './NewAdapterForm'
1814
import { MarkerSeverity } from './types'
1915
import ErrorPanel from './ErrorPanel'
2016
import { usePlausible } from 'next-plausible'
21-
import { useEditorState } from '../../hooks/editor-state'
2217
import EditorControls from './EditorControls'
2318
import Console from './Console'
2419
import BottomTitleBar, { BottomView } from './BottomTitleBar'
2520
import SaveMessage from './SaveMessage'
2621
import ImageLibrary from './ImageLibrary/ImageLibrary'
22+
import { useEditorState } from 'hooks/editor-state'
2723

2824
const 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

142131
const 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)

components/SubgraphEditor/Tabs.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React from 'react'
22
import styled from 'styled-components'
33
import CloseIcon from 'components/CloseIcon'
4-
import { FileData } from 'hooks/local-subgraphs'
54

65
const TabRow = styled.div`
76
display: flex;
@@ -42,6 +41,7 @@ const CloseButton = styled.button`
4241

4342
export interface TabState {
4443
type: 'schema' | 'mapping'
44+
name?: string
4545
fileId?: string | null
4646
open: boolean
4747
focused: boolean
@@ -51,15 +51,15 @@ interface TabsProps {
5151
current?: string | null
5252
onClose?: (fileId?: string) => void
5353
onSelect: (fileId?: string) => void
54-
openTabs: (TabState & { fileData?: FileData })[]
54+
openTabs: TabState[]
5555
}
5656

5757
export const Tabs = ({ onClose, openTabs, onSelect }: TabsProps) => {
5858
return (
5959
<TabRow>
6060
{openTabs.map(ot => (
6161
<Tab key={ot.fileId} $focused={ot.focused} onClick={() => onSelect(ot.fileId!)}>
62-
<div>{ot.fileData?.name || ''}</div>
62+
<div>{ot.name || ot.fileId || 'File'}</div>
6363
{ot.focused && onClose && (
6464
<CloseButton onClick={() => onClose(ot.fileId!)}>
6565
<CloseIcon />

hooks/gql-compiler.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export const useGqlCompiler = () => {
5858
const module = list.addAdaptersWithCode(compiledCode || code)
5959

6060
setState({ ...DEFAULT_STATE, code, module, compiledCode, list })
61-
} catch (e) {
61+
} catch (e: any) {
6262
console.warn(e)
6363
setState({ ...DEFAULT_STATE, error: e.message })
6464
}

0 commit comments

Comments
 (0)