@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,7 +1,8 @@
1
1
  import { useQuery, useReactiveFragment } from '@jsb188/graphql/client';
2
- import { childOrganizationsQry, myOrganizationsQry, organizationWorkflowsQry } from '../gql/queries/organizationQueries';
3
- import type { PaginationArgs, UseQueryParams } from '../types.d';
4
- import type { OrganizationOperationEnum } from '@jsb188/mday/types/organization.d';
2
+ import type { ChildOrgsFilterArgs } from '@jsb188/mday/types/organization.d.ts';
3
+ export { useInboundContact, useInboundContacts, useReactiveInboundContactFragment } from './use-inboundContact-qry.ts';
4
+ import { childOrganizationsQry, myOrganizationsQry, organizationMembersQry, organizationSitesQry } from '../gql/queries/organizationQueries.ts';
5
+ import type { PaginationArgs, UseQueryParams } from '../types.d.ts';
5
6
 
6
7
  const ORG_CHILDREN_LIMIT = 250;
7
8
 
@@ -19,45 +20,75 @@ export function useMyOrganizations(params: UseQueryParams = {}) {
19
20
  }
20
21
 
21
22
  /**
22
- * Fetch organization workflows
23
+ * Fetch organization members
23
24
  */
24
25
 
25
- export function useOrganizationWorkflows(organizationId: string | null, operation: OrganizationOperationEnum | null) {
26
- const { data, ...rest } = useQuery(organizationWorkflowsQry, {
26
+ export function useOrganizationMembers(
27
+ organizationId: string | null,
28
+ paramsOrShowGuests: UseQueryParams | boolean = {},
29
+ showGuests_: boolean = false,
30
+ ) {
31
+ const params = typeof paramsOrShowGuests === 'boolean' ? {} : paramsOrShowGuests;
32
+ const showGuests = typeof paramsOrShowGuests === 'boolean' ? paramsOrShowGuests : showGuests_;
33
+
34
+ const { data, ...rest } = useQuery(organizationMembersQry, {
27
35
  variables: {
28
36
  organizationId,
29
- operation,
37
+ showGuests,
30
38
  },
31
- skip: !organizationId || !operation,
39
+ skip: !organizationId || params.skip,
40
+ ...params,
32
41
  });
33
42
 
34
43
  return {
35
- organizationWorkflows: data?.organizationWorkflows,
44
+ organizationMembers: data?.organizationMembers,
36
45
  ...rest
37
46
  };
38
47
  }
39
48
 
40
49
  /**
41
- * Get reactive organization fragment
50
+ * Fetch organization sites
42
51
  */
43
52
 
44
- export function useReactiveOrganizationWorkflowFragment(workflowId: string, currentData?: any, queryCount?: number) {
53
+ export function useOrganizationSites(
54
+ organizationId: string | null,
55
+ parentId: string | null,
56
+ params: UseQueryParams = {},
57
+ ) {
58
+ const { data, ...rest } = useQuery(organizationSitesQry, {
59
+ variables: {
60
+ organizationId,
61
+ parentId,
62
+ },
63
+ skip: !organizationId || params.skip,
64
+ ...params,
65
+ });
66
+
67
+ return {
68
+ organizationSites: data?.organizationSites,
69
+ ...rest
70
+ };
71
+ }
72
+
73
+ export function useReactiveOrganization(orgId: string, currentData?: any, queryCount?: number) {
45
74
  return useReactiveFragment(
46
75
  currentData,
47
- [`$organizationInstructionsFragment:${workflowId}`],
76
+ [`$organizationFragment:${orgId}`],
48
77
  queryCount,
49
78
  );
50
79
  }
51
80
 
52
- /**
53
- * Get reactive organization fragment
81
+ /*
82
+ * Get reactive organization settings fragment data.
54
83
  */
55
-
56
- export function useReactiveOrganization(orgId: string, currentData?: any, queryCount?: number) {
84
+ export function useReactiveOrganizationSettings(orgId: string | null | undefined, currentData?: any, queryCount?: number) {
57
85
  return useReactiveFragment(
58
86
  currentData,
59
- [`$organizationFragment:${orgId}`],
87
+ orgId ? [`$organizationSettingsFragment:${orgId}`] : [],
60
88
  queryCount,
89
+ undefined,
90
+ undefined,
91
+ true,
61
92
  );
62
93
  }
63
94
 
@@ -71,10 +102,23 @@ export function useOrgRelFromMyOrganizations(organizationId: string | null) {
71
102
  });
72
103
 
73
104
  const orgRel = myOrganizations?.find((orgRel: any) => orgRel.organization?.id === organizationId) || null;
74
- const organization = useReactiveOrganization(orgRel?.organization?.id, orgRel?.organization);
105
+ const reactiveOrganization = useReactiveOrganization(orgRel?.organization?.id, orgRel?.organization);
106
+ const reactiveSettings = useReactiveOrganizationSettings(reactiveOrganization?.id, reactiveOrganization?.settings);
107
+ const organization = reactiveOrganization && reactiveSettings
108
+ ? {
109
+ ...reactiveOrganization,
110
+ settings: reactiveSettings,
111
+ }
112
+ : reactiveOrganization;
113
+ const organizationRelationship = orgRel && organization
114
+ ? {
115
+ ...orgRel,
116
+ organization,
117
+ }
118
+ : orgRel;
75
119
 
76
120
  return {
77
- organizationRelationship: orgRel,
121
+ organizationRelationship,
78
122
  organization,
79
123
  myOrganizations,
80
124
  ...rest
@@ -87,7 +131,7 @@ export function useOrgRelFromMyOrganizations(organizationId: string | null) {
87
131
 
88
132
  export function useChildOrganizations(variables: PaginationArgs & {
89
133
  organizationId?: string | null;
90
- // filters: ? not ready yet
134
+ filter?: ChildOrgsFilterArgs;
91
135
  }, params: UseQueryParams = {}) {
92
136
  const { data, ...rest } = useQuery(childOrganizationsQry, {
93
137
  variables: {
@@ -110,16 +154,21 @@ export function useChildOrganizations(variables: PaginationArgs & {
110
154
  * Get reactive organization fragment
111
155
  */
112
156
 
113
- export function useReactiveOrganizationChildFragment(childId: string, currentData?: any, queryCount?: number) {
157
+ export function useReactiveOrganizationChildFragment(childId: string, currentData?: any, queryCount?: number, mainPrioritizedFields?: string[]) {
158
+ const organizationId = currentData?.organization?.id;
159
+
114
160
  return useReactiveFragment(
115
161
  currentData,
116
162
  [
117
163
  `$organizationChildFragment:${childId}`,
164
+ organizationId ? [`$organizationFragment:${organizationId}`, 'organization'] : null,
118
165
  // [`$organizationChildArableFragment:${childId}`, null],
119
166
  // By having the second paramter as null, we only observe the reactive changes without setting the data
120
167
  // [`$logArableFragment:${logEntryId}`, null],
121
- ],
168
+ ].filter(Boolean) as Array<string | [string, string | null]>,
122
169
  queryCount,
170
+ undefined,
171
+ mainPrioritizedFields,
123
172
  );
124
173
  }
125
174
 
@@ -1,10 +1,7 @@
1
- import { checkACLPermission } from '@jsb188/mday/utils/organization';
2
- import type { ProductsFilterArgs } from '@jsb188/mday/types/product.d';
3
- import { useMemo } from 'react';
4
- import { useQuery, useReactiveFragment } from '../client';
5
- import { productAttendanceListQry, productsListQry } from '../gql/queries/productQueries';
6
- import type { PaginationArgs, UseQueryParams } from '../types';
7
- import { useOrgRelFromMyOrganizations } from './use-organization-qry';
1
+ import type { ProductsFilterArgs } from '@jsb188/mday/types/product.d.ts';
2
+ import { useQuery, useReactiveFragment } from '../client/index.ts';
3
+ import { productsListQry } from '../gql/queries/productQueries.ts';
4
+ import type { PaginationArgs, UseQueryParams } from '../types.d.ts';
8
5
 
9
6
  /**
10
7
  * Constants
@@ -68,42 +65,3 @@ export function useReactiveProductFragment(productId: string, currentData?: any,
68
65
  queryCount,
69
66
  );
70
67
  }
71
-
72
- /**
73
- * Fetch Org Event attendance, ACL, and Org Event fragment from cache
74
- */
75
-
76
- export function useProductAttendance(
77
- variables: {
78
- organizationId: string;
79
- productId: string;
80
- calDate: string;
81
- },
82
- params: UseQueryParams = {},
83
- ) {
84
-
85
- const { organizationId, productId, calDate } = variables;
86
- const { organizationRelationship } = useOrgRelFromMyOrganizations(organizationId);
87
-
88
- const { data, ...rest } = useQuery(productAttendanceListQry, {
89
- variables,
90
- skip: !productId || !calDate || !organizationId,
91
- ...params,
92
- });
93
-
94
- const productAttendanceList = data?.productAttendanceList;
95
- const notReady = !organizationRelationship || !productAttendanceList;
96
-
97
- const allowEdit = useMemo(() => {
98
- return checkACLPermission(organizationRelationship, 'products', 'MANAGE');
99
- }, [organizationRelationship?.acl, organizationRelationship?.role]);
100
-
101
- // console.log('viewerAccountId', viewerAccountId, organizationEvent?.accountId, isMyDocument, allowEdit);
102
-
103
- return {
104
- productAttendanceList,
105
- notReady,
106
- allowEdit,
107
- ...rest
108
- };
109
- }
@@ -1,7 +1,7 @@
1
- import type { ReportsFilterArgs, ReportsSortEnum } from '@jsb188/mday/types/report.d';
2
- import { useQuery, useReactiveFragment } from '../client';
3
- import { availableReportsQry, reportsQry } from '../gql/queries/reportQueries';
4
- import type { UseQueryParams } from '../types';
1
+ import type { ReportsFilterArgs, ReportsSortEnum } from '@jsb188/mday/types/report.d.ts';
2
+ import { useQuery, useReactiveFragment, useReactiveFragmentMap } from '../client/index.ts';
3
+ import { reportGroupsQry, reportQry, reportsQry, reportSubmissionsQry } from '../gql/queries/reportQueries.ts';
4
+ import type { UseQueryParams } from '../types.d.ts';
5
5
 
6
6
  /**
7
7
  * Get reactive report fragment
@@ -17,6 +17,28 @@ export function useReactiveReportFragment(reportId: string, currentData?: any, q
17
17
  );
18
18
  }
19
19
 
20
+ /**
21
+ * Get reactive report submission fragment
22
+ */
23
+
24
+ export function useReactiveReportSubmissionFragment(reportSubmissionId: string, currentData?: any, queryCount?: number) {
25
+ return useReactiveFragment(
26
+ currentData,
27
+ [
28
+ `$reportSubmissionFragment:${reportSubmissionId}`,
29
+ ],
30
+ queryCount,
31
+ );
32
+ }
33
+
34
+ /**
35
+ * Get reactive report column fragments for a row of report columns.
36
+ */
37
+
38
+ export function useReactiveReportColumnFragmentMap(currentData?: any[] | null) {
39
+ return useReactiveFragmentMap(currentData || null, 'reportColumnDataFragment');
40
+ }
41
+
20
42
  /**
21
43
  * Fetch list of reports by type and period
22
44
  */
@@ -25,7 +47,7 @@ export function useReports(
25
47
  variables: {
26
48
  organizationId: string;
27
49
  filter: ReportsFilterArgs;
28
- sort: ReportsSortEnum;
50
+ sort?: ReportsSortEnum;
29
51
  },
30
52
  params: UseQueryParams = {},
31
53
  ) {
@@ -34,7 +56,7 @@ export function useReports(
34
56
  const { skip, ...restParams } = params;
35
57
  const { data, ...rest } = useQuery(reportsQry, {
36
58
  variables,
37
- skip: skip || !organizationId || !filter,
59
+ skip: skip || !organizationId || !filter?.reportGroupId || !(filter?.startPeriod || filter?.endPeriod),
38
60
  ...restParams,
39
61
  });
40
62
 
@@ -45,16 +67,41 @@ export function useReports(
45
67
  }
46
68
 
47
69
  /**
48
- * Fetch list of available reports statuses
70
+ * Fetch one report by report source id
71
+ */
72
+
73
+ export function useReport(
74
+ organizationId: string | null,
75
+ reportSourceId: string | null,
76
+ params: UseQueryParams = {},
77
+ ) {
78
+ const { skip, ...restParams } = params;
79
+ const { data, ...rest } = useQuery(reportQry, {
80
+ variables: {
81
+ organizationId,
82
+ reportSourceId,
83
+ },
84
+ skip: skip || !organizationId || !reportSourceId,
85
+ ...restParams,
86
+ });
87
+
88
+ return {
89
+ report: data?.report,
90
+ ...rest
91
+ };
92
+ }
93
+
94
+ /**
95
+ * Fetch list of report groups for organization
49
96
  */
50
97
 
51
- export function useAvailableReports(
98
+ export function useReportGroups(
52
99
  organizationId: string | null,
53
100
  params: UseQueryParams = {},
54
101
  ) {
55
102
 
56
103
  const { skip, ...restParams } = params;
57
- const { data, ...rest } = useQuery(availableReportsQry, {
104
+ const { data, ...rest } = useQuery(reportGroupsQry, {
58
105
  variables: {
59
106
  organizationId,
60
107
  },
@@ -63,7 +110,44 @@ export function useAvailableReports(
63
110
  });
64
111
 
65
112
  return {
66
- availableReports: data?.availableReports,
113
+ reportGroups: data?.reportGroups,
114
+ ...rest
115
+ };
116
+ }
117
+
118
+ /**
119
+ * Fetch list of report submissions for organization
120
+ */
121
+
122
+ export function useReportSubmissions(
123
+ variables: {
124
+ organizationId: string | null;
125
+ filter: ReportsFilterArgs;
126
+ sort?: ReportsSortEnum;
127
+ limit?: number;
128
+ },
129
+ params: UseQueryParams = {},
130
+ ) {
131
+
132
+ const {
133
+ organizationId,
134
+ filter,
135
+ sort = 'PERIOD_DESC',
136
+ limit = 100,
137
+ } = variables;
138
+ const { skip, ...restParams } = params;
139
+ const { data, ...rest } = useQuery(reportSubmissionsQry, {
140
+ variables: {
141
+ ...variables,
142
+ sort,
143
+ limit,
144
+ },
145
+ skip: skip || !organizationId || !filter,
146
+ ...restParams,
147
+ });
148
+
149
+ return {
150
+ reportSubmissions: data?.reportSubmissions,
67
151
  ...rest
68
152
  };
69
153
  }
@@ -0,0 +1,181 @@
1
+ import type { UseMutationParams } from '@jsb188/graphql/types.d';
2
+ import { OpenModalPopUpFn } from '@jsb188/react/states';
3
+ import {
4
+ clearSheetCellMtn,
5
+ createSheetMtn,
6
+ deleteSheetRangeMtn,
7
+ deleteSheetRegionMtn,
8
+ editSheetCellsMtn,
9
+ updateSheetMtn,
10
+ upsertSheetDataTableRegionMtn,
11
+ upsertSheetRangeMtn,
12
+ } from '../gql/mutations/sheetMutations.ts';
13
+ import { useMutation } from './index.ts';
14
+
15
+ /*
16
+ * Create a sheet.
17
+ */
18
+
19
+ export function useCreateSheet(params: UseMutationParams = {}, openModalPopUp?: OpenModalPopUpFn) {
20
+ const [createSheet, mtnValues, mtnHandlers, updateObservers] = useMutation(
21
+ createSheetMtn,
22
+ {
23
+ openModalPopUp,
24
+ ...params,
25
+ },
26
+ );
27
+
28
+ return {
29
+ createSheet,
30
+ updateObservers,
31
+ ...mtnValues,
32
+ ...mtnHandlers,
33
+ };
34
+ }
35
+
36
+ /*
37
+ * Update a sheet.
38
+ */
39
+
40
+ export function useUpdateSheet(params: UseMutationParams = {}, openModalPopUp?: OpenModalPopUpFn) {
41
+ const [updateSheet, mtnValues, mtnHandlers, updateObservers] = useMutation(
42
+ updateSheetMtn,
43
+ {
44
+ openModalPopUp,
45
+ ...params,
46
+ },
47
+ );
48
+
49
+ return {
50
+ updateSheet,
51
+ updateObservers,
52
+ ...mtnValues,
53
+ ...mtnHandlers,
54
+ };
55
+ }
56
+
57
+ /*
58
+ * Edit multiple sheet cells.
59
+ */
60
+
61
+ export function useEditSheetCells(params: UseMutationParams = {}, openModalPopUp?: OpenModalPopUpFn) {
62
+ const [editSheetCells, mtnValues, mtnHandlers, updateObservers] = useMutation(
63
+ editSheetCellsMtn,
64
+ {
65
+ openModalPopUp,
66
+ ...params,
67
+ },
68
+ );
69
+
70
+ return {
71
+ editSheetCells,
72
+ updateObservers,
73
+ ...mtnValues,
74
+ ...mtnHandlers,
75
+ };
76
+ }
77
+
78
+ /*
79
+ * Clear one sheet cell.
80
+ */
81
+
82
+ export function useClearSheetCell(params: UseMutationParams = {}, openModalPopUp?: OpenModalPopUpFn) {
83
+ const [clearSheetCell, mtnValues, mtnHandlers, updateObservers] = useMutation(
84
+ clearSheetCellMtn,
85
+ {
86
+ openModalPopUp,
87
+ ...params,
88
+ },
89
+ );
90
+
91
+ return {
92
+ clearSheetCell,
93
+ updateObservers,
94
+ ...mtnValues,
95
+ ...mtnHandlers,
96
+ };
97
+ }
98
+
99
+ /*
100
+ * Create or update one sheet range.
101
+ */
102
+
103
+ export function useUpsertSheetRange(params: UseMutationParams = {}, openModalPopUp?: OpenModalPopUpFn) {
104
+ const [upsertSheetRange, mtnValues, mtnHandlers, updateObservers] = useMutation(
105
+ upsertSheetRangeMtn,
106
+ {
107
+ openModalPopUp,
108
+ ...params,
109
+ },
110
+ );
111
+
112
+ return {
113
+ upsertSheetRange,
114
+ updateObservers,
115
+ ...mtnValues,
116
+ ...mtnHandlers,
117
+ };
118
+ }
119
+
120
+ /*
121
+ * Delete one sheet range.
122
+ */
123
+
124
+ export function useDeleteSheetRange(params: UseMutationParams = {}, openModalPopUp?: OpenModalPopUpFn) {
125
+ const [deleteSheetRange, mtnValues, mtnHandlers, updateObservers] = useMutation(
126
+ deleteSheetRangeMtn,
127
+ {
128
+ openModalPopUp,
129
+ ...params,
130
+ },
131
+ );
132
+
133
+ return {
134
+ deleteSheetRange,
135
+ updateObservers,
136
+ ...mtnValues,
137
+ ...mtnHandlers,
138
+ };
139
+ }
140
+
141
+ /*
142
+ * Create or update one Data Table-backed sheet region.
143
+ */
144
+
145
+ export function useUpsertSheetDataTableRegion(params: UseMutationParams = {}, openModalPopUp?: OpenModalPopUpFn) {
146
+ const [upsertSheetDataTableRegion, mtnValues, mtnHandlers, updateObservers] = useMutation(
147
+ upsertSheetDataTableRegionMtn,
148
+ {
149
+ openModalPopUp,
150
+ ...params,
151
+ },
152
+ );
153
+
154
+ return {
155
+ upsertSheetDataTableRegion,
156
+ updateObservers,
157
+ ...mtnValues,
158
+ ...mtnHandlers,
159
+ };
160
+ }
161
+
162
+ /*
163
+ * Delete one sheet region.
164
+ */
165
+
166
+ export function useDeleteSheetRegion(params: UseMutationParams = {}, openModalPopUp?: OpenModalPopUpFn) {
167
+ const [deleteSheetRegion, mtnValues, mtnHandlers, updateObservers] = useMutation(
168
+ deleteSheetRegionMtn,
169
+ {
170
+ openModalPopUp,
171
+ ...params,
172
+ },
173
+ );
174
+
175
+ return {
176
+ deleteSheetRegion,
177
+ updateObservers,
178
+ ...mtnValues,
179
+ ...mtnHandlers,
180
+ };
181
+ }
@@ -0,0 +1,157 @@
1
+ import { makeVariablesKey } from '@jsb188/app/utils/logic.ts';
2
+ import { useQuery, useReactiveFragment, useReactiveFragmentMap } from '@jsb188/graphql/client';
3
+ import { sheetGridQry, sheetQry, sheetsQry } from '../gql/queries/sheetQueries.ts';
4
+ import type { UseQueryParams } from '../types.d.ts';
5
+
6
+ export type SheetGridViewportVariables = {
7
+ startRowIndex: number;
8
+ startColumnIndex: number;
9
+ rowCount: number;
10
+ columnCount: number;
11
+ };
12
+
13
+ /*
14
+ * Return the query key that belongs to one sheet grid variables key.
15
+ */
16
+
17
+ function getSheetGridQueryKey(variablesKey: string) {
18
+ return `#sheetGrid:${variablesKey}`;
19
+ }
20
+
21
+ /*
22
+ * Merge reactive SheetCell fragments back into a sheetGrid viewport.
23
+ */
24
+
25
+ function mergeReactiveCellsIntoSheetGrid(sheetGrid?: any | null, sheetGridCells?: any[] | null) {
26
+ if (!sheetGrid || !sheetGridCells) {
27
+ return sheetGrid;
28
+ }
29
+
30
+ if (sheetGridCells === sheetGrid.cells && !sheetGridCells.some((cell) => cell?.__deleted)) {
31
+ return sheetGrid;
32
+ }
33
+
34
+ const nextCells = sheetGridCells.filter((cell) => !cell?.__deleted);
35
+ if (nextCells.length === sheetGrid.cells?.length && nextCells.every((cell, index) => cell === sheetGrid.cells[index])) {
36
+ return sheetGrid;
37
+ }
38
+
39
+ return {
40
+ ...sheetGrid,
41
+ cells: nextCells,
42
+ };
43
+ }
44
+
45
+ /*
46
+ * Fetch sheets for an organization.
47
+ */
48
+
49
+ export function useSheets(
50
+ organizationId?: string | null,
51
+ active?: boolean | null,
52
+ params: UseQueryParams = {},
53
+ ) {
54
+ const { data, ...rest } = useQuery(sheetsQry, {
55
+ variables: {
56
+ organizationId,
57
+ active,
58
+ },
59
+ skip: !organizationId,
60
+ ...params,
61
+ });
62
+
63
+ return {
64
+ sheets: data?.sheets,
65
+ ...rest,
66
+ };
67
+ }
68
+
69
+ /*
70
+ * Fetch one sheet by id.
71
+ */
72
+
73
+ export function useSheet(
74
+ sheetId?: string | null,
75
+ organizationId?: string | null,
76
+ params: UseQueryParams = {},
77
+ ) {
78
+ const { data, ...rest } = useQuery(sheetQry, {
79
+ variables: {
80
+ organizationId,
81
+ sheetId,
82
+ },
83
+ skip: !organizationId || !sheetId,
84
+ ...params,
85
+ });
86
+
87
+ return {
88
+ sheet: data?.sheet,
89
+ ...rest,
90
+ };
91
+ }
92
+
93
+ /*
94
+ * Get reactive Sheet fragment.
95
+ */
96
+
97
+ export function useReactiveSheetFragment(sheetId: string, currentData?: any, queryCount?: number) {
98
+ return useReactiveFragment(
99
+ currentData,
100
+ [`$sheetFragment:${sheetId}`],
101
+ queryCount,
102
+ );
103
+ }
104
+
105
+ /*
106
+ * Get reactive SheetGrid fragment.
107
+ */
108
+
109
+ export function useReactiveSheetGridFragment(sheetGridId: string, currentData?: any, queryCount?: number) {
110
+ return useReactiveFragment(
111
+ currentData,
112
+ sheetGridId ? [`$sheetGridFragment:${sheetGridId}`] : [],
113
+ queryCount,
114
+ );
115
+ }
116
+
117
+ /*
118
+ * Get reactive SheetGrid cell fragments.
119
+ */
120
+
121
+ export function useReactiveSheetGridCells(sheetGrid?: any | null) {
122
+ const reactiveCells = useReactiveFragmentMap(sheetGrid?.cells || null, 'sheetCellFragment');
123
+
124
+ return mergeReactiveCellsIntoSheetGrid(sheetGrid, reactiveCells);
125
+ }
126
+
127
+ /*
128
+ * Fetch one sheet viewport by id.
129
+ */
130
+
131
+ export function useSheetGrid(
132
+ sheetId?: string | null,
133
+ organizationId?: string | null,
134
+ viewport?: SheetGridViewportVariables | null,
135
+ params: UseQueryParams = {},
136
+ ) {
137
+ const variables = {
138
+ organizationId,
139
+ sheetId,
140
+ viewport,
141
+ };
142
+ const { data, ...rest } = useQuery(sheetGridQry, {
143
+ variables,
144
+ skip: !organizationId || !sheetId || !viewport,
145
+ ...params,
146
+ });
147
+ const variablesKey = makeVariablesKey(variables);
148
+ const queryMatchesVariables = rest.queryKey === getSheetGridQueryKey(variablesKey) ||
149
+ rest.variablesKey === variablesKey;
150
+ const reactiveSheetGrid = useReactiveSheetGridFragment(sheetId || '', queryMatchesVariables ? data?.sheetGrid : undefined);
151
+ const sheetGrid = useReactiveSheetGridCells(reactiveSheetGrid);
152
+
153
+ return {
154
+ sheetGrid,
155
+ ...rest,
156
+ };
157
+ }