@jsb188/mday-web 1.1.8 → 1.1.11

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.
@@ -0,0 +1,842 @@
1
+ import { COMMON_CLASSNAMES } from '@jsb188/app/constants/app.ts';
2
+ import i18n from '@jsb188/app/i18n/index.ts';
3
+ import { cn } from '@jsb188/app/utils/string.ts';
4
+ import { useDataTables } from '@jsb188/graphql/hooks/use-dataTable-qry';
5
+ import { useSheets } from '@jsb188/graphql/hooks/use-sheet-qry';
6
+ import type { DataTableGQL } from '@jsb188/mday/types/dataTable.d.ts';
7
+ import type { GridItemSortEnum, SheetGQL } from '@jsb188/mday/types/sheet.d.ts';
8
+ import {
9
+ ContentGate,
10
+ type SetFloatingMessage,
11
+ } from '@jsb188/react-web/modules/layout/MainLayout';
12
+ import type { ToolbarItemObj } from '@jsb188/react-web/modules/Toolbar';
13
+ import { COMMON_ICON_NAMES } from '@jsb188/react-web/svgs/Icon';
14
+ import { TileItem, TileSection } from '@jsb188/react-web/ui/TileUI';
15
+ import { useOpenModalPopUp, usePopOver } from '@jsb188/react/states';
16
+ import type { POListIfaceItem } from '@jsb188/react/types/PopOver.d';
17
+ import {
18
+ memo,
19
+ useCallback,
20
+ useEffect,
21
+ useMemo,
22
+ useRef,
23
+ type MouseEvent,
24
+ } from 'react';
25
+ import { mapRenderableItems } from '../utils/list-web.ts';
26
+
27
+ const FILE_CONSTANTS = {
28
+ contentPaddingClassName: COMMON_CLASSNAMES.contentXPadding,
29
+ dataTableIconName: COMMON_ICON_NAMES.data_table,
30
+ gridCreateIconName: COMMON_ICON_NAMES.plus,
31
+ sectionDataTables: 'dataTables',
32
+ sectionSheets: 'sheets',
33
+ sheetIconName: COMMON_ICON_NAMES.sheet,
34
+ sortParamName: 's',
35
+ sortTitle: '2',
36
+ sortUpdatedDate: '1',
37
+ tileContextMenuIdPrefix: 'grids_tile_context_menu:',
38
+ } as const;
39
+
40
+ const GRIDS_SORT_OPTIONS = [
41
+ FILE_CONSTANTS.sortUpdatedDate,
42
+ FILE_CONSTANTS.sortTitle,
43
+ ] as const;
44
+
45
+ export type GridsSectionMode = 'all' | 'sheets' | 'dataTables';
46
+ export type GridsSortValue = (typeof GRIDS_SORT_OPTIONS)[number];
47
+
48
+ type GridItem = {
49
+ id: string;
50
+ __typename: 'Sheet' | 'DataTable';
51
+ __deleted?: boolean;
52
+ title: string;
53
+ description: string;
54
+ iconName: string;
55
+ to: string;
56
+ };
57
+
58
+ type GridsTileContextMenuValue = {
59
+ action: 'DELETE_GRID_ITEM' | 'OPEN_GRID_ITEM_NEW_TAB';
60
+ item: GridItem;
61
+ };
62
+
63
+ type GridsReadyData = {
64
+ dataTables: DataTableGQL[] | null;
65
+ sheets: SheetGQL[] | null;
66
+ };
67
+
68
+ type GridsSectionVisibility = {
69
+ showDataTables: boolean;
70
+ showSheets: boolean;
71
+ };
72
+
73
+ type GridsTileSectionProps = {
74
+ createDescription: string;
75
+ createTitle: string;
76
+ items: GridItem[];
77
+ mocked: boolean;
78
+ sectionId: string;
79
+ showCreateTile: boolean;
80
+ title: string;
81
+ onCreate: () => void;
82
+ };
83
+
84
+ interface GridsModuleProps {
85
+ contentClassName?: string;
86
+ notReadyClassName?: string;
87
+ organizationId: string;
88
+ sections?: GridsSectionMode;
89
+ setFloatingMessage?: SetFloatingMessage;
90
+ showCreateTiles?: boolean;
91
+ sort: GridItemSortEnum;
92
+ }
93
+
94
+ /*
95
+ * Return which grids sections should be visible for one module mode.
96
+ */
97
+ function getGridsSectionVisibility(
98
+ sections: GridsSectionMode,
99
+ ): GridsSectionVisibility {
100
+ return {
101
+ showDataTables: sections !== FILE_CONSTANTS.sectionSheets,
102
+ showSheets: sections !== FILE_CONSTANTS.sectionDataTables,
103
+ };
104
+ }
105
+
106
+ /*
107
+ * Return one stable reset-only signal value for all visible grids list queries.
108
+ */
109
+ function getGridsResetOnlyTime(
110
+ sheetsResetOnlyTime: string,
111
+ dataTablesResetOnlyTime: string,
112
+ ) {
113
+ return [sheetsResetOnlyTime, dataTablesResetOnlyTime]
114
+ .filter(Boolean)
115
+ .join('|');
116
+ }
117
+
118
+ /*
119
+ * Show the app refresh prompt when grids queries receive a RESET_ONLY emit.
120
+ */
121
+ function useGridsResetOnlyFloatingMessage(
122
+ resetOnlyTime: string,
123
+ setFloatingMessage?: SetFloatingMessage,
124
+ ) {
125
+ const lastUpdateTime = useRef(resetOnlyTime);
126
+
127
+ useEffect(() => {
128
+ if (!resetOnlyTime || !setFloatingMessage) {
129
+ return;
130
+ }
131
+
132
+ if (resetOnlyTime !== lastUpdateTime.current) {
133
+ setFloatingMessage({
134
+ type: 'REFRESH',
135
+ });
136
+ }
137
+
138
+ lastUpdateTime.current = resetOnlyTime;
139
+ }, [resetOnlyTime, setFloatingMessage]);
140
+ }
141
+
142
+ /*
143
+ * Keep the last fully loaded grids data available during replacement fetches.
144
+ */
145
+ function useLastGridsReadyData(p: {
146
+ dataTables?: DataTableGQL[] | null;
147
+ sheets?: SheetGQL[] | null;
148
+ showDataTables: boolean;
149
+ showSheets: boolean;
150
+ }) {
151
+ const { dataTables, sheets, showDataTables, showSheets } = p;
152
+ const readyDataRef = useRef<GridsReadyData | null>(null);
153
+
154
+ useEffect(() => {
155
+ if ((showSheets && !sheets) || (showDataTables && !dataTables)) {
156
+ return;
157
+ }
158
+
159
+ readyDataRef.current = {
160
+ dataTables: showDataTables ? dataTables || null : null,
161
+ sheets: showSheets ? sheets || null : null,
162
+ };
163
+ }, [dataTables, sheets, showDataTables, showSheets]);
164
+
165
+ return readyDataRef.current;
166
+ }
167
+
168
+ /*
169
+ * Return query variables for one grids data source when its section is visible.
170
+ */
171
+ function getGridsQueryVariables(
172
+ visible: boolean,
173
+ organizationId: string,
174
+ sort: GridItemSortEnum,
175
+ ) {
176
+ return visible
177
+ ? {
178
+ organizationId,
179
+ sort,
180
+ }
181
+ : null;
182
+ }
183
+
184
+ /*
185
+ * Return the best display title for one sheet.
186
+ */
187
+ function getSheetTitle(sheet: SheetGQL) {
188
+ return sheet.title || i18n.t('app.untitled_sheet');
189
+ }
190
+
191
+ /*
192
+ * Return the best display title for one dataTable.
193
+ */
194
+ function getDataTableTitle(dataTable: DataTableGQL) {
195
+ return dataTable.title || i18n.t('sheet.untitled_data_table');
196
+ }
197
+
198
+ /*
199
+ * Convert one sheet record into a grids tile item.
200
+ */
201
+ function mapSheetToGridItem(sheet: SheetGQL): GridItem | null {
202
+ if (!sheet.id) {
203
+ return null;
204
+ }
205
+
206
+ return {
207
+ id: sheet.id,
208
+ __typename: 'Sheet',
209
+ title: getSheetTitle(sheet),
210
+ description: sheet.description || sheet.name || '',
211
+ iconName: FILE_CONSTANTS.sheetIconName,
212
+ to: `/app/s/${sheet.id}`,
213
+ __deleted: Boolean(sheet.__deleted || sheet.deletedAt),
214
+ };
215
+ }
216
+
217
+ /*
218
+ * Convert one dataTable record into a grids tile item.
219
+ */
220
+ function mapDataTableToGridItem(dataTable: DataTableGQL): GridItem | null {
221
+ if (!dataTable.id) {
222
+ return null;
223
+ }
224
+
225
+ return {
226
+ id: dataTable.id,
227
+ __typename: 'DataTable',
228
+ title: getDataTableTitle(dataTable),
229
+ description: dataTable.description || dataTable.name || '',
230
+ iconName: FILE_CONSTANTS.dataTableIconName,
231
+ to: `/app/d/${dataTable.id}`,
232
+ __deleted: Boolean(dataTable.__deleted || dataTable.active === false),
233
+ };
234
+ }
235
+
236
+ /*
237
+ * Return the current grids sort value from search params.
238
+ */
239
+ export function getGridsSortValue(
240
+ searchParams: URLSearchParams,
241
+ sortParamName: string = FILE_CONSTANTS.sortParamName,
242
+ ): GridsSortValue {
243
+ const sortValue = searchParams.get(sortParamName);
244
+
245
+ return GRIDS_SORT_OPTIONS.includes(sortValue as GridsSortValue)
246
+ ? (sortValue as GridsSortValue)
247
+ : FILE_CONSTANTS.sortUpdatedDate;
248
+ }
249
+
250
+ /*
251
+ * Return the toolbar label for the selected grids sort option.
252
+ */
253
+ function getGridsSortText(sortValue: GridsSortValue) {
254
+ return i18n.t(
255
+ sortValue === FILE_CONSTANTS.sortTitle
256
+ ? 'app.grids_sort_title'
257
+ : 'app.grids_sort_updated_date',
258
+ );
259
+ }
260
+
261
+ /*
262
+ * Return the GraphQL sort value for the current grids URL sort value.
263
+ */
264
+ export function getGridsSortQueryValue(
265
+ sortValue: GridsSortValue,
266
+ ): GridItemSortEnum {
267
+ return sortValue === FILE_CONSTANTS.sortTitle
268
+ ? 'TITLE_ASC'
269
+ : 'UPDATED_AT_DESC';
270
+ }
271
+
272
+ /*
273
+ * Build a grids path with the selected sort search param.
274
+ */
275
+ function createGridsSortPath(
276
+ pathname: string,
277
+ searchParams: URLSearchParams,
278
+ sortValue: GridsSortValue,
279
+ sortParamName: string,
280
+ ) {
281
+ const nextSearchParams = new URLSearchParams(searchParams);
282
+ nextSearchParams.set(sortParamName, sortValue);
283
+
284
+ return `${pathname}?${nextSearchParams.toString()}`;
285
+ }
286
+
287
+ /*
288
+ * Return the app toolbar options for a grids surface.
289
+ */
290
+ export function getGridsToolbarOptions(
291
+ pathname: string,
292
+ searchParams: URLSearchParams,
293
+ sortValue: GridsSortValue,
294
+ sortParamName: string = FILE_CONSTANTS.sortParamName,
295
+ ): ToolbarItemObj[] {
296
+ return [
297
+ {
298
+ id: 'grids_sort',
299
+ text: getGridsSortText(sortValue),
300
+ hasValue: true,
301
+ hideClearIcon: true,
302
+ popOverClassName: 'min_w_200',
303
+ initialState: sortValue,
304
+ disablePopOverButton: true,
305
+ options: [
306
+ {
307
+ __type: 'LIST_SUBTITLE',
308
+ text: i18n.t('form.sort_by'),
309
+ },
310
+ {
311
+ __type: 'SINGLE_OPTION_LIST_ITEM',
312
+ value: FILE_CONSTANTS.sortUpdatedDate,
313
+ text: i18n.t('app.grids_sort_updated_date'),
314
+ },
315
+ {
316
+ __type: 'SINGLE_OPTION_LIST_ITEM',
317
+ value: FILE_CONSTANTS.sortTitle,
318
+ text: i18n.t('app.grids_sort_title'),
319
+ },
320
+ ],
321
+ getItemTo: (nextSortValue) =>
322
+ createGridsSortPath(
323
+ pathname,
324
+ searchParams,
325
+ nextSortValue === FILE_CONSTANTS.sortTitle
326
+ ? FILE_CONSTANTS.sortTitle
327
+ : FILE_CONSTANTS.sortUpdatedDate,
328
+ sortParamName,
329
+ ),
330
+ },
331
+ ];
332
+ }
333
+
334
+ /*
335
+ * Return sort state and toolbar items for a grids surface.
336
+ */
337
+ export function getGridsToolbarState(p: {
338
+ pathname: string;
339
+ searchParams: URLSearchParams;
340
+ sortParamName?: string;
341
+ }) {
342
+ const { pathname, searchParams, sortParamName } = p;
343
+ const sortValue = getGridsSortValue(searchParams, sortParamName);
344
+
345
+ return {
346
+ sortQueryValue: getGridsSortQueryValue(sortValue),
347
+ sortValue,
348
+ toolbarOptions: getGridsToolbarOptions(
349
+ pathname,
350
+ searchParams,
351
+ sortValue,
352
+ sortParamName,
353
+ ),
354
+ };
355
+ }
356
+
357
+ /*
358
+ * Convert a right-click event into a popover anchor rectangle.
359
+ */
360
+ function getGridsTileContextMenuRect(e: MouseEvent<Element>) {
361
+ const { clientX, clientY } = e;
362
+
363
+ return {
364
+ width: 0,
365
+ height: 0,
366
+ left: clientX,
367
+ right: clientX,
368
+ top: clientY,
369
+ bottom: clientY,
370
+ x: clientX,
371
+ y: clientY,
372
+ };
373
+ }
374
+
375
+ /*
376
+ * Build an absolute URL for a grids tile route action.
377
+ */
378
+ function getGridsTileRouteUrl(to: string) {
379
+ return new URL(
380
+ to,
381
+ globalThis.location?.origin || 'http://localhost',
382
+ ).toString();
383
+ }
384
+
385
+ /*
386
+ * Return the modal preset name used to delete one grids tile.
387
+ */
388
+ function getGridsTileDeleteModalName(item: GridItem) {
389
+ return item.__typename === 'Sheet' ? 'DELETE_SHEET' : 'DELETE_DATA_TABLE';
390
+ }
391
+
392
+ /*
393
+ * Return the modal props used to delete one grids tile.
394
+ */
395
+ function getGridsTileDeleteModalProps(item: GridItem, organizationId: string) {
396
+ return {
397
+ itemTitle: item.title,
398
+ organizationId,
399
+ ...(item.__typename === 'Sheet'
400
+ ? { sheetId: item.id }
401
+ : { dataTableId: item.id }),
402
+ };
403
+ }
404
+
405
+ /*
406
+ * Return the popover menu options for one grids tile.
407
+ */
408
+ function getGridsTileContextOptions(item: GridItem): POListIfaceItem[] {
409
+ return [
410
+ {
411
+ __type: 'LIST_ITEM',
412
+ iconName: COMMON_ICON_NAMES.open_external_link,
413
+ text: i18n.t('app.open_in_new_tab'),
414
+ value: {
415
+ action: 'OPEN_GRID_ITEM_NEW_TAB',
416
+ item,
417
+ } satisfies GridsTileContextMenuValue,
418
+ },
419
+ {
420
+ __type: 'LIST_ITEM',
421
+ iconName: COMMON_ICON_NAMES.delete,
422
+ text: i18n.t(
423
+ item.__typename === 'Sheet'
424
+ ? 'sheet.delete_sheet'
425
+ : 'sheet.delete_data_table',
426
+ ),
427
+ className: 'cl_err_hv',
428
+ value: {
429
+ action: 'DELETE_GRID_ITEM',
430
+ item,
431
+ } satisfies GridsTileContextMenuValue,
432
+ },
433
+ ];
434
+ }
435
+
436
+ /*
437
+ * Return the popover id for one grids tile context menu.
438
+ */
439
+ function getGridsTileContextMenuId(sectionId: string, item: GridItem) {
440
+ return `${FILE_CONSTANTS.tileContextMenuIdPrefix}${sectionId}:${item.__typename}:${item.id}`;
441
+ }
442
+
443
+ /*
444
+ * Render one mapped grids tile item with a right-click menu.
445
+ */
446
+ const GridsTileItem = memo((p: { item: GridItem; sectionId: string }) => {
447
+ const { item, sectionId } = p;
448
+ const { openPopOver, popOver } = usePopOver();
449
+ const popOverId = getGridsTileContextMenuId(sectionId, item);
450
+ const active = popOver?.id === popOverId;
451
+
452
+ /*
453
+ * Open the grid item context menu at the pointer location.
454
+ */
455
+ const onContextMenu = useCallback(
456
+ (e: MouseEvent<Element>) => {
457
+ if (item.__deleted) {
458
+ return;
459
+ }
460
+
461
+ e.preventDefault();
462
+ e.stopPropagation();
463
+
464
+ openPopOver({
465
+ name: 'PO_LIST',
466
+ id: popOverId,
467
+ animationClassName: 'anim_dropdown_top on_mount',
468
+ position: 'bottom_left',
469
+ offsetX: 0,
470
+ offsetY: 0,
471
+ rect: getGridsTileContextMenuRect(e),
472
+ variables: {
473
+ options: getGridsTileContextOptions(item),
474
+ shadowClassName: 'shadow_md',
475
+ },
476
+ zClassName: 'z8',
477
+ });
478
+ },
479
+ [item, openPopOver, popOverId],
480
+ );
481
+
482
+ return (
483
+ <TileItem
484
+ __deleted={item.__deleted}
485
+ active={active}
486
+ iconName={item.iconName}
487
+ title={item.title}
488
+ description={item.description}
489
+ to={item.to}
490
+ onContextMenu={onContextMenu}
491
+ />
492
+ );
493
+ });
494
+
495
+ GridsTileItem.displayName = 'GridsTileItem';
496
+
497
+ /*
498
+ * Render one presentational grids tile section.
499
+ */
500
+ const GridsTileSection = memo((p: GridsTileSectionProps) => {
501
+ return (
502
+ <TileSection className="mb_md" mocked={p.mocked} title={p.title}>
503
+ {p.items.map((item) => (
504
+ <GridsTileItem
505
+ key={`${item.__typename}:${item.id}`}
506
+ item={item}
507
+ sectionId={p.sectionId}
508
+ />
509
+ ))}
510
+
511
+ {!p.showCreateTile ? null : (
512
+ <TileItem
513
+ iconName={FILE_CONSTANTS.gridCreateIconName}
514
+ title={p.createTitle}
515
+ description={p.createDescription}
516
+ onClick={p.onCreate}
517
+ />
518
+ )}
519
+ </TileSection>
520
+ );
521
+ });
522
+
523
+ GridsTileSection.displayName = 'GridsTileSection';
524
+
525
+ /*
526
+ * Render the grids sheets section.
527
+ */
528
+ function GridsSheetsSection(p: {
529
+ organizationId: string;
530
+ sheets?: SheetGQL[] | null;
531
+ showCreateTile: boolean;
532
+ }) {
533
+ const { organizationId, sheets, showCreateTile } = p;
534
+ const openModalPopUp = useOpenModalPopUp();
535
+ const sheetItems = useMemo(
536
+ () => mapRenderableItems(sheets, mapSheetToGridItem),
537
+ [sheets],
538
+ );
539
+
540
+ /*
541
+ * Open the sheet creation flow.
542
+ */
543
+ const openCreateSheet = useCallback(() => {
544
+ openModalPopUp({
545
+ name: 'CREATE_SHEET',
546
+ preset: 'CREATE_SHEET',
547
+ props: {
548
+ organizationId,
549
+ },
550
+ });
551
+ }, [openModalPopUp, organizationId]);
552
+
553
+ return (
554
+ <GridsTileSection
555
+ createDescription={i18n.t('sheet.create_sheet_description')}
556
+ createTitle={i18n.t('sheet.create_sheet')}
557
+ items={sheetItems}
558
+ mocked={!sheets}
559
+ sectionId={FILE_CONSTANTS.sectionSheets}
560
+ showCreateTile={showCreateTile}
561
+ title={i18n.t('sheet.sheets')}
562
+ onCreate={openCreateSheet}
563
+ />
564
+ );
565
+ }
566
+
567
+ /*
568
+ * Render the grids data tables section.
569
+ */
570
+ function GridsDataTablesSection(p: {
571
+ dataTables?: DataTableGQL[] | null;
572
+ organizationId: string;
573
+ showCreateTile: boolean;
574
+ }) {
575
+ const { dataTables, organizationId, showCreateTile } = p;
576
+ const openModalPopUp = useOpenModalPopUp();
577
+ const dataTableItems = useMemo(
578
+ () => mapRenderableItems(dataTables, mapDataTableToGridItem),
579
+ [dataTables],
580
+ );
581
+
582
+ /*
583
+ * Open the data table creation flow.
584
+ */
585
+ const openCreateDataTable = useCallback(() => {
586
+ openModalPopUp({
587
+ name: 'CREATE_DATASET',
588
+ preset: 'CREATE_DATASET',
589
+ props: {
590
+ organizationId,
591
+ },
592
+ });
593
+ }, [openModalPopUp, organizationId]);
594
+
595
+ return (
596
+ <GridsTileSection
597
+ createDescription={i18n.t('sheet.create_data_table_description')}
598
+ createTitle={i18n.t('sheet.create_data_table')}
599
+ items={dataTableItems}
600
+ mocked={!dataTables}
601
+ sectionId={FILE_CONSTANTS.sectionDataTables}
602
+ showCreateTile={showCreateTile}
603
+ title={i18n.t('sheet.data_tables')}
604
+ onCreate={openCreateDataTable}
605
+ />
606
+ );
607
+ }
608
+
609
+ /*
610
+ * Return whether visible grids data is missing during a fetch.
611
+ */
612
+ function getGridsVisibleDataMissing(p: {
613
+ dataTables?: DataTableGQL[] | null;
614
+ sheets?: SheetGQL[] | null;
615
+ showDataTables: boolean;
616
+ showSheets: boolean;
617
+ }) {
618
+ const { dataTables, sheets, showDataTables, showSheets } = p;
619
+
620
+ return (showSheets && !sheets) || (showDataTables && !dataTables);
621
+ }
622
+
623
+ /*
624
+ * Return the data lists that should render while preserving last-ready data during fetches.
625
+ */
626
+ function getGridsDisplayData(p: {
627
+ dataTables?: DataTableGQL[] | null;
628
+ lastGridsReadyData: GridsReadyData | null;
629
+ sheets?: SheetGQL[] | null;
630
+ showDataTables: boolean;
631
+ showingLastGridsReadyData: boolean;
632
+ showSheets: boolean;
633
+ }) {
634
+ const {
635
+ dataTables,
636
+ lastGridsReadyData,
637
+ sheets,
638
+ showDataTables,
639
+ showingLastGridsReadyData,
640
+ showSheets,
641
+ } = p;
642
+
643
+ return {
644
+ displayDataTables: showDataTables
645
+ ? showingLastGridsReadyData
646
+ ? lastGridsReadyData?.dataTables
647
+ : dataTables
648
+ : null,
649
+ displaySheets: showSheets
650
+ ? showingLastGridsReadyData
651
+ ? lastGridsReadyData?.sheets
652
+ : sheets
653
+ : null,
654
+ };
655
+ }
656
+
657
+ /*
658
+ * Fetch grids data and render each visible grids section.
659
+ */
660
+ export function GridsModule(p: GridsModuleProps) {
661
+ const {
662
+ contentClassName,
663
+ notReadyClassName,
664
+ organizationId,
665
+ sections = 'all',
666
+ setFloatingMessage,
667
+ showCreateTiles = true,
668
+ sort,
669
+ } = p;
670
+ const { popOver, closePopOver } = usePopOver();
671
+ const openModalPopUp = useOpenModalPopUp();
672
+ const { showDataTables, showSheets } = getGridsSectionVisibility(sections);
673
+ const popOverHandlersRef = useRef({
674
+ closePopOver,
675
+ openModalPopUp,
676
+ });
677
+ const sheetsQueryVariables = useMemo(
678
+ () => getGridsQueryVariables(showSheets, organizationId, sort),
679
+ [organizationId, showSheets, sort],
680
+ );
681
+ const dataTablesQueryVariables = useMemo(
682
+ () => getGridsQueryVariables(showDataTables, organizationId, sort),
683
+ [organizationId, showDataTables, sort],
684
+ );
685
+ const {
686
+ dataTables,
687
+ error: dataTablesError,
688
+ loading: dataTablesLoading,
689
+ refetch: refetchDataTables,
690
+ resetOnlyTime: dataTablesResetOnlyTime,
691
+ } = useDataTables(dataTablesQueryVariables);
692
+ const {
693
+ sheets,
694
+ error: sheetsError,
695
+ loading: sheetsLoading,
696
+ refetch: refetchSheets,
697
+ resetOnlyTime: sheetsResetOnlyTime,
698
+ } = useSheets(sheetsQueryVariables);
699
+ const error =
700
+ (showSheets && sheetsError) || (showDataTables && dataTablesError);
701
+ const gridsLoading = Boolean(
702
+ (showSheets && sheetsLoading) || (showDataTables && dataTablesLoading),
703
+ );
704
+ const lastGridsReadyData = useLastGridsReadyData({
705
+ dataTables,
706
+ sheets,
707
+ showDataTables,
708
+ showSheets,
709
+ });
710
+ const visibleDataMissing = getGridsVisibleDataMissing({
711
+ dataTables,
712
+ sheets,
713
+ showDataTables,
714
+ showSheets,
715
+ });
716
+ const showingLastGridsReadyData =
717
+ gridsLoading && visibleDataMissing && !!lastGridsReadyData;
718
+ const { displayDataTables, displaySheets } = getGridsDisplayData({
719
+ dataTables,
720
+ lastGridsReadyData,
721
+ sheets,
722
+ showDataTables,
723
+ showingLastGridsReadyData,
724
+ showSheets,
725
+ });
726
+ const notReady = getGridsVisibleDataMissing({
727
+ dataTables: displayDataTables,
728
+ sheets: displaySheets,
729
+ showDataTables,
730
+ showSheets,
731
+ });
732
+ const gridsResetOnlyTime = getGridsResetOnlyTime(
733
+ showSheets ? sheetsResetOnlyTime : '',
734
+ showDataTables ? dataTablesResetOnlyTime : '',
735
+ );
736
+
737
+ popOverHandlersRef.current = {
738
+ closePopOver,
739
+ openModalPopUp,
740
+ };
741
+
742
+ useGridsResetOnlyFloatingMessage(gridsResetOnlyTime, setFloatingMessage);
743
+
744
+ /*
745
+ * Close the grids popover when the module unmounts.
746
+ */
747
+ useEffect(() => {
748
+ return () => {
749
+ popOverHandlersRef.current.closePopOver();
750
+ };
751
+ }, []);
752
+
753
+ /*
754
+ * Handle grids context menu actions from popover global state.
755
+ */
756
+ useEffect(() => {
757
+ const { action, id, value } = popOver?.globalState || {};
758
+ const contextValue = value as GridsTileContextMenuValue | null;
759
+
760
+ if (
761
+ action !== 'ITEM' ||
762
+ typeof id !== 'string' ||
763
+ !id.startsWith(FILE_CONSTANTS.tileContextMenuIdPrefix) ||
764
+ !contextValue
765
+ ) {
766
+ return;
767
+ }
768
+
769
+ const { item } = contextValue;
770
+ popOverHandlersRef.current.closePopOver();
771
+
772
+ switch (contextValue.action) {
773
+ case 'OPEN_GRID_ITEM_NEW_TAB':
774
+ globalThis.window?.open(
775
+ getGridsTileRouteUrl(item.to),
776
+ '_blank',
777
+ 'noopener,noreferrer',
778
+ );
779
+ return;
780
+ case 'DELETE_GRID_ITEM':
781
+ break;
782
+ default:
783
+ return;
784
+ }
785
+
786
+ const deleteModalName = getGridsTileDeleteModalName(item);
787
+
788
+ popOverHandlersRef.current.openModalPopUp({
789
+ name: deleteModalName,
790
+ preset: deleteModalName,
791
+ props: getGridsTileDeleteModalProps(item, organizationId),
792
+ });
793
+ }, [organizationId, popOver]);
794
+
795
+ /*
796
+ * Refetch every visible data source used by the grids.
797
+ */
798
+ const refetchGridItems = useCallback(() => {
799
+ if (showSheets) {
800
+ refetchSheets();
801
+ }
802
+
803
+ if (showDataTables) {
804
+ refetchDataTables();
805
+ }
806
+ }, [refetchDataTables, refetchSheets, showDataTables, showSheets]);
807
+
808
+ return (
809
+ <ContentGate
810
+ showLoadingIfNotReady
811
+ notReady={notReady}
812
+ loading={gridsLoading}
813
+ error={error || null}
814
+ notReadyClassName={notReadyClassName ?? 'fs v_center'}
815
+ onRefetch={refetchGridItems}
816
+ >
817
+ <div
818
+ className={cn(
819
+ 'rel pt_md pb_md',
820
+ FILE_CONSTANTS.contentPaddingClassName,
821
+ showingLastGridsReadyData && 'anim_data_loading',
822
+ contentClassName,
823
+ )}
824
+ >
825
+ {!showSheets ? null : (
826
+ <GridsSheetsSection
827
+ organizationId={organizationId}
828
+ sheets={displaySheets}
829
+ showCreateTile={showCreateTiles}
830
+ />
831
+ )}
832
+ {!showDataTables ? null : (
833
+ <GridsDataTablesSection
834
+ dataTables={displayDataTables}
835
+ organizationId={organizationId}
836
+ showCreateTile={showCreateTiles}
837
+ />
838
+ )}
839
+ </div>
840
+ </ContentGate>
841
+ );
842
+ }