@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,6 +1,11 @@
1
1
  import type { UseMutationParams } from '@jsb188/graphql/types.d';
2
2
  import { OpenModalPopUpFn, useCurrentAccount } from '@jsb188/react/states';
3
- import { changePhoneMtn, requestChangeEmailMtn, requestChangePhoneMtn, editAccountMtn } from '../gql/mutations/accountMutations.ts';
3
+ import {
4
+ changePhoneMtn,
5
+ requestChangeEmailMtn,
6
+ requestChangePhoneMtn,
7
+ editAccountMtn,
8
+ } from '../gql/mutations/accountMutations.ts';
4
9
  import { useMutation } from './index.ts';
5
10
 
6
11
  /**
@@ -8,38 +13,34 @@ import { useMutation } from './index.ts';
8
13
  */
9
14
 
10
15
  export function useEditAccount(
11
- params?: UseMutationParams | null,
12
- openModalPopUp?: OpenModalPopUpFn
16
+ params?: UseMutationParams | null,
17
+ openModalPopUp?: OpenModalPopUpFn,
13
18
  ) {
14
-
15
- const { account, settings, dispatchApp } = useCurrentAccount();
16
- const [editAccount, mtnValues, mtnHandlers] = useMutation(
17
- editAccountMtn,
18
- {
19
- // checkMountedBeforeCallback: true,
20
- openModalPopUp,
21
- ...params,
22
- onCompleted: (data, error) => {
23
- params?.onCompleted?.(data, error);
24
-
25
- const updatedData = data.editAccount;
26
- if (updatedData) {
27
- dispatchApp({
28
- __type: 'APP_EDIT_ACCOUNT',
29
- data: updatedData,
30
- });
31
- }
32
- }
33
- },
34
- );
35
-
36
- return {
37
- account,
38
- settings,
39
- editAccount,
40
- ...mtnValues,
41
- ...mtnHandlers,
42
- };
19
+ const { account, settings, dispatchApp } = useCurrentAccount();
20
+ const [editAccount, mtnValues, mtnHandlers] = useMutation(editAccountMtn, {
21
+ // checkMountedBeforeCallback: true,
22
+ openModalPopUp,
23
+ ...params,
24
+ onCompleted: (data, error) => {
25
+ params?.onCompleted?.(data, error);
26
+
27
+ const updatedData = data.editAccount;
28
+ if (updatedData) {
29
+ dispatchApp({
30
+ __type: 'APP_EDIT_ACCOUNT',
31
+ data: updatedData,
32
+ });
33
+ }
34
+ },
35
+ });
36
+
37
+ return {
38
+ account,
39
+ settings,
40
+ editAccount,
41
+ ...mtnValues,
42
+ ...mtnHandlers,
43
+ };
43
44
  }
44
45
 
45
46
  /**
@@ -47,24 +48,23 @@ export function useEditAccount(
47
48
  */
48
49
 
49
50
  export function useRequestChangeEmail(
50
- params?: UseMutationParams | null,
51
- openModalPopUp?: OpenModalPopUpFn
51
+ params?: UseMutationParams | null,
52
+ openModalPopUp?: OpenModalPopUpFn,
52
53
  ) {
53
-
54
- const [requestChangeEmail, mtnValues, mtnHandlers] = useMutation(
55
- requestChangeEmailMtn,
56
- {
57
- // checkMountedBeforeCallback: true,
58
- openModalPopUp,
59
- ...params,
60
- },
61
- );
62
-
63
- return {
64
- requestChangeEmail,
65
- ...mtnValues,
66
- ...mtnHandlers,
67
- };
54
+ const [requestChangeEmail, mtnValues, mtnHandlers] = useMutation(
55
+ requestChangeEmailMtn,
56
+ {
57
+ // checkMountedBeforeCallback: true,
58
+ openModalPopUp,
59
+ ...params,
60
+ },
61
+ );
62
+
63
+ return {
64
+ requestChangeEmail,
65
+ ...mtnValues,
66
+ ...mtnHandlers,
67
+ };
68
68
  }
69
69
 
70
70
  /**
@@ -72,23 +72,22 @@ export function useRequestChangeEmail(
72
72
  */
73
73
 
74
74
  export function useRequestChangePhone(
75
- params?: UseMutationParams | null,
76
- openModalPopUp?: OpenModalPopUpFn
75
+ params?: UseMutationParams | null,
76
+ openModalPopUp?: OpenModalPopUpFn,
77
77
  ) {
78
-
79
- const [requestChangePhone, mtnValues, mtnHandlers] = useMutation(
80
- requestChangePhoneMtn,
81
- {
82
- openModalPopUp,
83
- ...params,
84
- },
85
- );
86
-
87
- return {
88
- requestChangePhone,
89
- ...mtnValues,
90
- ...mtnHandlers,
91
- };
78
+ const [requestChangePhone, mtnValues, mtnHandlers] = useMutation(
79
+ requestChangePhoneMtn,
80
+ {
81
+ openModalPopUp,
82
+ ...params,
83
+ },
84
+ );
85
+
86
+ return {
87
+ requestChangePhone,
88
+ ...mtnValues,
89
+ ...mtnHandlers,
90
+ };
92
91
  }
93
92
 
94
93
  /**
@@ -96,21 +95,17 @@ export function useRequestChangePhone(
96
95
  */
97
96
 
98
97
  export function useChangePhone(
99
- params?: UseMutationParams | null,
100
- openModalPopUp?: OpenModalPopUpFn
98
+ params?: UseMutationParams | null,
99
+ openModalPopUp?: OpenModalPopUpFn,
101
100
  ) {
102
-
103
- const [changePhone, mtnValues, mtnHandlers] = useMutation(
104
- changePhoneMtn,
105
- {
106
- openModalPopUp,
107
- ...params,
108
- },
109
- );
110
-
111
- return {
112
- changePhone,
113
- ...mtnValues,
114
- ...mtnHandlers,
115
- };
101
+ const [changePhone, mtnValues, mtnHandlers] = useMutation(changePhoneMtn, {
102
+ openModalPopUp,
103
+ ...params,
104
+ });
105
+
106
+ return {
107
+ changePhone,
108
+ ...mtnValues,
109
+ ...mtnHandlers,
110
+ };
116
111
  }
@@ -0,0 +1,71 @@
1
+ import type { UseMutationParams } from '@jsb188/graphql/types.d';
2
+ import type { OpenModalPopUpFn } from '@jsb188/react/states';
3
+ import { createAgentMemoryMtn, deleteAgentMemoryMtn, editAgentMemoryMtn } from '../gql/mutations/agentMemoryMutations.ts';
4
+ import { useMutation } from './index.ts';
5
+
6
+ /*
7
+ * Add an explicitly authored memory for an authorized organization, member, or contact.
8
+ */
9
+ export function useCreateAgentMemory(params: UseMutationParams = {}, openModalPopUp?: OpenModalPopUpFn) {
10
+ const { onCompleted, ...rest } = params;
11
+ const [createAgentMemory, values, handlers] = useMutation(createAgentMemoryMtn, {
12
+ openModalPopUp,
13
+ onCompleted: (data, error, variables) => onCompleted?.(data?.createAgentMemory, error, variables),
14
+ ...rest,
15
+ });
16
+ return { createAgentMemory, ...values, ...handlers };
17
+ }
18
+
19
+ /**
20
+ * Edit one agent memory: its wording, its permanent flag, or its status.
21
+ *
22
+ * "permanent" and status promotion are available only here and never to an
23
+ * agent tool, so protecting a fact and promoting one stay decisions a person
24
+ * makes rather than something the assistant can do to itself.
25
+ */
26
+
27
+ export function useEditAgentMemory(params: UseMutationParams = {}, openModalPopUp?: OpenModalPopUpFn) {
28
+ const { onCompleted, ...rest } = params;
29
+
30
+ const [editAgentMemory, mtnValues, mtnHandlers] = useMutation(editAgentMemoryMtn, {
31
+ openModalPopUp,
32
+ onCompleted: (data, error, variables) => {
33
+ const updatedAgentMemory = data?.editAgentMemory;
34
+
35
+ onCompleted?.(updatedAgentMemory, error, variables);
36
+ },
37
+ ...rest,
38
+ });
39
+
40
+ return {
41
+ editAgentMemory,
42
+ ...mtnValues,
43
+ ...mtnHandlers,
44
+ };
45
+ }
46
+
47
+ /**
48
+ * Delete one agent memory.
49
+ *
50
+ * There is no soft delete and no revision history by design, so this is
51
+ * irreversible; only the per-log provenance snapshot records that the fact ever
52
+ * existed.
53
+ */
54
+
55
+ export function useDeleteAgentMemory(params: UseMutationParams = {}, openModalPopUp?: OpenModalPopUpFn) {
56
+ const { onCompleted, ...rest } = params;
57
+
58
+ const [deleteAgentMemory, mtnValues, mtnHandlers] = useMutation(deleteAgentMemoryMtn, {
59
+ openModalPopUp,
60
+ onCompleted: (data, error, variables) => {
61
+ onCompleted?.(data?.deleteAgentMemory, error, variables);
62
+ },
63
+ ...rest,
64
+ });
65
+
66
+ return {
67
+ deleteAgentMemory,
68
+ ...mtnValues,
69
+ ...mtnHandlers,
70
+ };
71
+ }
@@ -0,0 +1,56 @@
1
+ import { useQuery } from './index.ts';
2
+ import type {
3
+ AgentMemoryScopeEnum,
4
+ AgentMemoryStatusEnum,
5
+ AgentMemorySurfaceEnum,
6
+ AgentMemoryGQL,
7
+ } from '@jsb188/mday/types/agentMemory.d.ts';
8
+ import { agentMemoriesQry } from '../gql/queries/agentMemoryQueries.ts';
9
+ import type { UseQueryParams } from '../types.d.ts';
10
+ import { useReactiveFragmentMap } from '../client/index.ts';
11
+
12
+ /**
13
+ * Constants
14
+ */
15
+
16
+ const AGENT_MEMORIES_LIMIT = 200;
17
+
18
+ /**
19
+ * Fetch the assistant's memories for one organization.
20
+ *
21
+ * Organization managers may inspect current members' organization-scoped
22
+ * ACCOUNT rows; other viewers receive only their own ACCOUNT memory.
23
+ */
24
+
25
+ export function useAgentMemories(
26
+ variables: {
27
+ organizationId?: string | null;
28
+ scope?: AgentMemoryScopeEnum | null;
29
+ accountId?: string | null;
30
+ inboundContactId?: string | null;
31
+ surface?: AgentMemorySurfaceEnum | null;
32
+ status?: AgentMemoryStatusEnum | null;
33
+ },
34
+ params: UseQueryParams = {},
35
+ ) {
36
+ const { data, ...rest } = useQuery(agentMemoriesQry, {
37
+ ...params,
38
+ variables: {
39
+ ...variables,
40
+ limit: AGENT_MEMORIES_LIMIT,
41
+ },
42
+ skip: !variables.organizationId || params.skip,
43
+ });
44
+
45
+ return {
46
+ agentMemories: data?.agentMemories,
47
+ ...rest,
48
+ };
49
+ }
50
+
51
+ /*
52
+ * Observe memory changes using the same fragment identity emitted by the server.
53
+ */
54
+ export function useReactiveAgentMemories(memories?: AgentMemoryGQL[] | null) {
55
+ return useReactiveFragmentMap(memories || null, 'agentMemoryFragment');
56
+ }
@@ -0,0 +1,82 @@
1
+ import type { UseMutationParams, UseMutationParamsExtra } from '@jsb188/graphql/types.d';
2
+ import type { OpenModalPopUpFn } from '@jsb188/react/states';
3
+ import { useEffect, useRef } from 'react';
4
+ import {
5
+ deleteAgentNoteMtn,
6
+ markAgentNoteReadMtn,
7
+ refreshAgentNoteMtn,
8
+ updateAgentNoteMtn,
9
+ } from '../gql/mutations/agentNoteMutations.ts';
10
+ import { useMutation } from './index.ts';
11
+
12
+ /*
13
+ * Expose read marking for views whose explicit open action outlives detail mounts.
14
+ */
15
+ export function useMarkAgentNoteReadMutation() {
16
+ const [markAgentNoteRead] = useMutation(markAgentNoteReadMtn);
17
+ return { markAgentNoteRead };
18
+ }
19
+
20
+ /*
21
+ * Stamp that a person opened one agent note, once per mounted (organization,
22
+ * note) pair. The realtime refetches of an open note never stamp a read, so a
23
+ * page left open does not keep the note from being archived. Best-effort:
24
+ * errors are ignored.
25
+ */
26
+ export function useMarkAgentNoteRead(variables: { organizationId?: string | null; agentNoteId?: string | null }, skip = false) {
27
+ const { markAgentNoteRead } = useMarkAgentNoteReadMutation();
28
+ const markRef = useRef(markAgentNoteRead);
29
+ markRef.current = markAgentNoteRead;
30
+ const { organizationId, agentNoteId } = variables;
31
+
32
+ useEffect(() => {
33
+ if (skip || !organizationId || !agentNoteId) {
34
+ return;
35
+ }
36
+ void markRef.current({ variables: { organizationId, agentNoteId } }).catch(() => {
37
+ // The read stamp is best-effort; the note itself still loads
38
+ });
39
+ }, [skip, organizationId, agentNoteId]);
40
+ }
41
+
42
+ /*
43
+ * Explicitly update flat pin/archive and source automatic-creation arguments. Restoration checks freshness;
44
+ * server events publish the accepted state and list membership.
45
+ */
46
+ export function useUpdateAgentNote(params: UseMutationParamsExtra = {}, openModalPopUp?: OpenModalPopUpFn) {
47
+ const { onCompleted, ...rest } = params;
48
+ const [updateAgentNote, values, handlers] = useMutation(updateAgentNoteMtn, {
49
+ openModalPopUp,
50
+ onCompleted: (data, error, variables) => onCompleted?.(data?.updateAgentNote, error, variables),
51
+ ...rest,
52
+ });
53
+ return { updateAgentNote, ...values, ...handlers };
54
+ }
55
+
56
+ /*
57
+ * Refresh the shared fragment cache with the latest saved note, even when no
58
+ * job is queued. Server events deliver any subsequent background refresh.
59
+ */
60
+ export function useRefreshAgentNote(params: UseMutationParams = {}, openModalPopUp?: OpenModalPopUpFn) {
61
+ const { onCompleted, ...rest } = params;
62
+ const [refreshAgentNote, values, handlers] = useMutation(refreshAgentNoteMtn, {
63
+ openModalPopUp,
64
+ onCompleted: (data, error, variables) => onCompleted?.(data?.refreshAgentNote, error, variables),
65
+ ...rest,
66
+ });
67
+ return { refreshAgentNote, ...values, ...handlers };
68
+ }
69
+
70
+ /*
71
+ * Delete one note and its exclusively owned tables; shared tables remain standalone.
72
+ * INSIGHT deletion requires table-edit permission.
73
+ */
74
+ export function useDeleteAgentNote(params: UseMutationParams = {}, openModalPopUp?: OpenModalPopUpFn) {
75
+ const { onCompleted, ...rest } = params;
76
+ const [deleteAgentNote, values, handlers] = useMutation(deleteAgentNoteMtn, {
77
+ openModalPopUp,
78
+ onCompleted: (data, error, variables) => onCompleted?.(data?.deleteAgentNote, error, variables),
79
+ ...rest,
80
+ });
81
+ return { deleteAgentNote, ...values, ...handlers };
82
+ }
@@ -0,0 +1,190 @@
1
+ import { FRAGMENT_NAMES, getFragmentBaseName, makeFragmentKey } from '@jsb188/mday/constants/graphqlReactivity.ts';
2
+ import type {
3
+ AgentNoteBootstrapProgressGQL,
4
+ AgentNoteGQL,
5
+ AgentNoteKindEnum,
6
+ CampaignTemplateAgentNoteFocusGQL,
7
+ } from '@jsb188/mday/types/agentNote.d.ts';
8
+ import { useMemo } from 'react';
9
+ import { loadFragment } from '../cache/index.ts';
10
+ import {
11
+ agentNoteBootstrapProgressQry,
12
+ agentNoteQry,
13
+ agentNotesQry,
14
+ campaignTemplateAgentNoteFocusQry,
15
+ searchAgentNotesQry,
16
+ } from '../gql/queries/agentNoteQueries.ts';
17
+ import type { UseQueryParams } from '../types.d.ts';
18
+ import { useQuery, useReactiveFragment, useReactiveFragmentMap } from './index.ts';
19
+ import { useMarkAgentNoteRead } from './use-agentNote-mtn.ts';
20
+ import { getAgentNoteResponseRevision, getVisibleAgentNote } from './use-agentNote-recovery.helpers.ts';
21
+ import { useRecoverableAgentNoteQuery } from './use-agentNote-recovery.ts';
22
+
23
+ export type AgentNotesVariables = {
24
+ organizationId?: string | null;
25
+ };
26
+
27
+ export type SearchAgentNotesVariables = AgentNotesVariables & {
28
+ query: string;
29
+ includeArchived?: boolean | null;
30
+ kinds?: readonly AgentNoteKindEnum[] | null;
31
+ limit?: number | null;
32
+ };
33
+
34
+ export type AgentNoteVariables = {
35
+ organizationId?: string | null;
36
+ agentNoteId?: string | null;
37
+ };
38
+
39
+ export type CampaignTemplateAgentNoteFocusVariables = {
40
+ organizationId?: string | null;
41
+ campaignTemplateId?: string | null;
42
+ };
43
+
44
+ /*
45
+ * Read the safe initial-review status and recover missed status changes after reconnecting.
46
+ */
47
+ export function useAgentNoteBootstrapProgress(variables: { organizationId?: string | null }, params: UseQueryParams = {}) {
48
+ const { data, ...rest } = useRecoverableAgentNoteQuery(agentNoteBootstrapProgressQry, variables.organizationId, {
49
+ ...params,
50
+ variables,
51
+ skip: !variables.organizationId || !!params.skip,
52
+ revalidateOnMount: true,
53
+ });
54
+ const progress: AgentNoteBootstrapProgressGQL | null = rest.error ? null : data?.agentNoteBootstrapProgress || null;
55
+ return { progress, ...rest };
56
+ }
57
+
58
+ /*
59
+ * Fetch every visible homepage note once per organization. Local groups and
60
+ * display limits share this collection and never change its query variables.
61
+ */
62
+ export function useAgentNotes(variables: AgentNotesVariables, params: UseQueryParams = {}) {
63
+ const { data, ...rest } = useRecoverableAgentNoteQuery(agentNotesQry, variables.organizationId, {
64
+ ...params,
65
+ variables: {
66
+ organizationId: variables.organizationId,
67
+ },
68
+ skip: !variables.organizationId || !!params.skip,
69
+ });
70
+ const agentNotes: AgentNoteGQL[] | null = useReactiveFragmentMap(
71
+ data?.agentNotes || null,
72
+ getFragmentBaseName(FRAGMENT_NAMES.agentNote),
73
+ rest.updatedCount,
74
+ );
75
+
76
+ const visibleAgentNotes = useMemo(() => agentNotes?.filter((note) => !note.__deleted && !note.archivedAt) ?? null, [agentNotes]);
77
+ return { agentNotes: visibleAgentNotes, ...rest };
78
+ }
79
+
80
+ /*
81
+ * Search active and archived notes without pagination. The accepted query
82
+ * identifies the result so a debounced input can keep older rows visible
83
+ * without presenting them as matches for a newer draft.
84
+ */
85
+ export function useSearchAgentNotes(variables: SearchAgentNotesVariables, params: UseQueryParams = {}) {
86
+ const query = variables.query.trim();
87
+ const { data, ...rest } = useRecoverableAgentNoteQuery(searchAgentNotesQry, variables.organizationId, {
88
+ ...params,
89
+ variables: {
90
+ organizationId: variables.organizationId,
91
+ query,
92
+ kinds: variables.kinds,
93
+ includeArchived: variables.includeArchived ?? true,
94
+ limit: variables.limit ?? 16,
95
+ },
96
+ skip: !variables.organizationId || !query || !!params.skip,
97
+ revalidateOnMount: true,
98
+ });
99
+ const revision = getAgentNoteResponseRevision({ ...data, error: rest.error }, 'searchAgentNotes');
100
+ const agentNotes: AgentNoteGQL[] | null = useReactiveFragmentMap(
101
+ revision === null ? null : data.searchAgentNotes.items,
102
+ getFragmentBaseName(FRAGMENT_NAMES.agentNote),
103
+ rest.updatedCount,
104
+ );
105
+
106
+ const visibleAgentNotes = useMemo(
107
+ () =>
108
+ agentNotes?.filter(
109
+ (note) =>
110
+ !note.__deleted &&
111
+ (variables.includeArchived !== false || !note.archivedAt) &&
112
+ (!variables.kinds?.length || variables.kinds.includes(note.kind)),
113
+ ) ?? null,
114
+ [agentNotes, variables.includeArchived, variables.kinds],
115
+ );
116
+ return { agentNotes: visibleAgentNotes, acceptedQuery: revision === null ? null : query, revision, ...rest };
117
+ }
118
+
119
+ /*
120
+ * Fetch one agent note in full. Opening it stamps one read
121
+ * (markAgentNoteRead, once per mounted note); realtime fragments update its
122
+ * content without refetching or stamping another read. cacheFirst reuses full normalized
123
+ * details from any view without a request. Views that retain their own open
124
+ * identity across remounts pass markRead: false and own the read stamp.
125
+ */
126
+ export function useAgentNote(
127
+ variables: AgentNoteVariables,
128
+ params: UseQueryParams & { markRead?: boolean; cacheFirst?: boolean } = {},
129
+ ) {
130
+ const { markRead = true, cacheFirst = false, ...queryParams } = params;
131
+ const skip = !variables.organizationId || !variables.agentNoteId || !!params.skip;
132
+ const cachedNote: AgentNoteGQL | null = variables.agentNoteId
133
+ ? loadFragment(makeFragmentKey(FRAGMENT_NAMES.agentNote, variables.agentNoteId))
134
+ : null;
135
+ // Summary rows omit body and links; only a full detail or a deletion can satisfy this read.
136
+ const useCachedNote = !!(
137
+ cacheFirst &&
138
+ cachedNote &&
139
+ cachedNote.organizationId === variables.organizationId &&
140
+ (cachedNote.__deleted || (cachedNote.body !== undefined && Array.isArray(cachedNote.links)))
141
+ );
142
+ const { data, ...rest } = useRecoverableAgentNoteQuery(agentNoteQry, variables.organizationId, {
143
+ ...queryParams,
144
+ variables,
145
+ skip: skip || useCachedNote,
146
+ });
147
+ useMarkAgentNoteRead(variables, skip || !markRead);
148
+ const agentNote = useReactiveAgentNoteFragment(variables.agentNoteId, data?.agentNote, rest.updatedCount);
149
+
150
+ return {
151
+ ...rest,
152
+ agentNote:
153
+ skip || agentNote?.organizationId !== variables.organizationId
154
+ ? null
155
+ : getVisibleAgentNote(useCachedNote ? { agentNote: cachedNote } : data, agentNote, rest.error),
156
+ loading: useCachedNote ? false : rest.loading,
157
+ };
158
+ }
159
+
160
+ /*
161
+ * Observe the shared note identity, including full details previously loaded by another view.
162
+ */
163
+ export function useReactiveAgentNoteFragment(agentNoteId?: string | null, currentData?: AgentNoteGQL | null, queryCount?: number) {
164
+ const note: AgentNoteGQL | null = useReactiveFragment(
165
+ currentData || null,
166
+ agentNoteId ? [makeFragmentKey(FRAGMENT_NAMES.agentNote, agentNoteId)] : [],
167
+ queryCount,
168
+ );
169
+ return note?.id === agentNoteId ? note : null;
170
+ }
171
+
172
+ /*
173
+ * Fetch the organization's focus for one web form template: the focus the
174
+ * note of every campaign created from the template afterwards starts with
175
+ * (focus is null when none is set; existing campaigns keep their own). The
176
+ * server resets this query when the focus is set or cleared.
177
+ */
178
+ export function useCampaignTemplateAgentNoteFocus(variables: CampaignTemplateAgentNoteFocusVariables, params: UseQueryParams = {}) {
179
+ const { data, ...rest } = useQuery(campaignTemplateAgentNoteFocusQry, {
180
+ ...params,
181
+ variables,
182
+ skip: !variables.organizationId || !variables.campaignTemplateId || !!params.skip,
183
+ });
184
+ const campaignTemplateAgentNoteFocus: CampaignTemplateAgentNoteFocusGQL | null = data?.campaignTemplateAgentNoteFocus || null;
185
+
186
+ return {
187
+ campaignTemplateAgentNoteFocus,
188
+ ...rest,
189
+ };
190
+ }
@@ -0,0 +1,68 @@
1
+ import {
2
+ agentNoteRecoveryReadSucceeded,
3
+ getAgentNoteResponseRevision,
4
+ getVisibleAgentNote,
5
+ } from './use-agentNote-recovery.helpers.ts';
6
+
7
+ /* Fail note response regressions with the expected user-visible behavior. */
8
+ function assert(value: unknown, message: string): asserts value {
9
+ if (!value) throw new Error(message);
10
+ }
11
+
12
+ Deno.test('missing details are successful authoritative reads but partial errors cannot acknowledge freshness', () => {
13
+ assert(
14
+ agentNoteRecoveryReadSucceeded({ agentNote: null, error: { errorCode: 20232, statusCode: 404 } }, 'agentNote'),
15
+ 'The exact server missing-note response is authoritative absence',
16
+ );
17
+ assert(
18
+ !agentNoteRecoveryReadSucceeded({ agentNote: null, error: { errorCode: '20052', statusCode: 404 } }, 'agentNote'),
19
+ 'A different authorization error cannot acknowledge note freshness',
20
+ );
21
+ assert(
22
+ !agentNoteRecoveryReadSucceeded({ error: { errorCode: '20232', statusCode: 404 } }, 'agentNote'),
23
+ 'A missing response field cannot acknowledge a detail snapshot',
24
+ );
25
+ assert(
26
+ !agentNoteRecoveryReadSucceeded({ agentNote: { id: 'stale' }, error: { errorCode: '20232', statusCode: 404 } }, 'agentNote'),
27
+ 'A partial object is not authoritative absence',
28
+ );
29
+ assert(
30
+ agentNoteRecoveryReadSucceeded({ agentNote: null }, 'agentNote'),
31
+ 'Deletion while offline must settle to an authoritative missing result',
32
+ );
33
+ assert(
34
+ !agentNoteRecoveryReadSucceeded({ agentNote: null, error: { errorCode: '30000' } }, 'agentNote'),
35
+ 'Partial data with an error must retry',
36
+ );
37
+ assert(
38
+ !agentNoteRecoveryReadSucceeded({ agentNotes: [] }, 'agentNote'),
39
+ 'A response for another query cannot establish the baseline',
40
+ );
41
+ });
42
+
43
+ Deno.test('search freshness requires valid items and revision in the same successful response', () => {
44
+ for (const result of [
45
+ { searchAgentNotes: null },
46
+ { searchAgentNotes: { items: [], revision: 4 } },
47
+ { searchAgentNotes: { items: null, revision: '4' } },
48
+ { searchAgentNotes: { items: [], revision: '-1' } },
49
+ { searchAgentNotes: { items: [], revision: '4' }, __clientResultRejected: true },
50
+ { searchAgentNotes: { items: [], revision: '4' }, aborted: true },
51
+ ]) {
52
+ assert(!agentNoteRecoveryReadSucceeded(result, 'searchAgentNotes'), 'Malformed or rejected searches remain pending');
53
+ assert(getAgentNoteResponseRevision(result, 'searchAgentNotes') === null, 'Invalid snapshots have no accepted revision');
54
+ }
55
+ assert(
56
+ getAgentNoteResponseRevision({ searchAgentNotes: { items: [], revision: '9007199254740993' } }, 'searchAgentNotes') ===
57
+ '9007199254740993',
58
+ 'Bigint revisions must retain text precision',
59
+ );
60
+ });
61
+
62
+ Deno.test('a fragment tombstone hides retained detail content without a query refetch', () => {
63
+ const note = { id: 'note-1', body: 'Old content' };
64
+ assert(
65
+ getVisibleAgentNote({ agentNote: note }, { ...note, __deleted: true }, null) === null,
66
+ 'Deleted fragments must disappear immediately',
67
+ );
68
+ });