Repository navigation
Expand file tree
/
Copy pathObjectMap.tsx
More file actions
1747 lines (1659 loc) · 84.2 KB
/
Copy pathObjectMap.tsx
File metadata and controls
1747 lines (1659 loc) · 84.2 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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* ObjectUI
* Copyright (c) 2024-present ObjectStack Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* ObjectMap Component
*
* A specialized map visualization component that works with ObjectQL data sources.
* Displays records as markers/pins on a map based on location data.
* Implements the map view type from @objectstack/spec view.zod ListView schema.
*
* Features:
* - Interactive map with markers
* - Location-based data visualization
* - Popup/tooltip on marker click
* - Works with object/value data providers
*/
import React, { useEffect, useState, useMemo, useRef, useCallback } from 'react';
import type { ObjectMapSchema, ObjectMapConfig, DataSource, ViewData } from '@object-ui/types';
import { ObjectMapConfigSchema } from '@object-ui/types/zod';
import {
useNavigationOverlay,
NonGridRowCeilingNote,
useDataInvalidation,
useSettledSchema,
useFilterScope,
useResolvedFilter,
useObjectTranslation,
useHostMapStyle,
classifyLoadError,
} from '@object-ui/react';
import { NavigationOverlay, DataEmptyState, LazyIcon, cn, useIsMobile } from '@object-ui/components';
import { usePermissions } from '@object-ui/permissions';
import { RecordDetailPanel } from '@object-ui/plugin-detail';
import {
buildExpandFields,
convertSortToQueryParams,
getRecordDisplayName,
recordDisplayValueAt,
resolveRecordSourceConfig,
resolveRecordSourceObjectName,
ValueDataSource,
applyNonGridRowCeiling,
nonGridRowCeilingQuery,
type NonGridCeilingResult,
} from '@object-ui/core';
import MapGL, { NavigationControl, Marker, Popup } from 'react-map-gl/maplibre';
import type { MapRef } from 'react-map-gl/maplibre';
import 'maplibre-gl/dist/maplibre-gl.css';
import {
computeMarkerBounds,
boundsCenter,
EMPTY_VIEW_ZOOM,
FIT_MAX_ZOOM,
FIT_PADDING_PX,
UNFITTED_CENTER_ZOOM,
} from './camera';
/**
* Public MapLibre demo style — free, unauthenticated, but not intended for
* production traffic (rate-limited, no uptime guarantee). The LAST fallback
* (objectstack#26163): used only when the view configures no style and the
* host provides no deployment default (`useHostMapStyle`, the Console's
* runtime-config `mapStyleUrl`). A load failure swaps the map for the record
* list below rather than leaving a grey canvas.
*/
const DEFAULT_MAP_STYLE = 'https://demotiles.maplibre.org/style.json';
/**
* Can this browser start a MapLibre map at all (objectui#11819)?
*
* MapLibre draws through WebGL2 and nothing else. Without it the `Map`
* constructor does not throw: `_setupPainter` emits a `GPUInitializationError`
* and the constructor returns a map with no painter. That event fires INSIDE
* the constructor, before react-map-gl attaches its listeners, so `onError`
* never hears it (MapLibre prints it to the console instead). react-map-gl then
* hands the painter-less map to the `Marker` children, whose `map.project`
* throws, and the error boundary's unmount runs `map.remove()`, which throws
* again on the missing painter — `Cannot read properties of undefined (reading
* 'destroy')`, the crash card the page used to show.
*
* So the question is asked here, before `MapGL` mounts, with the call MapLibre
* itself makes. A map that is never constructed has nothing to clean up. The
* probe's own context is released at once rather than left to the collector:
* browsers cap the live WebGL contexts a page may hold.
*
* With no DOM (a server render) there is nothing to probe and nothing would be
* constructed either — react-map-gl builds the map in an effect — so the
* answer is yes, and the browser's own render asks again.
*/
function canStartMap(): boolean {
if (typeof document === 'undefined') return true;
try {
const gl = document.createElement('canvas').getContext('webgl2');
if (!gl) return false;
gl.getExtension('WEBGL_lose_context')?.loseContext();
return true;
} catch {
return false;
}
}
export interface ObjectMapProps {
schema: ObjectMapSchema;
dataSource?: DataSource;
className?: string;
/**
* Records to render directly, bypassing this component's own fetch — the
* shape `ListView` passes when it already holds the rows. Declared as its
* own prop (not read off the `rest` spread) so the fetch effect can depend
* on this one value: naming the whole `rest` object in that effect's deps
* instead would refetch on every render, since `rest` is a fresh object
* each render (objectui#5003).
*/
data?: any[];
onMarkerClick?: (record: any) => void;
/**
* TWO parameters since objectui#9357, and the second is not decoration: this
* prop reaches `useNavigationOverlay` as its `onRowClick`, and `handleClick`
* invokes it as `onRowClick(record, event)` — the modifier payload a host
* needs to implement Cmd/Ctrl/middle-click for itself. Declaring one
* parameter hid the second on the ONE line a host reads. Spelled `any` and
* not `HandleClickModifiers` for the reason objectui#9341 measured on
* `ObjectKanbanSchema.onCardClick`: that interface lives in
* `@object-ui/react`, the published twins in `@object-ui/types` may not name
* it, and a host that discovered the payload from the implementation
* annotated it `React.MouseEvent` — which a narrower declaration refuses
* contravariantly. `BaseSchema`'s own `onClick` / `onChange` / `onSubmit`
* already use this spelling for exactly this situation.
*/
onRowClick?: (record: any, event?: any) => void;
onEdit?: (record: any) => void;
onDelete?: (record: any) => void;
/** Enable marker clustering for dense data */
enableClustering?: boolean;
/**
* Clustering grid granularity (default: 50). NOT screen pixels — the grid
* cell edge is `clusterRadius / 2 ** zoom`, in coordinate DEGREES (the
* marker's `[lng, lat]`), so a larger value groups more aggressively and
* the effective granularity shrinks exponentially as zoom increases. See
* `clusterMarkers` below.
*/
clusterRadius?: number;
}
/**
* The FLAT spelling of `ObjectMapConfig`'s keys, as ObjectView / ListView emit it.
*
* Both flatteners build an `object-map` schema by spreading `options.map`'s
* CONTENTS at the top level (`plugin-view/src/ObjectView.tsx` `case 'map'`,
* `plugin-list/src/ListView.tsx` `case 'map'`) — the product carries these keys
* and NO `map` key at all. That is an internal transport form, not an authoring
* surface, so it is declared here in the consumer rather than in
* `ObjectMapSchema` (maintainer ruling on objectui#5018, 2026-08-17).
*
* `locationField` / `titleField` are the two that `ObjectMapSchema` still
* publishes as well, from before the ruling; the rest exist only here.
*/
type FlatMapConfigKeys = Omit< ObjectMapConfig, 'style' >;
/** What `getMapConfig` may read: the declared schema plus the internal flat form. */
type MapConfigSource = ObjectMapSchema & FlatMapConfigKeys;
/**
* The flat keys, DERIVED from `ObjectMapConfigSchema` — the same zod object
* `getMapConfig` validates the `map` block against — rather than hand-listed,
* so a key added to (or removed from) the declaration reaches the shadow
* diagnostic below without a second edit (objectui#5177). `style` is excluded
* because the FLAT form is where its namespace collides with `BaseSchema.style`
* (see `FlatMapConfigKeys` above and `warnOnTopLevelStyleUrl`) — not because
* the declaration omits it; `ObjectMapConfigSchema` carries `style` too.
*
* `ObjectView` / `ListView` derive their own flatten whitelist from this exact
* schema (imported from `@object-ui/types/zod`, already a dependency of both —
* no new coupling to this package's heavier `react-map-gl` / `maplibre-gl`
* dependencies), so all three sites read one canonical set of keys.
*/
const FLAT_MAP_CONFIG_KEYS = (Object.keys(ObjectMapConfigSchema.shape) as (keyof ObjectMapConfig)[]).filter(
(key): key is keyof FlatMapConfigKeys => key !== 'style',
);
/**
* Helper to get data configuration from schema.
*
* The ruled three-rung ladder itself (`data`, then `staticData`, then
* `objectName`) is `resolveRecordSourceConfig` in `@object-ui/core` — ONE
* implementation of a contract published on both faces (`77cb489b4`), which
* this file used to hand-copy (`ce2aaefe1`).
*
* What used to stay here was the head above it: the array shorthand, which
* lifted `data: [...]` to `{ provider: 'value', items }`.
* ⛔ IT IS GONE (objectui#8348, decision batch #83, maintainer verbatim
* 「8348 以协议为准」 — the contract decides). Its old justification was that the
* shorthand is "a deliberate, commented convention across this block family",
* i.e. that the other blocks accept it too. The ruling replaces that argument
* with the row: a renderer honours the `data` spelling its block's PUBLISHED row
* declares and no other.
*
* The row is `ComponentPropsMap['object-map'].data` in the INSTALLED
* `@objectstack/spec` — the `ViewData` union over OBJECT variants — which
* ruling batch #136 item 3 (Q1-C) had the protocol gain; until it was
* installable this block was judged through this repo's own
* `ObjectMapSchema.data`, spelled the same. ⛔ Not restated here as a fact
* about a version: `ObjectMap.dataArmSpecRow-8348.test.tsx` derives the arm
* from the installed row and turns red if a release moves it. A bare array is
* off that row, and this block's registration declares `data` on the same
* OBJECT arm (`type: 'object'`, objectui#10394), so it says the same.
*
* ⛔ WHAT THIS REACHES, per CARRIER. The authored key used to have a second
* carrier: `SchemaRenderer` spread every non-metadata node key as a React prop,
* so an authored `data` array also arrived as the `data` prop, which outranks
* the schema (objectui#5003 order). objectui#9571 stopped that spread for
* object-arm blocks, so through `SchemaRenderer` the array no longer draws
* either; a HOST's own `data` prop is untouched. Both halves are pinned in
* `ObjectMap.schemaDataShorthand.test.tsx`.
*
* The declared spellings for inline rows are
* `data: { provider: 'value', items: [...] }` and `staticData: [...]`, both
* unchanged.
*/
function getDataConfig(schema: ObjectMapSchema): ViewData | null {
return resolveRecordSourceConfig(schema, 'view-data');
}
const isDev = (): boolean =>
(globalThis as { process?: { env?: Record<string, string | undefined> } }).process?.env
?.NODE_ENV !== 'production';
/**
* Warn once per distinct legacy stash, not once per evaluation: `getMapConfig`
* runs on every render of the map (hover, zoom, search all re-render it), and a
* warning that floods the console is a warning that gets muted. Mirrors the
* warn-once discipline of the visibility-predicate diagnostics in
* `app-shell/src/views/metadata-admin/predicate.ts` (objectui#4049).
*/
const warnedLegacyFilterMapConfigs = new Set<string>();
/**
* `filter` is the query filter, not a configuration slot — objectui#4034
* (source thread objectstack#7138).
*
* `getMapConfig` used to ALSO read the map's configuration out of
* `schema.filter.map` (and its `style` half), a shape predating the declared
* `{ name: 'map', type: 'object' }` input. That read is gone: the block
* consumes only what it declares. Authoring the config under `filter.map`
* therefore has no effect, so say so in dev rather than dropping the author's
* markers without a trace. Since objectui#8169 there are no default field names
* left to fall back on, so such a map renders the refusal state below — which
* names the declaration that is missing, but cannot know about the stash this
* author actually wrote. This warning is the only thing that can.
*
* Deliberately narrow, to stay off legitimate query filters:
* - OWN property only. `'map' in someArray` is TRUE via `Array.prototype.map`,
* which is precisely how the deleted probe misfired on every array-shaped
* filter — including the `and` node a dataSource binding merges
* (objectstack#7121). An inherited method is not an authoring mistake.
* - object-valued only. `filter: { map: 'x' }` reads as a filter on a field
* named `map`, not as a stashed MapConfig.
*/
function warnOnLegacyFilterMapConfig(schema: MapConfigSource): void {
if (!isDev()) return;
// `filter` is declared as a JSON-Rules array; the legacy stash this probe
// exists for is an OBJECT, which is why the shape is re-tested at runtime
// rather than trusted from the declaration.
const filter: unknown = schema.filter;
if (!filter || typeof filter !== 'object' || Array.isArray(filter)) return;
if (!Object.prototype.hasOwnProperty.call(filter, 'map')) return;
const legacy = (filter as Record<string, unknown>).map;
if (!legacy || typeof legacy !== 'object') return;
let memo: string;
try {
memo = `${schema.type ?? 'map'}::${schema.objectName ?? ''}::${JSON.stringify(legacy)}`;
} catch {
// Circular author data — still worth one warning, just not a keyed one.
memo = `${schema.type ?? 'map'}::${schema.objectName ?? ''}::<unserializable>`;
}
if (warnedLegacyFilterMapConfigs.has(memo)) return;
warnedLegacyFilterMapConfigs.add(memo);
console.warn(
'[ObjectMap] `filter.map` is no longer read as map configuration, so this map has NO ' +
'coordinate binding and renders the "Map configuration required" refusal ' +
'(objectui#8169 — the `latitude` / `longitude` / `location` defaults are gone). `filter` is ' +
'the query filter; the map config belongs under the declared `map` input — move it to ' +
'`schema.map` (`{ type: \'object-map\', map: { latitudeField, longitudeField, titleField } }`). ' +
'The old spelling was never documented and could not survive a `dataSource` binding, whose ' +
'merged filter is an `and` node with no `map` key at all (objectstack#7121). If `map` is ' +
'genuinely a field you are filtering on, ignore this — the filter itself is passed through ' +
'untouched. objectui#4034.',
);
}
/**
* Warn once per distinct dropped URL, for the same reason the diagnostics
* around it warn once: this runs on every render of the map.
*/
const warnedTopLevelStyleUrls = new Set<string>();
/**
* Top-level `style` is `BaseSchema.style` — inline CSS — and is NOT a map style
* (objectui#5017).
*
* `getMapConfig` used to read it FIRST, ahead of both declared spellings, so a
* map node carrying the base face's own `style: { height: '400px' }` handed that
* object to MapGL's `mapStyle` prop: no validation, no diagnostic, and a
* collision `@object-ui/types` had already gone out of its way to avoid — it
* named the key `mapStyle` (not `style`) for exactly this reason
* (`objectql.ts`, `ObjectMapSchema.mapStyle`). The consumer contradicted the
* declaration it was named after; that read is gone.
*
* The OBJECT form needs no diagnostic: it is legal base-face authoring, and
* dropping it is the fix. The STRING form does, because it is the one shape
* that used to work:
* - It is the shape a view author's `map: { style: '<url>' }` USED to arrive
* as: both flatteners spread the CONTENTS of `options.map` at the top level,
* so `style` crossed into this key's namespace. ⛔ NOT ANY MORE — since
* objectui#9950 each declared key has a flat SPELLING and `style` is carried
* out as `mapStyle`, so the declared block now arrives and is read, and
* neither producer of an `object-map` node (`ObjectView` / `ListView`, the
* only two) emits a top-level `style` at all. What still reaches here is a
* node authored — or host-composed — with a STRING `style` written directly
* on it: not spec-authorable (`@objectstack/spec`'s list-view schemas are
* strict and declare no `map` block at all), but runtime-reachable, so say
* what happened instead of silently painting the demo tiles.
* - A string is not valid `BaseSchema.style` either (that is a record), so a
* string here is unambiguously "the author meant a map style" — there is no
* legitimate CSS reading to mistake it for.
*
* ⇒ THE REMEDY BELOW MUST NAME A SPELLING THE RUNTIME READS (objectui#10002,
* the rule objectui#9031 set for a sibling sink): this text is read at the
* moment the author is already being corrected. It used to end "spell it
* `map: { mapStyle }` there" — a key `ObjectMapConfigSchema` does not
* declare, so no flatten whitelist carries it and `getMapConfig` never reads
* it; obeying the correction produced a SECOND silent discard. The remedy is
* not defended by a string assertion (that is how it rotted). Every key it
* prescribes inside a `map` block is parsed out of the warning this function
* really emits and then measured end to end — through the declared block and
* through the real `ListView` flatten — in
* `ObjectMap.styleRemedyText-10002.test.tsx`.
*/
function warnOnTopLevelStyleUrl(schema: MapConfigSource): void {
if (!isDev()) return;
// Re-tested at runtime rather than trusted from the declaration: the
// declaration says this key is a CSS record, and the shape this probe exists
// for is precisely the one that violates it.
const raw: unknown = schema.style;
if (typeof raw !== 'string' || raw === '') return;
const memo = `${schema.type ?? 'map'}::${schema.objectName ?? ''}::${raw}`;
if (warnedTopLevelStyleUrls.has(memo)) return;
warnedTopLevelStyleUrls.add(memo);
console.warn(
'[ObjectMap] A top-level `style` is NOT read as a map style, so this map is rendering with ' +
`the DEFAULT public demo tiles and \`${raw}\` was dropped. \`style\` is the base schema's ` +
'INLINE CSS key (a record of CSS properties), which every node may carry; the map style is ' +
'`mapStyle` — named that way precisely to avoid this collision. Write `mapStyle: ' +
`'${raw}'\` at the top level, or \`map: { style: '${raw}' }\` in the declared config block. ` +
'On a view, write that same declared `map: { style: ... }`: ObjectView / ListView flatten ' +
'the view\'s `map` block through a whitelist of the keys `ObjectMapConfigSchema` declares, and that ' +
'whitelist carries `style` out under the flat spelling `mapStyle` (objectui#9950), so the ' +
'declared block arrives and is read. `mapStyle` is the TOP-LEVEL spelling only — inside ' +
'the `map` block it is undeclared, and the flatten drops it. objectui#5017.',
);
}
/**
* Warn once per distinct shadowing, for the same reason `getMapConfig` warns
* once per legacy stash: this runs on every render of the map.
*/
const warnedShadowedFlatKeys = new Set<string>();
/**
* The `map` block won and the internal flat keys alongside it were ignored —
* say which ones, in dev.
*
* Silence here is what the precedence rule costs if it is not diagnosed: two
* spellings of the same configuration in one schema, one of them inert. The
* ruling picks the author's block over the flatten product deliberately
* (maintainer, objectui#5018, 2026-08-17), so the diagnostic names what was
* dropped rather than leaving the author to discover it from the markers.
*
* It cannot fire on the ordinary ObjectView / ListView path: both flatteners
* emit the flat keys and NO `map` key, so this branch is not even reached for
* their output. Reaching it means one schema carries both spellings.
*/
function warnOnShadowedFlatMapKeys(schema: MapConfigSource): void {
if (!isDev()) return;
// `key` is a `FlatMapConfigKeys` key and `MapConfigSource` carries those, so
// the read is typed as it stands; no conversion is needed (objectui#11355).
const shadowed = FLAT_MAP_CONFIG_KEYS.filter((key) => schema[key] !== undefined);
if (shadowed.length === 0) return;
const memo = `${schema.type ?? 'map'}::${schema.objectName ?? ''}::${shadowed.join(',')}`;
if (warnedShadowedFlatKeys.has(memo)) return;
warnedShadowedFlatKeys.add(memo);
console.warn(
`[ObjectMap] The \`map\` block configures this map, so these top-level keys are ` +
`IGNORED: ${shadowed.map((k) => `\`${k}\``).join(', ')}. The \`map\` block is the ` +
'authoring shape; the flat top-level spelling is the internal form ObjectView/ListView ' +
'produce when they flatten the view\'s `map` block, and what the author wrote outranks it. Move ' +
'anything you still need into `map`, or drop the `map` block. objectui#5018.',
);
}
/**
* Helper to get map configuration from schema
*
* PRECEDENCE (maintainer ruling on objectui#5018, 2026-08-17): the declared
* `map` block is checked FIRST and wins outright; the flat top-level spelling
* is consulted only when no `map` block is present. This is the reverse of the
* pre-#5018 order, which let the internal flatten product silently shadow an
* authored block. Safe for the producer path because neither flattener emits a
* `map` key at all — see `FlatMapConfigKeys`.
*/
function getMapConfig(schema: MapConfigSource): ObjectMapConfig {
warnOnLegacyFilterMapConfig(schema);
warnOnTopLevelStyleUrl(schema);
// A custom style may be set alongside any of the shapes below — read it
// once so every return path can carry it through.
//
// The two DECLARED spellings, and only those: `mapStyle` on the schema
// (`ObjectMapSchema.mapStyle`) and `style` inside the declared config block
// (`ObjectMapConfig.style`). The top-level `style` this used to read first is
// `BaseSchema.style` — inline CSS, a different key with a different meaning —
// and is no longer consumed here at all (objectui#5017; see
// `warnOnTopLevelStyleUrl`).
//
// PRECEDENCE: `mapStyle` first, then `map.style` — on EVERY return path,
// including the declared block's below (objectui#11168 slice 3, the seat's
// ruling A). The installed `@objectstack/spec` row says so in `mapStyle`'s own
// describe ("Read before `map.style`"), and this lane follows the spec. The
// block path used to hand `config.style` back first, so a map writing both
// drew `map.style`; no authored producer writes both.
const style: string | undefined = schema.mapStyle || schema.map?.style;
// 1. The declared configuration input: `{ name: 'map', type: 'object' }` at
// the `object-map` / `map` registrations. The author face, and the winner
// whenever it is present.
const config: ObjectMapConfig | null = schema.map ?? null;
if (config) {
const result = ObjectMapConfigSchema.safeParse(config);
if (!result.success) {
console.warn(`[ObjectMap] Invalid map configuration:`, result.error.format());
}
warnOnShadowedFlatMapKeys(schema);
return { ...config, style };
}
// 2. The internal flat form — the ObjectView / ListView flatten product.
if (schema.locationField || schema.latitudeField) {
return {
locationField: schema.locationField,
latitudeField: schema.latitudeField,
longitudeField: schema.longitudeField,
// No `|| 'name'` (objectui#5953). An ABSENT binding must stay absent so
// the read site can hand the decision to `getRecordDisplayName`; the
// literal used to forge a binding the author never wrote, and a forged
// `'name'` outranks the object's own declared `nameField` at step 0 of
// that resolver. A binding the flatten product really carries survives
// here and still wins.
titleField: schema.titleField,
descriptionField: schema.descriptionField,
zoom: schema.zoom,
center: schema.center,
style,
};
}
// 3. NOTHING IS GUESSED (objectui#8169 — maintainer ruling 2026-09-07
// 「同意」, decision batch #67, option B).
//
// This branch used to return four field-name guesses — `latitude`,
// `longitude`, `location`, `description` — carried over from before the
// declared `map` input existed. objectui#5953 deleted the `titleField` guess
// from this very branch and deliberately KEPT the coordinate ones, on the
// reasoning that "nothing else can read a location out of an unconfigured
// record". The premise held; the ruling reverses its conclusion — that is
// exactly why the answer is a REFUSAL rather than a guess. Bindings are
// never fabricated; an unbound surface refuses. The same principle as
// 「日期轴永不虚构」 behind the 2026-09-01 ruling (date axes, 总监批 #28) and
// objectui#8168 (the chart category axis), generalised one field over.
//
// What the guesses actually shipped was the silent-credible-wrong shape
// objectstack#13748 ruled against: a record set that happens to spell its
// columns `latitude` / `longitude` plotted on a view that declared no map
// binding at all, while the same view over any other spelling rendered an
// empty map. Neither outcome says what is missing. `hasCoordinateBinding`
// below now sends both to the refusal state, which does.
//
// No camera is synthesized here either, and never was (objectui#4941): a
// fabricated `zoom` / `center` is indistinguishable from a declared one at
// the read site, and the old defaults (zoom 10 at the origin) SUPPRESSED the
// fit for exactly the views that need it most. `style` still travels,
// because it is read from a DECLARED spelling above (`mapStyle` or
// `map.style`) and forges nothing.
return { style };
}
/**
* Is this configuration BOUND to coordinates at all?
*
* The refusal gate for objectui#8169. Deliberately the same two reads
* `extractCoordinates` performs, in the same order, so the question "will any
* record ever place a marker" is answered once rather than per record:
* a `locationField`, or a COMPLETE `latitudeField` + `longitudeField` pair.
* A half pair is not a binding — `extractCoordinates` skips its lat/lng arm
* unless both are present — so it refuses, which is also what the refusal
* message tells the author to write.
*
* ⚠️ Keep this in lockstep with `extractCoordinates`: a key that becomes
* placeable there and is not named here renders a refusal over records that
* would have plotted.
*
* Applies to EVERY branch above, not only the unconfigured one. A declared
* `map` block that names no coordinate field (`map: { titleField: 'name' }`)
* is just as unbound as an absent one, and used to render an empty map under
* the excluded-records notice.
*/
function hasCoordinateBinding(config: ObjectMapConfig): boolean {
return Boolean(config.locationField) || Boolean(config.latitudeField && config.longitudeField);
}
/**
* Extract coordinates from a record based on configuration
*/
function extractCoordinates(record: any, config: ObjectMapConfig): [number, number] | null {
// Try latitude/longitude fields
if (config.latitudeField && config.longitudeField) {
const lat = record[config.latitudeField];
const lng = record[config.longitudeField];
if (typeof lat === 'number' && typeof lng === 'number') {
return [lat, lng];
}
}
// Try location field
if (config.locationField) {
const location = record[config.locationField];
// Handle object format: { lat: number, lng: number }
if (typeof location === 'object' && location !== null) {
const lat = location.lat || location.latitude;
const lng = location.lng || location.lon || location.longitude;
if (typeof lat === 'number' && typeof lng === 'number') {
return [lat, lng];
}
}
// Handle string format: "lat,lng"
if (typeof location === 'string') {
const parts = location.split(',').map(s => parseFloat(s.trim()));
if (parts.length === 2 && !isNaN(parts[0]) && !isNaN(parts[1])) {
return [parts[0], parts[1]];
}
}
// Handle array format: [lat, lng]
if (Array.isArray(location) && location.length === 2) {
const lat = parseFloat(location[0]);
const lng = parseFloat(location[1]);
if (!isNaN(lat) && !isNaN(lng)) {
return [lat, lng];
}
}
}
return null;
}
interface MarkerData {
id: string;
title: string;
description?: string;
coordinates: [number, number];
data: any;
}
interface ClusterData {
id: string;
coordinates: [number, number];
markers: MarkerData[];
isCluster: boolean;
}
/**
* Simple grid-based marker clustering.
* Groups markers that are close to each other at a given zoom level.
*/
function clusterMarkers(markers: MarkerData[], zoom: number, radius: number = 50): ClusterData[] {
if (markers.length <= 1 || zoom >= 15) {
return markers.map(m => ({
id: m.id,
coordinates: m.coordinates,
markers: [m],
isCluster: false,
}));
}
// Grid cell size based on zoom (larger cells at lower zoom)
const cellSize = radius / Math.pow(2, zoom);
const grid = new Map<string, MarkerData[]>();
markers.forEach(marker => {
const cellX = Math.floor(marker.coordinates[0] / cellSize);
const cellY = Math.floor(marker.coordinates[1] / cellSize);
const key = `${cellX}:${cellY}`;
if (!grid.has(key)) grid.set(key, []);
grid.get(key)!.push(marker);
});
const clusters: ClusterData[] = [];
grid.forEach((group, key) => {
if (group.length === 1) {
clusters.push({
id: group[0].id,
coordinates: group[0].coordinates,
markers: group,
isCluster: false,
});
} else {
// Compute centroid
const avgLng = group.reduce((sum, m) => sum + m.coordinates[0], 0) / group.length;
const avgLat = group.reduce((sum, m) => sum + m.coordinates[1], 0) / group.length;
clusters.push({
id: `cluster-${key}`,
coordinates: [avgLng, avgLat],
markers: group,
isCluster: true,
});
}
});
return clusters;
}
export const ObjectMap: React.FC<ObjectMapProps> = ({
schema,
dataSource,
className,
data: dataProp,
onMarkerClick,
onRowClick,
onEdit,
onDelete,
enableClustering,
clusterRadius = 50,
}) => {
const [data, setData] = useState<any[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<Error | null>(null);
/**
* Did the platform row ceiling bite, and how large was the whole filtered
* result set (objectui#7210)? Carried from the response that knew it —
* `data.length === NON_GRID_ROW_CEILING` cannot tell a capped result set
* apart from one that is exactly that size.
*
* ⚠️ The exempt path is the HOST `data` prop and only it — rows a host
* component handed down are not ours to cap, and we issued no query whose
* total a footnote could name. An inline `value` set IS capped
* (objectui#9061, porting objectui#8769): it goes through the same adapter
* query as every other provider, so the ceiling arrives with the same `$top`
* and the same footnote. This docblock used to say both paths were exempt.
*/
const [rowCeiling, setRowCeiling] = useState<NonGridCeilingResult | null>(null);
const [selectedMarkerId, setSelectedMarkerId] = useState<string | null>(null);
const [searchQuery, setSearchQuery] = useState('');
// Mobile UX (round 3)
const isMobile = useIsMobile();
const mapRef = useRef<MapRef | null>(null);
const [userLocation, setUserLocation] = useState<{ lng: number; lat: number } | null>(null);
const [geoError, setGeoError] = useState<string | null>(null);
const [geoBusy, setGeoBusy] = useState(false);
const { t } = useObjectTranslation();
// Style/tile load failures (bad network, unreachable style server, a tile
// host the deployment's network blocks) don't throw from react-map-gl — they
// emit an `error` event. The map left behind is a grey canvas: the markers
// float over nothing, so the view swaps it for the same record list a
// browser without WebGL2 gets (objectstack#25889). Holds MapLibre's own
// reason, `''` when it gave none; `null` while the map is healthy. Kept
// across refetches — the same style would fail again — until the user asks
// for the map back.
const [mapStyleError, setMapStyleError] = useState<string | null>(null);
const handleMapError = useCallback((e: { error?: Error & { status?: number } }) => {
setMapStyleError(e?.error?.message ?? '');
}, []);
const retryMap = useCallback(() => setMapStyleError(null), []);
// Asked once per mount, before `MapGL` is ever mounted: a browser without
// WebGL2 gets the record list instead of a map (see `canStartMap`). There is
// no retry for that one — the browser will not grow WebGL2 by asking again.
const [mapCanStart] = useState(canStartMap);
const showRecordList = !mapCanStart || mapStyleError !== null;
const requestUserLocation = useCallback(() => {
if (typeof navigator === 'undefined' || !navigator.geolocation) {
setGeoError('Geolocation is not available in this browser.');
return;
}
setGeoBusy(true);
setGeoError(null);
navigator.geolocation.getCurrentPosition(
(pos) => {
const { longitude, latitude } = pos.coords;
setUserLocation({ lng: longitude, lat: latitude });
setGeoBusy(false);
try {
mapRef.current?.flyTo({ center: [longitude, latitude], zoom: 12, duration: 800 });
} catch {
/* mapRef may be null in some test envs */
}
},
(err) => {
setGeoBusy(false);
setGeoError(err.message || 'Unable to retrieve location.');
},
{ enableHighAccuracy: false, timeout: 8000, maximumAge: 60_000 },
);
}, []);
/**
* Memoized on the ONE value `getDataConfig` reads, and nothing else
* (objectui#6018) — the same shape `mapConfig` below landed for
* objectui#5976, now that the two lines agree.
*
* `dataConfig` is a dependency of the fetch effect, and that effect calls
* `setData`, so a fresh identity here is a refetch loop rather than mere
* waste. That hazard is real and is what the previous form's "prevent
* infinite loops" comment recorded. What has changed is the PRICE of the
* guard, not the guard: identity was bought by calling `getDataConfig` bare
* in the render body and re-serializing the result with `JSON.stringify` on
* EVERY render, only to hand back the object the memo already held.
*
* `[schema]` is the whole dependency, not a shorthand for one: `getDataConfig`
* is a pure function of `schema` and reads exactly three keys off it
* (`data`, `staticData`, `objectName`), nothing ambient. So no deep-compare
* key is needed to make the identity hold — the identity that reaches this
* component is already stable across the renders that matter. Every
* re-render `ObjectMap` causes ITSELF (data landing, the object definition
* landing, search typing, zoom, selection, geolocation) leaves the prop
* untouched by construction, which is precisely the loop path; and the three
* callers upstream each hand over a memoized node: `SchemaRenderer`'s
* `evaluatedSchema`, the gate's `mapped` in `useElementDataSourceSchema`,
* and `ListView`'s `viewComponentSchema`.
*
* Dropping the serialize is also a correctness move, not only a cost one.
* `JSON.stringify` is not a total function — it THROWS on a value it cannot
* serialize — and the passthrough branch of `getDataConfig` returns the
* author's own `schema.data` object verbatim, inline `value` rows included.
* So a record graph carrying a back-reference (an `$expand`-ed lookup handed
* to the block as inline data) or a `BigInt` id took the whole map subtree
* down from the render body. Comparing identities never serializes, so the
* config no longer has to be serializable at all.
* `ObjectMap.dataConfigMemo.test.tsx` pins both halves.
*/
const dataConfig = useMemo(() => getDataConfig(schema), [schema]);
/**
* Memoized on the ONE value `getMapConfig` reads, and nothing else
* (objectui#5976). This call used to sit bare in the render body, so
* `mapConfig` carried a fresh object identity on every render — and the
* marker transform below names it in its dependency array, so that `useMemo`
* recomputed on every single render while declaring that it does not. The
* invalidation cascaded from there: `filteredMarkers` → `clusteredData` /
* `markerBounds` → `initialViewState` all key on the array it produces.
*
* `[schema]` is the whole dependency, not a shorthand for one: `getMapConfig`
* is a pure function of `schema` and reads nothing else. No deep-compare key
* is needed to make that identity hold, because the identity that reaches
* this component is ALREADY stable across the renders that matter — every
* re-render `ObjectMap` causes itself (data landing, the object definition
* landing, search typing, zoom, selection, geolocation) leaves the prop
* untouched by construction, and the three callers upstream each hand over a
* memoized node: `SchemaRenderer`'s `evaluatedSchema`, the gate's `mapped`
* in `useElementDataSourceSchema`, and `ListView`'s `viewComponentSchema`.
*
* This deliberately did NOT copy the `JSON.stringify` dep key the
* `dataConfig` line above used to carry, and as of objectui#6018 that line
* no longer carries it either — both are keyed on `[schema]` now. The
* serialize idiom bought stability by paying a full serialize on every
* render; it is also key-order sensitive and drops `undefined` values — an
* equality THIS config in particular cannot afford, since an ABSENT
* `titleField` is load-bearing here (objectui#5953) and must never compare
* equal to a present one. Identity is enough; nothing here needs value
* equality.
*/
const mapConfig = useMemo(() => getMapConfig(schema), [schema]);
// The style the map draws, and whose it is (objectstack#26163): the view's
// own (`mapStyle`, then `map.style`, already folded into `mapConfig.style`),
// then the deployment's default the host provides (the operator's
// `OS_CONSOLE_MAP_STYLE_URL`, served as runtime-config `mapStyleUrl`), then
// the public demo style. The owner picks the failure notice's remedy below.
const hostMapStyle = useHostMapStyle();
const mapStyleOwner: 'view' | 'deployment' | 'default' = mapConfig.style
? 'view'
: hostMapStyle
? 'deployment'
: 'default';
const drawnMapStyle = mapConfig.style || hostMapStyle || DEFAULT_MAP_STYLE;
const hasInlineData = dataConfig?.provider === 'value';
/**
* The two fetch effects below used to key on `dataConfig` itself — the
* whole memoised object identity. `useMemo` carries no semantic
* guarantee: React is permitted to discard its cache and recompute, and
* `getDataConfig(schema)` builds a fresh `{ provider, object }` /
* `{ provider, items }` wrapper object on every call even when `schema`
* itself hasn't changed. So a discard (not just a `schema` change) was
* enough to re-run both effects and refetch. These three are every
* primitive field either effect actually reads off `dataConfig`; keying
* on them instead of the container object makes a cache discard a no-op
* for both effects, and returns `useMemo` here to being a pure
* optimisation rather than a correctness dependency (objectui#6592).
*/
const dataProvider = dataConfig?.provider;
// NOT a delegation site for `resolveRecordSourceObjectName` (`b041b9c0c`):
// this is the data config's OWN object, deliberately `undefined` for every
// other provider so a `value` map's `objectName` changing cannot move
// this dependency. The shared reader's second rung would put `objectName`
// here and re-run both effects on a value they do not read.
const dataObjectName = dataConfig?.provider === 'object' ? dataConfig.object : undefined;
const dataItems = dataConfig?.provider === 'value' ? dataConfig.items : undefined;
/**
* The object this map is BOUND to — the resolved record source's object when
* it names one, else the schema's own `objectName` (`b041b9c0c`, the
* ladder `77cb489b4` declared). Hoisted to render scope so the definition read below
* keys on one named value instead of re-deriving the ladder inline.
*/
const recordSourceObjectName = resolveRecordSourceObjectName(schema, dataConfig);
/**
* The object definition, and whether the read for THIS object has SETTLED:
* one piece of state, through the shared hook (objectui#10664).
*
* The map held the definition in a local `useState` fed by its own metadata
* effect, and listed it in the fetch effect's dependencies below. The
* definition lands after the first query, so every mount read twice: once
* with `buildExpandFields` seeing no fields (no `$expand`), once after. It is
* the shape `ObjectTimeline` (objectui#7895) and `ObjectGallery`
* (objectui#7903) left, and the gate below is theirs.
*
* The key is `recordSourceObjectName`, the object the replaced effect read. On
* the branch the gate holds (`dataProvider === 'object'`) it names the same
* object the query does, `dataObjectName`.
*
* An inline `value` set passes no source, as `ObjectCalendar` does: it has no
* definition to read, and the hook settles with none at once. Every other
* path keeps the read, host rows included, because the marker titles resolve
* from it (ADR-0079).
*
* ⚠️ The gate is only safe because the hook SETTLES ON EVERY EXIT
* (objectui#7232): no source, no `getObjectSchema`, no key, and a read that
* threw. The replaced effect returned without settling on all four.
*/
const { ready: objectSchemaReady, def: objectSchema } = useSettledSchema<any>(
recordSourceObjectName ?? '',
hasInlineData ? undefined : dataSource,
);
// Permissions context, read here rather than inside the fetch effect below:
// an effect's DEPENDENCY ARRAY is evaluated during render, so `perms` has to
// be a binding that already exists by the time this component's render
// reaches that effect (objectui#7429, same structural note PR #7229 /
// PR #7428 recorded for `ListView`'s memo and `ObjectCalendar`'s effect).
const perms = usePermissions();
/**
* objectui#10623 — the data-invalidation bus (`notifyDataChanged` from
* `@object-ui/react`), read the objectui#10494 way: the nonce moves when a
* write to the object this map QUERIES is declared, and the fetch effect
* below names it, so the markers are re-read. Subscribed only on the
* `object` provider without host rows: a host `data` array is the host's to
* refresh, and an inline `value` set queries its own array, not an object.
*
* ⭐ A re-read of the SAME query is SILENT, in `ObjectGantt`'s sense
* (objectui#7237 / objectui#10035): it does not flip `loading`, and a failure
* keeps the last good rows instead of reporting. The `loading` gate further
* down unmounts `MapGL` for the duration of a fetch. That is the right answer
* when the QUERY changed (the one-shot camera then re-fits the new record
* set) and the wrong one for a write to the same query, because it would throw
* away the camera the user has panned and zoomed: the loss a remount causes,
* one level down (AGENTS.md #8's corollary: refresh data, don't rebuild UI).
* The same goes for a failure: the error screen is also an early return
* above `MapGL`.
*
* A run is silent only when BOTH hold:
* - the nonce moved since the effect's last run (the bus asked), and
* - its query inputs are, value for value, the ones whose rows were last
* COMMITTED, so the rows on screen already answer this query.
* A query change that lands in the same commit as a bus event therefore
* still goes through the gate and re-fits. So does a bus event that lands
* before the first rows, or after a changed query failed: nothing on screen
* answers that query yet, so its failure must be reported.
*/
const fetchesForItself = !Array.isArray(dataProp) && dataProvider === 'object';
const invalidationNonce = useDataInvalidation(fetchesForItself ? dataObjectName : undefined);
const answeredInvalidationRef = useRef(invalidationNonce);
/** The query inputs of the run whose rows were last committed. */
const committedQueryRef = useRef<readonly unknown[] | null>(null);
/**
* Only the NEWEST run may commit rows, an error or the loading flag
* (`ObjectGantt`'s `reloadSeqRef`). The cleanup below advances it too, so a
* run superseded by a re-run, or outliving an unmount, commits nothing.
*/
const fetchSeqRef = useRef(0);
// objectui#10666 — the node's own `filter`, with every context token
// (`{current_user_id}`, `{current_org_id}`, the date macros) resolved ONCE
// through `@object-ui/core`'s shared `resolveFilterPlaceholders`, against the
// session scope the host provides, and HELD by structure (`useResolvedFilter`
// in `@object-ui/react`). A directly authored map sent the literal token on
// `$filter` before. Both query paths below (the `object` fetch and the inline
// `ValueDataSource`), the committed-query key and the effect's dependency
// list read THIS, never the raw `schema.filter`, so a re-render that
// rebuilds an equal filter does not re-query.
const filterScope = useFilterScope();
const queryFilter = useResolvedFilter(schema.filter, filterScope);
// Fetch data based on provider
useEffect(() => {
// ⭐ objectui#10664: the object definition GATES the object query; it does
// not refine it afterwards. `objectSchema` stays in the dependency list and
// the two are one mechanism: the dependency re-runs this effect when the
// definition lands, and this line stops the first run from spending a query
// before it has. Removing either half restores the double read.
//
// Scoped to the branch that issues that query. Host rows and an inline
// `value` set build no expansion, so there is nothing for them to wait on.
//
// The gate closes only when the key moves, and on this branch the key is
// the queried object, so a closed gate always means a CHANGED query. The
// placeholder is held for the window: the rows in state answer the previous
// object, and the query this run would have issued goes through the loading
// gate anyway.
if (fetchesForItself && !objectSchemaReady) {
setLoading(true);
return;
}
// Every input of the query below: this effect's dependency list, less the
// nonce, the readiness flag and `fetchesForItself` (a function of two
// members already in it). Compared value for value (`Object.is`), the way
// React compares the list itself.
const query = [dataProp, dataProvider, dataObjectName, dataItems, dataSource, hasInlineData, queryFilter, schema.sort, objectSchema, perms] as const;
const committed = committedQueryRef.current;
const answersShownQuery = committed !== null && committed.length === query.length && query.every((v, i) => Object.is(v, committed[i]));
const nonceMoved = invalidationNonce !== answeredInvalidationRef.current;
answeredInvalidationRef.current = invalidationNonce;
const silent = nonceMoved && answersShownQuery;
const seq = ++fetchSeqRef.current;
const isCurrent = () => fetchSeqRef.current === seq;
// The one place a run commits rows. Committed rows answer this run's query,
// so no earlier failure describes the screen any more: the error is cleared
// HERE (objectui#10578's rule on `ObjectGantt`), never when a run starts.
const commit = (rows: unknown[], ceiling: NonGridCeilingResult | null) => {
if (!isCurrent()) return;
setData(rows);
setRowCeiling(ceiling);
setError(null);
committedQueryRef.current = query;
};
const fetchData = async () => {
try {
if (!silent) setLoading(true);
// Prioritize data passed via props (from ListView). `dataProp` is a
// declared prop (not the `rest` spread), so it can sit in this
// effect's dependency array below without turning into a
// refetch-every-render trap (objectui#5003).
if (Array.isArray(dataProp)) {
commit(dataProp, null);
return;
}
if (hasInlineData && dataProvider === 'value') {
// THE INLINE PROVIDER NO LONGER EXITS BEFORE THE QUERY
// (objectui#9061, porting objectui#8769's repair off `ObjectGantt`).
//
// This branch used to be `setData(dataItems); return;` — taken
// BEFORE the `find` below, which is the ONE site in this file that
// lowers `schema.filter` onto `$filter`, `schema.sort` onto
// `$orderby` (via `convertSortToQueryParams`) and the objectui#7210
// ceiling onto `$top`. So an authored `filter` reached nothing and
// every authored row was plotted: the fail-OPEN direction, because
// the key that was dropped is the key that NARROWS. Accepting a
// declared key one cannot honour is the defect, and `ValueDataSource`
// honours all three over its own array, so they are honoured here.
//
// ⚠️ NOT a literal transplant of the gantt's diff, and the difference
// is structural rather than cosmetic. `ObjectGantt` resolves ONE