-
Notifications
You must be signed in to change notification settings - Fork 569
/
Copy pathuseJsonTree.tsx
136 lines (116 loc) · 3.43 KB
/
useJsonTree.tsx
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { useJson } from "./useJson";
import { inferType, JSONValueType } from "@jsonhero/json-infer-types";
import { JSONHeroPath } from "@jsonhero/path";
import { IconComponent } from "~/useColumnView";
import { formatValue } from "~/utilities/formatter";
import { iconForType } from "~/utilities/icons";
import {
createContext,
ReactNode,
useCallback,
useContext,
useMemo,
useRef,
} from "react";
import { useVirtualTree, UseVirtualTreeInstance } from "./useVirtualTree";
import invariant from "tiny-invariant";
import { useJsonDoc } from "./useJsonDoc";
const initialRect = { width: 800, height: 600 };
export type JsonTreeOptions = {
overscan?: number;
};
export type UseJsonTreeInstance = {
tree: UseVirtualTreeInstance<JsonTreeViewNode>;
parentRef: React.RefObject<HTMLDivElement>;
};
export type JsonTreeViewType = UseJsonTreeInstance;
const JsonTreeViewContext = createContext<JsonTreeViewType>(
{} as JsonTreeViewType
);
export function JsonTreeViewProvider({
children,
...options
}: { children: ReactNode } & JsonTreeOptions) {
const instance = useJsonTree(options);
return (
<JsonTreeViewContext.Provider value={instance}>
{children}
</JsonTreeViewContext.Provider>
);
}
export function useJsonTree(options: JsonTreeOptions): UseJsonTreeInstance {
const parentRef = useRef<HTMLDivElement>(null);
const { doc } = useJsonDoc();
const [json] = useJson();
const jsonNodes = useMemo(() => {
return generateTreeViewNodes(json);
}, [json]);
const tree = useVirtualTree({
id: doc.id,
nodes: jsonNodes,
count: 0,
getScrollElement: () => parentRef.current,
estimateSize: useCallback((index) => 32, []),
initialRect,
overscan: options.overscan,
persistState: true,
});
return { tree, parentRef };
}
export function useJsonTreeViewContext(): JsonTreeViewType {
const context = useContext(JsonTreeViewContext);
invariant(
context,
"useJsonTreeViewContext must be used within a JsonTreeViewContext.Provider"
);
return context;
}
export type JsonTreeViewNode = {
id: string;
name: string;
title: string;
subtitle?: string;
longTitle?: string;
icon?: IconComponent;
children?: Array<JsonTreeViewNode>;
};
export function generateTreeViewNodes(json: unknown): Array<JsonTreeViewNode> {
const info = inferType(json);
const path = new JSONHeroPath("$");
return generateChildren(info, path) ?? [];
}
function generateChildren(
info: JSONValueType,
path: JSONHeroPath
): Array<JsonTreeViewNode> | undefined {
if (info.name === "array") {
return info.value.map((item, index) => {
const itemInfo = inferType(item);
const itemPath = path.child(index.toString());
return {
id: itemPath.toString(),
name: index.toString(),
title: index.toString(),
longTitle: `Index ${index.toString()}`,
subtitle: formatValue(itemInfo),
icon: iconForType(itemInfo),
children: generateChildren(itemInfo, itemPath),
};
});
}
if (info.name === "object") {
return Object.entries(info.value).map(([key, value]) => {
const cleanKey = key.replace(/\./g, "\\.");
const itemInfo = inferType(value);
const itemPath = path.child(cleanKey);
return {
id: itemPath.toString(),
name: key,
title: key,
subtitle: formatValue(itemInfo),
icon: iconForType(itemInfo),
children: generateChildren(itemInfo, itemPath),
};
});
}
}