forked from eslint/code-explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson-ast.tsx
More file actions
44 lines (39 loc) · 1.13 KB
/
json-ast.tsx
File metadata and controls
44 lines (39 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { Accordion } from "@/components/ui/accordion";
import { Editor } from "@/components/editor";
import { useAST } from "@/hooks/use-ast";
import { useExplorer } from "@/hooks/use-explorer";
import {
JsonAstTreeItem,
type JsonAstTreeItemProperties,
} from "./json-ast-tree-item";
import type { FC } from "react";
import { parseError } from "@/lib/parse-error";
import { ErrorState } from "../error-boundary";
export const JsonAst: FC = () => {
const result = useAST();
const { viewModes } = useExplorer();
const { astView } = viewModes;
if (!result.ok) {
const message = parseError(result.errors[0]);
return <ErrorState message={message} />;
}
const ast = JSON.stringify(result.ast, null, 2);
if (astView === "tree") {
return (
<Accordion
type="multiple"
className="px-8 pb-4 font-mono space-y-3"
defaultValue={["0-Document"]}
>
<JsonAstTreeItem
data={result.ast as JsonAstTreeItemProperties["data"]}
index={0}
esqueryMatchedNodes={
result.esqueryMatchedNodes as JsonAstTreeItemProperties["esqueryMatchedNodes"]
}
/>
</Accordion>
);
}
return <Editor readOnly value={ast} />;
};