-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathGridMiddlewarePlugin.tsx
More file actions
265 lines (241 loc) · 8.12 KB
/
GridMiddlewarePlugin.tsx
File metadata and controls
265 lines (241 loc) · 8.12 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
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
import React, { useCallback, useEffect } from 'react';
import {
PluginType,
type WidgetMiddlewarePlugin,
type WidgetMiddlewareComponentProps,
type WidgetMiddlewarePanelProps,
} from '@deephaven/plugin';
import { type dh } from '@deephaven/jsapi-types';
import Log from '@deephaven/log';
import { Button } from '@deephaven/components';
import { vsGear } from '@deephaven/icons';
import {
type TableOption,
type TableOptionPanelProps,
useTableOptionsHost,
defaultTableOptionsRegistry,
} from '@deephaven/iris-grid';
const log = Log.module('GridMiddlewarePlugin');
/**
* Example middleware plugin that wraps the GridWidgetPlugin.
* This demonstrates how middleware can intercept and enhance widget rendering.
*
* Middleware plugins:
* - Must set `isMiddleware: true`
* - Receive the wrapped component as `Component` prop
* - Must render `Component` to continue the chain
* - Are chained in registration order (first registered = outermost wrapper)
*/
function GridMiddleware({
Component,
...props
}: WidgetMiddlewareComponentProps<dh.Table>): JSX.Element {
// Register the option when the middleware mounts (not as a module side effect)
useEffect(() => {
defaultTableOptionsRegistry.register(MiddlewareCustomOption);
return () => {
defaultTableOptionsRegistry.unregister(MIDDLEWARE_OPTION_TYPE);
};
}, []);
// Log when middleware is mounted (for debugging)
useEffect(() => {
log.debug('GridMiddleware (component) mounted');
return () => {
log.debug('GridMiddleware (component) unmounted');
};
}, []);
// Pass through to the wrapped component
// Middleware can add context providers, state, or modify props here
// eslint-disable-next-line react/jsx-props-no-spreading
return <Component {...props} />;
}
GridMiddleware.displayName = 'GridMiddleware';
/**
* Custom option type for the middleware plugin.
* Using a unique string to avoid conflicts with built-in option types.
*/
const MIDDLEWARE_OPTION_TYPE = 'middleware-custom-option';
/**
* A sample configuration panel similar to SelectDistinctBuilder.
* Demonstrates how middleware plugins can use the useTableOptionsHost hook
* to access and modify grid state.
*/
function MiddlewareConfigPanel(_props: TableOptionPanelProps): JSX.Element {
// Access the Table Options context for state and dispatch
const { gridState, dispatch, closePanel } = useTableOptionsHost();
const {
model,
selectDistinctColumns,
customColumns,
quickFilters,
advancedFilters,
searchValue,
selectedSearchColumns,
sorts,
reverse,
} = gridState;
const handleButtonClick = useCallback(() => {
log.info('MiddlewareConfigPanel button clicked!');
// eslint-disable-next-line no-console
console.log('MiddlewareConfigPanel: Sample button clicked!');
// eslint-disable-next-line no-console
console.log('Current selectDistinctColumns:', selectDistinctColumns);
// eslint-disable-next-line no-console
console.log('Current customColumns:', customColumns);
// eslint-disable-next-line no-console
console.log('Current quickFilters:', quickFilters);
// eslint-disable-next-line no-console
console.log('Current advancedFilters:', advancedFilters);
// eslint-disable-next-line no-console
console.log('Current searchValue:', searchValue, 'columns:', selectedSearchColumns);
// eslint-disable-next-line no-console
console.log('Current sorts:', sorts, 'reverse:', reverse);
}, [
selectDistinctColumns,
customColumns,
quickFilters,
advancedFilters,
searchValue,
selectedSearchColumns,
sorts,
reverse,
]);
const handleClearSelectDistinct = useCallback(() => {
log.info('Clearing selectDistinctColumns');
dispatch({ type: 'SET_SELECT_DISTINCT_COLUMNS', columns: [] });
closePanel();
}, [dispatch, closePanel]);
const handleClearFilters = useCallback(() => {
log.info('Clearing all filters');
dispatch({ type: 'CLEAR_ALL_FILTERS' });
closePanel();
}, [dispatch, closePanel]);
const hasFilters =
quickFilters.size > 0 ||
advancedFilters.size > 0 ||
searchValue !== '' ||
sorts.length > 0;
return (
<div className="container mt-3">
<p className="text-muted small">Columns: {model.columns?.length ?? 0}</p>
<p className="text-muted small">
Select Distinct:{' '}
{selectDistinctColumns.length > 0
? selectDistinctColumns.join(', ')
: 'None'}
</p>
<p className="text-muted small">
Custom Columns:{' '}
{customColumns.length > 0 ? customColumns.join(', ') : 'None'}
</p>
<p className="text-muted small">
Quick Filters: {quickFilters.size > 0 ? quickFilters.size : 'None'}
</p>
<p className="text-muted small">
Advanced Filters:{' '}
{advancedFilters.size > 0 ? advancedFilters.size : 'None'}
</p>
<p className="text-muted small">
Cross-Column Search:{' '}
{searchValue || 'None'}
{selectedSearchColumns.length > 0
? ` (in ${selectedSearchColumns.join(', ')})`
: ''}
</p>
<p className="text-muted small">
Sorts: {sorts.length > 0 ? sorts.length : 'None'}
{reverse ? ' (reversed)' : ''}
</p>
<div className="d-flex flex-column gap-2 mt-3">
<Button kind="primary" onClick={handleButtonClick}>
Log State to Console
</Button>
{selectDistinctColumns.length > 0 && (
<Button kind="secondary" onClick={handleClearSelectDistinct}>
Clear Select Distinct
</Button>
)}
{hasFilters && (
<Button kind="secondary" onClick={handleClearFilters}>
Clear All Filters & Sorts
</Button>
)}
</div>
<p className="text-muted small mt-3">
This panel demonstrates using the useTableOptionsHost hook to access and
modify grid state from a plugin.
</p>
</div>
);
}
MiddlewareConfigPanel.displayName = 'MiddlewareConfigPanel';
/**
* Middleware custom option registered with the Table Options registry.
* This demonstrates how plugins can add custom options via the registry.
*/
const MiddlewareCustomOption: TableOption = {
type: MIDDLEWARE_OPTION_TYPE,
menuItem: {
title: 'Middleware Custom Option',
subtitle: 'Opens a configuration panel',
icon: vsGear,
// Show at top of menu
order: -100,
// Always available
isAvailable: () => true,
},
Panel: MiddlewareConfigPanel,
};
// Note: Registration moved to GridMiddleware component to avoid side effects at module load time
// defaultTableOptionsRegistry.register(MiddlewareCustomOption);
/**
* Panel middleware that wraps the GridPanelPlugin.
* This is used when the base plugin has a panelComponent defined.
*/
function GridPanelMiddleware({
Component,
...props
}: WidgetMiddlewarePanelProps<dh.Table>): JSX.Element {
// Log when panel middleware is mounted (for debugging)
useEffect(() => {
log.debug('GridMiddleware (panel) mounted');
return () => {
log.debug('GridMiddleware (panel) unmounted');
};
}, []);
// Simply pass through - registry handles the option
return (
<Component
/* eslint-disable-next-line react/jsx-props-no-spreading */
{...props}
/>
);
}
GridPanelMiddleware.displayName = 'GridPanelMiddleware';
/**
* Middleware plugin configuration for GridWidgetPlugin.
* This plugin wraps the base grid widget and can be used to:
* - Add custom Table Options menu items
* - Inject additional context or state
* - Add UI elements around the grid
* - Intercept and modify props before they reach the grid
*
* Since GridPluginConfig has a panelComponent, we must also provide
* a panelComponent to have our middleware applied.
*/
const GridMiddlewarePluginConfig: WidgetMiddlewarePlugin<dh.Table> = {
name: '@deephaven/grid-middleware',
title: 'Grid Middleware',
type: PluginType.WIDGET_PLUGIN,
component: GridMiddleware,
panelComponent: GridPanelMiddleware,
supportedTypes: [
'Table',
'TreeTable',
'HierarchicalTable',
'PartitionedTable',
],
isMiddleware: true,
};
export { GridMiddleware, GridPanelMiddleware };
export default GridMiddlewarePluginConfig;