-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Kevin Ingersoll <[email protected]>
- Loading branch information
Showing
11 changed files
with
430 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@latticexyz/explorer": patch | ||
--- | ||
|
||
Explore page now has a full-featured SQL editor with syntax highlighting, autocomplete, and query validation. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 63 additions & 25 deletions
88
packages/explorer/src/app/(explorer)/[chainName]/worlds/[worldAddress]/explore/SQLEditor.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
packages/explorer/src/app/(explorer)/[chainName]/worlds/[worldAddress]/explore/consts.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { editor } from "monaco-editor/esm/vs/editor/editor.api"; | ||
|
||
export const monacoOptions: editor.IStandaloneEditorConstructionOptions = { | ||
fontSize: 14, | ||
fontWeight: "normal", | ||
wordWrap: "off", | ||
lineNumbers: "off", | ||
lineNumbersMinChars: 0, | ||
overviewRulerLanes: 0, | ||
overviewRulerBorder: false, | ||
hideCursorInOverviewRuler: true, | ||
lineDecorationsWidth: 0, | ||
glyphMargin: false, | ||
folding: false, | ||
scrollBeyondLastColumn: 0, | ||
scrollbar: { | ||
horizontal: "hidden", | ||
vertical: "hidden", | ||
alwaysConsumeMouseWheel: false, | ||
handleMouseWheel: false, | ||
}, | ||
find: { | ||
addExtraSpaceOnTop: false, | ||
autoFindInSelection: "never", | ||
seedSearchStringFromSelection: "never", | ||
}, | ||
minimap: { enabled: false }, | ||
wordBasedSuggestions: "off", | ||
links: false, | ||
occurrencesHighlight: "off", | ||
cursorStyle: "line-thin", | ||
renderLineHighlight: "none", | ||
contextmenu: false, | ||
roundedSelection: false, | ||
hover: { | ||
delay: 100, | ||
}, | ||
acceptSuggestionOnEnter: "on", | ||
automaticLayout: true, | ||
fixedOverflowWidgets: true, | ||
}; |
23 changes: 23 additions & 0 deletions
23
...orer/src/app/(explorer)/[chainName]/worlds/[worldAddress]/explore/useMonacoErrorMarker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { useCallback } from "react"; | ||
import { useMonaco } from "@monaco-editor/react"; | ||
|
||
export function useMonacoErrorMarker() { | ||
const monaco = useMonaco(); | ||
return useCallback( | ||
({ message, startColumn, endColumn }: { message: string; startColumn: number; endColumn: number }) => { | ||
if (monaco) { | ||
monaco.editor.setModelMarkers(monaco.editor.getModels()[0], "sql", [ | ||
{ | ||
severity: monaco.MarkerSeverity.Error, | ||
message, | ||
startLineNumber: 1, | ||
startColumn, | ||
endLineNumber: 1, | ||
endColumn, | ||
}, | ||
]); | ||
} | ||
}, | ||
[monaco], | ||
); | ||
} |
60 changes: 60 additions & 0 deletions
60
...orer/src/app/(explorer)/[chainName]/worlds/[worldAddress]/explore/useMonacoSuggestions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { useEffect } from "react"; | ||
import { Table } from "@latticexyz/config"; | ||
import { useMonaco } from "@monaco-editor/react"; | ||
import { useQueryAutocomplete } from "./useQueryAutocomplete"; | ||
|
||
const monacoSuggestionsMap = { | ||
KEYWORD: "Keyword", | ||
TABLE: "Field", | ||
COLUMN: "Field", | ||
} as const; | ||
|
||
export function useMonacoSuggestions(table?: Table) { | ||
const monaco = useMonaco(); | ||
const queryAutocomplete = useQueryAutocomplete(table); | ||
|
||
useEffect(() => { | ||
if (!monaco) return; | ||
|
||
const provider = monaco.languages.registerCompletionItemProvider("sql", { | ||
triggerCharacters: [" ", ".", ","], | ||
|
||
provideCompletionItems: (model, position) => { | ||
if (!queryAutocomplete) { | ||
return { suggestions: [] }; | ||
} | ||
|
||
const textUntilPosition = model.getValueInRange({ | ||
startLineNumber: 1, | ||
startColumn: 1, | ||
endLineNumber: position.lineNumber, | ||
endColumn: position.column, | ||
}); | ||
|
||
const word = model.getWordUntilPosition(position); | ||
const range = { | ||
startLineNumber: position.lineNumber, | ||
startColumn: word.startColumn, | ||
endLineNumber: position.lineNumber, | ||
endColumn: word.endColumn, | ||
}; | ||
|
||
const suggestions = queryAutocomplete | ||
.autocomplete(textUntilPosition) | ||
.map(({ value, optionType }) => ({ | ||
label: value, | ||
kind: monaco.languages.CompletionItemKind[monacoSuggestionsMap[optionType]], | ||
insertText: value, | ||
range, | ||
// move keyword optionType to the top of suggestions list | ||
sortText: optionType !== "KEYWORD" ? "0" : "1", | ||
})) | ||
.filter(({ label }) => !!label); | ||
|
||
return { suggestions }; | ||
}, | ||
}); | ||
|
||
return () => provider.dispose(); | ||
}, [monaco, queryAutocomplete]); | ||
} |
21 changes: 21 additions & 0 deletions
21
...orer/src/app/(explorer)/[chainName]/worlds/[worldAddress]/explore/useQueryAutocomplete.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { useParams } from "next/navigation"; | ||
import { SQLAutocomplete, SQLDialect } from "sql-autocomplete"; | ||
import { Address } from "viem"; | ||
import { useMemo } from "react"; | ||
import { Table } from "@latticexyz/config"; | ||
import { useChain } from "../../../../hooks/useChain"; | ||
import { constructTableName } from "../../../../utils/constructTableName"; | ||
|
||
export function useQueryAutocomplete(table?: Table) { | ||
const { id: chainId } = useChain(); | ||
const { worldAddress } = useParams<{ worldAddress: Address }>(); | ||
|
||
return useMemo(() => { | ||
if (!table || !worldAddress || !chainId) return null; | ||
|
||
const tableName = constructTableName(table, worldAddress as Address, chainId); | ||
const columnNames = Object.keys(table.schema); | ||
|
||
return new SQLAutocomplete(SQLDialect.PLpgSQL, [tableName], columnNames); | ||
}, [table, worldAddress, chainId]); | ||
} |
72 changes: 72 additions & 0 deletions
72
...xplorer/src/app/(explorer)/[chainName]/worlds/[worldAddress]/explore/useQueryValidator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { useParams } from "next/navigation"; | ||
import { Parser } from "node-sql-parser"; | ||
import { Address } from "viem"; | ||
import { useCallback } from "react"; | ||
import { Table } from "@latticexyz/config"; | ||
import { useMonaco } from "@monaco-editor/react"; | ||
import { useChain } from "../../../../hooks/useChain"; | ||
import { constructTableName } from "../../../../utils/constructTableName"; | ||
import { useMonacoErrorMarker } from "./useMonacoErrorMarker"; | ||
|
||
const sqlParser = new Parser(); | ||
|
||
export function useQueryValidator(table?: Table) { | ||
const monaco = useMonaco(); | ||
const { worldAddress } = useParams(); | ||
const { id: chainId } = useChain(); | ||
const setErrorMarker = useMonacoErrorMarker(); | ||
|
||
return useCallback( | ||
(value: string) => { | ||
if (!monaco || !table) return true; | ||
|
||
try { | ||
const ast = sqlParser.astify(value); | ||
if ("columns" in ast && Array.isArray(ast.columns)) { | ||
for (const column of ast.columns) { | ||
const columnName = column.expr.column; | ||
if (!Object.keys(table.schema).includes(columnName)) { | ||
setErrorMarker({ | ||
message: `Column '${columnName}' does not exist in the table schema.`, | ||
startColumn: value.indexOf(columnName) + 1, | ||
endColumn: value.indexOf(columnName) + columnName.length + 1, | ||
}); | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
if ("from" in ast && Array.isArray(ast.from)) { | ||
for (const tableInfo of ast.from) { | ||
if ("table" in tableInfo) { | ||
const selectedTableName = tableInfo.table; | ||
const tableName = constructTableName(table, worldAddress as Address, chainId); | ||
|
||
if (selectedTableName !== tableName) { | ||
setErrorMarker({ | ||
message: `Only '${tableName}' is available for this query.`, | ||
startColumn: value.indexOf(selectedTableName) + 1, | ||
endColumn: value.indexOf(selectedTableName) + selectedTableName.length + 1, | ||
}); | ||
return false; | ||
} | ||
} | ||
} | ||
} | ||
|
||
monaco.editor.setModelMarkers(monaco.editor.getModels()[0], "sql", []); | ||
return true; | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
setErrorMarker({ | ||
message: error.message, | ||
startColumn: 1, | ||
endColumn: value.length + 1, | ||
}); | ||
} | ||
return false; | ||
} | ||
}, | ||
[monaco, table, setErrorMarker, worldAddress, chainId], | ||
); | ||
} |
Oops, something went wrong.