-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathuseJYCM.tsx
112 lines (98 loc) · 3.07 KB
/
useJYCM.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
import { EVENT_PAIR } from "@@/common";
import { IMonacoJsonHighlighter } from "@@/components/monaco-json-highlighter";
import { IDiffDetailItem } from "@@/typings";
import { jsonPathToPathKey } from "@@/utils";
import { useEffect, useRef, useState } from "react";
import useDecorations from "./useDecorations";
import useJsonInfo from "./useJsonInfo";
export type IUseJYCMProps = {
leftJsonStr: string;
rightJsonStr: string;
diffResult: any;
};
export const useJYCM = ({ leftJsonStr, rightJsonStr, diffResult }: IUseJYCMProps) => {
const [jsonPathKeyPairs, setJsonPathKeyPairs] = useState<{
left: { [_: string]: string };
right: { [_: string]: string };
}>({ left: {}, right: {} });
const [diffDetailDict, setDiffDetailDict] = useState<{
[_: string]: IDiffDetailItem;
}>({});
useEffect(() => {
if (diffResult) {
const _diffDetailDict = diffResult;
setDiffDetailDict(_diffDetailDict);
if (_diffDetailDict[EVENT_PAIR]) {
const _jsonPathKeyPair: any = { left: {}, right: {} };
_diffDetailDict[EVENT_PAIR].forEach((p: any) => {
_jsonPathKeyPair.left[p.left_path as string] = p.right_path;
_jsonPathKeyPair.right[p.right_path as string] = p.left_path;
});
setJsonPathKeyPairs(_jsonPathKeyPair);
}
}
}, [diffResult]);
const [activeLeftJsonPath, setActiveLeftJsonPath] = useState<any[]>([]);
const [activeRightJsonPath, setActiveRightJsonPath] = useState<any[]>([]);
const { jsonRows: leftJsonRows, pathKey2Index: leftPathKey2Index } =
useJsonInfo(leftJsonStr);
const { jsonRows: rightJsonRows, pathKey2Index: rightPathKey2Index } =
useJsonInfo(rightJsonStr);
const {
decorations: leftDecorations,
diffDetailDict: leftJsonPath2DiffDetail,
} = useDecorations(
diffDetailDict,
leftPathKey2Index,
"left",
leftJsonRows,
activeLeftJsonPath
);
const {
decorations: rightDecorations,
diffDetailDict: rightJsonPath2DiffDetail,
} = useDecorations(
diffDetailDict,
rightPathKey2Index,
"right",
rightJsonRows,
activeRightJsonPath
);
const leftEditorRef = useRef<IMonacoJsonHighlighter>(null);
const rightEditorRef = useRef<IMonacoJsonHighlighter>(null);
const [pairInfo, setPairInfo] = useState({});
useEffect(() => {
setPairInfo({
...leftJsonPath2DiffDetail[jsonPathToPathKey(activeLeftJsonPath)],
...rightJsonPath2DiffDetail[jsonPathToPathKey(activeRightJsonPath)],
});
}, [
activeLeftJsonPath,
activeRightJsonPath,
leftJsonPath2DiffDetail,
rightJsonPath2DiffDetail,
]);
return {
jsonPathKeyPairs,
setJsonPathKeyPairs,
diffDetailDict,
setDiffDetailDict,
activeLeftJsonPath,
setActiveLeftJsonPath,
activeRightJsonPath,
setActiveRightJsonPath,
leftJsonRows,
leftPathKey2Index,
rightJsonRows,
rightPathKey2Index,
leftDecorations,
leftJsonPath2DiffDetail,
rightDecorations,
rightJsonPath2DiffDetail,
leftEditorRef,
rightEditorRef,
pairInfo,
};
};
export type IUseJYCM = ReturnType<typeof useJYCM>;
export default useJYCM;