@jsb188/graphql 1.1.10 → 1.1.12

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 (176) hide show
  1. package/audience.d.ts +343 -0
  2. package/cache/config.ts +98 -50
  3. package/cache/index.chat-activity.test.ts +119 -0
  4. package/cache/index.chat.test.ts +92 -0
  5. package/cache/index.helpers.ts +96 -0
  6. package/cache/index.regression.test.ts +149 -0
  7. package/cache/index.test.ts +1017 -127
  8. package/cache/index.ts +1256 -1269
  9. package/cache/optimistic-ui-runtime.ts +46 -0
  10. package/cache/optimistic-ui.ts +401 -0
  11. package/cache/persist.ts +800 -0
  12. package/cache/store.ts +395 -0
  13. package/cache/value.helpers.ts +17 -0
  14. package/client/draft-persistence-indexed-db.ts +413 -0
  15. package/client/draft-persistence.ts +152 -0
  16. package/client/index.ts +3 -0
  17. package/client/request.helpers.ts +14 -0
  18. package/client/request.ts +870 -164
  19. package/deno.lock +187 -0
  20. package/deno.test.json +10 -4
  21. package/gql/eagerQueryMap.ts +12 -0
  22. package/gql/fragments/accountAuthFragments.ts +5 -0
  23. package/gql/fragments/accountFragments.ts +0 -3
  24. package/gql/fragments/accountSessionFragments.ts +16 -0
  25. package/gql/fragments/agentMemoryFragments.ts +23 -0
  26. package/gql/fragments/agentNoteFragments.ts +150 -0
  27. package/gql/fragments/aiChatFragments.ts +42 -0
  28. package/gql/fragments/audienceFragments.ts +141 -0
  29. package/gql/fragments/campaignFragments.ts +566 -0
  30. package/gql/fragments/dataTableFragments.ts +50 -192
  31. package/gql/fragments/emailFragments.ts +32 -17
  32. package/gql/fragments/fieldDocFragments.ts +264 -0
  33. package/gql/fragments/inboundContactFragments.ts +2 -0
  34. package/gql/fragments/logFragments.ts +101 -69
  35. package/gql/fragments/organizationFragments.ts +53 -5
  36. package/gql/fragments/qrCodeFragments.ts +29 -0
  37. package/gql/fragments/scheduleFragments.ts +667 -0
  38. package/gql/fragments/sheetFragments.ts +304 -63
  39. package/gql/fragments/storageFragments.ts +45 -0
  40. package/gql/fragments/workflowFragments.ts +41 -5
  41. package/gql/mutations/accountMutations.ts +0 -2
  42. package/gql/mutations/agentMemoryMutations.ts +68 -0
  43. package/gql/mutations/agentNoteMutations.ts +62 -0
  44. package/gql/mutations/aiChatMutations.ts +49 -4
  45. package/gql/mutations/audienceMutations.ts +71 -0
  46. package/gql/mutations/authMutations.ts +203 -18
  47. package/gql/mutations/billingMutations.ts +17 -27
  48. package/gql/mutations/campaignMutations.ts +258 -0
  49. package/gql/mutations/contactMutations.ts +23 -0
  50. package/gql/mutations/dataTableMutations.ts +19 -54
  51. package/gql/mutations/fieldDocMutations.ts +173 -0
  52. package/gql/mutations/index.ts +10 -2
  53. package/gql/mutations/logMutations.ts +6 -29
  54. package/gql/mutations/organizationMutations.ts +19 -4
  55. package/gql/mutations/qrCodeMutations.ts +20 -0
  56. package/gql/mutations/scheduleMutations.ts +1121 -0
  57. package/gql/mutations/sheetMutations.ts +221 -60
  58. package/gql/mutations/storageMutations.ts +19 -0
  59. package/gql/mutations/workflowMutations.ts +60 -0
  60. package/gql/queries/agentMemoryQueries.ts +28 -0
  61. package/gql/queries/agentNoteQueries.ts +89 -0
  62. package/gql/queries/aiChatQueries.ts +70 -2
  63. package/gql/queries/audienceQueries.ts +128 -0
  64. package/gql/queries/authQueries.ts +47 -0
  65. package/gql/queries/billingQueries.ts +118 -6
  66. package/gql/queries/campaignQueries.ts +236 -0
  67. package/gql/queries/dataTableQueries.ts +61 -13
  68. package/gql/queries/emailQueries.ts +26 -17
  69. package/gql/queries/emailReplyReportQueries.ts +76 -0
  70. package/gql/queries/fieldDocQueries.ts +166 -0
  71. package/gql/queries/inboundContactQueries.ts +4 -0
  72. package/gql/queries/index.ts +10 -2
  73. package/gql/queries/logQueries.ts +68 -30
  74. package/gql/queries/organizationQueries.ts +65 -1
  75. package/gql/queries/qrCodeQueries.ts +20 -0
  76. package/gql/queries/scheduleQueries.ts +691 -0
  77. package/gql/queries/sheetQueries.ts +244 -26
  78. package/gql/queries/storageQueries.ts +38 -0
  79. package/gql/queries/workflowQueries.ts +77 -2
  80. package/hooks/index.helpers.test.ts +50 -0
  81. package/hooks/index.helpers.ts +52 -0
  82. package/hooks/index.ts +2254 -982
  83. package/hooks/use-account-mtn.ts +76 -81
  84. package/hooks/use-agentMemory-mtn.ts +71 -0
  85. package/hooks/use-agentMemory-qry.ts +56 -0
  86. package/hooks/use-agentNote-mtn.ts +82 -0
  87. package/hooks/use-agentNote-qry.ts +190 -0
  88. package/hooks/use-agentNote-recovery.helpers.test.ts +68 -0
  89. package/hooks/use-agentNote-recovery.helpers.ts +58 -0
  90. package/hooks/use-agentNote-recovery.ts +72 -0
  91. package/hooks/use-aiChat-mtn.ts +220 -141
  92. package/hooks/use-aiChat-qry.helpers.test.ts +63 -0
  93. package/hooks/use-aiChat-qry.helpers.ts +124 -0
  94. package/hooks/use-aiChat-qry.ts +341 -237
  95. package/hooks/use-aiChat-stream-mtn.ts +14 -0
  96. package/hooks/use-app-qry.ts +12 -9
  97. package/hooks/use-audience-mtn.ts +194 -0
  98. package/hooks/use-audience-qry.helpers.test.ts +80 -0
  99. package/hooks/use-audience-qry.helpers.ts +63 -0
  100. package/hooks/use-audience-qry.ts +330 -0
  101. package/hooks/use-auth-mtn.ts +435 -252
  102. package/hooks/use-auth-qry.ts +51 -0
  103. package/hooks/use-billing-mtn.ts +31 -0
  104. package/hooks/use-billing-qry.ts +76 -0
  105. package/hooks/use-campaign-generation-recovery.ts +53 -0
  106. package/hooks/use-campaign-mtn.ts +436 -0
  107. package/hooks/use-campaign-qry.helpers.ts +40 -0
  108. package/hooks/use-campaign-qry.ts +834 -0
  109. package/hooks/use-dataTable-mtn.ts +37 -46
  110. package/hooks/use-dataTable-qry.ts +102 -128
  111. package/hooks/use-email-qry.helpers.ts +37 -0
  112. package/hooks/use-email-qry.ts +56 -86
  113. package/hooks/use-emailCampaignReplyReport-qry.ts +27 -0
  114. package/hooks/use-fieldDoc-mtn.ts +262 -0
  115. package/hooks/use-fieldDoc-qry.helpers.test.ts +65 -0
  116. package/hooks/use-fieldDoc-qry.helpers.ts +49 -0
  117. package/hooks/use-fieldDoc-qry.ts +212 -0
  118. package/hooks/use-inboundContact-mtn.ts +19 -16
  119. package/hooks/use-inboundContact-qry.ts +62 -56
  120. package/hooks/use-integration-mtn.ts +56 -38
  121. package/hooks/use-integration-qry.ts +24 -9
  122. package/hooks/use-log-mtn.ts +88 -59
  123. package/hooks/use-log-qry.ts +150 -73
  124. package/hooks/use-organization-mtn.ts +334 -277
  125. package/hooks/use-organization-qry.ts +155 -129
  126. package/hooks/use-publicFormEmail-mtn.ts +27 -0
  127. package/hooks/use-qrCode-mtn.ts +40 -0
  128. package/hooks/use-qrCode-qry.ts +78 -0
  129. package/hooks/use-schedule-mtn.ts +589 -0
  130. package/hooks/use-schedule-qry.ts +738 -0
  131. package/hooks/use-schedule-reactive.ts +486 -0
  132. package/hooks/use-sheet-mtn.ts +157 -79
  133. package/hooks/use-sheet-qry.ts +221 -92
  134. package/hooks/use-storage-mtn.ts +81 -41
  135. package/hooks/use-storage-qry.ts +128 -14
  136. package/hooks/use-workflow-mtn.ts +72 -0
  137. package/hooks/use-workflow-qry.ts +184 -47
  138. package/libs/ai-chat-outgoing.helpers.ts +153 -0
  139. package/libs/ai-chat-outgoing.ts +11 -0
  140. package/libs/ai-chat-outgoing.types.ts +25 -0
  141. package/libs/ai-chat-run-realtime.ts +20 -0
  142. package/libs/ai-chat-submission-realtime.ts +150 -0
  143. package/libs/fragment-realtime-sequence.ts +55 -0
  144. package/libs/fragment-recovery.test.ts +326 -0
  145. package/libs/fragment-recovery.ts +325 -0
  146. package/libs/open-sheet.ts +138 -0
  147. package/libs/orchestrator-realtime.helpers.test.ts +229 -0
  148. package/libs/orchestrator-realtime.helpers.ts +317 -0
  149. package/libs/orchestrator-realtime.ts +433 -0
  150. package/libs/request-cache-regression.test.ts +122 -0
  151. package/libs/request-managed-session.test.ts +574 -0
  152. package/libs/request-no-store.test.ts +419 -0
  153. package/libs/sheet-command-batch.ts +44 -0
  154. package/libs/sheet-view-realtime-router.test.ts +214 -0
  155. package/libs/sheet-view-realtime-router.ts +99 -0
  156. package/libs/sse-ticket.test.ts +26 -0
  157. package/loaders/campaign.test.ts +186 -0
  158. package/loaders/campaign.ts +110 -0
  159. package/loaders/emailPreference.ts +75 -0
  160. package/loaders/rest.ts +12 -0
  161. package/package.json +119 -71
  162. package/schedule.d.ts +1790 -0
  163. package/sse.helpers.ts +19 -0
  164. package/sse.ts +70 -106
  165. package/tests/agent-note-detail-cache.test.ts +120 -0
  166. package/tests/agent-note-search.test.ts +444 -0
  167. package/tsconfig.json +11 -10
  168. package/types.d.ts +77 -26
  169. package/utils.ts +78 -49
  170. package/gql/fragments/billingFragments.ts +0 -25
  171. package/gql/fragments/productFragments.ts +0 -39
  172. package/gql/fragments/reportFragments.ts +0 -90
  173. package/gql/queries/productQueries.ts +0 -31
  174. package/gql/queries/reportQueries.ts +0 -134
  175. package/hooks/use-product-qry.ts +0 -67
  176. package/hooks/use-report-qry.ts +0 -153
@@ -1,23 +1,28 @@
1
1
  import type { UseMutationParams } from '@jsb188/graphql/types.d';
2
- import { OpenModalPopUpFn } from '@jsb188/react/states';
3
- import { deleteDataTableRowMtn, editDataTableCellMtn, editDataTableCellsMtn, editDataTableDesignMtn } from '../gql/mutations/dataTableMutations.ts';
2
+ import type { OpenModalPopUpFn } from '@jsb188/react/states';
3
+ import {
4
+ deleteDataTableMtn,
5
+ exportDataTableToFileMtn,
6
+ renameDataTableMtn,
7
+ } from '../gql/mutations/dataTableMutations.ts';
4
8
  import { useMutation } from './index.ts';
5
9
 
6
10
  /**
7
- * Edit a single dataTable cell value.
11
+ * Rename a data table. The response updates the shared data table fragment.
8
12
  */
9
13
 
10
- export function useEditDataTableCell(params: UseMutationParams = {}, openModalPopUp?: OpenModalPopUpFn) {
11
- const [editDataTableCell, mtnValues, mtnHandlers, updateObservers] = useMutation(
12
- editDataTableCellMtn,
13
- {
14
+ export function useRenameDataTable(
15
+ params: UseMutationParams = {},
16
+ openModalPopUp?: OpenModalPopUpFn,
17
+ ) {
18
+ const [renameDataTable, mtnValues, mtnHandlers, updateObservers] =
19
+ useMutation(renameDataTableMtn, {
14
20
  openModalPopUp,
15
21
  ...params,
16
- },
17
- );
22
+ });
18
23
 
19
24
  return {
20
- editDataTableCell,
25
+ renameDataTable,
21
26
  updateObservers,
22
27
  ...mtnValues,
23
28
  ...mtnHandlers,
@@ -25,20 +30,23 @@ export function useEditDataTableCell(params: UseMutationParams = {}, openModalPo
25
30
  }
26
31
 
27
32
  /**
28
- * Edit multiple dataTable cell values.
33
+ * Delete (archive) a standalone data table. A table that AI notes read or
34
+ * workflows write needs confirmReferenceDependents: true (error 20248 carries
35
+ * their count); a table an AI note owns is refused (error 20243).
29
36
  */
30
37
 
31
- export function useEditDataTableCells(params: UseMutationParams = {}, openModalPopUp?: OpenModalPopUpFn) {
32
- const [editDataTableCells, mtnValues, mtnHandlers, updateObservers] = useMutation(
33
- editDataTableCellsMtn,
34
- {
38
+ export function useDeleteDataTable(
39
+ params: UseMutationParams = {},
40
+ openModalPopUp?: OpenModalPopUpFn,
41
+ ) {
42
+ const [deleteDataTable, mtnValues, mtnHandlers, updateObservers] =
43
+ useMutation(deleteDataTableMtn, {
35
44
  openModalPopUp,
36
45
  ...params,
37
- },
38
- );
46
+ });
39
47
 
40
48
  return {
41
- editDataTableCells,
49
+ deleteDataTable,
42
50
  updateObservers,
43
51
  ...mtnValues,
44
52
  ...mtnHandlers,
@@ -46,40 +54,23 @@ export function useEditDataTableCells(params: UseMutationParams = {}, openModalP
46
54
  }
47
55
 
48
56
  /**
49
- * Delete a single source dataTable row.
57
+ * Export a data table's live rows to a CSV file the client downloads (the
58
+ * same file shape as a sheet export). A table with more rows than one export
59
+ * holds is refused (error 20249 carries the row cap).
50
60
  */
51
61
 
52
- export function useDeleteDataTableRow(params: UseMutationParams = {}, openModalPopUp?: OpenModalPopUpFn) {
53
- const [deleteDataTableRow, mtnValues, mtnHandlers] = useMutation(
54
- deleteDataTableRowMtn,
55
- {
62
+ export function useExportDataTableToFile(
63
+ params: UseMutationParams = {},
64
+ openModalPopUp?: OpenModalPopUpFn,
65
+ ) {
66
+ const [exportDataTableToFile, mtnValues, mtnHandlers, updateObservers] =
67
+ useMutation(exportDataTableToFileMtn, {
56
68
  openModalPopUp,
57
69
  ...params,
58
- },
59
- );
70
+ });
60
71
 
61
72
  return {
62
- deleteDataTableRow,
63
- ...mtnValues,
64
- ...mtnHandlers,
65
- };
66
- }
67
-
68
- /**
69
- * Edit the saved design object for one dataTable.
70
- */
71
-
72
- export function useEditDataTableDesign(params: UseMutationParams = {}, openModalPopUp?: OpenModalPopUpFn) {
73
- const [editDataTableDesign, mtnValues, mtnHandlers, updateObservers] = useMutation(
74
- editDataTableDesignMtn,
75
- {
76
- openModalPopUp,
77
- ...params,
78
- },
79
- );
80
-
81
- return {
82
- editDataTableDesign,
73
+ exportDataTableToFile,
83
74
  updateObservers,
84
75
  ...mtnValues,
85
76
  ...mtnHandlers,
@@ -1,172 +1,146 @@
1
- import { useQuery, useReactiveFragment, useReactiveFragmentMap } from '@jsb188/graphql/client';
2
- import { makeVariablesKey } from '@jsb188/app/utils/logic.ts';
3
- import { dataTableRowsQry, dataTablesQry } from '../gql/queries/dataTableQueries.ts';
4
- import type { UseQueryParams } from '../types.d.ts';
5
-
6
- /**
7
- * Constants
8
- */
9
-
10
- const SHEET_ROWS_LIMIT = 200;
1
+ import { FRAGMENT_NAMES, getFragmentBaseName, makeFragmentKey } from '@jsb188/mday/constants/graphqlReactivity.ts';
2
+ import { GRID_ITEM_LIST_LIMIT } from '@jsb188/mday/constants/sheet.ts';
3
+ import type { DtSortDirectionEnum } from '@jsb188/mday/types/dt.d.ts';
4
+ import type { GridItemSortEnum } from '@jsb188/mday/types/sheet.d.ts';
5
+ import { dataTableQry, dataTableRowsQry, dataTableRunsQry, dataTablesQry } from '../gql/queries/dataTableQueries.ts';
6
+ import type { PaginationArgs, UseQueryParams } from '../types.d.ts';
7
+ import { useQuery, useReactiveFragment, useReactiveFragmentMap } from './index.ts';
8
+
9
+ export type DataTablesFilterVariables = {
10
+ search?: string | null;
11
+ };
12
+
13
+ export type DataTablesVariables = PaginationArgs & {
14
+ filter?: DataTablesFilterVariables | null;
15
+ organizationId?: string | null;
16
+ sort?: GridItemSortEnum | null;
17
+ };
18
+
19
+ export type DataTableRowsVariables = {
20
+ organizationId?: string | null;
21
+ dataTableId?: string | null;
22
+ sort?: { column: string; direction: DtSortDirectionEnum } | null;
23
+ cursor?: string | null;
24
+ limit?: number | null;
25
+ };
26
+
27
+ export type DataTableRunsVariables = {
28
+ organizationId?: string | null;
29
+ dataTableId?: string | null;
30
+ limit?: number | null;
31
+ };
11
32
 
12
33
  /*
13
- * Return all dataTable cell records nested under the provided dataTable rows.
34
+ * Return whether one useDataTables input is the paginated variables object.
14
35
  */
15
-
16
- function getDataTableRowCells(dataTableRows?: any[] | null) {
17
- return dataTableRows?.flatMap((row) => row.cells || []) || null;
36
+ function isDataTablesVariables(value: string | null | undefined | DataTablesVariables): value is DataTablesVariables {
37
+ return !!value && typeof value === 'object';
18
38
  }
19
39
 
20
40
  /*
21
- * Merge reactive dataTable cell fragments back into their parent dataTable rows.
41
+ * Return GraphQL variables for the paginated dataTables query.
22
42
  */
23
-
24
- function mergeReactiveCellsIntoRows(dataTableRows?: any[] | null, dataTableCells?: any[] | null) {
25
- if (!dataTableRows || !dataTableCells) {
26
- return dataTableRows;
27
- }
28
-
29
- const cellMap = new Map(dataTableCells.map((cell) => [cell.id, cell]));
30
- let changedRows = false;
31
-
32
- const nextRows = dataTableRows.map((row) => {
33
- let changedCells = false;
34
- const nextCells = row.cells?.map((cell: any) => {
35
- const nextCell = cellMap.get(cell.id) || cell;
36
- if (nextCell !== cell) {
37
- changedCells = true;
38
- }
39
-
40
- return nextCell;
41
- }) || [];
42
-
43
- if (!changedCells) {
44
- return row;
45
- }
46
-
47
- changedRows = true;
48
-
49
- return {
50
- ...row,
51
- cells: nextCells,
52
- };
53
- });
54
-
55
- return changedRows ? nextRows : dataTableRows;
43
+ function getDataTablesQueryVariables(organizationIdOrVariables?: string | null | DataTablesVariables): DataTablesVariables {
44
+ const variables = isDataTablesVariables(organizationIdOrVariables)
45
+ ? organizationIdOrVariables
46
+ : { organizationId: organizationIdOrVariables };
47
+ return {
48
+ ...variables,
49
+ after: variables.after ?? true,
50
+ cursor: variables.cursor ?? null,
51
+ filter: variables.filter ?? null,
52
+ limit: variables.limit ?? GRID_ITEM_LIST_LIMIT,
53
+ sort: variables.sort || 'UPDATED_AT_DESC',
54
+ };
56
55
  }
57
56
 
58
- /*
59
- * Get reactive dataTable rows and their nested cell fragments.
57
+ /**
58
+ * Fetch the listed data tables of an organization the viewer may read, newest first.
60
59
  */
60
+ export function useDataTables(organizationId?: string | null | DataTablesVariables, params: UseQueryParams = {}) {
61
+ const variables = getDataTablesQueryVariables(organizationId);
62
+ const { data, ...rest } = useQuery(dataTablesQry, {
63
+ variables,
64
+ ...params,
65
+ skip: !variables.organizationId || !!params.skip,
66
+ });
67
+ const dataTables = useReactiveFragmentMap(data?.dataTables || null, getFragmentBaseName(FRAGMENT_NAMES.dataTable));
61
68
 
62
- export function useReactiveDataTableRows(dataTableRows?: any[] | null) {
63
- const reactiveRows = useReactiveFragmentMap(dataTableRows || null, 'dataTableRowFragment');
64
- const reactiveCells = useReactiveFragmentMap(getDataTableRowCells(reactiveRows || dataTableRows), 'dataTableCellFragment');
65
-
66
- return mergeReactiveCellsIntoRows(reactiveRows || dataTableRows, reactiveCells);
69
+ return {
70
+ dataTables,
71
+ ...rest,
72
+ };
67
73
  }
68
74
 
69
75
  /*
70
- * Return the query key that belongs to one dataTable rows variables payload.
71
- */
72
-
73
- function getDataTableRowsQueryKey(variables: Record<string, unknown>) {
74
- return `#dataTableRows:${makeVariablesKey(variables)}`;
75
- }
76
-
77
- /**
78
- * Fetch dataTables for an organization.
76
+ * Fetch one data table (metadata, columns, sync state and owner note).
79
77
  */
80
-
81
- export function useDataTables(
82
- organizationId?: string | null,
83
- active?: boolean | null,
84
- params: UseQueryParams = {},
85
- ) {
86
- const { data, ...rest } = useQuery(dataTablesQry, {
78
+ export function useDataTable(dataTableId?: string | null, organizationId?: string | null, params: UseQueryParams = {}) {
79
+ const { data, ...rest } = useQuery(dataTableQry, {
87
80
  variables: {
88
81
  organizationId,
89
- active,
82
+ dataTableId,
90
83
  },
91
- skip: !organizationId,
92
84
  ...params,
85
+ skip: !organizationId || !dataTableId || !!params.skip,
93
86
  });
87
+ const dataTable = useReactiveDataTableFragment(dataTableId || '', data?.dataTable || null, rest.updatedCount);
94
88
 
95
89
  return {
96
- dataTables: data?.dataTables,
90
+ dataTable: dataTable?.id === dataTableId ? dataTable : null,
97
91
  ...rest,
98
92
  };
99
93
  }
100
94
 
101
- /**
102
- * Fetch rows and cells for one dataTable.
95
+ /*
96
+ * Fetch one keyset page of a data table's live rows (100 by default), in id
97
+ * order or sorted by one column. The server resets this query (RESET_ONLY)
98
+ * when the table's rows change.
103
99
  */
104
-
105
- export function useDataTableRows(
106
- dataTableId?: string | null,
107
- organizationId?: string | null,
108
- cursor?: string | null,
109
- limit: number = SHEET_ROWS_LIMIT,
110
- filter?: {
111
- viewId?: string | null;
112
- } | null,
113
- params: UseQueryParams = {},
114
- ) {
115
- const variables = {
116
- dataTableId,
117
- organizationId,
118
- cursor,
119
- limit,
120
- filter,
100
+ export function useDataTableRows(variables: DataTableRowsVariables, params: UseQueryParams = {}) {
101
+ const queryVariables = {
102
+ organizationId: variables.organizationId,
103
+ dataTableId: variables.dataTableId,
104
+ sort: variables.sort ?? null,
105
+ cursor: variables.cursor ?? null,
106
+ limit: variables.limit ?? 100,
121
107
  };
122
108
  const { data, ...rest } = useQuery(dataTableRowsQry, {
123
- variables,
124
- skip: !organizationId || !dataTableId,
109
+ variables: queryVariables,
125
110
  ...params,
111
+ skip: !queryVariables.organizationId || !queryVariables.dataTableId || !!params.skip,
126
112
  });
127
113
 
128
- const queryMatchesVariables = rest.queryKey === getDataTableRowsQueryKey(variables) ||
129
- rest.variablesKey === makeVariablesKey(variables);
130
- const dataTableRows = useReactiveDataTableRows(queryMatchesVariables ? data?.dataTableRows : null);
131
-
132
114
  return {
133
- dataTableRows: queryMatchesVariables ? dataTableRows : undefined,
115
+ dataTableRows: data?.dataTableRows || null,
134
116
  ...rest,
135
117
  };
136
118
  }
137
119
 
138
- /**
139
- * Get reactive dataTable fragment.
140
- */
141
-
142
- export function useReactiveDataTableFragment(dataTableId: string, currentData?: any, queryCount?: number) {
143
- return useReactiveFragment(
144
- currentData,
145
- [`$dataTableFragment:${dataTableId}`],
146
- queryCount,
147
- );
148
- }
149
-
150
- /**
151
- * Get reactive dataTable row fragment.
120
+ /*
121
+ * Fetch the newest change batches (runs) of a data table.
152
122
  */
123
+ export function useDataTableRuns(variables: DataTableRunsVariables, params: UseQueryParams = {}) {
124
+ const queryVariables = {
125
+ organizationId: variables.organizationId,
126
+ dataTableId: variables.dataTableId,
127
+ limit: variables.limit ?? 10,
128
+ };
129
+ const { data, ...rest } = useQuery(dataTableRunsQry, {
130
+ variables: queryVariables,
131
+ ...params,
132
+ skip: !queryVariables.organizationId || !queryVariables.dataTableId || !!params.skip,
133
+ });
153
134
 
154
- export function useReactiveDataTableRowFragment(dataTableRowId: string, currentData?: any, queryCount?: number) {
155
- return useReactiveFragment(
156
- currentData,
157
- [`$dataTableRowFragment:${dataTableRowId}`],
158
- queryCount,
159
- );
135
+ return {
136
+ dataTableRuns: data?.dataTableRuns || null,
137
+ ...rest,
138
+ };
160
139
  }
161
140
 
162
141
  /**
163
- * Get reactive dataTable cell fragment.
142
+ * Get the reactive data table fragment.
164
143
  */
165
-
166
- export function useReactiveDataTableCellFragment(dataTableCellId: string, currentData?: any, queryCount?: number) {
167
- return useReactiveFragment(
168
- currentData,
169
- [`$dataTableCellFragment:${dataTableCellId}`],
170
- queryCount,
171
- );
144
+ export function useReactiveDataTableFragment(dataTableId: string, currentData?: any, queryCount?: number) {
145
+ return useReactiveFragment(currentData, dataTableId ? [makeFragmentKey(FRAGMENT_NAMES.dataTable, dataTableId)] : [], queryCount);
172
146
  }
@@ -0,0 +1,37 @@
1
+ import type { EmailMessagesFilterInput } from '@jsb188/mday/types/email.d.ts';
2
+
3
+ /*
4
+ * Whitelist GraphQL input fields, trim searches, and preserve false observation filters.
5
+ */
6
+ export function normalizeEmailMessagesFilter(filter?: EmailMessagesFilterInput | null): EmailMessagesFilterInput | null {
7
+ return filter
8
+ ? {
9
+ startDate: filter.startDate || null,
10
+ endDate: filter.endDate || null,
11
+ query: filter.query?.trim() || null,
12
+ directions: filter.directions?.length ? filter.directions : null,
13
+ kinds: filter.kinds?.length ? filter.kinds : null,
14
+ statusGroups: filter.statusGroups?.length ? filter.statusGroups : null,
15
+ statuses: filter.statuses?.length ? filter.statuses : null,
16
+ campaignId: filter.campaignId || null,
17
+ emailAddress: filter.emailAddress?.trim() || null,
18
+ fromEmail: filter.fromEmail?.trim() || null,
19
+ toEmail: filter.toEmail?.trim() || null,
20
+ subject: filter.subject?.trim() || null,
21
+ delivered: filter.delivered ?? null,
22
+ opened: filter.opened ?? null,
23
+ }
24
+ : null;
25
+ }
26
+
27
+ /*
28
+ * Keep detail cache records inside the currently requested organization and email.
29
+ */
30
+ export function getScopedEmailRecord<T extends { id: string; organizationId: string }>(
31
+ email: T | null | undefined,
32
+ organizationId: string | null | undefined,
33
+ emailId: string | null | undefined,
34
+ skip: boolean,
35
+ ): T | null {
36
+ return !skip && organizationId && emailId && email?.organizationId === organizationId && email.id === emailId ? email : null;
37
+ }
@@ -1,112 +1,82 @@
1
1
  import { useQuery, useReactiveFragment } from '@jsb188/graphql/client';
2
- import type { InboundEmailsSortEnum } from '@jsb188/mday/types/email.d.ts';
3
- import { loadFragment } from '../cache/index.ts';
4
- import { inboundEmailQry, inboundEmailsQry } from '../gql/queries/emailQueries.ts';
2
+ import type { EmailMessageGQL, EmailMessagesFilterInput, EmailMessagesSortEnum } from '@jsb188/mday/types/email.d.ts';
3
+ import { emailMessageQry, emailMessagesQry } from '../gql/queries/emailQueries.ts';
5
4
  import type { PaginationArgs, UseQueryParams } from '../types.d.ts';
5
+ import { getScopedEmailRecord, normalizeEmailMessagesFilter } from './use-email-qry.helpers.ts';
6
6
 
7
7
  /**
8
8
  * Constants
9
9
  */
10
10
 
11
- const INBOUND_EMAILS_LIMIT = 250;
12
-
13
- /**
14
- * Keep inbound email list filters aligned with the GraphQL input shape.
15
- */
16
- function normalizeInboundEmailsFilter(filter?: {
17
- startDate?: string | null;
18
- endDate?: string | null;
19
- query?: string | null;
20
- statuses?: string[] | null;
21
- } | null) {
22
- return filter
23
- ? {
24
- startDate: filter.startDate || null,
25
- endDate: filter.endDate || null,
26
- query: filter.query || null,
27
- statuses: filter.statuses || null,
28
- }
29
- : null;
30
- }
11
+ const EMAIL_MESSAGES_LIST_LIMIT = 250;
31
12
 
32
13
  /*
33
- * Fetch one inbound email.
14
+ * Fetch one inbound or outbound email message.
34
15
  */
35
16
 
36
- export function useInboundEmail(
37
- variables: {
38
- organizationId?: string | null;
39
- inboundEmailId?: string | null;
40
- },
41
- params: UseQueryParams = {},
17
+ export function useEmailMessage(
18
+ variables: {
19
+ organizationId?: string | null;
20
+ emailMessageId?: string | null;
21
+ },
22
+ params: UseQueryParams = {},
42
23
  ) {
43
- const cachedInboundEmail = variables.inboundEmailId
44
- ? loadFragment(`$inboundEmailFragment:${variables.inboundEmailId}`)
45
- : null;
46
- const shouldFetchInboundEmail = !!variables.organizationId && !!variables.inboundEmailId && !cachedInboundEmail && !params.skip;
47
- const { data, ...rest } = useQuery(inboundEmailQry, {
48
- ...params,
49
- variables,
50
- skip: !shouldFetchInboundEmail,
51
- });
52
- const inboundEmail = useReactiveInboundEmailFragment(
53
- variables.inboundEmailId || '',
54
- data?.inboundEmail || cachedInboundEmail,
55
- rest.updatedCount,
56
- );
24
+ const skip = !variables.organizationId || !variables.emailMessageId || !!params.skip;
25
+ const cachedEmailMessage = useReactiveEmailMessageFragment(variables.emailMessageId || '', null);
26
+ // Keep the query active so list-row fragments cannot hide the detail fields.
27
+ const { data, ...rest } = useQuery(emailMessageQry, {
28
+ ...params,
29
+ variables,
30
+ skip,
31
+ });
32
+ const currentData = rest.queryKey === `#${rest.queryName}:${rest.variablesKey}` ? data : null;
33
+ const emailMessage =
34
+ currentData?.emailMessage === null
35
+ ? null
36
+ : getScopedEmailRecord<EmailMessageGQL>(currentData?.emailMessage, variables.organizationId, variables.emailMessageId, skip) ||
37
+ getScopedEmailRecord<EmailMessageGQL>(cachedEmailMessage, variables.organizationId, variables.emailMessageId, skip);
57
38
 
58
- return {
59
- inboundEmail,
60
- initialLoading: shouldFetchInboundEmail && !data,
61
- ...rest
62
- };
39
+ return {
40
+ emailMessage,
41
+ initialLoading: !skip && !emailMessage && !rest.error && currentData?.emailMessage === undefined,
42
+ ...rest,
43
+ };
63
44
  }
64
45
 
65
46
  /**
66
- * Fetch inbound emails.
47
+ * Fetch inbound and outbound email messages as one list.
67
48
  */
68
49
 
69
- export function useInboundEmails(
70
- variables: PaginationArgs & {
71
- organizationId?: string | null;
72
- filter?: {
73
- startDate?: string | null;
74
- endDate?: string | null;
75
- query?: string | null;
76
- statuses?: string[] | null;
77
- } | null;
78
- sort?: InboundEmailsSortEnum | null;
79
- },
80
- params: UseQueryParams = {},
50
+ export function useEmailMessages(
51
+ variables: PaginationArgs & {
52
+ organizationId?: string | null;
53
+ filter?: EmailMessagesFilterInput | null;
54
+ sort?: EmailMessagesSortEnum | null;
55
+ },
56
+ params: UseQueryParams = {},
81
57
  ) {
82
- const { data, ...rest } = useQuery(inboundEmailsQry, {
83
- ...params,
84
- variables: {
85
- ...variables,
86
- filter: normalizeInboundEmailsFilter(variables.filter),
87
- cursor: null,
88
- after: true,
89
- limit: INBOUND_EMAILS_LIMIT
90
- },
91
- skip: !variables.organizationId || params.skip,
92
- });
58
+ const { data, ...rest } = useQuery(emailMessagesQry, {
59
+ ...params,
60
+ variables: {
61
+ ...variables,
62
+ filter: normalizeEmailMessagesFilter(variables.filter),
63
+ cursor: variables.cursor ?? null,
64
+ after: variables.after ?? true,
65
+ limit: variables.limit ?? EMAIL_MESSAGES_LIST_LIMIT,
66
+ },
67
+ skip: !variables.organizationId || params.skip,
68
+ });
93
69
 
94
- return {
95
- inboundEmails: data?.inboundEmails,
96
- ...rest
97
- };
70
+ return {
71
+ emailMessages: data?.emailMessages,
72
+ ...rest,
73
+ };
98
74
  }
99
75
 
100
76
  /**
101
- * Get reactive inbound email fragment.
77
+ * Get reactive email message fragment.
102
78
  */
103
79
 
104
- export function useReactiveInboundEmailFragment(inboundEmailId: string, currentData?: any, queryCount?: number) {
105
- return useReactiveFragment(
106
- currentData,
107
- [
108
- `$inboundEmailFragment:${inboundEmailId}`,
109
- ],
110
- queryCount,
111
- );
80
+ export function useReactiveEmailMessageFragment(emailMessageId: string, currentData?: any, queryCount?: number) {
81
+ return useReactiveFragment(currentData, [`$emailMessageFragment:${emailMessageId}`], queryCount);
112
82
  }
@@ -0,0 +1,27 @@
1
+ import type { EmailCampaignReplyReportGQL } from '@jsb188/mday/types/emailReplyReport.d.ts';
2
+ import { useQuery } from '../client/index.ts';
3
+ import { emailCampaignReplyReportQry } from '../gql/queries/emailReplyReportQueries.ts';
4
+ import type { UseQueryParams } from '../types.d.ts';
5
+ import { getCampaignQueryData } from './use-campaign-qry.helpers.ts';
6
+
7
+ /*
8
+ * Read one complete campaign report into mounted query state, hiding retained
9
+ * output after scope changes or read errors. RESET_ONLY preserves the displayed
10
+ * snapshot until the ordinary query refresh action is acknowledged.
11
+ */
12
+ export function useEmailCampaignReplyReport(
13
+ variables: { organizationId?: string | null; campaignId?: string | null },
14
+ params: UseQueryParams = {},
15
+ ) {
16
+ const skipped = !variables.organizationId || !variables.campaignId || !!params.skip;
17
+ const { data, ...rest } = useQuery(emailCampaignReplyReportQry, {
18
+ ...params,
19
+ variables,
20
+ skip: skipped,
21
+ });
22
+ const currentData = getCampaignQueryData({ data, ...rest }, skipped || !!rest.error);
23
+ return {
24
+ ...rest,
25
+ report: (currentData?.emailCampaignReplyReport ?? null) as EmailCampaignReplyReportGQL | null,
26
+ };
27
+ }