plugin-block-cross-join 0.1.6 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,415 +1,408 @@
1
- /**
2
- * This file is part of the NocoBase (R) project.
3
- * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
- * Authors: NocoBase Team.
5
- *
6
- * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
- * For more information, please refer to: https://www.nocobase.com/agreement.
8
- */
9
-
10
- import React, { useState, useMemo, useCallback } from 'react';
11
- import { Modal, Tabs, Form, Select, Button, Space, Radio, Input, Table, Checkbox, Typography, Divider } from 'antd';
12
- import { PlusOutlined, DeleteOutlined } from '@ant-design/icons';
13
- import { useDataSourceManager } from '@nocobase/client';
14
-
15
- const { Text } = Typography;
16
-
17
- interface JoinDef {
18
- dataSource: string;
19
- collection: string;
20
- joinType: 'left' | 'inner';
21
- leftField: string;
22
- rightField: string;
23
- }
24
-
25
- interface ColumnDef {
26
- source: number;
27
- field: string;
28
- alias?: string;
29
- jsonExpand?: string[];
30
- selected?: boolean;
31
- }
32
-
33
- interface CrossJoinConfig {
34
- primarySource: { dataSource: string; collection: string };
35
- joins: JoinDef[];
36
- columns: ColumnDef[];
37
- }
38
-
39
- interface Props {
40
- visible: boolean;
41
- onCancel: () => void;
42
- onSubmit: (config: CrossJoinConfig) => void;
43
- initialConfig?: CrossJoinConfig;
44
- }
45
-
46
- function useDataSourceOptions() {
47
- const dm = useDataSourceManager();
48
- const dataSources = dm?.getDataSources() || [];
49
-
50
- return useMemo(
51
- () =>
52
- dataSources
53
- .filter((ds) => ds.status === 'loaded')
54
- .map((ds) => ({
55
- label: ds.displayName || ds.key,
56
- value: ds.key,
57
- ds,
58
- })),
59
- [dataSources],
60
- );
61
- }
62
-
63
- function useCollectionOptions(dsKey: string) {
64
- const dm = useDataSourceManager();
65
- return useMemo(() => {
66
- if (!dsKey || !dm) return [];
67
- const ds = dm.getDataSource(dsKey);
68
- if (!ds) return [];
69
- const collections = ds.collectionManager.getCollections() || [];
70
- return collections.map((c) => ({
71
- label: c.title || c.name,
72
- value: c.name,
73
- }));
74
- }, [dm, dsKey]);
75
- }
76
-
77
- function useFieldOptions(dsKey: string, collectionName: string) {
78
- const dm = useDataSourceManager();
79
- return useMemo(() => {
80
- if (!dsKey || !collectionName || !dm) return [];
81
- const ds = dm.getDataSource(dsKey);
82
- if (!ds) return [];
83
- const collection = ds.collectionManager.getCollection(collectionName);
84
- if (!collection) return [];
85
- const fields = collection.getFields() || [];
86
- return fields.map((f) => ({
87
- label: f.uiSchema?.title || f.name,
88
- value: f.name,
89
- type: f.type,
90
- interface: f.interface,
91
- }));
92
- }, [dm, dsKey, collectionName]);
93
- }
94
-
95
- // Tab 1: Primary Source
96
- const PrimarySourceTab: React.FC<{
97
- value: { dataSource: string; collection: string };
98
- onChange: (val: { dataSource: string; collection: string }) => void;
99
- }> = ({ value, onChange }) => {
100
- const dsOptions = useDataSourceOptions();
101
- const collectionOptions = useCollectionOptions(value.dataSource);
102
-
103
- return (
104
- <Form layout="vertical">
105
- <Form.Item label="Datasource">
106
- <Select
107
- value={value.dataSource}
108
- options={dsOptions}
109
- placeholder="Select datasource"
110
- onChange={(v) => onChange({ dataSource: v, collection: '' })}
111
- showSearch
112
- filterOption={(input, option) => (option?.label as string)?.toLowerCase().includes(input.toLowerCase())}
113
- />
114
- </Form.Item>
115
- <Form.Item label="Collection">
116
- <Select
117
- value={value.collection || undefined}
118
- options={collectionOptions}
119
- placeholder="Select collection"
120
- onChange={(v) => onChange({ ...value, collection: v })}
121
- showSearch
122
- filterOption={(input, option) => (option?.label as string)?.toLowerCase().includes(input.toLowerCase())}
123
- disabled={!value.dataSource}
124
- />
125
- </Form.Item>
126
- </Form>
127
- );
128
- };
129
-
130
- // Tab 2: Joins
131
- const JoinRow: React.FC<{
132
- join: JoinDef;
133
- index: number;
134
- primaryDs: string;
135
- primaryCollection: string;
136
- onChange: (index: number, join: JoinDef) => void;
137
- onRemove: (index: number) => void;
138
- }> = ({ join, index, primaryDs, primaryCollection, onChange, onRemove }) => {
139
- const dsOptions = useDataSourceOptions();
140
- const collectionOptions = useCollectionOptions(join.dataSource);
141
- const leftFieldOptions = useFieldOptions(primaryDs, primaryCollection);
142
- const rightFieldOptions = useFieldOptions(join.dataSource, join.collection);
143
-
144
- return (
145
- <div style={{ border: '1px solid #d9d9d9', borderRadius: 6, padding: 16, marginBottom: 12 }}>
146
- <div style={{ display: 'flex', justifyContent: 'space-between', marginBottom: 8 }}>
147
- <Text strong>Join #{index + 1}</Text>
148
- <Button type="text" danger icon={<DeleteOutlined />} onClick={() => onRemove(index)} />
149
- </div>
150
- <Form layout="vertical" size="small">
151
- <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 12 }}>
152
- <Form.Item label="Datasource" style={{ marginBottom: 8 }}>
153
- <Select
154
- value={join.dataSource}
155
- options={dsOptions}
156
- onChange={(v) => onChange(index, { ...join, dataSource: v, collection: '', rightField: '' })}
157
- showSearch
158
- filterOption={(input, option) => (option?.label as string)?.toLowerCase().includes(input.toLowerCase())}
159
- />
160
- </Form.Item>
161
- <Form.Item label="Collection" style={{ marginBottom: 8 }}>
162
- <Select
163
- value={join.collection || undefined}
164
- options={collectionOptions}
165
- onChange={(v) => onChange(index, { ...join, collection: v, rightField: '' })}
166
- disabled={!join.dataSource}
167
- showSearch
168
- filterOption={(input, option) => (option?.label as string)?.toLowerCase().includes(input.toLowerCase())}
169
- />
170
- </Form.Item>
171
- </div>
172
- <Form.Item label="Join Type" style={{ marginBottom: 8 }}>
173
- <Radio.Group value={join.joinType} onChange={(e) => onChange(index, { ...join, joinType: e.target.value })}>
174
- <Radio value="left">Left Join</Radio>
175
- <Radio value="inner">Inner Join</Radio>
176
- </Radio.Group>
177
- </Form.Item>
178
- <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 12 }}>
179
- <Form.Item label="Left Field (primary)" style={{ marginBottom: 0 }}>
180
- <Select
181
- value={join.leftField || undefined}
182
- options={leftFieldOptions}
183
- onChange={(v) => onChange(index, { ...join, leftField: v })}
184
- placeholder="Field from primary"
185
- showSearch
186
- filterOption={(input, option) => (option?.label as string)?.toLowerCase().includes(input.toLowerCase())}
187
- />
188
- </Form.Item>
189
- <Form.Item label="Right Field (join)" style={{ marginBottom: 0 }}>
190
- <Select
191
- value={join.rightField || undefined}
192
- options={rightFieldOptions}
193
- onChange={(v) => onChange(index, { ...join, rightField: v })}
194
- placeholder="Field from join table"
195
- disabled={!join.collection}
196
- showSearch
197
- filterOption={(input, option) => (option?.label as string)?.toLowerCase().includes(input.toLowerCase())}
198
- />
199
- </Form.Item>
200
- </div>
201
- </Form>
202
- </div>
203
- );
204
- };
205
-
206
- const JoinsTab: React.FC<{
207
- joins: JoinDef[];
208
- primaryDs: string;
209
- primaryCollection: string;
210
- onChange: (joins: JoinDef[]) => void;
211
- }> = ({ joins, primaryDs, primaryCollection, onChange }) => {
212
- const handleAdd = () => {
213
- onChange([...joins, { dataSource: 'main', collection: '', joinType: 'left', leftField: '', rightField: '' }]);
214
- };
215
- const handleChange = (index: number, join: JoinDef) => {
216
- const next = [...joins];
217
- next[index] = join;
218
- onChange(next);
219
- };
220
- const handleRemove = (index: number) => {
221
- onChange(joins.filter((_, i) => i !== index));
222
- };
223
-
224
- return (
225
- <div>
226
- {joins.map((join, i) => (
227
- <JoinRow
228
- key={i}
229
- join={join}
230
- index={i}
231
- primaryDs={primaryDs}
232
- primaryCollection={primaryCollection}
233
- onChange={handleChange}
234
- onRemove={handleRemove}
235
- />
236
- ))}
237
- <Button type="dashed" icon={<PlusOutlined />} onClick={handleAdd} block>
238
- Add Join
239
- </Button>
240
- </div>
241
- );
242
- };
243
-
244
- // Tab 3: Columns
245
- const ColumnsTab: React.FC<{
246
- columns: ColumnDef[];
247
- primaryDs: string;
248
- primaryCollection: string;
249
- joins: JoinDef[];
250
- onChange: (cols: ColumnDef[]) => void;
251
- }> = ({ columns, primaryDs, primaryCollection, joins, onChange }) => {
252
- const primaryFields = useFieldOptions(primaryDs, primaryCollection);
253
-
254
- // Build all available fields from all sources
255
- const allFields = useMemo(() => {
256
- const fields: { source: number; sourceName: string; field: string; label: string; type: string }[] = [];
257
-
258
- for (const f of primaryFields) {
259
- fields.push({
260
- source: 0,
261
- sourceName: primaryCollection,
262
- field: f.value,
263
- label: `${primaryCollection}.${f.label}`,
264
- type: f.type,
265
- });
266
- }
267
-
268
- return fields;
269
- }, [primaryFields, primaryCollection]);
270
-
271
- // Auto-populate columns if empty
272
- const handleAutoPopulate = useCallback(() => {
273
- const cols: ColumnDef[] = allFields.map((f) => ({
274
- source: f.source,
275
- field: f.field,
276
- selected: true,
277
- }));
278
- // Add join fields
279
- // (join fields will be added dynamically when they're loaded)
280
- onChange(cols);
281
- }, [allFields, onChange]);
282
-
283
- const tableColumns = [
284
- {
285
- title: 'Include',
286
- dataIndex: 'selected',
287
- width: 70,
288
- render: (_: any, record: ColumnDef, index: number) => (
289
- <Checkbox
290
- checked={record.selected !== false}
291
- onChange={(e) => {
292
- const next = [...columns];
293
- next[index] = { ...next[index], selected: e.target.checked };
294
- onChange(next);
295
- }}
296
- />
297
- ),
298
- },
299
- {
300
- title: 'Source',
301
- dataIndex: 'source',
302
- width: 100,
303
- render: (val: number) => (val === 0 ? 'Primary' : `Join #${val}`),
304
- },
305
- { title: 'Field', dataIndex: 'field', width: 150 },
306
- {
307
- title: 'Alias',
308
- dataIndex: 'alias',
309
- width: 150,
310
- render: (_: any, record: ColumnDef, index: number) => (
311
- <Input
312
- size="small"
313
- value={record.alias || ''}
314
- placeholder={record.field}
315
- onChange={(e) => {
316
- const next = [...columns];
317
- next[index] = { ...next[index], alias: e.target.value || undefined };
318
- onChange(next);
319
- }}
320
- />
321
- ),
322
- },
323
- ];
324
-
325
- return (
326
- <div>
327
- <div style={{ marginBottom: 12 }}>
328
- <Button size="small" onClick={handleAutoPopulate}>
329
- Auto-populate from sources
330
- </Button>
331
- </div>
332
- {columns.length > 0 ? (
333
- <Table
334
- dataSource={columns}
335
- columns={tableColumns}
336
- rowKey={(_, i) => String(i)}
337
- size="small"
338
- pagination={false}
339
- scroll={{ y: 300 }}
340
- />
341
- ) : (
342
- <Text type="secondary">
343
- Click &quot;Auto-populate&quot; to add fields from all configured sources, or they will be auto-detected at
344
- runtime.
345
- </Text>
346
- )}
347
- </div>
348
- );
349
- };
350
-
351
- export const CrossJoinConfigurator: React.FC<Props> = ({ visible, onCancel, onSubmit, initialConfig }) => {
352
- const [primarySource, setPrimarySource] = useState<{ dataSource: string; collection: string }>(
353
- initialConfig?.primarySource || { dataSource: 'main', collection: '' },
354
- );
355
- const [joins, setJoins] = useState<JoinDef[]>(initialConfig?.joins || []);
356
- const [columns, setColumns] = useState<ColumnDef[]>(initialConfig?.columns || []);
357
-
358
- const handleOk = () => {
359
- const selectedColumns = columns.filter((c) => c.selected !== false);
360
- onSubmit({
361
- primarySource,
362
- joins,
363
- columns: selectedColumns.map(({ selected, ...rest }) => rest),
364
- });
365
- };
366
-
367
- const isValid = primarySource.dataSource && primarySource.collection;
368
-
369
- return (
370
- <Modal
371
- title="Configure Cross Join"
372
- open={visible}
373
- onCancel={onCancel}
374
- onOk={handleOk}
375
- okButtonProps={{ disabled: !isValid }}
376
- width={720}
377
- destroyOnClose
378
- >
379
- <Tabs
380
- items={[
381
- {
382
- key: 'primary',
383
- label: 'Primary Source',
384
- children: <PrimarySourceTab value={primarySource} onChange={setPrimarySource} />,
385
- },
386
- {
387
- key: 'joins',
388
- label: `Joins (${joins.length})`,
389
- children: (
390
- <JoinsTab
391
- joins={joins}
392
- primaryDs={primarySource.dataSource}
393
- primaryCollection={primarySource.collection}
394
- onChange={setJoins}
395
- />
396
- ),
397
- },
398
- {
399
- key: 'columns',
400
- label: 'Columns',
401
- children: (
402
- <ColumnsTab
403
- columns={columns}
404
- primaryDs={primarySource.dataSource}
405
- primaryCollection={primarySource.collection}
406
- joins={joins}
407
- onChange={setColumns}
408
- />
409
- ),
410
- },
411
- ]}
412
- />
413
- </Modal>
414
- );
415
- };
1
+ // SPDX-License-Identifier: Apache-2.0
2
+
3
+ import React, { useState, useMemo, useCallback } from 'react';
4
+ import { Modal, Tabs, Form, Select, Button, Space, Radio, Input, Table, Checkbox, Typography, Divider } from 'antd';
5
+ import { PlusOutlined, DeleteOutlined } from '@ant-design/icons';
6
+ import { useApp } from '@nocobase/client-v2';
7
+
8
+ const { Text } = Typography;
9
+
10
+ interface JoinDef {
11
+ dataSource: string;
12
+ collection: string;
13
+ joinType: 'left' | 'inner';
14
+ leftField: string;
15
+ rightField: string;
16
+ }
17
+
18
+ interface ColumnDef {
19
+ source: number;
20
+ field: string;
21
+ alias?: string;
22
+ jsonExpand?: string[];
23
+ selected?: boolean;
24
+ }
25
+
26
+ interface CrossJoinConfig {
27
+ primarySource: { dataSource: string; collection: string };
28
+ joins: JoinDef[];
29
+ columns: ColumnDef[];
30
+ }
31
+
32
+ interface Props {
33
+ visible: boolean;
34
+ onCancel: () => void;
35
+ onSubmit: (config: CrossJoinConfig) => void;
36
+ initialConfig?: CrossJoinConfig;
37
+ }
38
+
39
+ function useDataSourceOptions() {
40
+ const app = useApp();
41
+ const dataSources = app?.dataSourceManager?.dataSources ? [...app.dataSourceManager.dataSources.values()] : [] || [];
42
+
43
+ return useMemo(
44
+ () =>
45
+ dataSources
46
+ .filter((ds) => ds.status === 'loaded')
47
+ .map((ds) => ({
48
+ label: ds.displayName || ds.key,
49
+ value: ds.key,
50
+ ds,
51
+ })),
52
+ [app],
53
+ );
54
+ }
55
+
56
+ function useCollectionOptions(dsKey: string) {
57
+ const app = useApp();
58
+ return useMemo(() => {
59
+ if (!dsKey || !dm) return [];
60
+ const ds = app?.dataSourceManager?.get(dsKey);
61
+ if (!ds) return [];
62
+ const collections = ds.collectionManager.getCollections() || [];
63
+ return collections.map((c) => ({
64
+ label: c.title || c.name,
65
+ value: c.name,
66
+ }));
67
+ }, [app, dsKey]);
68
+ }
69
+
70
+ function useFieldOptions(dsKey: string, collectionName: string) {
71
+ const app = useApp();
72
+ return useMemo(() => {
73
+ if (!dsKey || !collectionName || !dm) return [];
74
+ const ds = app?.dataSourceManager?.get(dsKey);
75
+ if (!ds) return [];
76
+ const collection = ds.collectionManager.getCollection(collectionName);
77
+ if (!collection) return [];
78
+ const fields = collection.getFields() || [];
79
+ return fields.map((f) => ({
80
+ label: f.uiSchema?.title || f.name,
81
+ value: f.name,
82
+ type: f.type,
83
+ interface: f.interface,
84
+ }));
85
+ }, [app, dsKey, collectionName]);
86
+ }
87
+
88
+ // Tab 1: Primary Source
89
+ const PrimarySourceTab: React.FC<{
90
+ value: { dataSource: string; collection: string };
91
+ onChange: (val: { dataSource: string; collection: string }) => void;
92
+ }> = ({ value, onChange }) => {
93
+ const dsOptions = useDataSourceOptions();
94
+ const collectionOptions = useCollectionOptions(value.dataSource);
95
+
96
+ return (
97
+ <Form layout="vertical">
98
+ <Form.Item label="Datasource">
99
+ <Select
100
+ value={value.dataSource}
101
+ options={dsOptions}
102
+ placeholder="Select datasource"
103
+ onChange={(v) => onChange({ dataSource: v, collection: '' })}
104
+ showSearch
105
+ filterOption={(input, option) => (option?.label as string)?.toLowerCase().includes(input.toLowerCase())}
106
+ />
107
+ </Form.Item>
108
+ <Form.Item label="Collection">
109
+ <Select
110
+ value={value.collection || undefined}
111
+ options={collectionOptions}
112
+ placeholder="Select collection"
113
+ onChange={(v) => onChange({ ...value, collection: v })}
114
+ showSearch
115
+ filterOption={(input, option) => (option?.label as string)?.toLowerCase().includes(input.toLowerCase())}
116
+ disabled={!value.dataSource}
117
+ />
118
+ </Form.Item>
119
+ </Form>
120
+ );
121
+ };
122
+
123
+ // Tab 2: Joins
124
+ const JoinRow: React.FC<{
125
+ join: JoinDef;
126
+ index: number;
127
+ primaryDs: string;
128
+ primaryCollection: string;
129
+ onChange: (index: number, join: JoinDef) => void;
130
+ onRemove: (index: number) => void;
131
+ }> = ({ join, index, primaryDs, primaryCollection, onChange, onRemove }) => {
132
+ const dsOptions = useDataSourceOptions();
133
+ const collectionOptions = useCollectionOptions(join.dataSource);
134
+ const leftFieldOptions = useFieldOptions(primaryDs, primaryCollection);
135
+ const rightFieldOptions = useFieldOptions(join.dataSource, join.collection);
136
+
137
+ return (
138
+ <div style={{ border: '1px solid #d9d9d9', borderRadius: 6, padding: 16, marginBottom: 12 }}>
139
+ <div style={{ display: 'flex', justifyContent: 'space-between', marginBottom: 8 }}>
140
+ <Text strong>Join #{index + 1}</Text>
141
+ <Button type="text" danger icon={<DeleteOutlined />} onClick={() => onRemove(index)} />
142
+ </div>
143
+ <Form layout="vertical" size="small">
144
+ <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 12 }}>
145
+ <Form.Item label="Datasource" style={{ marginBottom: 8 }}>
146
+ <Select
147
+ value={join.dataSource}
148
+ options={dsOptions}
149
+ onChange={(v) => onChange(index, { ...join, dataSource: v, collection: '', rightField: '' })}
150
+ showSearch
151
+ filterOption={(input, option) => (option?.label as string)?.toLowerCase().includes(input.toLowerCase())}
152
+ />
153
+ </Form.Item>
154
+ <Form.Item label="Collection" style={{ marginBottom: 8 }}>
155
+ <Select
156
+ value={join.collection || undefined}
157
+ options={collectionOptions}
158
+ onChange={(v) => onChange(index, { ...join, collection: v, rightField: '' })}
159
+ disabled={!join.dataSource}
160
+ showSearch
161
+ filterOption={(input, option) => (option?.label as string)?.toLowerCase().includes(input.toLowerCase())}
162
+ />
163
+ </Form.Item>
164
+ </div>
165
+ <Form.Item label="Join Type" style={{ marginBottom: 8 }}>
166
+ <Radio.Group value={join.joinType} onChange={(e) => onChange(index, { ...join, joinType: e.target.value })}>
167
+ <Radio value="left">Left Join</Radio>
168
+ <Radio value="inner">Inner Join</Radio>
169
+ </Radio.Group>
170
+ </Form.Item>
171
+ <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 12 }}>
172
+ <Form.Item label="Left Field (primary)" style={{ marginBottom: 0 }}>
173
+ <Select
174
+ value={join.leftField || undefined}
175
+ options={leftFieldOptions}
176
+ onChange={(v) => onChange(index, { ...join, leftField: v })}
177
+ placeholder="Field from primary"
178
+ showSearch
179
+ filterOption={(input, option) => (option?.label as string)?.toLowerCase().includes(input.toLowerCase())}
180
+ />
181
+ </Form.Item>
182
+ <Form.Item label="Right Field (join)" style={{ marginBottom: 0 }}>
183
+ <Select
184
+ value={join.rightField || undefined}
185
+ options={rightFieldOptions}
186
+ onChange={(v) => onChange(index, { ...join, rightField: v })}
187
+ placeholder="Field from join table"
188
+ disabled={!join.collection}
189
+ showSearch
190
+ filterOption={(input, option) => (option?.label as string)?.toLowerCase().includes(input.toLowerCase())}
191
+ />
192
+ </Form.Item>
193
+ </div>
194
+ </Form>
195
+ </div>
196
+ );
197
+ };
198
+
199
+ const JoinsTab: React.FC<{
200
+ joins: JoinDef[];
201
+ primaryDs: string;
202
+ primaryCollection: string;
203
+ onChange: (joins: JoinDef[]) => void;
204
+ }> = ({ joins, primaryDs, primaryCollection, onChange }) => {
205
+ const handleAdd = () => {
206
+ onChange([...joins, { dataSource: 'main', collection: '', joinType: 'left', leftField: '', rightField: '' }]);
207
+ };
208
+ const handleChange = (index: number, join: JoinDef) => {
209
+ const next = [...joins];
210
+ next[index] = join;
211
+ onChange(next);
212
+ };
213
+ const handleRemove = (index: number) => {
214
+ onChange(joins.filter((_, i) => i !== index));
215
+ };
216
+
217
+ return (
218
+ <div>
219
+ {joins.map((join, i) => (
220
+ <JoinRow
221
+ key={i}
222
+ join={join}
223
+ index={i}
224
+ primaryDs={primaryDs}
225
+ primaryCollection={primaryCollection}
226
+ onChange={handleChange}
227
+ onRemove={handleRemove}
228
+ />
229
+ ))}
230
+ <Button type="dashed" icon={<PlusOutlined />} onClick={handleAdd} block>
231
+ Add Join
232
+ </Button>
233
+ </div>
234
+ );
235
+ };
236
+
237
+ // Tab 3: Columns
238
+ const ColumnsTab: React.FC<{
239
+ columns: ColumnDef[];
240
+ primaryDs: string;
241
+ primaryCollection: string;
242
+ joins: JoinDef[];
243
+ onChange: (cols: ColumnDef[]) => void;
244
+ }> = ({ columns, primaryDs, primaryCollection, joins, onChange }) => {
245
+ const primaryFields = useFieldOptions(primaryDs, primaryCollection);
246
+
247
+ // Build all available fields from all sources
248
+ const allFields = useMemo(() => {
249
+ const fields: { source: number; sourceName: string; field: string; label: string; type: string }[] = [];
250
+
251
+ for (const f of primaryFields) {
252
+ fields.push({
253
+ source: 0,
254
+ sourceName: primaryCollection,
255
+ field: f.value,
256
+ label: `${primaryCollection}.${f.label}`,
257
+ type: f.type,
258
+ });
259
+ }
260
+
261
+ return fields;
262
+ }, [primaryFields, primaryCollection]);
263
+
264
+ // Auto-populate columns if empty
265
+ const handleAutoPopulate = useCallback(() => {
266
+ const cols: ColumnDef[] = allFields.map((f) => ({
267
+ source: f.source,
268
+ field: f.field,
269
+ selected: true,
270
+ }));
271
+ // Add join fields
272
+ // (join fields will be added dynamically when they're loaded)
273
+ onChange(cols);
274
+ }, [allFields, onChange]);
275
+
276
+ const tableColumns = [
277
+ {
278
+ title: 'Include',
279
+ dataIndex: 'selected',
280
+ width: 70,
281
+ render: (_: any, record: ColumnDef, index: number) => (
282
+ <Checkbox
283
+ checked={record.selected !== false}
284
+ onChange={(e) => {
285
+ const next = [...columns];
286
+ next[index] = { ...next[index], selected: e.target.checked };
287
+ onChange(next);
288
+ }}
289
+ />
290
+ ),
291
+ },
292
+ {
293
+ title: 'Source',
294
+ dataIndex: 'source',
295
+ width: 100,
296
+ render: (val: number) => (val === 0 ? 'Primary' : `Join #${val}`),
297
+ },
298
+ { title: 'Field', dataIndex: 'field', width: 150 },
299
+ {
300
+ title: 'Alias',
301
+ dataIndex: 'alias',
302
+ width: 150,
303
+ render: (_: any, record: ColumnDef, index: number) => (
304
+ <Input
305
+ size="small"
306
+ value={record.alias || ''}
307
+ placeholder={record.field}
308
+ onChange={(e) => {
309
+ const next = [...columns];
310
+ next[index] = { ...next[index], alias: e.target.value || undefined };
311
+ onChange(next);
312
+ }}
313
+ />
314
+ ),
315
+ },
316
+ ];
317
+
318
+ return (
319
+ <div>
320
+ <div style={{ marginBottom: 12 }}>
321
+ <Button size="small" onClick={handleAutoPopulate}>
322
+ Auto-populate from sources
323
+ </Button>
324
+ </div>
325
+ {columns.length > 0 ? (
326
+ <Table
327
+ dataSource={columns}
328
+ columns={tableColumns}
329
+ rowKey={(_, i) => String(i)}
330
+ size="small"
331
+ pagination={false}
332
+ scroll={{ y: 300 }}
333
+ />
334
+ ) : (
335
+ <Text type="secondary">
336
+ Click &quot;Auto-populate&quot; to add fields from all configured sources, or they will be auto-detected at
337
+ runtime.
338
+ </Text>
339
+ )}
340
+ </div>
341
+ );
342
+ };
343
+
344
+ export const CrossJoinConfigurator: React.FC<Props> = ({ visible, onCancel, onSubmit, initialConfig }) => {
345
+ const [primarySource, setPrimarySource] = useState<{ dataSource: string; collection: string }>(
346
+ initialConfig?.primarySource || { dataSource: 'main', collection: '' },
347
+ );
348
+ const [joins, setJoins] = useState<JoinDef[]>(initialConfig?.joins || []);
349
+ const [columns, setColumns] = useState<ColumnDef[]>(initialConfig?.columns || []);
350
+
351
+ const handleOk = () => {
352
+ const selectedColumns = columns.filter((c) => c.selected !== false);
353
+ onSubmit({
354
+ primarySource,
355
+ joins,
356
+ columns: selectedColumns.map(({ selected, ...rest }) => rest),
357
+ });
358
+ };
359
+
360
+ const isValid = primarySource.dataSource && primarySource.collection;
361
+
362
+ return (
363
+ <Modal
364
+ title="Configure Cross Join"
365
+ open={visible}
366
+ onCancel={onCancel}
367
+ onOk={handleOk}
368
+ okButtonProps={{ disabled: !isValid }}
369
+ width={720}
370
+ destroyOnClose
371
+ >
372
+ <Tabs
373
+ items={[
374
+ {
375
+ key: 'primary',
376
+ label: 'Primary Source',
377
+ children: <PrimarySourceTab value={primarySource} onChange={setPrimarySource} />,
378
+ },
379
+ {
380
+ key: 'joins',
381
+ label: `Joins (${joins.length})`,
382
+ children: (
383
+ <JoinsTab
384
+ joins={joins}
385
+ primaryDs={primarySource.dataSource}
386
+ primaryCollection={primarySource.collection}
387
+ onChange={setJoins}
388
+ />
389
+ ),
390
+ },
391
+ {
392
+ key: 'columns',
393
+ label: 'Columns',
394
+ children: (
395
+ <ColumnsTab
396
+ columns={columns}
397
+ primaryDs={primarySource.dataSource}
398
+ primaryCollection={primarySource.collection}
399
+ joins={joins}
400
+ onChange={setColumns}
401
+ />
402
+ ),
403
+ },
404
+ ]}
405
+ />
406
+ </Modal>
407
+ );
408
+ };