forked from mui/mui-x
-
Notifications
You must be signed in to change notification settings - Fork 1
/
useDemoData.ts
220 lines (184 loc) · 5.85 KB
/
useDemoData.ts
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
import * as React from 'react';
import LRUCache from 'lru-cache';
import { GridColumnVisibilityModel } from '@mui/x-data-grid-premium';
import { GridDemoData, getRealGridData } from '../services/real-data-service';
import { getCommodityColumns } from '../columns/commodities.columns';
import { getEmployeeColumns } from '../columns/employees.columns';
import asyncWorker from '../services/asyncWorker';
import { GridColDefGenerator } from '../services/gridColDefGenerator';
import {
AddPathToDemoDataOptions,
DemoTreeDataValue,
addTreeDataOptionsToDemoData,
} from '../services/tree-data-generator';
const dataCache = new LRUCache<string, DemoTreeDataValue>({
max: 10,
ttl: 60 * 5 * 1e3, // 5 minutes
});
export type DemoDataReturnType = {
data: DemoTreeDataValue;
loading: boolean;
setRowLength: (count: number) => void;
loadNewData: () => void;
};
type DataSet = 'Commodity' | 'Employee';
export interface UseDemoDataOptions {
dataSet: DataSet;
rowLength: number;
maxColumns?: number;
visibleFields?: string[];
editable?: boolean;
treeData?: AddPathToDemoDataOptions;
}
// Generate fake data from a seed.
// It's about x20 faster than getRealData.
export async function extrapolateSeed(
rowLength: number,
data: GridDemoData,
): Promise<GridDemoData> {
return new Promise<any>((resolve) => {
const seed = data.rows;
const rows = data.rows.slice();
const tasks = { current: rowLength - seed.length };
function work() {
const row = {} as any;
for (let j = 0; j < data.columns.length; j += 1) {
const column = data.columns[j];
const index = Math.round(Math.random() * (seed.length - 1));
if (column.field === 'id') {
row.id = `id-${tasks.current + seed.length}`;
} else {
row[column.field] = seed[index][column.field];
}
}
rows.push(row);
tasks.current -= 1;
}
asyncWorker({
work,
done: () => resolve({ ...data, rows }),
tasks,
});
});
}
export const deepFreeze = <T>(object: T): T => {
// Retrieve the property names defined on object
const propNames = Object.getOwnPropertyNames(object);
// Freeze properties before freezing self
// eslint-disable-next-line no-restricted-syntax
for (const name of propNames) {
const value = object[name as keyof T];
if (value && typeof value === 'object') {
deepFreeze(value);
}
}
return Object.freeze(object);
};
export interface ColumnsOptions
extends Pick<UseDemoDataOptions, 'dataSet' | 'editable' | 'maxColumns' | 'visibleFields'> {}
export const getColumnsFromOptions = (options: ColumnsOptions): GridColDefGenerator[] => {
let columns =
options.dataSet === 'Commodity' ? getCommodityColumns(options.editable) : getEmployeeColumns();
if (options.visibleFields) {
columns = columns.map((col) =>
options.visibleFields?.includes(col.field) ? col : { ...col, hide: true },
);
}
if (options.maxColumns) {
columns = columns.slice(0, options.maxColumns);
}
return columns;
};
export const getInitialState = (options: UseDemoDataOptions, columns: GridColDefGenerator[]) => {
const columnVisibilityModel: GridColumnVisibilityModel = {};
columns.forEach((col) => {
if (col.hide) {
columnVisibilityModel[col.field] = false;
}
});
const groupingField = options.treeData?.groupingField;
if (groupingField) {
columnVisibilityModel![groupingField] = false;
}
return { columns: { columnVisibilityModel } };
};
export const useDemoData = (options: UseDemoDataOptions): DemoDataReturnType => {
const [rowLength, setRowLength] = React.useState(options.rowLength);
const [index, setIndex] = React.useState(0);
const [loading, setLoading] = React.useState(true);
const columns = React.useMemo(() => {
return getColumnsFromOptions({
dataSet: options.dataSet,
editable: options.editable,
maxColumns: options.maxColumns,
visibleFields: options.visibleFields,
});
}, [options.dataSet, options.editable, options.maxColumns, options.visibleFields]);
const [data, setData] = React.useState<DemoTreeDataValue>(() => {
return addTreeDataOptionsToDemoData(
{
columns,
rows: [],
initialState: getInitialState(options, columns),
},
options.treeData,
);
});
React.useEffect(() => {
const cacheKey = `${options.dataSet}-${rowLength}-${index}-${options.maxColumns}`;
// Cache to allow fast switch between the JavaScript and TypeScript version
// of the demos.
if (dataCache.has(cacheKey)) {
const newData = dataCache.get(cacheKey)!;
setData(newData);
setLoading(false);
return undefined;
}
let active = true;
(async () => {
setLoading(true);
let newData: DemoTreeDataValue;
if (rowLength > 1000) {
newData = await getRealGridData(1000, columns);
newData = await extrapolateSeed(rowLength, newData);
} else {
newData = await getRealGridData(rowLength, columns);
}
if (!active) {
return;
}
newData = addTreeDataOptionsToDemoData(newData, {
maxDepth: options.treeData?.maxDepth,
groupingField: options.treeData?.groupingField,
averageChildren: options.treeData?.averageChildren,
});
// It's quite slow. No need for it in production.
if (process.env.NODE_ENV !== 'production') {
deepFreeze(newData);
}
dataCache.set(cacheKey, newData);
setData(newData);
setLoading(false);
})();
return () => {
active = false;
};
}, [
rowLength,
options.dataSet,
options.maxColumns,
options.treeData?.maxDepth,
options.treeData?.groupingField,
options.treeData?.averageChildren,
index,
columns,
]);
return {
data,
loading,
setRowLength,
loadNewData: () => {
setIndex((oldIndex) => oldIndex + 1);
},
};
};