-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDataVisualizer.tsx
282 lines (269 loc) · 8.87 KB
/
DataVisualizer.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/* eslint-disable react-native/no-inline-styles */
import { usePlugin } from 'flipper-plugin';
import React, { useEffect, useRef, useState } from 'react';
import { CanonicalObjectSchemaProperty } from 'realm';
import { plugin } from '..';
import { DropdownPropertyType, MenuItemGenerator, DeserializedRealmObject, SortedObjectSchema, PlainRealmObject, RealmObjectReference } from '../CommonTypes';
import {
CustomDropdown,
} from '../components/CustomDropdown';
import { DataTable } from '../components/DataTable';
import { FieldEdit } from '../components/objectManipulation/FieldEdit';
import { ObjectEdit } from '../components/objectManipulation/ObjectEdit';
import {
InspectionDataType,
RealmDataInspector,
} from '../components/RealmDataInspector';
type DataVisualizerProps = {
objects: Array<DeserializedRealmObject>;
schemas: Array<SortedObjectSchema>;
currentSchema: SortedObjectSchema;
sortingDirection: 'ascend' | 'descend' | null;
sortingColumn: string | null;
hasMore: boolean;
totalObjects?: number;
enableSort: boolean;
clickAction?: (object: DeserializedRealmObject) => void;
fetchMore: () => void;
handleDataInspector?: () => void;
};
const DataVisualizer = ({
objects,
schemas,
currentSchema,
sortingDirection,
sortingColumn,
hasMore,
totalObjects,
enableSort,
clickAction,
fetchMore,
}: DataVisualizerProps) => {
/** Hooks to manage the state of the DataInspector and open/close the sidebar. */
const [inspectionData, setInspectionData] = useState<InspectionDataType>();
const [showSidebar, setShowSidebar] = useState(false);
const [goBackStack, setGoBackStack] = useState<Array<InspectionDataType>>([]);
const [goForwardStack, setGoForwardStack] = useState<Array<InspectionDataType>>([]);
/** Hook to open/close the editing dialog and set its properties. */
const [editingObject, setEditingObject] = useState<{
editing: boolean;
object?: DeserializedRealmObject;
// schemaProperty?: SchemaProperty;
type?: 'field' | 'object';
fieldName?: string;
}>({
editing: false,
});
const pluginState = usePlugin(plugin);
const { removeObject, getObject } = pluginState;
const { selectedRealm } = pluginState.state.get();
/** refs to keep track of the current scrolling position for the context menu */
const scrollX = useRef(0);
const scrollY = useRef(0);
/** Functions for deleting and editing rows/objects */
const deleteRow = (row: DeserializedRealmObject) => {
removeObject(row);
};
const editField = (
row: DeserializedRealmObject,
schemaProperty: CanonicalObjectSchemaProperty,
) => {
setEditingObject({
editing: true,
object: row,
type: 'field',
fieldName: schemaProperty.name,
});
};
const editObject = (row: DeserializedRealmObject) => {
setEditingObject({
editing: true,
object: row,
type: 'object',
});
};
/** Generate MenuItem objects for the context menu with all necessary data and functions.*/
const generateMenuItems: MenuItemGenerator = (
row: DeserializedRealmObject,
schemaProperty: CanonicalObjectSchemaProperty,
schema: Realm.ObjectSchema,
) => [
{
key: 1,
text: 'Inspect Object',
onClick: () => {
setNewInspectionData(
{
data: {
[schema.name]: row.realmObject,
},
view: 'object',
isReference: false,
},
true,
);
},
},
{
key: 2,
text: 'Inspect Property',
onClick: () => {
const propertyValue = row.realmObject[schemaProperty.name]
//@ts-expect-error Property value should have objectKey if it has objectType.
const isReference = propertyValue && schemaProperty.objectType && propertyValue.objectKey
setNewInspectionData(
{
data: isReference ? propertyValue as RealmObjectReference : {
[schema.name + '.' + schemaProperty.name]:
propertyValue,
},
view: isReference ? 'object' : 'property',
isReference,
},
true,
);
}
},
{
key: 3,
text: 'Edit Object',
onClick: () => editObject(row)
},
{
key: 4,
text: 'Edit Property',
onClick: () => editField(row, schemaProperty),
},
{
key: 5,
text: 'Delete Object',
onClick: () => deleteRow(row),
},
];
/** Managing dropdown properties.*/
const [dropdownProp, setdropdownProp] = useState<DropdownPropertyType>({
generateMenuItems,
currentSchema: currentSchema,
record: null,
schemaProperty: null,
visible: false,
pointerX: 0,
pointerY: 0,
scrollX: 0,
scrollY: 0,
});
/** Hook to close the dropdown when clicked outside of it. */
useEffect(() => {
const closeDropdown = () => {
setdropdownProp({ ...dropdownProp, visible: false });
};
document.body.addEventListener('click', closeDropdown);
return () => document.body.removeEventListener('click', closeDropdown);
});
/** Handler to keep track of the current x and y position of the scrollcontainer. This is needed to render the dropdown in the correct place when scrolled. */
const handleScroll = (event: React.BaseSyntheticEvent) => {
const { scrollLeft, scrollTop } = event.target;
scrollX.current = scrollLeft;
scrollY.current = scrollTop;
};
/** Take the current dropdownProp and update it with the current x and y scroll values.
This cannot be done with useState because it would cause too many rerenders.*/
const updatedDropdownProp = {
...dropdownProp,
scrollX: scrollX.current,
scrollY: scrollY.current,
};
return (
<div
onScroll={handleScroll}
style={{
width: '100%',
height: '100%',
boxSizing: 'border-box',
position: 'relative',
overflow: 'scroll',
// TODO: This is a temporary solution to help force the horizontal scrollbar to appear. https://github.com/realm/realm-flipper-plugin/issues/90
marginBottom: 200,
}}
>
{editingObject.object && editingObject.editing && editingObject.type === 'object' ? (
<ObjectEdit
schema={currentSchema}
initialObject={editingObject.object}
setVisible={(val: boolean) => {
setEditingObject((obj) => ({
...obj,
editing: val,
}));
}}
visible={editingObject.editing}
/>
) : editingObject.editing &&
editingObject.type === 'field' &&
editingObject.object ? (
<FieldEdit
schema={currentSchema}
fieldName={editingObject.fieldName as string}
setVisible={(val: boolean) => {
setEditingObject((obj) => ({
...obj,
editing: val,
}));
}}
visible={editingObject.editing}
value={editingObject.object}
/>
) : null}
<DataTable
objects={objects}
schemas={schemas}
hasMore={hasMore}
sortingDirection={sortingDirection}
sortingColumn={sortingColumn}
currentSchema={currentSchema}
totalObjects={totalObjects}
generateMenuItems={generateMenuItems}
setdropdownProp={setdropdownProp}
dropdownProp={dropdownProp}
scrollX={scrollX.current}
scrollY={scrollY.current}
enableSort={enableSort}
setNewInspectionData={setNewInspectionData}
fetchMore={fetchMore}
clickAction={clickAction}
getObject={(object: RealmObjectReference, schemaName: string) => {return getObject(selectedRealm, schemaName, object.objectKey)}}
/>
<CustomDropdown {...updatedDropdownProp} />
<RealmDataInspector
schemas={schemas}
inspectionData={inspectionData}
setInspectionData={setInspectionData}
showSidebar={showSidebar}
setShowSidebar={setShowSidebar}
goBackStack={goBackStack}
setGoBackStack={setGoBackStack}
goForwardStack={goForwardStack}
setGoForwardStack={setGoForwardStack}
setNewInspectionData={setNewInspectionData}
getObject={(object: RealmObjectReference) => {return getObject(selectedRealm, object.objectType!, object.objectKey)}}
/>
</div>
);
// update inspectionData and push object to GoBackStack
function setNewInspectionData(
newInspectionData: InspectionDataType,
wipeStacks?: boolean,
) {
showSidebar ? null : setShowSidebar(true);
if (inspectionData !== undefined && !wipeStacks) {
goBackStack.push(inspectionData);
setGoBackStack(goBackStack);
setGoForwardStack([]);
} else if (wipeStacks) {
setGoBackStack([]);
setGoForwardStack([]);
}
setInspectionData(newInspectionData);
}
};
export default DataVisualizer;