@jsb188/graphql 1.1.7 → 1.1.10

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 (73) hide show
  1. package/cache/config.ts +14 -4
  2. package/cache/index.test.ts +319 -0
  3. package/cache/index.ts +522 -85
  4. package/client/index.ts +2 -2
  5. package/client/request.ts +48 -9
  6. package/deno.test.json +6 -0
  7. package/gql/fragments/accountFragments.ts +1 -0
  8. package/gql/fragments/aiChatFragments.ts +1 -0
  9. package/gql/fragments/dataTableFragments.ts +212 -0
  10. package/gql/fragments/emailFragments.ts +26 -0
  11. package/gql/fragments/inboundContactFragments.ts +15 -0
  12. package/gql/fragments/integrationFragments.ts +22 -0
  13. package/gql/fragments/logFragments.ts +40 -12
  14. package/gql/fragments/organizationFragments.ts +59 -24
  15. package/gql/fragments/productFragments.ts +0 -24
  16. package/gql/fragments/reportFragments.ts +52 -10
  17. package/gql/fragments/sheetFragments.ts +145 -0
  18. package/gql/fragments/storageFragments.ts +3 -1
  19. package/gql/fragments/workflowFragments.ts +43 -0
  20. package/gql/mutations/accountMutations.ts +24 -2
  21. package/gql/mutations/aiChatMutations.ts +28 -1
  22. package/gql/mutations/authMutations.ts +3 -3
  23. package/gql/mutations/billingMutations.ts +1 -1
  24. package/gql/mutations/dataTableMutations.ts +84 -0
  25. package/gql/mutations/inboundContactMutations.ts +24 -0
  26. package/gql/mutations/index.ts +12 -8
  27. package/gql/mutations/integrationMutations.ts +25 -0
  28. package/gql/mutations/logMutations.ts +15 -1
  29. package/gql/mutations/organizationMutations.ts +151 -2
  30. package/gql/mutations/sheetMutations.ts +168 -0
  31. package/gql/mutations/storageMutations.ts +1 -2
  32. package/gql/queries/aiChatQueries.ts +1 -1
  33. package/gql/queries/billingQueries.ts +1 -1
  34. package/gql/queries/dataTableQueries.ts +45 -0
  35. package/gql/queries/emailQueries.ts +44 -0
  36. package/gql/queries/inboundContactQueries.ts +40 -0
  37. package/gql/queries/index.ts +11 -6
  38. package/gql/queries/logQueries.ts +47 -20
  39. package/gql/queries/organizationQueries.ts +62 -8
  40. package/gql/queries/productQueries.ts +1 -40
  41. package/gql/queries/reportQueries.ts +81 -13
  42. package/gql/queries/sheetQueries.ts +72 -0
  43. package/gql/queries/workflowQueries.ts +30 -0
  44. package/hooks/index.ts +208 -58
  45. package/hooks/use-account-mtn.ts +52 -4
  46. package/hooks/use-aiChat-mtn.ts +62 -6
  47. package/hooks/use-aiChat-qry.ts +76 -24
  48. package/hooks/use-app-qry.ts +2 -2
  49. package/hooks/use-auth-mtn.ts +5 -5
  50. package/hooks/use-dataTable-mtn.ts +87 -0
  51. package/hooks/use-dataTable-qry.ts +172 -0
  52. package/hooks/use-email-qry.ts +112 -0
  53. package/hooks/use-inboundContact-mtn.ts +31 -0
  54. package/hooks/use-inboundContact-qry.ts +88 -0
  55. package/hooks/use-integration-mtn.ts +59 -0
  56. package/hooks/use-integration-qry.ts +15 -0
  57. package/hooks/use-log-mtn.ts +6 -6
  58. package/hooks/use-log-qry.ts +38 -35
  59. package/hooks/use-organization-mtn.ts +288 -16
  60. package/hooks/use-organization-qry.ts +71 -22
  61. package/hooks/use-product-qry.ts +4 -46
  62. package/hooks/use-report-qry.ts +94 -10
  63. package/hooks/use-sheet-mtn.ts +181 -0
  64. package/hooks/use-sheet-qry.ts +157 -0
  65. package/hooks/use-storage-mtn.ts +6 -5
  66. package/hooks/use-storage-qry.ts +1 -1
  67. package/hooks/use-workflow-qry.ts +72 -0
  68. package/package.json +23 -4
  69. package/sse.ts +2 -2
  70. package/tsconfig.json +12 -0
  71. package/types.d.ts +2 -1
  72. package/utils.ts +2 -4
  73. package/gql/fragments/actionFragments.ts +0 -16
@@ -1,13 +1,13 @@
1
1
  import { getENVVariable } from '@jsb188/app';
2
- import { getAuthToken } from '@jsb188/app/utils/api';
3
- import { makeVariablesKey } from '@jsb188/app/utils/logic';
2
+ import { getAuthToken } from '@jsb188/app/utils/api.ts';
3
+ import { makeVariablesKey } from '@jsb188/app/utils/logic.ts';
4
4
  import SSE from '@jsb188/sse';
5
5
  import { useEffect, useState } from 'react';
6
- import { loadFragment, loadQuery, resetQuery, updateQuery } from '../cache/';
7
- import { useQuery } from '../client';
8
- import { aiChatMessagesQry, aiChatQry, aiChatsQry } from '../gql/queries/aiChatQueries';
9
- import handleSSEData, { type PublishPayload } from '../sse';
10
- import type { UpdateObserversFn, UseQueryParams } from '../types.d';
6
+ import { loadFragment, loadQuery, resetQuery, updateQuery } from '../cache/index.ts';
7
+ import { useQuery, useReactiveFragmentMap } from '../client/index.ts';
8
+ import { aiChatMessagesQry, aiChatQry, aiChatsQry } from '../gql/queries/aiChatQueries.ts';
9
+ import handleSSEData, { type PublishPayload } from '../sse.ts';
10
+ import type { UpdateObserversFn, UseQueryParams } from '../types.d.ts';
11
11
 
12
12
  /**
13
13
  * Constants
@@ -16,6 +16,37 @@ import type { UpdateObserversFn, UseQueryParams } from '../types.d';
16
16
  const AI_CHATS_LIMIT = 70; // Must match server code exactly
17
17
  const AI_CHAT_MESSAGES_LIMIT = 55; // Must match server code exactly
18
18
 
19
+ /**
20
+ * Params for AI chat query hook with reactive SSE auth
21
+ */
22
+
23
+ interface UseAIChatWithSSEParams extends UseQueryParams {
24
+ authToken?: string | null;
25
+ }
26
+
27
+ /**
28
+ * Helper; build AI chats query key
29
+ */
30
+
31
+ function getAIChatsQueryKey(
32
+ organizationId?: string | null,
33
+ filter?: 'CAL_DATE' | 'CHATS' | 'ARCHIVED',
34
+ ) {
35
+ if (!organizationId || !filter) {
36
+ return '';
37
+ }
38
+
39
+ const variablesKey = makeVariablesKey({
40
+ organizationId,
41
+ filter,
42
+ cursor: null,
43
+ after: false,
44
+ limit: AI_CHATS_LIMIT
45
+ });
46
+
47
+ return `#aiChats:${variablesKey}`;
48
+ }
49
+
19
50
  /**
20
51
  * Helper; get generating (status) key from SSE data
21
52
  */
@@ -48,14 +79,16 @@ function getGeneratingFragmentId(sseData: PublishPayload) {
48
79
  * Fetch AI chat
49
80
  */
50
81
 
51
- export function useAIChatWithSSE(aiChatId?: string, initialSessionKey?: string | null, params: UseQueryParams = {}) {
82
+ export function useAIChatWithSSE(aiChatId?: string, initialSessionKey?: string | null, params: UseAIChatWithSSEParams = {}) {
83
+
84
+ const { authToken, ...queryParams } = params;
52
85
 
53
86
  const { data, ...rest } = useQuery(aiChatQry, {
54
87
  variables: {
55
88
  aiChatId
56
89
  },
57
90
  skip: !aiChatId,
58
- ...params,
91
+ ...queryParams,
59
92
  });
60
93
 
61
94
  const { updateObservers } = rest;
@@ -70,6 +103,8 @@ export function useAIChatWithSSE(aiChatId?: string, initialSessionKey?: string |
70
103
  // So we have to write the logic to make it work without perpetual session.
71
104
 
72
105
  if (dataAIChatId && sessionKey) {
106
+ const currentAuthToken = authToken ?? getAuthToken();
107
+
73
108
  // Only listen to events if data is loaded
74
109
  const connection = new SSE({
75
110
  host: getENVVariable('SSE_SERVER') as string,
@@ -77,7 +112,7 @@ export function useAIChatWithSSE(aiChatId?: string, initialSessionKey?: string |
77
112
  // path: 'test-sse',
78
113
  searchParams: {
79
114
  session_key: sessionKey,
80
- auth_token: getAuthToken()
115
+ auth_token: currentAuthToken
81
116
  }
82
117
  });
83
118
 
@@ -103,19 +138,15 @@ export function useAIChatWithSSE(aiChatId?: string, initialSessionKey?: string |
103
138
  // removeNetworkListeners();
104
139
  };
105
140
  }
106
- }, [dataAIChatId, sessionKey]);
141
+ }, [authToken, dataAIChatId, sessionKey]);
107
142
 
108
143
  useEffect(() => {
109
144
  // Every time AIChat is fetched, check if it exists in lists cache
110
145
  if (dataAIChatId) {
111
- const variablesKey = makeVariablesKey({
112
- filter: aiChat.calDate ? 'CAL_DATE' : 'CHATS',
113
- cursor: null,
114
- after: false,
115
- limit: AI_CHATS_LIMIT
116
- });
117
-
118
- const queryKey = `#aiChats:${variablesKey}`;
146
+ const queryKey = getAIChatsQueryKey(
147
+ aiChat.organizationId,
148
+ aiChat.calDate ? 'CAL_DATE' : aiChat.archivedAt ? 'ARCHIVED' : 'CHATS'
149
+ );
119
150
  const queryCache = loadQuery(queryKey);
120
151
 
121
152
  if (queryCache) {
@@ -183,20 +214,33 @@ export function useAIChatMessages(aiChatId?: string, params: UseQueryParams = {}
183
214
  * Update sidebar AI chats query
184
215
  */
185
216
 
186
- export function updateAIChats(aiChat: any, updateObservers: UpdateObserversFn) {
217
+ export function updateAIChats(
218
+ aiChat: any,
219
+ organizationId: string | null | undefined,
220
+ updateObservers: UpdateObserversFn
221
+ ) {
187
222
  if (aiChat) {
188
223
  const isCalDate = !!aiChat.calDate;
189
- const queryKey = `#aiChats:$after:false$filter:${isCalDate ? 'cal_date' : 'chats'}$limit:${AI_CHATS_LIMIT}`;
224
+ const isArchived = !!aiChat.archivedAt;
225
+ const queryKey = getAIChatsQueryKey(
226
+ organizationId || aiChat.organizationId,
227
+ isCalDate ? 'CAL_DATE' : isArchived ? 'ARCHIVED' : 'CHATS'
228
+ );
229
+ const fragmentKey = `$aiChatFragment:${aiChat.id}`;
190
230
 
191
231
  updateQuery(
192
232
  queryKey,
193
- (res) => {
233
+ (res: any[]) => {
234
+ if (res.some((item: any) => item?.__flat?.data?.[0] === fragmentKey)) {
235
+ return res;
236
+ }
237
+
194
238
  return [
195
239
  {
196
240
  cursor: aiChat.cursor,
197
241
  __flat: {
198
242
  __cache: true,
199
- data: [`$aiChatFragment:${aiChat.id}`]
243
+ data: [fragmentKey]
200
244
  }
201
245
  },
202
246
  ...res
@@ -212,7 +256,7 @@ export function updateAIChats(aiChat: any, updateObservers: UpdateObserversFn) {
212
256
  * Fetch AI chats list
213
257
  */
214
258
 
215
- export function useAIChats(organizationId: string, filter?: 'CAL_DATE' | 'CHATS') {
259
+ export function useAIChats(organizationId: string, filter?: 'CAL_DATE' | 'CHATS' | 'ARCHIVED') {
216
260
  const { data, ...other } = useQuery(aiChatsQry, {
217
261
  variables: {
218
262
  organizationId,
@@ -229,3 +273,11 @@ export function updateAIChats(aiChat: any, updateObservers: UpdateObserversFn) {
229
273
  ...other
230
274
  };
231
275
  }
276
+
277
+ /**
278
+ * Get reactive AI chat fragments for a list.
279
+ */
280
+
281
+ export function useReactiveAIChatFragmentMap(currentData?: any[] | null) {
282
+ return useReactiveFragmentMap(currentData || null, 'aiChatFragment');
283
+ }
@@ -1,6 +1,6 @@
1
1
  import { useQuery } from '@jsb188/graphql/client';
2
- import { updateNoteQry } from '../gql/queries/appQueries';
3
- import type { UseQueryParams } from '../types.d';
2
+ import { updateNoteQry } from '../gql/queries/appQueries.ts';
3
+ import type { UseQueryParams } from '../types.d.ts';
4
4
 
5
5
  /**
6
6
  * Fetch update notes
@@ -1,12 +1,12 @@
1
- import i18n from '@jsb188/app/i18n';
2
- import { deleteAuthToken } from '@jsb188/app/platform';
1
+ import i18n from '@jsb188/app/i18n/index.ts';
2
+ import { deleteAuthToken } from '@jsb188/app/platform.ts';
3
3
  import type { UseMutationParams } from '@jsb188/graphql/types.d';
4
4
  import type { OpenModalPopUpFn } from '@jsb188/react/states';
5
5
  import { AppContext, onGuestInit, useOpenModalPopUp } from '@jsb188/react/states';
6
6
  import React, { useContext, useEffect, useState } from 'react';
7
- import { changeAccountPasswordMtn, checkUsernameOrEmailMtn, confirmPasswordMtn, confirmPhoneVerificationCodeMtn, requestTokenizedEmailMtn, sendPhoneVerificationCodeMtn, signUpWithEmailMtn } from '../gql/mutations/authMutations';
8
- import { signOutMtn } from '../gql/mutations/userAuthMutations';
9
- import { useMutation } from './index';
7
+ import { changeAccountPasswordMtn, checkUsernameOrEmailMtn, confirmPasswordMtn, confirmPhoneVerificationCodeMtn, requestTokenizedEmailMtn, sendPhoneVerificationCodeMtn, signUpWithEmailMtn } from '../gql/mutations/authMutations.ts';
8
+ import { signOutMtn } from '../gql/mutations/userAuthMutations.ts';
9
+ import { useMutation } from './index.ts';
10
10
 
11
11
  /**
12
12
  * Types
@@ -0,0 +1,87 @@
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';
4
+ import { useMutation } from './index.ts';
5
+
6
+ /**
7
+ * Edit a single dataTable cell value.
8
+ */
9
+
10
+ export function useEditDataTableCell(params: UseMutationParams = {}, openModalPopUp?: OpenModalPopUpFn) {
11
+ const [editDataTableCell, mtnValues, mtnHandlers, updateObservers] = useMutation(
12
+ editDataTableCellMtn,
13
+ {
14
+ openModalPopUp,
15
+ ...params,
16
+ },
17
+ );
18
+
19
+ return {
20
+ editDataTableCell,
21
+ updateObservers,
22
+ ...mtnValues,
23
+ ...mtnHandlers,
24
+ };
25
+ }
26
+
27
+ /**
28
+ * Edit multiple dataTable cell values.
29
+ */
30
+
31
+ export function useEditDataTableCells(params: UseMutationParams = {}, openModalPopUp?: OpenModalPopUpFn) {
32
+ const [editDataTableCells, mtnValues, mtnHandlers, updateObservers] = useMutation(
33
+ editDataTableCellsMtn,
34
+ {
35
+ openModalPopUp,
36
+ ...params,
37
+ },
38
+ );
39
+
40
+ return {
41
+ editDataTableCells,
42
+ updateObservers,
43
+ ...mtnValues,
44
+ ...mtnHandlers,
45
+ };
46
+ }
47
+
48
+ /**
49
+ * Delete a single source dataTable row.
50
+ */
51
+
52
+ export function useDeleteDataTableRow(params: UseMutationParams = {}, openModalPopUp?: OpenModalPopUpFn) {
53
+ const [deleteDataTableRow, mtnValues, mtnHandlers] = useMutation(
54
+ deleteDataTableRowMtn,
55
+ {
56
+ openModalPopUp,
57
+ ...params,
58
+ },
59
+ );
60
+
61
+ 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,
83
+ updateObservers,
84
+ ...mtnValues,
85
+ ...mtnHandlers,
86
+ };
87
+ }
@@ -0,0 +1,172 @@
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;
11
+
12
+ /*
13
+ * Return all dataTable cell records nested under the provided dataTable rows.
14
+ */
15
+
16
+ function getDataTableRowCells(dataTableRows?: any[] | null) {
17
+ return dataTableRows?.flatMap((row) => row.cells || []) || null;
18
+ }
19
+
20
+ /*
21
+ * Merge reactive dataTable cell fragments back into their parent dataTable rows.
22
+ */
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;
56
+ }
57
+
58
+ /*
59
+ * Get reactive dataTable rows and their nested cell fragments.
60
+ */
61
+
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);
67
+ }
68
+
69
+ /*
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.
79
+ */
80
+
81
+ export function useDataTables(
82
+ organizationId?: string | null,
83
+ active?: boolean | null,
84
+ params: UseQueryParams = {},
85
+ ) {
86
+ const { data, ...rest } = useQuery(dataTablesQry, {
87
+ variables: {
88
+ organizationId,
89
+ active,
90
+ },
91
+ skip: !organizationId,
92
+ ...params,
93
+ });
94
+
95
+ return {
96
+ dataTables: data?.dataTables,
97
+ ...rest,
98
+ };
99
+ }
100
+
101
+ /**
102
+ * Fetch rows and cells for one dataTable.
103
+ */
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,
121
+ };
122
+ const { data, ...rest } = useQuery(dataTableRowsQry, {
123
+ variables,
124
+ skip: !organizationId || !dataTableId,
125
+ ...params,
126
+ });
127
+
128
+ const queryMatchesVariables = rest.queryKey === getDataTableRowsQueryKey(variables) ||
129
+ rest.variablesKey === makeVariablesKey(variables);
130
+ const dataTableRows = useReactiveDataTableRows(queryMatchesVariables ? data?.dataTableRows : null);
131
+
132
+ return {
133
+ dataTableRows: queryMatchesVariables ? dataTableRows : undefined,
134
+ ...rest,
135
+ };
136
+ }
137
+
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.
152
+ */
153
+
154
+ export function useReactiveDataTableRowFragment(dataTableRowId: string, currentData?: any, queryCount?: number) {
155
+ return useReactiveFragment(
156
+ currentData,
157
+ [`$dataTableRowFragment:${dataTableRowId}`],
158
+ queryCount,
159
+ );
160
+ }
161
+
162
+ /**
163
+ * Get reactive dataTable cell fragment.
164
+ */
165
+
166
+ export function useReactiveDataTableCellFragment(dataTableCellId: string, currentData?: any, queryCount?: number) {
167
+ return useReactiveFragment(
168
+ currentData,
169
+ [`$dataTableCellFragment:${dataTableCellId}`],
170
+ queryCount,
171
+ );
172
+ }
@@ -0,0 +1,112 @@
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';
5
+ import type { PaginationArgs, UseQueryParams } from '../types.d.ts';
6
+
7
+ /**
8
+ * Constants
9
+ */
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
+ }
31
+
32
+ /*
33
+ * Fetch one inbound email.
34
+ */
35
+
36
+ export function useInboundEmail(
37
+ variables: {
38
+ organizationId?: string | null;
39
+ inboundEmailId?: string | null;
40
+ },
41
+ params: UseQueryParams = {},
42
+ ) {
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
+ );
57
+
58
+ return {
59
+ inboundEmail,
60
+ initialLoading: shouldFetchInboundEmail && !data,
61
+ ...rest
62
+ };
63
+ }
64
+
65
+ /**
66
+ * Fetch inbound emails.
67
+ */
68
+
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 = {},
81
+ ) {
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
+ });
93
+
94
+ return {
95
+ inboundEmails: data?.inboundEmails,
96
+ ...rest
97
+ };
98
+ }
99
+
100
+ /**
101
+ * Get reactive inbound email fragment.
102
+ */
103
+
104
+ export function useReactiveInboundEmailFragment(inboundEmailId: string, currentData?: any, queryCount?: number) {
105
+ return useReactiveFragment(
106
+ currentData,
107
+ [
108
+ `$inboundEmailFragment:${inboundEmailId}`,
109
+ ],
110
+ queryCount,
111
+ );
112
+ }
@@ -0,0 +1,31 @@
1
+ import type { UseMutationParams } from '@jsb188/graphql/types.d';
2
+ import type { OpenModalPopUpFn } from '@jsb188/react/states';
3
+ import { editInboundContactMtn } from '../gql/mutations/inboundContactMutations.ts';
4
+ import { useMutation } from './index.ts';
5
+
6
+ /**
7
+ * Edit one inbound contact memory record.
8
+ */
9
+
10
+ export function useEditInboundContact(
11
+ params: UseMutationParams = {},
12
+ openModalPopUp?: OpenModalPopUpFn,
13
+ ) {
14
+ const { onCompleted, ...rest } = params;
15
+
16
+ const [editInboundContact, mtnValues, mtnHandlers] = useMutation(editInboundContactMtn, {
17
+ openModalPopUp,
18
+ onCompleted: (data, error, variables) => {
19
+ const updatedInboundContact = data?.editInboundContact;
20
+
21
+ onCompleted?.(updatedInboundContact, error, variables);
22
+ },
23
+ ...rest,
24
+ });
25
+
26
+ return {
27
+ editInboundContact,
28
+ ...mtnValues,
29
+ ...mtnHandlers,
30
+ };
31
+ }
@@ -0,0 +1,88 @@
1
+ import { useQuery, useReactiveFragment } from '@jsb188/graphql/client';
2
+ import type { InboundContactsSortEnum } from '@jsb188/mday/types/inboundContact.d.ts';
3
+ import { loadFragment } from '../cache/index.ts';
4
+ import { inboundContactQry, inboundContactsQry } from '../gql/queries/inboundContactQueries.ts';
5
+ import type { PaginationArgs, UseQueryParams } from '../types.d.ts';
6
+
7
+ /**
8
+ * Constants
9
+ */
10
+
11
+ const INBOUND_CONTACTS_LIMIT = 250;
12
+
13
+ /*
14
+ * Fetch one inbound contact.
15
+ */
16
+
17
+ export function useInboundContact(
18
+ variables: {
19
+ organizationId?: string | null;
20
+ inboundContactId?: string | null;
21
+ },
22
+ params: UseQueryParams = {},
23
+ ) {
24
+ const cachedInboundContact = variables.inboundContactId
25
+ ? loadFragment(`$inboundContactFragment:${variables.inboundContactId}`)
26
+ : null;
27
+ const shouldFetchInboundContact = !!variables.organizationId && !!variables.inboundContactId && !cachedInboundContact && !params.skip;
28
+ const { data, ...rest } = useQuery(inboundContactQry, {
29
+ ...params,
30
+ variables,
31
+ skip: !shouldFetchInboundContact,
32
+ });
33
+ const inboundContact = useReactiveInboundContactFragment(
34
+ variables.inboundContactId || '',
35
+ data?.inboundContact || cachedInboundContact,
36
+ rest.updatedCount,
37
+ );
38
+
39
+ return {
40
+ inboundContact,
41
+ initialLoading: shouldFetchInboundContact && !data,
42
+ ...rest
43
+ };
44
+ }
45
+
46
+ /**
47
+ * Fetch inbound contacts.
48
+ */
49
+
50
+ export function useInboundContacts(
51
+ variables: PaginationArgs & {
52
+ organizationId?: string | null;
53
+ filter?: unknown;
54
+ sort?: InboundContactsSortEnum | null;
55
+ },
56
+ params: UseQueryParams = {},
57
+ ) {
58
+ const { filter: _filter, ...queryVariables } = variables;
59
+ const { data, ...rest } = useQuery(inboundContactsQry, {
60
+ ...params,
61
+ variables: {
62
+ ...queryVariables,
63
+ cursor: null,
64
+ after: true,
65
+ limit: INBOUND_CONTACTS_LIMIT
66
+ },
67
+ skip: !variables.organizationId || params.skip,
68
+ });
69
+
70
+ return {
71
+ inboundContacts: data?.inboundContacts,
72
+ ...rest
73
+ };
74
+ }
75
+
76
+ /**
77
+ * Get reactive inbound contact fragment.
78
+ */
79
+
80
+ export function useReactiveInboundContactFragment(inboundContactId: string, currentData?: any, queryCount?: number) {
81
+ return useReactiveFragment(
82
+ currentData,
83
+ [
84
+ `$inboundContactFragment:${inboundContactId}`,
85
+ ],
86
+ queryCount,
87
+ );
88
+ }