-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathview-config-utils.ts
More file actions
408 lines (367 loc) · 16.7 KB
/
Copy pathview-config-utils.ts
File metadata and controls
408 lines (367 loc) · 16.7 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/**
* ViewConfigPanel — Shared Utilities
*
* Extracted from ViewConfigPanel to enable reuse across the
* schema-driven config panel framework.
*/
import type { FilterGroup, SortItem } from '@object-ui/components';
import { VIEW_FILTER_OPERATORS, VIEW_FILTER_OPERATOR_ALIASES } from '@objectstack/spec/ui';
import type { ViewFilterOperator } from '@objectstack/spec/ui';
// ---------------------------------------------------------------------------
// Operator mapping: @objectstack/spec → FilterBuilder
// ---------------------------------------------------------------------------
//
// One direction only. A stored view filter is *read* into a FilterBuilder here;
// the write direction was retired with the legacy `buildViewConfigSchema`
// engine, and the studio's spec-driven inspector persists the authored body
// itself. The retired table was also objectui's last emitter of `'not in'`
// (with a space), `before` and `after` as filter-AST operators — spellings the
// server used to drop silently (#2901, objectstack#3948).
/**
* Every canonical `VIEW_FILTER_OPERATORS` member, mapped onto the operator id
* the FilterBuilder renders — `null` where the builder has no equivalent.
*
* Total by construction: the type is keyed by `ViewFilterOperator`, so an
* operator added to the spec's view vocabulary fails to compile here instead
* of reaching the builder as a raw spelling its dropdown cannot select.
*
* **Every canonical operator now maps.** The four that did not —
* `starts_with`/`ends_with`/`is_null`/`is_not_null` — were unmapped because the
* FilterBuilder had no such operator; #2942 added `startsWith`/`endsWith`/
* `isNull`/`isNotNull` to it, and this table did not follow, so a stored view
* carrying them still reached the builder as a raw spelling it could by then
* have rendered. The parity guard catches that class now: a canonical operator
* whose name folds onto a builder id it is not mapped to fails the test.
*
* `is_null`/`is_not_null` map to `isNull`/`isNotNull` and NOT to
* `isEmpty`/`isNotEmpty` — the builder now draws both pairs, and folding the
* NULL predicate onto the empty-string one would silently rewrite the author's
* operator the next time the view was saved.
*/
const CANONICAL_TO_BUILDER: Record<ViewFilterOperator, string | null> = {
'equals': 'equals',
'not_equals': 'notEquals',
'contains': 'contains',
'not_contains': 'notContains',
'starts_with': 'startsWith',
'ends_with': 'endsWith',
'greater_than': 'greaterThan',
'less_than': 'lessThan',
'greater_than_or_equal': 'greaterOrEqual',
'less_than_or_equal': 'lessOrEqual',
'in': 'in',
'not_in': 'notIn',
'is_empty': 'isEmpty',
'is_not_empty': 'isNotEmpty',
'is_null': 'isNull',
'is_not_null': 'isNotNull',
'before': 'before',
'after': 'after',
'between': 'between',
};
/**
* Infix and short spellings a stored *filter array* carries instead of a view
* spelling — `['amount', '>', 100]`. These are `AST_OPERATOR_MAP` keys
* (`data/filter.zod.ts`) that case-folding alone cannot reach.
*/
const INFIX_TO_CANONICAL: Record<string, ViewFilterOperator> = {
'=': 'equals',
'==': 'equals',
'!=': 'not_equals',
'<>': 'not_equals',
'>': 'greater_than',
'<': 'less_than',
'>=': 'greater_than_or_equal',
'<=': 'less_than_or_equal',
'nin': 'not_in',
'like': 'contains',
};
/** Case- and separator-insensitive key: `not_in`, `notIn`, `'not in'` → `notin`. */
const fold = (op: string) => op.toLowerCase().replace(/[\s_-]+/g, '');
/**
* Every spelling the spec itself recognises, folded onto its canonical member.
*
* Derived from the spec's own canonical list and legacy-alias table rather than
* restated, so the ~30 aliases `VIEW_FILTER_OPERATOR_ALIASES` still folds — all
* of them live in stored metadata, because `saveMeta` persists the authored body
* verbatim — are covered without this file enumerating them. Hand-enumerating
* spellings is what left `before`/`after` unmapped in the first place.
*/
const FOLDED_TO_CANONICAL: Map<string, ViewFilterOperator> = new Map([
...VIEW_FILTER_OPERATORS.map(op => [fold(op), op] as const),
...Object.entries(VIEW_FILTER_OPERATOR_ALIASES).map(([alias, op]) => [fold(alias), op] as const),
]);
/**
* Resolve any stored operator spelling to a FilterBuilder operator id.
*
* Returns the input unchanged when no mapping exists, so an unrecognised
* operator is visible in the UI rather than silently coerced to `equals`.
*/
export function specToBuilderOperator(op: string): string {
const raw = String(op ?? '').trim();
if (!raw) return 'equals';
const canonical = INFIX_TO_CANONICAL[raw.toLowerCase()] ?? FOLDED_TO_CANONICAL.get(fold(raw));
return (canonical ? CANONICAL_TO_BUILDER[canonical] : null) ?? raw;
}
/** Exported for the parity guard — the mapping's canonical half. */
export const __CANONICAL_TO_BUILDER = CANONICAL_TO_BUILDER;
// ---------------------------------------------------------------------------
// Field type normalization: ObjectUI → FilterBuilder
// ---------------------------------------------------------------------------
/**
* Normalize raw field types to the 5 categories supported by FilterBuilder/SortBuilder.
* Lookup-like types (lookup, master_detail, user, owner) map to 'select' because
* FilterBuilder handles them identically when options are provided — the distinction
* between select and lookup operators is handled within FilterBuilder itself via the
* original field.type passed through ListView's filterFields.
*/
export function normalizeFieldType(rawType?: string): 'text' | 'number' | 'boolean' | 'date' | 'select' {
const t = (rawType || '').toLowerCase();
if (['integer', 'int', 'float', 'double', 'number', 'currency', 'money', 'percent', 'rating'].includes(t)) return 'number';
if (['date', 'datetime', 'datetime_tz', 'timestamp', 'time'].includes(t)) return 'date';
if (['boolean', 'bool', 'checkbox', 'switch'].includes(t)) return 'boolean';
if (['select', 'picklist', 'single_select', 'multi_select', 'enum', 'status', 'lookup', 'master_detail', 'user', 'owner'].includes(t)) return 'select';
return 'text';
}
// ---------------------------------------------------------------------------
// Spec-style filter bridge
// ---------------------------------------------------------------------------
function parseTriplet(arr: any[]): { id: string; field: string; operator: string; value: any } | null {
if (!Array.isArray(arr) || arr.length < 2) return null;
const [field, op, value] = arr;
if (typeof field !== 'string' || typeof op !== 'string') return null;
return {
id: crypto.randomUUID(),
field,
operator: specToBuilderOperator(op),
value: value ?? '',
};
}
function parseSingleOrNested(item: any): Array<{ id: string; field: string; operator: string; value: any }> {
if (Array.isArray(item)) {
const triplet = parseTriplet(item);
return triplet ? [triplet] : [];
}
if (typeof item === 'object' && item !== null && item.field) {
return [{
id: item.id || crypto.randomUUID(),
field: item.field,
operator: specToBuilderOperator(item.operator),
value: item.value ?? '',
}];
}
return [];
}
export function parseSpecFilter(raw: any): { logic: 'and' | 'or'; conditions: Array<{ id: string; field: string; operator: string; value: any }> } {
if (!Array.isArray(raw) || raw.length === 0) {
return { logic: 'and', conditions: [] };
}
// Detect ['and', ...conditions] or ['or', ...conditions]
if (typeof raw[0] === 'string' && (raw[0] === 'and' || raw[0] === 'or')) {
const logic = raw[0] as 'and' | 'or';
const rest = raw.slice(1);
const conditions = rest.flatMap((item: any) => parseSingleOrNested(item));
return { logic, conditions };
}
// Detect single triplet: ['field', '=', value] (all primitives at top level)
if (raw.length >= 2 && raw.length <= 3 && typeof raw[0] === 'string' && typeof raw[1] === 'string' && !Array.isArray(raw[0])) {
// Check it's not an array of arrays
if (!Array.isArray(raw[2])) {
const cond = parseTriplet(raw);
return { logic: 'and', conditions: cond ? [cond] : [] };
}
}
// Detect array of conditions: [[...], [...]] or [{...}, {...}]
if (Array.isArray(raw[0]) || (typeof raw[0] === 'object' && raw[0] !== null && !Array.isArray(raw[0]))) {
const conditions = raw.flatMap((item: any) => parseSingleOrNested(item));
return { logic: 'and', conditions };
}
// Fallback: try as single triplet
const cond = parseTriplet(raw);
return { logic: 'and', conditions: cond ? [cond] : [] };
}
// ---------------------------------------------------------------------------
// Parse helpers
// ---------------------------------------------------------------------------
/** Parse comma-separated string to trimmed non-empty string array */
export function parseCommaSeparated(input: string): string[] {
return input.split(',').map(s => s.trim()).filter(Boolean);
}
/** Parse comma-separated string to positive number array */
export function parseNumberList(input: string): number[] {
return input.split(',').map(s => Number(s.trim())).filter(n => !isNaN(n) && n > 0);
}
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
/** View type labels for display */
export const VIEW_TYPE_LABELS: Record<string, string> = {
grid: 'Grid',
kanban: 'Kanban',
calendar: 'Calendar',
gallery: 'Gallery',
timeline: 'Timeline',
gantt: 'Gantt',
map: 'Map',
chart: 'Chart',
};
/** All available view type keys */
export const VIEW_TYPE_OPTIONS = Object.keys(VIEW_TYPE_LABELS);
/**
* Row height options with Tailwind gap classes for visual icons.
* Aligned with @objectstack/spec RowHeight enum — all 5 values.
*/
export const ROW_HEIGHT_OPTIONS: Array<{ value: string; gapClass: string }> = [
{ value: 'compact', gapClass: 'gap-0' },
{ value: 'short', gapClass: 'gap-px' },
{ value: 'medium', gapClass: 'gap-0.5' },
{ value: 'tall', gapClass: 'gap-1' },
{ value: 'extra_tall', gapClass: 'gap-1.5' },
];
// ---------------------------------------------------------------------------
// Field options derivation
// ---------------------------------------------------------------------------
export interface FieldOption {
value: string;
label: string;
type: 'text' | 'number' | 'boolean' | 'date' | 'select';
options?: any[];
/** Raw (unnormalized) field type from objectDef. Used by view-type
* predicates (isImageLikeField, isGeoLikeField) that need finer signals
* than the 5-bucket `type`. Optional for backward-compat. */
rawType?: string;
/** Raw field name (same as `value` in current callers but kept distinct
* so future callers can carry through display-name heuristics without
* affecting persistence keys). */
rawName?: string;
}
/** Derive field options from an objectDef for FilterBuilder/SortBuilder/Selects */
export function deriveFieldOptions(objectDef: { fields?: Record<string, any> }): FieldOption[] {
if (!objectDef.fields) return [];
return Object.entries(objectDef.fields)
.filter(([key, field]: [string, any]) => {
if (key.startsWith('_')) return false;
if (field?.hidden) return false;
return true;
})
.map(([key, field]: [string, any]) => ({
value: key,
label: field.label || key,
type: normalizeFieldType(field.type),
options: field.options,
rawType: field.type,
rawName: key,
}));
}
/** Convert draft filter → FilterGroup for FilterBuilder */
export function toFilterGroup(draftFilter: any): FilterGroup {
const parsed = parseSpecFilter(draftFilter);
return { id: 'root', logic: parsed.logic, conditions: parsed.conditions };
}
/** Convert draft sort → SortItem[] for SortBuilder */
export function toSortItems(draftSort: any): SortItem[] {
return (Array.isArray(draftSort) ? draftSort : []).map((s: any) => ({
id: s.id || crypto.randomUUID(),
field: s.field || '',
order: (s.order || s.direction || 'asc') as 'asc' | 'desc',
}));
}
// ---------------------------------------------------------------------------
// Field-role detection
// ---------------------------------------------------------------------------
//
// These predicates classify object fields by their suitability for specific
// view roles (kanban group-by, gallery image, map latitude, etc.). They are
// used by CreateViewDialog to filter eligible fields and to compute whether a
// view type is at all available for the current object.
//
// Each predicate accepts the *raw* objectDef field (unnormalized) so that we
// can match on richer signals (semantic field type, name conventions) than
// the 5-bucket normalized type.
const IMAGE_FIELD_TYPES = new Set([
'image', 'image_url', 'photo', 'picture', 'avatar',
'file', 'attachment', 'attachments', 'media',
'url', 'link', // URL fields commonly hold image links in CRM datasets
]);
const IMAGE_FIELD_NAME_HINTS = [
'image', 'photo', 'picture', 'avatar', 'thumbnail', 'thumb',
'logo', 'cover', 'banner', 'icon', 'attachment',
];
/**
* True when the field is plausibly an image source for a gallery view.
* Matches on field type (image/file/url-like) OR field name (avatar/photo/…).
*
* Accepts either a raw object-definition field (`{ type, name }`) or a derived
* `FieldOption` (`{ rawType, rawName, value }`).
*/
export function isImageLikeField(
field: { type?: string; name?: string; key?: string; rawType?: string; rawName?: string; value?: string } | undefined | null,
): boolean {
if (!field) return false;
const type = (field.rawType || field.type || '').toLowerCase();
if (IMAGE_FIELD_TYPES.has(type)) return true;
const name = (field.rawName || field.name || field.key || field.value || '').toLowerCase();
if (!name) return false;
return IMAGE_FIELD_NAME_HINTS.some((hint) => name.includes(hint));
}
const GEO_FIELD_TYPES = new Set([
'geolocation', 'geo', 'geo_point', 'geopoint', 'location', 'latlng', 'lnglat',
]);
/**
* True when the field is plausibly the latitude OR longitude component of a
* geo coordinate. `axis` selects which one.
*
* Matches on:
* - dedicated geo types (always considered both axes)
* - field name conventions (`lat`, `latitude`, `lng`, `lon`, `longitude`)
*
* Accepts either a raw object-definition field or a derived `FieldOption`.
*/
export function isGeoLikeField(
field: { type?: string; name?: string; key?: string; rawType?: string; rawName?: string; value?: string } | undefined | null,
axis: 'latitude' | 'longitude',
): boolean {
if (!field) return false;
const type = (field.rawType || field.type || '').toLowerCase();
if (GEO_FIELD_TYPES.has(type)) return true;
const name = (field.rawName || field.name || field.key || field.value || '').toLowerCase();
if (!name) return false;
if (axis === 'latitude') {
return /(^|[_-])lat([^a-z]|itude)?($|[_-])/.test(name) || name === 'lat';
}
return /(^|[_-])(lng|lon|long)([^a-z]|gitude)?($|[_-])/.test(name)
|| name === 'lng' || name === 'lon';
}
/**
* Pick the first option whose field name matches one of the preferred
* lowercase substrings. Falls back to the first option, or undefined when
* the list is empty. Used for "smart default" auto-pick in CreateViewDialog.
*/
export function pickPreferredField(
options: Array<{ value: string; label?: string }>,
preferredNames: readonly string[],
): string | undefined {
if (options.length === 0) return undefined;
for (const pref of preferredNames) {
const found = options.find((o) => o.value.toLowerCase().includes(pref));
if (found) return found.value;
}
return options[0]?.value;
}
/** Common preferred names for kanban grouping (status-like fields). */
export const KANBAN_GROUP_PREFERRED: readonly string[] = [
'status', 'stage', 'state', 'priority', 'category', 'type',
];
/** Common preferred names for primary date fields. */
export const PRIMARY_DATE_PREFERRED: readonly string[] = [
'start_date', 'startdate', 'due_date', 'duedate',
'event_date', 'date', 'created_at', 'createdat', 'created',
];
/** Common preferred names for end date fields (gantt). */
export const END_DATE_PREFERRED: readonly string[] = [
'end_date', 'enddate', 'finish_date', 'completion_date', 'closed_at',
];
/** Common preferred names for human-readable title fields. */
export const TITLE_PREFERRED: readonly string[] = [
'name', 'title', 'subject', 'label', 'full_name', 'display_name',
];