@jsb188/graphql 1.1.11 → 1.1.13

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.
Files changed (174) hide show
  1. package/LICENSE +21 -0
  2. package/audience.d.ts +205 -0
  3. package/cache/config.ts +65 -28
  4. package/cache/index.helpers.ts +96 -0
  5. package/cache/index.ts +364 -808
  6. package/cache/mounted-queries.ts +33 -0
  7. package/cache/optimistic-ui-runtime.ts +46 -0
  8. package/cache/optimistic-ui.ts +401 -0
  9. package/cache/persist.ts +107 -22
  10. package/cache/realtime-fence.ts +47 -0
  11. package/cache/realtime-live.ts +17 -0
  12. package/cache/store.ts +22 -64
  13. package/cache/value.helpers.ts +17 -0
  14. package/{drafts/store.ts → client/draft-persistence-indexed-db.ts} +19 -3
  15. package/{drafts/index.ts → client/draft-persistence.ts} +13 -39
  16. package/client/index.ts +4 -2
  17. package/client/request.helpers.ts +14 -0
  18. package/client/request.ts +122 -232
  19. package/client/storage-asset.ts +58 -0
  20. package/gql/fragments/accountAuthFragments.ts +0 -4
  21. package/gql/fragments/agentMemoryFragments.ts +5 -1
  22. package/gql/fragments/agentNoteFragments.ts +156 -0
  23. package/gql/fragments/aiChatFragments.ts +35 -11
  24. package/gql/fragments/aiChatLinkFragments.ts +8 -0
  25. package/gql/fragments/audienceFragments.ts +76 -0
  26. package/gql/fragments/dataTableFragments.ts +48 -80
  27. package/gql/fragments/emailFragments.ts +49 -21
  28. package/gql/fragments/emailOutreachFragments.ts +281 -0
  29. package/gql/fragments/fieldDocFragments.ts +222 -40
  30. package/gql/fragments/inboundContactFragments.ts +2 -0
  31. package/gql/fragments/libraryFragments.ts +120 -0
  32. package/gql/fragments/logFragments.ts +93 -2
  33. package/gql/fragments/organizationFragments.ts +12 -0
  34. package/gql/fragments/qrCodeFragments.ts +29 -0
  35. package/gql/fragments/scheduleFragments.ts +574 -145
  36. package/gql/fragments/sheetFragments.ts +86 -424
  37. package/gql/fragments/storageFragments.ts +25 -1
  38. package/gql/fragments/workWaitFragments.ts +13 -0
  39. package/gql/fragments/workflowFragments.ts +32 -4
  40. package/gql/mutations/agentMemoryMutations.ts +32 -0
  41. package/gql/mutations/agentNoteMutations.ts +64 -0
  42. package/gql/mutations/aiChatLinkMutations.ts +13 -0
  43. package/gql/mutations/aiChatMutations.ts +34 -4
  44. package/gql/mutations/audienceMutations.ts +58 -0
  45. package/gql/mutations/authMutations.ts +24 -28
  46. package/gql/mutations/billingMutations.ts +24 -0
  47. package/gql/mutations/contactMutations.ts +23 -0
  48. package/gql/mutations/dataTableMutations.ts +18 -75
  49. package/gql/mutations/emailMutations.ts +22 -0
  50. package/gql/mutations/emailOutreachMutations.ts +139 -0
  51. package/gql/mutations/fieldDocMutations.ts +162 -13
  52. package/gql/mutations/index.ts +10 -5
  53. package/gql/mutations/organizationMutations.ts +6 -0
  54. package/gql/mutations/qrCodeMutations.ts +20 -0
  55. package/gql/mutations/scheduleMutations.ts +389 -417
  56. package/gql/mutations/sheetMutations.ts +62 -118
  57. package/gql/mutations/storageMutations.ts +1 -1
  58. package/gql/mutations/workflowMutations.ts +94 -0
  59. package/gql/queries/agentMemoryQueries.ts +12 -2
  60. package/gql/queries/agentNoteQueries.ts +86 -0
  61. package/gql/queries/aiChatQueries.ts +63 -12
  62. package/gql/queries/audienceQueries.ts +104 -0
  63. package/gql/queries/authQueries.ts +20 -4
  64. package/gql/queries/billingQueries.ts +114 -9
  65. package/gql/queries/dataTableQueries.ts +43 -75
  66. package/gql/queries/emailOutreachQueries.ts +282 -0
  67. package/gql/queries/emailQueries.ts +48 -64
  68. package/gql/queries/emailReplyReportQueries.ts +127 -0
  69. package/gql/queries/fieldDocQueries.ts +128 -98
  70. package/gql/queries/inboundContactQueries.ts +15 -4
  71. package/gql/queries/index.ts +5 -3
  72. package/gql/queries/libraryQueries.ts +112 -0
  73. package/gql/queries/logQueries.ts +86 -12
  74. package/gql/queries/organizationQueries.ts +44 -12
  75. package/gql/queries/qrCodeQueries.ts +27 -0
  76. package/gql/queries/scheduleQueries.ts +401 -278
  77. package/gql/queries/sheetQueries.ts +94 -165
  78. package/gql/queries/storageQueries.ts +11 -4
  79. package/gql/queries/workflowQueries.ts +61 -12
  80. package/gql/reactivity.ts +98 -0
  81. package/gql/recordReads.ts +171 -0
  82. package/hooks/index.helpers.ts +52 -0
  83. package/hooks/index.ts +920 -715
  84. package/hooks/use-agentMemory-mtn.ts +29 -31
  85. package/hooks/use-agentMemory-qry.ts +15 -3
  86. package/hooks/use-agentNote-mtn.ts +89 -0
  87. package/hooks/use-agentNote-qry.ts +162 -0
  88. package/hooks/use-agentNote-recovery.helpers.ts +58 -0
  89. package/hooks/use-agentNote-recovery.ts +57 -0
  90. package/hooks/use-aiChat-mtn.ts +104 -115
  91. package/hooks/use-aiChat-qry.helpers.ts +124 -0
  92. package/hooks/use-aiChat-qry.ts +254 -387
  93. package/hooks/use-aiChat-stream-mtn.ts +14 -0
  94. package/hooks/use-audience-mtn.ts +170 -0
  95. package/hooks/use-audience-qry.helpers.ts +39 -0
  96. package/hooks/use-audience-qry.ts +245 -0
  97. package/hooks/use-auth-mtn.ts +30 -1
  98. package/hooks/use-billing-mtn.ts +31 -0
  99. package/hooks/use-billing-qry.ts +76 -0
  100. package/hooks/use-dataTable-mtn.ts +23 -115
  101. package/hooks/use-dataTable-qry.ts +69 -914
  102. package/hooks/use-email-mtn.ts +26 -0
  103. package/hooks/use-email-qry.helpers.ts +37 -0
  104. package/hooks/use-email-qry.ts +44 -174
  105. package/hooks/use-emailOutreach-mtn.ts +211 -0
  106. package/hooks/use-emailOutreach-qry.helpers.ts +25 -0
  107. package/hooks/use-emailOutreach-qry.ts +367 -0
  108. package/hooks/use-emailOutreachReplyReport-qry.ts +46 -0
  109. package/hooks/use-fieldDoc-mtn.ts +244 -32
  110. package/hooks/use-fieldDoc-qry.helpers.ts +49 -0
  111. package/hooks/use-fieldDoc-qry.ts +134 -102
  112. package/hooks/use-inboundContact-qry.ts +7 -9
  113. package/hooks/use-library-qry.ts +233 -0
  114. package/hooks/use-log-mtn.ts +25 -12
  115. package/hooks/use-log-qry.ts +99 -46
  116. package/hooks/use-organization-qry.ts +16 -60
  117. package/hooks/use-qrCode-mtn.ts +40 -0
  118. package/hooks/use-qrCode-qry.ts +78 -0
  119. package/hooks/use-schedule-mtn.ts +231 -348
  120. package/hooks/use-schedule-qry.ts +208 -139
  121. package/hooks/use-schedule-reactive.ts +218 -0
  122. package/hooks/use-sheet-mtn.ts +91 -199
  123. package/hooks/use-sheet-qry.ts +88 -237
  124. package/hooks/use-storage-qry.ts +84 -61
  125. package/hooks/use-workflow-mtn.ts +84 -23
  126. package/hooks/use-workflow-qry.ts +60 -61
  127. package/libs/ai-chat-link-events.ts +78 -0
  128. package/libs/ai-chat-outgoing.helpers.ts +153 -0
  129. package/libs/ai-chat-outgoing.ts +11 -0
  130. package/libs/ai-chat-outgoing.types.ts +25 -0
  131. package/libs/ai-chat-run-realtime.ts +20 -0
  132. package/libs/ai-chat-submission-realtime.ts +150 -0
  133. package/libs/data-changes.helpers.ts +161 -0
  134. package/libs/data-changes.ts +382 -0
  135. package/libs/fragment-realtime-sequence.ts +55 -0
  136. package/libs/open-sheet.ts +138 -0
  137. package/libs/record-read.ts +327 -0
  138. package/libs/sheet-command-batch.ts +45 -0
  139. package/libs/sheet-realtime-packet.ts +53 -0
  140. package/libs/sheet-view-realtime-router.ts +99 -0
  141. package/loaders/emailPreference.ts +69 -0
  142. package/loaders/rest.ts +12 -0
  143. package/package.json +77 -25
  144. package/schedule.d.ts +1183 -0
  145. package/sse.helpers.ts +19 -0
  146. package/sse.ts +26 -115
  147. package/types.d.ts +48 -25
  148. package/cache/index.test.ts +0 -881
  149. package/client/data-table-realtime-subscriptions.test.ts +0 -740
  150. package/client/data-table-realtime-subscriptions.ts +0 -955
  151. package/client/data-table-realtime.test.ts +0 -792
  152. package/client/data-table-realtime.ts +0 -1218
  153. package/client/fragment-realtime-sequence.ts +0 -52
  154. package/client/open-sheet.ts +0 -137
  155. package/client/request-managed-session.test.ts +0 -574
  156. package/client/request-no-store.test.ts +0 -157
  157. package/client/sse-ticket.test.ts +0 -26
  158. package/deno.lock +0 -170
  159. package/deno.test.json +0 -10
  160. package/gql/fragments/agentDocFragments.ts +0 -46
  161. package/gql/fragments/billingFragments.ts +0 -14
  162. package/gql/fragments/campaignFragments.ts +0 -277
  163. package/gql/mutations/agentDocMutations.ts +0 -177
  164. package/gql/mutations/campaignMutations.ts +0 -215
  165. package/gql/queries/agentDocQueries.ts +0 -120
  166. package/gql/queries/appQueries.ts +0 -14
  167. package/gql/queries/campaignQueries.ts +0 -127
  168. package/hooks/use-agentDoc-mtn.ts +0 -285
  169. package/hooks/use-agentDoc-qry.ts +0 -208
  170. package/hooks/use-app-qry.ts +0 -22
  171. package/hooks/use-campaign-mtn.ts +0 -377
  172. package/hooks/use-campaign-qry.ts +0 -257
  173. package/loaders/campaign.ts +0 -137
  174. package/tsconfig.json +0 -13
@@ -1,39 +1,15 @@
1
- import {
2
- graphqlRequestNoStore,
3
- useQuery,
4
- useReactiveFragment,
5
- useReactiveFragmentMap,
6
- } from '@jsb188/graphql/client';
1
+ import { useQuery, useReactiveFragment, useReactiveFragmentMap } from '@jsb188/graphql/client';
2
+ import { FRAGMENT_NAMES, getFragmentBaseName, makeFragmentKey } from '@jsb188/mday/constants/graphqlReactivity.ts';
7
3
  import { GRID_ITEM_LIST_LIMIT } from '@jsb188/mday/constants/sheet.ts';
8
- import type { DataTableRowGQL } from '@jsb188/mday/types/dataTable.d.ts';
9
- import type {
10
- GridItemSortEnum,
11
- SheetGridViewportObj,
12
- SheetJournalSeqInput,
13
- SheetOperationalHistoryGQL,
14
- } from '@jsb188/mday/types/sheet.d.ts';
15
- import { normalizeSheetJournalSeq } from '@jsb188/mday/utils/sheetSequence.ts';
16
- import {
17
- FRAGMENT_NAMES,
18
- getFragmentBaseName,
19
- makeFragmentKey,
20
- } from '@jsb188/mday/constants/graphqlReactivity.ts';
4
+ import type { GridItemSortEnum, SheetTabGQL } from '@jsb188/mday/types/sheet.d.ts';
21
5
  import { useEffect, useMemo, useRef, useState } from 'react';
22
- import { fetchOpenSheetOnce } from '../client/open-sheet.ts';
23
- import {
24
- sheetAccessGrantsQry,
25
- sheetAccessQry,
26
- sheetOperationalHistoryQry,
27
- sheetRegionSourcePreviewRowsQry,
28
- sheetsQry,
29
- sheetTabChangesQry,
30
- } from '../gql/queries/sheetQueries.ts';
6
+ import { sheetAccessGrantsQry, sheetAccessQry, sheetsQry } from '../gql/queries/sheetQueries.ts';
31
7
  import type { PaginationArgs, UseQueryParams } from '../types.d.ts';
32
8
 
33
9
  export {
34
10
  fetchOpenSheetOnce,
35
11
  getOpenSheetQueryVariables,
36
- } from '../client/open-sheet.ts';
12
+ } from '../libs/open-sheet.ts';
37
13
 
38
14
  export type SheetsVariables = PaginationArgs & {
39
15
  organizationId?: string | null;
@@ -58,18 +34,14 @@ function mapSheetDeletedStatus(sheet: any) {
58
34
  /*
59
35
  * Return whether one useSheets input is the paginated variables object.
60
36
  */
61
- function isSheetsVariables(
62
- value: string | null | undefined | SheetsVariables,
63
- ): value is SheetsVariables {
37
+ function isSheetsVariables(value: string | null | undefined | SheetsVariables): value is SheetsVariables {
64
38
  return !!value && typeof value === 'object';
65
39
  }
66
40
 
67
41
  /*
68
42
  * Return GraphQL variables for the paginated sheets query.
69
43
  */
70
- function getSheetsQueryVariables(
71
- organizationIdOrVariables?: string | null | SheetsVariables,
72
- ): SheetsVariables {
44
+ function getSheetsQueryVariables(organizationIdOrVariables?: string | null | SheetsVariables): SheetsVariables {
73
45
  if (isSheetsVariables(organizationIdOrVariables)) {
74
46
  return {
75
47
  ...organizationIdOrVariables,
@@ -93,24 +65,15 @@ function getSheetsQueryVariables(
93
65
  * Fetch sheets for an organization.
94
66
  */
95
67
 
96
- export function useSheets(
97
- organizationId?: string | null | SheetsVariables,
98
- params: UseQueryParams = {},
99
- ) {
68
+ export function useSheets(organizationId?: string | null | SheetsVariables, params: UseQueryParams = {}) {
100
69
  const variables = getSheetsQueryVariables(organizationId);
101
70
  const { data, ...rest } = useQuery(sheetsQry, {
102
71
  variables,
103
72
  skip: !variables.organizationId,
104
73
  ...params,
105
74
  });
106
- const sheets = useReactiveFragmentMap(
107
- data?.sheets || null,
108
- getFragmentBaseName(FRAGMENT_NAMES.sheet),
109
- );
110
- const sheetsWithDeletedStatus = useMemo(
111
- () => sheets?.map(mapSheetDeletedStatus) || sheets,
112
- [sheets],
113
- );
75
+ const sheets = useReactiveFragmentMap(data?.sheets || null, getFragmentBaseName(FRAGMENT_NAMES.sheet));
76
+ const sheetsWithDeletedStatus = useMemo(() => sheets?.map(mapSheetDeletedStatus) || sheets, [sheets]);
114
77
 
115
78
  return {
116
79
  sheets: sheetsWithDeletedStatus,
@@ -118,42 +81,11 @@ export function useSheets(
118
81
  };
119
82
  }
120
83
 
121
- /*
122
- * Fetch source rows for an unsaved generated Sheet region preview.
123
- */
124
- export function useSheetRegionSourcePreviewRows(
125
- organizationId?: string | null,
126
- region?: Record<string, any> | null,
127
- limit?: number | null,
128
- params: UseQueryParams = {},
129
- ) {
130
- const variables = {
131
- organizationId,
132
- region,
133
- limit,
134
- };
135
- const { data, ...rest } = useQuery(sheetRegionSourcePreviewRowsQry, {
136
- variables,
137
- ...params,
138
- skip: !organizationId || !region || !limit || Boolean(params.skip),
139
- });
140
-
141
- return {
142
- sheetRegionSourcePreviewRows:
143
- (data?.sheetRegionSourcePreviewRows as DataTableRowGQL[] | null) || null,
144
- ...rest,
145
- };
146
- }
147
-
148
84
  /*
149
85
  * Fetch one sheet's viewer-specific access facts (works signed out).
150
86
  */
151
87
 
152
- export function useSheetAccess(
153
- sheetId?: string | null,
154
- params: UseQueryParams = {},
155
- authorizationScopeId?: string | null,
156
- ) {
88
+ export function useSheetAccess(sheetId?: string | null, params: UseQueryParams = {}, authorizationScopeId?: string | null) {
157
89
  const { data, ...rest } = useQuery(sheetAccessQry, {
158
90
  variables: {
159
91
  // Not consumed by GraphQL; it isolates viewer-specific client cache and
@@ -174,17 +106,11 @@ export function useSheetAccess(
174
106
  /*
175
107
  * Keep viewer-specific Sheet access live after an openSheet bootstrap. The
176
108
  * initial refetch is intentionally effect-driven so it never blocks bootstrap
177
- * paint; subsequent server RESET_ONLY events are handled by useQuery.
109
+ * paint; later `touched` sheetAccess changes refetch it through useQuery.
178
110
  */
179
- export function useSheetAccessMonitor(
180
- sheetId?: string | null,
181
- enabled: boolean = true,
182
- monitorScopeId: string = '',
183
- ) {
111
+ export function useSheetAccessMonitor(sheetId?: string | null, enabled: boolean = true, monitorScopeId: string = '') {
184
112
  const activeSheetId = enabled ? String(sheetId || '') : '';
185
- const refreshKey = activeSheetId
186
- ? JSON.stringify([activeSheetId, monitorScopeId])
187
- : '';
113
+ const refreshKey = activeSheetId ? JSON.stringify([activeSheetId, monitorScopeId]) : '';
188
114
  const [monitorState, setMonitorState] = useState<{
189
115
  key: string;
190
116
  status: 'READY' | 'ERROR';
@@ -192,6 +118,8 @@ export function useSheetAccessMonitor(
192
118
  const query = useSheetAccess(
193
119
  sheetId,
194
120
  {
121
+ // This monitor explicitly refetches once per access scope below.
122
+ revalidateOnMount: false,
195
123
  skip: !enabled,
196
124
  onCompleted: () => {
197
125
  setMonitorState({ key: refreshKey, status: 'READY' });
@@ -235,10 +163,7 @@ export function useSheetAccessMonitor(
235
163
  return {
236
164
  ...query,
237
165
  loading: enabled && !monitorSettled ? true : query.loading,
238
- sheetAccess:
239
- monitorSettled && monitorState.status === 'READY'
240
- ? query.sheetAccess
241
- : null,
166
+ sheetAccess: monitorSettled && monitorState.status === 'READY' ? query.sheetAccess : null,
242
167
  };
243
168
  }
244
169
 
@@ -247,11 +172,7 @@ export function useSheetAccessMonitor(
247
172
  * the access settings screen alongside the Sheet fragment's base policy.
248
173
  */
249
174
 
250
- export function useSheetAccessGrants(
251
- organizationId?: string | null,
252
- sheetId?: string | null,
253
- params: UseQueryParams = {},
254
- ) {
175
+ export function useSheetAccessGrants(organizationId?: string | null, sheetId?: string | null, params: UseQueryParams = {}) {
255
176
  const { data, ...rest } = useQuery(sheetAccessGrantsQry, {
256
177
  variables: {
257
178
  organizationId,
@@ -268,92 +189,92 @@ export function useSheetAccessGrants(
268
189
  }
269
190
 
270
191
  /*
271
- * Prefetch one authoritative openSheet payload without storing the raw
272
- * transport response in the normalized query cache.
192
+ * Preserve versioned-manifest membership and tab-strip metadata while applying
193
+ * reactive design/content fields. Legacy SheetTab fragments have no manifest
194
+ * ordering fence and therefore cannot add, remove, rename, reorder, or hide a
195
+ * tab once openSheet supplies a manifest revision.
273
196
  */
274
- export function prefetchOpenSheet(
275
- sheetId: string,
276
- params: {
277
- authToken?: string | null;
278
- selectedTabId: string;
279
- sinceSeq?: string | null;
280
- baseSnapshotSeq?: string | null;
281
- baseSnapshotEncodingVersion?: number | null;
282
- baseSnapshotChecksum?: string | null;
283
- viewport?: SheetGridViewportObj | null;
284
- },
285
- ) {
286
- return fetchOpenSheetOnce(sheetId, {
287
- selectedTabId: params.selectedTabId,
288
- sinceSeq: params.sinceSeq,
289
- baseSnapshotSeq: params.baseSnapshotSeq,
290
- baseSnapshotEncodingVersion: params.baseSnapshotEncodingVersion,
291
- baseSnapshotChecksum: params.baseSnapshotChecksum,
292
- viewport: params.viewport,
293
- authToken: params.authToken,
197
+ function getSheetTabsWithAuthoritativeManifestState(reactiveTabs?: SheetTabGQL[] | null, authoritativeTabs?: SheetTabGQL[] | null) {
198
+ if (!reactiveTabs || !authoritativeTabs) {
199
+ return reactiveTabs;
200
+ }
201
+
202
+ const authoritativeTabsById = new Map(authoritativeTabs.map((tab) => [String(tab.id || ''), tab]));
203
+ let changed = false;
204
+
205
+ const projectedTabs = reactiveTabs.map((tab) => {
206
+ const sheetTabId = String(tab.id || '');
207
+ const authoritativeTab = authoritativeTabsById.get(sheetTabId);
208
+ const reactiveDeleted = Boolean((tab as SheetTabGQL & { __deleted?: boolean }).__deleted);
209
+ if (!authoritativeTab) {
210
+ return tab;
211
+ }
212
+
213
+ const hidden = Boolean(authoritativeTab.hidden);
214
+ if (
215
+ String(tab.id || '') === String(authoritativeTab.id || '') &&
216
+ tab.name === authoritativeTab.name &&
217
+ tab.position === authoritativeTab.position &&
218
+ Boolean(tab.hidden) === hidden &&
219
+ !reactiveDeleted
220
+ ) {
221
+ return tab;
222
+ }
223
+
224
+ changed = true;
225
+ return {
226
+ ...tab,
227
+ __deleted: undefined,
228
+ hidden,
229
+ id: authoritativeTab.id,
230
+ name: authoritativeTab.name,
231
+ position: authoritativeTab.position,
232
+ };
294
233
  });
234
+
235
+ return changed ? projectedTabs : reactiveTabs;
295
236
  }
296
237
 
297
238
  /*
298
- * Get reactive Sheet fragment.
239
+ * Get a Sheet fragment whose versioned tab catalog comes from openSheet while
240
+ * ordinary Sheet and SheetTab reactive fields continue to merge independently.
299
241
  */
300
-
301
- export function useReactiveSheetFragment(
302
- sheetId: string,
303
- currentData?: any,
304
- queryCount?: number,
305
- ) {
306
- const sheet = useReactiveFragment(
307
- currentData,
308
- sheetId ? [makeFragmentKey(FRAGMENT_NAMES.sheet, sheetId)] : [],
309
- queryCount,
310
- );
242
+ export function useReactiveSheetFragment(sheetId: string, currentData?: any, queryCount?: number) {
243
+ const sheet = useReactiveFragment(currentData, sheetId ? [makeFragmentKey(FRAGMENT_NAMES.sheet, sheetId)] : [], queryCount);
244
+ const authoritativeManifestTabs =
245
+ currentData?.manifestRevision !== undefined && Array.isArray(currentData?.tabs) ? currentData.tabs : null;
311
246
  const reactiveTabs = useReactiveFragmentMap(
312
- Array.isArray(sheet?.tabs) ? sheet.tabs : null,
247
+ authoritativeManifestTabs || (Array.isArray(sheet?.tabs) ? sheet.tabs : null),
313
248
  getFragmentBaseName(FRAGMENT_NAMES.sheetTab),
314
249
  queryCount,
315
250
  );
316
- const sheetWithDeletedStatus = useMemo(
317
- () =>
318
- mapSheetDeletedStatus(
319
- sheet && reactiveTabs
320
- ? {
321
- ...sheet,
322
- tabs: reactiveTabs.filter((tab: any) => !tab?.__deleted),
323
- }
324
- : sheet,
325
- ),
326
- [reactiveTabs, sheet],
327
- );
251
+ const sheetWithDeletedStatus = useMemo(() => {
252
+ const projectedTabs = getSheetTabsWithAuthoritativeManifestState(
253
+ reactiveTabs,
254
+ authoritativeManifestTabs || (Array.isArray(currentData?.tabs) ? currentData.tabs : null),
255
+ );
256
+
257
+ return mapSheetDeletedStatus(
258
+ sheet && projectedTabs
259
+ ? {
260
+ ...sheet,
261
+ ...(authoritativeManifestTabs ? { manifestRevision: currentData.manifestRevision } : {}),
262
+ tabs: projectedTabs.filter((tab: any) => !tab?.__deleted),
263
+ }
264
+ : sheet,
265
+ );
266
+ }, [authoritativeManifestTabs, currentData?.manifestRevision, currentData?.tabs, reactiveTabs, sheet]);
328
267
 
329
268
  return sheetWithDeletedStatus;
330
269
  }
331
270
 
332
- /*
333
- * Get reactive SheetCell fragments.
334
- */
335
- export function useReactiveSheetCells(cells?: any[] | null) {
336
- return useReactiveFragmentMap(
337
- cells || null,
338
- getFragmentBaseName(FRAGMENT_NAMES.sheetCell),
339
- );
340
- }
341
-
342
271
  /*
343
272
  * Get one background sheet import job's reactive status fragment. The server
344
273
  * emits $sheetImportJobFragment updates to the requesting account as the
345
274
  * import job progresses and finishes.
346
275
  */
347
- export function useReactiveSheetImportJobFragment(
348
- importJobId: string,
349
- currentData?: any,
350
- ) {
351
- return useReactiveFragment(
352
- currentData,
353
- importJobId
354
- ? [makeFragmentKey(FRAGMENT_NAMES.sheetImportJob, importJobId)]
355
- : [],
356
- );
276
+ export function useReactiveSheetImportJobFragment(importJobId: string, currentData?: any) {
277
+ return useReactiveFragment(currentData, importJobId ? [makeFragmentKey(FRAGMENT_NAMES.sheetImportJob, importJobId)] : []);
357
278
  }
358
279
 
359
280
  /*
@@ -364,77 +285,7 @@ export function getSheetViewFragmentKey(sheetViewId: string) {
364
285
  return makeFragmentKey(FRAGMENT_NAMES.sheetView, sheetViewId);
365
286
  }
366
287
 
367
- /*
368
- * Imperatively fetch one Sheet tab's journal tail (every change after the
369
- * client's last-seen seq) to close a detected realtime seq gap without a full
370
- * authoritative tab snapshot. Returns the raw sheetTabChanges result or null;
371
- * tailAvailable: false means the caller must request a new snapshot.
372
- *
373
- * This path deliberately bypasses query/fragment caches and the shared
374
- * in-flight request map. Recovery cursors are one-shot values, and requests
375
- * carrying distinct auth tokens must never share a promise.
376
- */
377
-
378
- export async function fetchSheetTabChangesTail(
379
- organizationId: string,
380
- sheetId: string,
381
- sheetTabId: string,
382
- afterSeq: SheetJournalSeqInput,
383
- params: {
384
- authToken?: string | null;
385
- } = {},
386
- ): Promise<any> {
387
- const variables = {
388
- organizationId,
389
- sheetId,
390
- sheetTabId,
391
- afterSeq: normalizeSheetJournalSeq(afterSeq),
392
- };
393
-
394
- const result = (await graphqlRequestNoStore(sheetTabChangesQry, variables, {
395
- authToken: params.authToken,
396
- })) as { sheetTabChanges?: any; error?: unknown } | null;
397
- if (result?.error) {
398
- return null;
399
- }
400
-
401
- return result?.sheetTabChanges || null;
402
- }
403
-
404
- /*
405
- * Read one browser-tab session's payload-free durable undo/redo stacks.
406
- */
407
- export function useSheetOperationalHistoryQuery(
408
- params: {
409
- organizationId?: string | null;
410
- sessionId?: string | null;
411
- sheetId?: string | null;
412
- sheetTabId?: string | null;
413
- },
414
- queryParams: UseQueryParams = {},
415
- ) {
416
- const variables = {
417
- organizationId: params.organizationId,
418
- sessionId: params.sessionId,
419
- sheetId: params.sheetId,
420
- sheetTabId: params.sheetTabId,
421
- };
422
- const skip =
423
- !variables.organizationId ||
424
- !variables.sessionId ||
425
- !variables.sheetId ||
426
- !variables.sheetTabId ||
427
- Boolean(queryParams.skip);
428
- const { data, ...rest } = useQuery(sheetOperationalHistoryQry, {
429
- ...queryParams,
430
- variables,
431
- skip,
432
- });
433
-
434
- return {
435
- sheetOperationalHistory:
436
- (data?.sheetOperationalHistory as SheetOperationalHistoryGQL | null) ||
437
- null,
438
- ...rest,
439
- };
288
+ /* Observe cached sheet metadata for a list of identities without fetching documents or opening collaboration sessions. */
289
+ export function useReactiveSheetFragmentMap(currentData: { id: string }[] | null) {
290
+ return useReactiveFragmentMap(currentData, getFragmentBaseName(FRAGMENT_NAMES.sheet));
440
291
  }
@@ -1,51 +1,26 @@
1
+ import { FRAGMENT_NAMES, makeFragmentKey } from '@jsb188/mday/constants/graphqlReactivity.ts';
2
+ import { useCallback, useEffect, useRef, useState } from 'react';
1
3
  import type { GeneratedFileGQL } from '@jsb188/mday/types/storage.d.ts';
2
- import {
3
- FRAGMENT_NAMES,
4
- makeFragmentKey,
5
- } from '@jsb188/mday/constants/graphqlReactivity.ts';
6
- import {
7
- generatedFileQry,
8
- generatedFilesQry,
9
- } from '../gql/queries/storageQueries.ts';
10
4
  import { useQuery, useReactiveFragment } from '../client/index.ts';
5
+ import { generatedFileQry, generatedFilesQry } from '../gql/queries/storageQueries.ts';
11
6
  import type { PaginationArgs, UseQueryParams } from '../types.d.ts';
12
7
 
13
8
  /**
14
9
  * Constants
15
10
  */
16
11
 
17
- const GENERATED_FILES_LIST_LIMIT = 100;
18
-
19
- /**
20
- * Get reactive log fragment
21
- */
22
-
23
- export function useReactiveStorageFragment(
24
- storageId: string,
25
- currentData?: any,
26
- queryCount?: number,
27
- ) {
28
- return useReactiveFragment(
29
- currentData,
30
- [makeFragmentKey(FRAGMENT_NAMES.storageFile, storageId)],
31
- queryCount,
32
- );
33
- }
12
+ const FILE_CONSTANTS = {
13
+ listLimit: 100,
14
+ pollIntervalMS: 10000,
15
+ maxPollAttempts: 30,
16
+ };
34
17
 
35
18
  /**
36
19
  * Get reactive generated file fragment (AI CSV exports).
37
20
  */
38
21
 
39
- export function useReactiveGeneratedFileFragment(
40
- code: string,
41
- currentData?: any,
42
- queryCount?: number,
43
- ) {
44
- return useReactiveFragment(
45
- currentData,
46
- [makeFragmentKey(FRAGMENT_NAMES.generatedFile, code)],
47
- queryCount,
48
- );
22
+ export function useReactiveGeneratedFileFragment(code: string, currentData?: any, queryCount?: number) {
23
+ return useReactiveFragment(currentData, [makeFragmentKey(FRAGMENT_NAMES.generatedFile, code)], queryCount);
49
24
  }
50
25
 
51
26
  /**
@@ -64,7 +39,7 @@ export function useGeneratedFiles(
64
39
  ...variables,
65
40
  cursor: null,
66
41
  after: true,
67
- limit: GENERATED_FILES_LIST_LIMIT,
42
+ limit: FILE_CONSTANTS.listLimit,
68
43
  },
69
44
  skip: !variables.organizationId || params.skip,
70
45
  });
@@ -75,38 +50,86 @@ export function useGeneratedFiles(
75
50
  };
76
51
  }
77
52
 
78
- /**
79
- * Fetch one generated file; serves reactive cache data first so a deep link
80
- * that arrives after the list loaded does not refetch.
53
+ /*
54
+ * Reauthorize one generated-file detail whenever it opens, then observe its
55
+ * fragment. Requester-only events may not reach other authorized viewers, so
56
+ * pending details also refresh at a bounded rate while mounted.
81
57
  */
82
-
83
- export function useGeneratedFile(
84
- variables: {
85
- organizationId?: string | null;
86
- code?: string | null;
87
- },
88
- params: UseQueryParams = {},
89
- ) {
90
- const cachedGeneratedFile = useReactiveGeneratedFileFragment(
91
- variables.code || '',
92
- null,
93
- );
94
- const shouldFetchGeneratedFile =
95
- !!variables.organizationId &&
96
- !!variables.code &&
97
- !cachedGeneratedFile &&
98
- !params.skip;
58
+ export function useGeneratedFile(variables: { organizationId?: string | null; code?: string | null }, params: UseQueryParams = {}) {
59
+ const detailKey = `${variables.organizationId || ''}:${variables.code || ''}`;
60
+ const [pollAttempt, setPollAttempt] = useState(0);
61
+ const shouldFetch = !!variables.organizationId && !!variables.code && !params.skip;
62
+ const activation = useRef({
63
+ detailKey,
64
+ shouldFetch,
65
+ requested: false,
66
+ refreshed: false,
67
+ });
68
+ if (activation.current.detailKey !== detailKey || activation.current.shouldFetch !== shouldFetch) {
69
+ activation.current = {
70
+ detailKey,
71
+ shouldFetch,
72
+ requested: false,
73
+ refreshed: false,
74
+ };
75
+ }
99
76
  const { data, ...rest } = useQuery(generatedFileQry, {
100
77
  ...params,
78
+ persistTime: 0,
101
79
  variables,
102
- skip: !shouldFetchGeneratedFile,
80
+ skip: !shouldFetch,
103
81
  });
82
+ const reactiveFile = useReactiveGeneratedFileFragment(variables.code || '', data?.generatedFile, rest.updatedCount);
83
+ const matchesVariables = rest.variables?.organizationId === variables.organizationId && rest.variables?.code === variables.code;
84
+ if (shouldFetch && rest.loading === true) activation.current.requested = true;
85
+ if (activation.current.requested && matchesVariables && rest.loading === false) activation.current.refreshed = true;
86
+ const refreshed = shouldFetch && activation.current.refreshed;
87
+ // A stale fragment never overrides an authorization error or a fresh null lookup.
88
+ const generatedFile =
89
+ refreshed && matchesVariables && !rest.error && data?.generatedFile
90
+ ? ((reactiveFile || data.generatedFile) as GeneratedFileGQL)
91
+ : undefined;
92
+ const pending = generatedFile?.status === 'PENDING' || generatedFile?.status === 'PROCESSING';
93
+ const shouldPoll = shouldFetch && pending && !rest.error && !rest.loading && rest.screenIsFocused && rest.connectedToServer;
94
+
95
+ useEffect(() => {
96
+ setPollAttempt(0);
97
+ }, [detailKey, shouldFetch]);
98
+
99
+ /*
100
+ * A just-fetched query can have age zero and skip automatic revalidation.
101
+ * Refresh it on activation unless useQuery has already started a request.
102
+ */
103
+ useEffect(() => {
104
+ if (!shouldFetch || refreshed || rest.loading) return;
105
+ const timeoutId = globalThis.setTimeout(() => {
106
+ void rest.refetch();
107
+ }, 0);
108
+ return () => globalThis.clearTimeout(timeoutId);
109
+ }, [detailKey, shouldFetch, refreshed, rest.loading, rest.refetch]);
110
+
111
+ useEffect(() => {
112
+ if (!shouldPoll || pollAttempt >= FILE_CONSTANTS.maxPollAttempts) return;
113
+ const timeoutId = globalThis.setTimeout(() => {
114
+ setPollAttempt((attempt) => attempt + 1);
115
+ void rest.refetch();
116
+ }, FILE_CONSTANTS.pollIntervalMS);
117
+ return () => globalThis.clearTimeout(timeoutId);
118
+ }, [shouldPoll, pollAttempt, rest.refetch]);
119
+
120
+ /*
121
+ * A user-requested refresh starts a new bounded polling window.
122
+ */
123
+ const refetch = useCallback(() => {
124
+ setPollAttempt(0);
125
+ return rest.refetch();
126
+ }, [rest.refetch]);
104
127
 
105
128
  return {
106
- generatedFile: (cachedGeneratedFile || data?.generatedFile) as
107
- | GeneratedFileGQL
108
- | undefined,
109
- initialLoading: shouldFetchGeneratedFile && !data,
110
129
  ...rest,
130
+ generatedFile,
131
+ initialLoading: shouldFetch && !refreshed && !rest.error,
132
+ pollingExhausted: pending && pollAttempt >= FILE_CONSTANTS.maxPollAttempts,
133
+ refetch,
111
134
  };
112
135
  }