@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,157 +1,226 @@
1
1
  import { useQuery } from '@jsb188/graphql/client';
2
- import {
3
- scheduleCalendarsQry,
4
- scheduleEvaluationQry,
5
- scheduleOccurrencesQry,
6
- scheduleRequestsQry,
7
- } from '../gql/queries/scheduleQueries.ts';
2
+ import type { DocumentNode } from 'graphql';
3
+ import * as documents from '../gql/queries/scheduleQueries.ts';
4
+ import type * as Schedule from '../schedule.d.ts';
8
5
  import type { UseQueryParams } from '../types.d.ts';
9
6
 
7
+ type ScheduleQueryResult<K extends string, R> = Omit<
8
+ ReturnType<typeof useQuery>,
9
+ 'data'
10
+ > &
11
+ Record<K, R | null>;
12
+
10
13
  /*
11
- * Fetch the calendars one organization can see, optionally including the ones
12
- * its network shares with it.
14
+ * Compose one schedule read hook. Variables stay top-level so they line up
15
+ * with the scope keys of the server's changes; the query is skipped until every
16
+ * required variable is set. A string argument fills the first required variable.
13
17
  */
14
-
15
- export function useScheduleCalendars(
16
- organizationId?: string | null,
17
- includeNetwork?: boolean,
18
- params: UseQueryParams = {},
18
+ function composeScheduleQueryHook<V extends object, R, K extends string>(
19
+ document: DocumentNode,
20
+ resultKey: K,
21
+ requiredKeys: Array<keyof V & string>,
19
22
  ) {
20
- const { data, ...rest } = useQuery(scheduleCalendarsQry, {
21
- variables: {
22
- organizationId,
23
- includeNetwork: !!includeNetwork,
24
- },
25
- ...params,
26
- skip: !organizationId || !!params.skip,
27
- });
28
-
29
- return {
30
- ...rest,
31
- scheduleCalendars: data?.scheduleCalendars || null,
23
+ /*
24
+ * Read one schedule query and return its result under the query's name.
25
+ */
26
+ return function useScheduleQuery(
27
+ p: Partial<V> | string | null | undefined,
28
+ params: UseQueryParams = {},
29
+ ): ScheduleQueryResult<K, R> {
30
+ const variables = (
31
+ typeof p === 'string' ? { [requiredKeys[0]]: p } : p || {}
32
+ ) as Partial<V>;
33
+ const { data, ...rest } = useQuery(document, {
34
+ ...params,
35
+ variables: { ...variables, ...params.variables },
36
+ skip: requiredKeys.some((key) => !variables[key]) || !!params.skip,
37
+ });
38
+ return {
39
+ ...rest,
40
+ [resultKey]: (data?.[resultKey] ?? null) as R | null,
41
+ } as ScheduleQueryResult<K, R>;
32
42
  };
33
43
  }
34
44
 
35
45
  /*
36
- * Fetch every occurrence overlapping one window on one calendar.
37
- *
38
- * Deliberately one calendar per call rather than a list: the realtime reset
39
- * key matches on an exact per-variable value, so a change on one calendar
40
- * leaves every other overlaid calendar's cached window untouched. A page
41
- * showing three calendars runs three of these.
42
- *
43
- * The window bounds are passed as ISO strings so the cache key is stable —
44
- * two Date objects for the same instant are different cache keys.
45
- */
46
-
47
- export function useScheduleOccurrences(
48
- p: {
49
- organizationId?: string | null;
50
- calendarId?: string | null;
51
- fromISO?: string | null;
52
- toISO?: string | null;
53
- publishedOnly?: boolean;
54
- mineOnly?: boolean;
55
- limit?: number | null;
56
- },
57
- params: UseQueryParams = {},
58
- ) {
59
- const { data, ...rest } = useQuery(scheduleOccurrencesQry, {
60
- variables: {
61
- organizationId: p.organizationId,
62
- calendarId: p.calendarId,
63
- from: p.fromISO,
64
- to: p.toISO,
65
- publishedOnly: !!p.publishedOnly,
66
- mineOnly: !!p.mineOnly,
67
- limit: p.limit ?? null,
68
- },
69
- ...params,
70
- skip:
71
- !p.organizationId ||
72
- !p.calendarId ||
73
- !p.fromISO ||
74
- !p.toISO ||
75
- !!params.skip,
76
- });
77
-
78
- return {
79
- ...rest,
80
- scheduleOccurrences: data?.scheduleOccurrences || null,
81
- };
82
- }
46
+ * Read the organization's work calendars with their dated exceptions.
47
+ */
48
+ export const useScheduleWorkCalendars = composeScheduleQueryHook<
49
+ Schedule.ScheduleWorkCalendarsVariables,
50
+ Array<Schedule.ScheduleWorkCalendarGQL>,
51
+ 'scheduleWorkCalendars'
52
+ >(documents.scheduleWorkCalendarsQry, 'scheduleWorkCalendars', [
53
+ 'organizationId',
54
+ ]);
83
55
 
84
56
  /*
85
- * Evaluate one calendar window for constraint breaches.
86
- *
87
- * Never cached: the entire value of this query is that it reflects the
88
- * schedule as it stands right now, so a stale verdict is worse than no
89
- * verdict — it would tell a manager the roster is legal after somebody else
90
- * broke it.
57
+ * Read one work calendar with its dated exceptions.
91
58
  */
59
+ export const useScheduleWorkCalendar = composeScheduleQueryHook<
60
+ Schedule.ScheduleWorkCalendarVariables,
61
+ Schedule.ScheduleWorkCalendarGQL,
62
+ 'scheduleWorkCalendar'
63
+ >(documents.scheduleWorkCalendarQry, 'scheduleWorkCalendar', [
64
+ 'organizationId',
65
+ 'workCalendarId',
66
+ ]);
92
67
 
93
- export function useScheduleEvaluation(
94
- p: {
95
- organizationId?: string | null;
96
- calendarId?: string | null;
97
- fromISO?: string | null;
98
- toISO?: string | null;
99
- },
100
- params: UseQueryParams = {},
101
- ) {
102
- const { data, ...rest } = useQuery(scheduleEvaluationQry, {
103
- variables: {
104
- organizationId: p.organizationId,
105
- calendarId: p.calendarId,
106
- from: p.fromISO,
107
- to: p.toISO,
108
- },
109
- ...params,
110
- skip:
111
- !p.organizationId ||
112
- !p.calendarId ||
113
- !p.fromISO ||
114
- !p.toISO ||
115
- !!params.skip,
116
- });
117
-
118
- return {
119
- ...rest,
120
- scheduleEvaluation: data?.scheduleEvaluation || null,
121
- };
122
- }
68
+ /*
69
+ * Read the organization's resources, optionally of some kinds only.
70
+ */
71
+ export const useScheduleResources = composeScheduleQueryHook<
72
+ Schedule.ScheduleResourcesVariables,
73
+ Array<Schedule.ScheduleResourceGQL>,
74
+ 'scheduleResources'
75
+ >(documents.scheduleResourcesQry, 'scheduleResources', ['organizationId']);
123
76
 
124
77
  /*
125
- * Fetch schedule requests: the approval queue, one's own, or the open shifts
126
- * offered to the viewer.
78
+ * Read one resource with its capacity periods and the viewer's own channels.
127
79
  */
80
+ export const useScheduleResource = composeScheduleQueryHook<
81
+ Schedule.ScheduleResourceVariables,
82
+ Schedule.ScheduleResourceGQL,
83
+ 'scheduleResource'
84
+ >(documents.scheduleResourceQry, 'scheduleResource', [
85
+ 'organizationId',
86
+ 'resourceId',
87
+ ]);
128
88
 
129
- export function useScheduleRequests(
130
- p: {
131
- organizationId?: string | null;
132
- calendarId?: string | null;
133
- statuses?: string[] | null;
134
- mineOnly?: boolean;
135
- offeredToMe?: boolean;
136
- limit?: number | null;
137
- },
138
- params: UseQueryParams = {},
139
- ) {
140
- const { data, ...rest } = useQuery(scheduleRequestsQry, {
141
- variables: {
142
- organizationId: p.organizationId,
143
- calendarId: p.calendarId ?? null,
144
- statuses: p.statuses ?? null,
145
- mineOnly: !!p.mineOnly,
146
- offeredToMe: !!p.offeredToMe,
147
- limit: p.limit ?? null,
148
- },
149
- ...params,
150
- skip: !p.organizationId || !!params.skip,
151
- });
152
-
153
- return {
154
- ...rest,
155
- scheduleRequests: data?.scheduleRequests || null,
156
- };
157
- }
89
+ /*
90
+ * Read the activity code types of an organization or one project.
91
+ */
92
+ export const useScheduleCodeTypes = composeScheduleQueryHook<
93
+ Schedule.ScheduleCodeTypesVariables,
94
+ Array<Schedule.ScheduleCodeTypeGQL>,
95
+ 'scheduleCodeTypes'
96
+ >(documents.scheduleCodeTypesQry, 'scheduleCodeTypes', ['organizationId']);
97
+
98
+ /*
99
+ * Read per-day resource demand and availability for a project histogram.
100
+ */
101
+ export const useScheduleResourceUsage = composeScheduleQueryHook<
102
+ Schedule.ScheduleResourceUsageVariables,
103
+ Array<Schedule.ScheduleResourceUsageDayGQL>,
104
+ 'scheduleResourceUsage'
105
+ >(documents.scheduleResourceUsageQry, 'scheduleResourceUsage', [
106
+ 'organizationId',
107
+ 'projectId',
108
+ 'from',
109
+ 'to',
110
+ ]);
111
+
112
+ /*
113
+ * Read the schedule calendars (locations) the viewer can see.
114
+ */
115
+ export const useScheduleCalendars = composeScheduleQueryHook<
116
+ Schedule.ScheduleCalendarsVariables,
117
+ Array<Schedule.ScheduleCalendarGQL>,
118
+ 'scheduleCalendars'
119
+ >(documents.scheduleCalendarsQry, 'scheduleCalendars', ['organizationId']);
120
+
121
+ /*
122
+ * Read one calendar's roster window for calendar-local dates fromDate..toDate:
123
+ * its seats with the roles, rooms and people they name.
124
+ */
125
+ export const useScheduleShifts = composeScheduleQueryHook<
126
+ Schedule.ScheduleShiftsVariables,
127
+ Schedule.ScheduleRosterWindowGQL,
128
+ 'scheduleShifts'
129
+ >(documents.scheduleShiftsQry, 'scheduleShifts', [
130
+ 'organizationId',
131
+ 'calendarId',
132
+ 'fromDate',
133
+ 'toDate',
134
+ ]);
135
+
136
+ /*
137
+ * Read the viewer's own seats across calendars (identity-authorized).
138
+ */
139
+ export const useScheduleMyShifts = composeScheduleQueryHook<
140
+ Schedule.ScheduleMyShiftsVariables,
141
+ Schedule.ScheduleShiftWindowGQL,
142
+ 'scheduleMyShifts'
143
+ >(documents.scheduleMyShiftsQry, 'scheduleMyShifts', [
144
+ 'organizationId',
145
+ 'fromDate',
146
+ 'toDate',
147
+ ]);
148
+
149
+ /*
150
+ * Read one calendar's stored rule findings for fromDate..toDate.
151
+ */
152
+ export const useScheduleViolations = composeScheduleQueryHook<
153
+ Schedule.ScheduleViolationsVariables,
154
+ Array<Schedule.ScheduleViolationGQL>,
155
+ 'scheduleViolations'
156
+ >(documents.scheduleViolationsQry, 'scheduleViolations', [
157
+ 'organizationId',
158
+ 'calendarId',
159
+ 'fromDate',
160
+ 'toDate',
161
+ ]);
162
+
163
+ /*
164
+ * Read time off the viewer may see.
165
+ */
166
+ export const useScheduleTimeOffList = composeScheduleQueryHook<
167
+ Schedule.ScheduleTimeOffListVariables,
168
+ Array<Schedule.ScheduleTimeOffGQL>,
169
+ 'scheduleTimeOffList'
170
+ >(documents.scheduleTimeOffListQry, 'scheduleTimeOffList', ['organizationId']);
171
+
172
+ /*
173
+ * Read the to-dos of one location (calendarId) or one project (projectId): name
174
+ * exactly one. Default statuses: the open ones.
175
+ */
176
+ export const useScheduleTasks = composeScheduleQueryHook<
177
+ Schedule.ScheduleTasksVariables,
178
+ Array<Schedule.ScheduleTaskGQL>,
179
+ 'scheduleTasks'
180
+ >(documents.scheduleTasksQry, 'scheduleTasks', ['organizationId']);
181
+
182
+ /*
183
+ * Read the viewer's own to-dos across the organization (by their own PERSON).
184
+ * Default statuses: the open ones.
185
+ */
186
+ export const useScheduleMyTasks = composeScheduleQueryHook<
187
+ Schedule.ScheduleMyTasksVariables,
188
+ Array<Schedule.ScheduleTaskGQL>,
189
+ 'scheduleMyTasks'
190
+ >(documents.scheduleMyTasksQry, 'scheduleMyTasks', ['organizationId']);
191
+
192
+ /*
193
+ * Read the organization's projects.
194
+ */
195
+ export const useScheduleProjects = composeScheduleQueryHook<
196
+ Schedule.ScheduleProjectsVariables,
197
+ Array<Schedule.ScheduleProjectGQL>,
198
+ 'scheduleProjects'
199
+ >(documents.scheduleProjectsQry, 'scheduleProjects', ['organizationId']);
200
+
201
+ /*
202
+ * Read one project's network: WBS, activities, links, assignments and inputs.
203
+ */
204
+ export const useScheduleProjectNetwork = composeScheduleQueryHook<
205
+ Schedule.ScheduleProjectNetworkVariables,
206
+ Schedule.ScheduleProjectNetworkGQL,
207
+ 'scheduleProjectNetwork'
208
+ >(documents.scheduleProjectNetworkQry, 'scheduleProjectNetwork', ['projectId']);
209
+
210
+ /*
211
+ * Read one project's schedule runs, newest first.
212
+ */
213
+ export const useScheduleProjectRuns = composeScheduleQueryHook<
214
+ Schedule.ScheduleProjectRunsVariables,
215
+ Array<Schedule.ScheduleProjectRunGQL>,
216
+ 'scheduleProjectRuns'
217
+ >(documents.scheduleProjectRunsQry, 'scheduleProjectRuns', ['projectId']);
218
+
219
+ /*
220
+ * Read one project's baselines.
221
+ */
222
+ export const useScheduleBaselines = composeScheduleQueryHook<
223
+ Schedule.ScheduleBaselinesVariables,
224
+ Array<Schedule.ScheduleBaselineGQL>,
225
+ 'scheduleBaselines'
226
+ >(documents.scheduleBaselinesQry, 'scheduleBaselines', ['projectId']);
@@ -0,0 +1,218 @@
1
+ import { useEffect, useMemo, useRef } from 'react';
2
+ import {
3
+ FRAGMENT_NAMES,
4
+ QUERY_NAMES,
5
+ makeFragmentKey,
6
+ getFragmentBaseName,
7
+ type WSFragmentName,
8
+ } from '@jsb188/mday/constants/graphqlReactivity.ts';
9
+ import { useRealtimeConnectionGenerationValue } from '@jsb188/react/states';
10
+ import { graphqlRequestNoStore } from '../client/request.ts';
11
+ import { resetQueryByTarget } from '../cache/index.ts';
12
+ import { scheduleCalendarRevisionQry, scheduleProjectRevisionQry } from '../gql/queries/scheduleQueries.ts';
13
+ import { useReactiveFragment, useReactiveFragmentMap, useUpdateObservers } from './index.ts';
14
+ import type * as Schedule from '../schedule.d.ts';
15
+
16
+ // A row the server deleted: a realtime tombstone or a soft-deleted row
17
+ type ScheduleReactiveRow = { __deleted?: boolean; deletedAt?: string | null };
18
+
19
+ /* Observe one row by its canonical fragment key; a deleted row reads as null. */
20
+ function useReactiveScheduleRow<T>(
21
+ fragmentName: WSFragmentName,
22
+ id: string | null | undefined,
23
+ data: T | null,
24
+ queryCount?: number | boolean,
25
+ ): T | null {
26
+ const row = useReactiveFragment(data, id ? [makeFragmentKey(fragmentName, id)] : [], queryCount);
27
+ return row?.__deleted || row?.deletedAt ? null : row;
28
+ }
29
+
30
+ /* Observe rows of one fragment and drop the rows the server deleted. */
31
+ function useReactiveScheduleRows<T>(fragmentName: WSFragmentName, rows: T[] | null, queryCount?: number | boolean): T[] | null {
32
+ const live = useReactiveFragmentMap(rows, getFragmentBaseName(fragmentName), queryCount);
33
+ return useMemo(() => live?.filter((row: T & ScheduleReactiveRow) => !row.__deleted && !row.deletedAt) ?? null, [live]);
34
+ }
35
+
36
+ /* Observe one project row using the server's canonical fragment key. */
37
+ export function useReactiveScheduleProject(
38
+ id: string | null | undefined,
39
+ data: Schedule.ScheduleProjectGQL | null,
40
+ queryCount?: number | boolean,
41
+ ): Schedule.ScheduleProjectGQL | null {
42
+ return useReactiveScheduleRow(FRAGMENT_NAMES.scheduleProject, id, data, queryCount);
43
+ }
44
+
45
+ /* Observe activity rows and remove authoritative deletion tombstones. */
46
+ export function useReactiveScheduleActivityMap(
47
+ rows: Schedule.ScheduleActivityGQL[] | null,
48
+ queryCount?: number | boolean,
49
+ ): Schedule.ScheduleActivityGQL[] | null {
50
+ return useReactiveScheduleRows(FRAGMENT_NAMES.scheduleActivity, rows, queryCount);
51
+ }
52
+
53
+ /* Observe relationship rows and remove authoritative deletion tombstones. */
54
+ export function useReactiveScheduleRelationshipMap(
55
+ rows: Schedule.ScheduleRelationshipGQL[] | null,
56
+ queryCount?: number | boolean,
57
+ ): Schedule.ScheduleRelationshipGQL[] | null {
58
+ return useReactiveScheduleRows(FRAGMENT_NAMES.scheduleRelationship, rows, queryCount);
59
+ }
60
+
61
+ /* Observe one project run row using the server's canonical fragment key. */
62
+ export function useReactiveScheduleProjectRun(
63
+ id: string | null | undefined,
64
+ data: Schedule.ScheduleProjectRunGQL | null,
65
+ queryCount?: number | boolean,
66
+ ): Schedule.ScheduleProjectRunGQL | null {
67
+ return useReactiveScheduleRow(FRAGMENT_NAMES.scheduleRun, id, data, queryCount);
68
+ }
69
+
70
+ /* Observe project run rows and remove authoritative deletion tombstones. */
71
+ export function useReactiveScheduleProjectRunMap(
72
+ rows: Schedule.ScheduleProjectRunGQL[] | null,
73
+ queryCount?: number | boolean,
74
+ ): Schedule.ScheduleProjectRunGQL[] | null {
75
+ return useReactiveScheduleRows(FRAGMENT_NAMES.scheduleRun, rows, queryCount);
76
+ }
77
+
78
+ /* Observe calendar rows and remove deleted calendars. */
79
+ export function useReactiveScheduleCalendarMap(
80
+ rows: Schedule.ScheduleCalendarGQL[] | null,
81
+ queryCount?: number | boolean,
82
+ ): Schedule.ScheduleCalendarGQL[] | null {
83
+ return useReactiveScheduleRows(FRAGMENT_NAMES.scheduleCalendar, rows, queryCount);
84
+ }
85
+
86
+ /* Observe seat rows and remove deleted drafts; canceled seats stay. */
87
+ export function useReactiveScheduleShiftMap(
88
+ rows: Schedule.ScheduleShiftGQL[] | null,
89
+ queryCount?: number | boolean,
90
+ ): Schedule.ScheduleShiftGQL[] | null {
91
+ return useReactiveScheduleRows(FRAGMENT_NAMES.scheduleShift, rows, queryCount);
92
+ }
93
+
94
+ /* Observe role rows and remove deleted roles; archived roles stay. */
95
+ export function useReactiveScheduleRoleMap(
96
+ rows: Schedule.ScheduleRoleGQL[] | null,
97
+ queryCount?: number | boolean,
98
+ ): Schedule.ScheduleRoleGQL[] | null {
99
+ return useReactiveScheduleRows(FRAGMENT_NAMES.scheduleRole, rows, queryCount);
100
+ }
101
+
102
+ /* Observe resource rows and remove authoritative deletion tombstones. */
103
+ export function useReactiveScheduleResourceMap(
104
+ rows: Schedule.ScheduleResourceGQL[] | null,
105
+ queryCount?: number | boolean,
106
+ ): Schedule.ScheduleResourceGQL[] | null {
107
+ return useReactiveScheduleRows(FRAGMENT_NAMES.scheduleResource, rows, queryCount);
108
+ }
109
+
110
+ /* Observe work calendar rows and remove deleted work calendars. */
111
+ export function useReactiveScheduleWorkCalendarMap(
112
+ rows: Schedule.ScheduleWorkCalendarGQL[] | null,
113
+ queryCount?: number | boolean,
114
+ ): Schedule.ScheduleWorkCalendarGQL[] | null {
115
+ return useReactiveScheduleRows(FRAGMENT_NAMES.scheduleWorkCalendar, rows, queryCount);
116
+ }
117
+
118
+ /* Observe time-off rows and remove authoritative deletion tombstones. */
119
+ export function useReactiveScheduleTimeOffMap(
120
+ rows: Schedule.ScheduleTimeOffGQL[] | null,
121
+ queryCount?: number | boolean,
122
+ ): Schedule.ScheduleTimeOffGQL[] | null {
123
+ return useReactiveScheduleRows(FRAGMENT_NAMES.scheduleTimeOff, rows, queryCount);
124
+ }
125
+
126
+ /* Observe rule findings and remove authoritative deletion tombstones. */
127
+ export function useReactiveScheduleViolationMap(
128
+ rows: Schedule.ScheduleViolationGQL[] | null,
129
+ queryCount?: number | boolean,
130
+ ): Schedule.ScheduleViolationGQL[] | null {
131
+ return useReactiveScheduleRows(FRAGMENT_NAMES.scheduleViolation, rows, queryCount);
132
+ }
133
+
134
+ /* Reset the calendar-scoped window queries (seats and findings) of one calendar. */
135
+ function resetScheduleCalendarWindows(
136
+ calendarId: string,
137
+ organizationId: string | undefined,
138
+ updateObservers: ReturnType<typeof useUpdateObservers>,
139
+ ) {
140
+ const match = { calendarId, ...(organizationId ? { organizationId } : {}) };
141
+ for (const name of [QUERY_NAMES.scheduleShifts, QUERY_NAMES.scheduleViolations]) {
142
+ resetQueryByTarget({ name, match }, true, updateObservers);
143
+ }
144
+ }
145
+
146
+ /* Recover missed realtime events with one indexed revision read on focus or reconnect. */
147
+ function useScheduleRevisionCheck(p: {
148
+ kind: 'project' | 'calendar';
149
+ id: string | null | undefined;
150
+ organizationId?: string;
151
+ revision: string | number | null | undefined;
152
+ }) {
153
+ const generation = useRealtimeConnectionGenerationValue();
154
+ const updateObservers = useUpdateObservers();
155
+ const current = useRef(p);
156
+ const lastCheck = useRef(0);
157
+ current.current = p;
158
+ useEffect(() => {
159
+ let disposed = false;
160
+ let controller: AbortController | null = null;
161
+ /* Compare only the currently mounted document and ignore stale in-flight reads. */
162
+ const check = async () => {
163
+ const input = current.current;
164
+ if (!input.id || input.revision == null || document.visibilityState !== 'visible' || Date.now() - lastCheck.current < 2000)
165
+ return;
166
+ lastCheck.current = Date.now();
167
+ controller?.abort();
168
+ controller = new AbortController();
169
+ const result =
170
+ input.kind === 'project'
171
+ ? await graphqlRequestNoStore(scheduleProjectRevisionQry, { projectId: input.id }, undefined, controller.signal)
172
+ : await graphqlRequestNoStore(scheduleCalendarRevisionQry, { calendarId: input.id }, undefined, controller.signal);
173
+ if (disposed || current.current.id !== input.id || result.error || result.aborted) return;
174
+ const server = input.kind === 'project' ? result.scheduleProject?.networkRevision : result.scheduleCalendar?.revision;
175
+ if (server == null || String(server) === String(current.current.revision)) return;
176
+ if (input.kind === 'project') {
177
+ resetQueryByTarget({ name: QUERY_NAMES.scheduleProjectNetwork, match: { projectId: input.id } }, true, updateObservers);
178
+ } else {
179
+ resetScheduleCalendarWindows(input.id, input.organizationId, updateObservers);
180
+ }
181
+ };
182
+ void check();
183
+ document.addEventListener('visibilitychange', check);
184
+ window.addEventListener('focus', check);
185
+ return () => {
186
+ disposed = true;
187
+ controller?.abort();
188
+ document.removeEventListener('visibilitychange', check);
189
+ window.removeEventListener('focus', check);
190
+ };
191
+ }, [generation, p.id, p.kind]);
192
+ }
193
+
194
+ /* Reconcile a project network after a missed socket event or background interval. */
195
+ export function useScheduleProjectRevisionCheck(
196
+ projectId: string | null | undefined,
197
+ loadedNetworkRevision: string | number | null | undefined,
198
+ ) {
199
+ useScheduleRevisionCheck({
200
+ kind: 'project',
201
+ id: projectId,
202
+ revision: loadedNetworkRevision,
203
+ });
204
+ }
205
+
206
+ /* Reconcile a calendar's seats and findings after a missed socket event or background interval. */
207
+ export function useScheduleCalendarRevisionCheck(
208
+ calendarId: string | null | undefined,
209
+ loadedRevision: string | number | null | undefined,
210
+ organizationId?: string,
211
+ ) {
212
+ useScheduleRevisionCheck({
213
+ kind: 'calendar',
214
+ id: calendarId,
215
+ revision: loadedRevision,
216
+ organizationId,
217
+ });
218
+ }