-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathutils.ts
More file actions
68 lines (59 loc) · 2.02 KB
/
utils.ts
File metadata and controls
68 lines (59 loc) · 2.02 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import type { RefObject } from '@mui/x-internals/types';
import {
GRID_ROOT_GROUP_ID,
type GridGroupNode,
type GridKeyValue,
type GridRowId,
type GridRowTreeConfig,
} from '@mui/x-data-grid';
import { RequestQueue, type RequestStatus } from '@base-ui/utils/RequestQueue';
import type { GridPrivateApiPro } from '../../../models';
/**
* Thin wrapper around `RequestQueue` that ties request lifecycle to
* the Data Grid's loading-state bookkeeping.
*/
export class GridRequestQueue {
private requestQueue: RequestQueue<GridRowId>;
private api: GridPrivateApiPro;
constructor(apiRef: RefObject<GridPrivateApiPro>) {
this.api = apiRef.current;
this.requestQueue = new RequestQueue<GridRowId>({
fetchFn: (id) => {
this.api.fetchRowChildren(id);
return Promise.resolve();
},
});
}
public queue = (ids: GridRowId[]) => {
const loadingIds = Object.fromEntries(ids.map((id) => [id, true]));
this.api.setState((state) => ({
...state,
dataSource: {
...state.dataSource,
loading: {
...state.dataSource.loading,
...loadingIds,
},
},
}));
return this.requestQueue.queue(ids);
};
public setRequestSettled = (id: GridRowId) => this.requestQueue.setRequestSettled(id);
public clear = () => this.requestQueue.clear();
public clearPendingRequest = (id: GridRowId) => {
this.api.dataSource.setChildrenLoading(id, false);
return this.requestQueue.clearPendingRequest(id);
};
public getRequestStatus = (id: GridRowId): RequestStatus => this.requestQueue.getRequestStatus(id);
}
export const getGroupKeys = (tree: GridRowTreeConfig, rowId: GridRowId) => {
const rowNode = tree[rowId];
let currentNodeId = rowNode.parent;
const groupKeys: GridKeyValue[] = [];
while (currentNodeId && currentNodeId !== GRID_ROOT_GROUP_ID) {
const currentNode = tree[currentNodeId] as GridGroupNode;
groupKeys.push(currentNode.groupingKey ?? '');
currentNodeId = currentNode.parent;
}
return groupKeys.reverse();
};