@jsb188/graphql 1.1.5 → 1.1.6
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.
- package/cache/config.ts +11 -4
- package/cache/index.ts +249 -103
- package/gql/fragments/actionFragments.ts +16 -0
- package/gql/fragments/logFragments.ts +72 -2
- package/gql/fragments/organizationFragments.ts +64 -5
- package/gql/fragments/otherFragments.ts +10 -8
- package/gql/fragments/productFragments.ts +63 -0
- package/gql/fragments/reportFragments.ts +47 -0
- package/gql/mutations/accountMutations.ts +10 -0
- package/gql/mutations/authMutations.ts +11 -16
- package/gql/mutations/index.ts +1 -0
- package/gql/mutations/logMutations.ts +20 -6
- package/gql/mutations/organizationMutations.ts +42 -0
- package/gql/mutations/otherMutations.ts +0 -17
- package/gql/mutations/userAuthMutations.ts +0 -12
- package/gql/queries/aiChatQueries.ts +3 -0
- package/gql/queries/index.ts +6 -0
- package/gql/queries/logQueries.ts +60 -11
- package/gql/queries/organizationQueries.ts +61 -1
- package/gql/queries/productQueries.ts +70 -0
- package/gql/queries/reportQueries.ts +60 -0
- package/hooks/index.ts +128 -41
- package/hooks/use-account-mtn.ts +26 -1
- package/hooks/use-aiChat-mtn.ts +17 -1
- package/hooks/use-aiChat-qry.ts +19 -10
- package/hooks/use-auth-mtn.ts +25 -2
- package/hooks/use-log-mtn.ts +4 -4
- package/hooks/use-log-qry.ts +47 -16
- package/hooks/use-organization-mtn.ts +105 -0
- package/hooks/use-organization-qry.ts +119 -15
- package/hooks/use-product-qry.ts +109 -0
- package/hooks/use-report-qry.ts +69 -0
- package/package.json +11 -11
- package/sse.ts +11 -0
- package/gql/queries/otherQueries.ts +0 -12
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { gql } from 'graphql-tag';
|
|
2
|
+
import { accountFragment } from '../fragments/accountFragments';
|
|
3
|
+
import { organizationComplianceFragment, organizationFragment } from '../fragments/organizationFragments';
|
|
4
|
+
import { productAttendanceFragment, productCalEventFragment, productFragment, productLivestockFragment } from '../fragments/productFragments';
|
|
5
|
+
|
|
6
|
+
export const productsListQry = gql`
|
|
7
|
+
query productsList (
|
|
8
|
+
$organizationId: GenericID!
|
|
9
|
+
$filter: ProductsFilter!
|
|
10
|
+
$cursor: Cursor
|
|
11
|
+
$after: Boolean!
|
|
12
|
+
$limit: Int!
|
|
13
|
+
) {
|
|
14
|
+
productsList (
|
|
15
|
+
organizationId: $organizationId
|
|
16
|
+
filter: $filter
|
|
17
|
+
cursor: $cursor
|
|
18
|
+
after: $after
|
|
19
|
+
limit: $limit
|
|
20
|
+
) {
|
|
21
|
+
...productFragment
|
|
22
|
+
|
|
23
|
+
details {
|
|
24
|
+
...on ProductCalEvent {
|
|
25
|
+
...productCalEventFragment
|
|
26
|
+
}
|
|
27
|
+
...on ProductLivestock {
|
|
28
|
+
...productLivestockFragment
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
${productFragment}
|
|
35
|
+
${productCalEventFragment}
|
|
36
|
+
${productLivestockFragment}
|
|
37
|
+
`;
|
|
38
|
+
|
|
39
|
+
export const productAttendanceListQry = gql`
|
|
40
|
+
query productAttendanceList (
|
|
41
|
+
$productId: GenericID!
|
|
42
|
+
$organizationId: GenericID!
|
|
43
|
+
$calDate: CalDateString!
|
|
44
|
+
) {
|
|
45
|
+
productAttendanceList (
|
|
46
|
+
productId: $productId
|
|
47
|
+
organizationId: $organizationId
|
|
48
|
+
calDate: $calDate
|
|
49
|
+
) {
|
|
50
|
+
...productAttendanceFragment
|
|
51
|
+
|
|
52
|
+
organization {
|
|
53
|
+
...organizationFragment
|
|
54
|
+
|
|
55
|
+
compliance {
|
|
56
|
+
...organizationComplianceFragment
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
checkedBy {
|
|
61
|
+
...accountFragment
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
${productAttendanceFragment}
|
|
67
|
+
${organizationFragment}
|
|
68
|
+
${organizationComplianceFragment}
|
|
69
|
+
${accountFragment}
|
|
70
|
+
`;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { gql } from 'graphql-tag';
|
|
2
|
+
import { reportColumnDataFragment, reportFragment, reportAvailabilityFragment, reportRowDataFragment, reportSectionFragment } from '../fragments/reportFragments';
|
|
3
|
+
|
|
4
|
+
export const reportsQry = gql`
|
|
5
|
+
query reports (
|
|
6
|
+
$organizationId: GenericID!
|
|
7
|
+
$filter: ReportsFilter!
|
|
8
|
+
$sort: ReportsSort
|
|
9
|
+
) {
|
|
10
|
+
reports (
|
|
11
|
+
organizationId: $organizationId
|
|
12
|
+
filter: $filter
|
|
13
|
+
sort: $sort
|
|
14
|
+
) {
|
|
15
|
+
...reportFragment
|
|
16
|
+
|
|
17
|
+
sections {
|
|
18
|
+
...reportSectionFragment
|
|
19
|
+
|
|
20
|
+
rows {
|
|
21
|
+
...reportRowDataFragment
|
|
22
|
+
|
|
23
|
+
columns {
|
|
24
|
+
...reportColumnDataFragment
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
# This needs to be deprecated and merged to sections
|
|
30
|
+
# Its only used for cleaning logs right now
|
|
31
|
+
|
|
32
|
+
rows {
|
|
33
|
+
...reportRowDataFragment
|
|
34
|
+
|
|
35
|
+
columns {
|
|
36
|
+
...reportColumnDataFragment
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
${reportFragment}
|
|
43
|
+
${reportSectionFragment}
|
|
44
|
+
${reportRowDataFragment}
|
|
45
|
+
${reportColumnDataFragment}
|
|
46
|
+
`;
|
|
47
|
+
|
|
48
|
+
export const availableReportsQry = gql`
|
|
49
|
+
query availableReports (
|
|
50
|
+
$organizationId: GenericID!
|
|
51
|
+
) {
|
|
52
|
+
availableReports (
|
|
53
|
+
organizationId: $organizationId
|
|
54
|
+
) {
|
|
55
|
+
...reportAvailabilityFragment
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
${reportAvailabilityFragment}
|
|
60
|
+
`;
|
package/hooks/index.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { getENVVariable } from '@jsb188/app';
|
|
2
2
|
import type { ServerErrorObj, SimpleErrorType } from '@jsb188/app/types/app.d';
|
|
3
3
|
import { normalizeServerError } from '@jsb188/app/utils/api';
|
|
4
|
-
import { delay } from '@jsb188/app/utils/logic';
|
|
4
|
+
import { delay, makeVariablesKey } from '@jsb188/app/utils/logic';
|
|
5
5
|
import { isServerErrorGQL } from '@jsb188/graphql/utils';
|
|
6
6
|
import { useConnectedToServerValue, useFragmentObserverValue, useQueryObserverValue, useScreenIsFocusedValue, useSetFragmentObserver, useSetQueryObserver } from '@jsb188/react/states';
|
|
7
7
|
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
8
|
-
import { checkDataCompleteness, fetchCachedData, loadDataFromCache, loadFragment,
|
|
8
|
+
import { checkDataCompleteness, clearQueryResetStatus, fetchCachedData, getQueryRefreshTime, loadDataFromCache, loadFragment, removeStaleCache } from '../cache';
|
|
9
9
|
import { QUERY_EXPIRE_TIMES } from '../cache/config';
|
|
10
10
|
import { graphqlRequest } from '../client/request';
|
|
11
11
|
import type { AnyModalPopUpFn, GraphQLHandlers, GraphQLQueryOptions, OnCompletedGQLFn, OnErrorGQLFn, UpdateDataObserverArgs, UpdateObserversFn } from '../types';
|
|
@@ -62,6 +62,7 @@ type GraphQLQueryResult = {
|
|
|
62
62
|
fetchMore: (obj: GraphQLRequestOptions) => Promise<any>;
|
|
63
63
|
queryName: string;
|
|
64
64
|
queryKey: string;
|
|
65
|
+
refreshTime: string;
|
|
65
66
|
variablesKey: string;
|
|
66
67
|
updatedCount: number;
|
|
67
68
|
updateObservers: UpdateObserversFn;
|
|
@@ -127,34 +128,86 @@ export function useUpdateObservers(): UpdateObserversFn {
|
|
|
127
128
|
export function useWatchQuery(
|
|
128
129
|
query: any,
|
|
129
130
|
variablesKey: string,
|
|
130
|
-
|
|
131
|
+
lastUpdatedCount: number,
|
|
132
|
+
loading: boolean,
|
|
133
|
+
) { // [updatedCount, forceRefetch, qryReset]
|
|
134
|
+
|
|
135
|
+
const qryObserver = useQueryObserverValue();
|
|
136
|
+
const [qryReset, setQryReset] = useState({
|
|
137
|
+
triggerTime: '',
|
|
138
|
+
refreshTime: '', // This value is fixed and never resets (this should be used as a tracker)
|
|
139
|
+
refreshTimeActive: '', // This value resets when loading starts
|
|
140
|
+
});
|
|
131
141
|
|
|
132
|
-
const
|
|
142
|
+
const queryWatcher = useMemo(() => {
|
|
143
|
+
if (qryObserver.name) {
|
|
144
|
+
const queryDefs = query.definitions.filter((d: any) => d.kind === 'OperationDefinition');
|
|
145
|
+
const queryName = queryDefs?.[0]?.name?.value;
|
|
133
146
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
147
|
+
if (queryName) {
|
|
148
|
+
const qryTriggerTime = getQueryRefreshTime(queryName, variablesKey);
|
|
149
|
+
|
|
150
|
+
if (qryTriggerTime) {
|
|
151
|
+
clearQueryResetStatus(queryName, variablesKey);
|
|
152
|
+
return [lastUpdatedCount + 1, true, qryTriggerTime];
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const matchedQry = queryDefs
|
|
157
|
+
.find((d: any) =>
|
|
158
|
+
qryObserver.name.startsWith('#' + d.name.value) ||
|
|
159
|
+
('#' + d.name.value + ':') === qryObserver.name
|
|
160
|
+
);
|
|
139
161
|
|
|
140
162
|
if (!matchedQry) {
|
|
141
|
-
return [0, false];
|
|
163
|
+
return [0, false, ''];
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const matchedQueryName = matchedQry.name.value;
|
|
167
|
+
const matchedQueryId = `#${matchedQueryName}:${variablesKey}`;
|
|
168
|
+
const forceRefetch = !!qryObserver.forceRefetch;
|
|
169
|
+
|
|
170
|
+
let newUpdatedCount = qryObserver.name === matchedQueryId || forceRefetch ? qryObserver.count : 0;
|
|
171
|
+
if (forceRefetch) {
|
|
172
|
+
newUpdatedCount += 1;
|
|
142
173
|
}
|
|
143
174
|
|
|
144
|
-
|
|
145
|
-
|
|
175
|
+
return [newUpdatedCount, forceRefetch, ''];
|
|
176
|
+
}
|
|
146
177
|
|
|
147
|
-
|
|
148
|
-
|
|
178
|
+
return [0, false, ''];
|
|
179
|
+
}, [qryObserver]);
|
|
149
180
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
181
|
+
useEffect(() => {
|
|
182
|
+
if (!loading) {
|
|
183
|
+
if (qryReset.triggerTime && qryReset.refreshTimeActive === '') {
|
|
184
|
+
setQryReset((prev) => ({
|
|
185
|
+
...prev,
|
|
186
|
+
refreshTime: qryReset.triggerTime,
|
|
187
|
+
refreshTimeActive: qryReset.triggerTime,
|
|
188
|
+
}));
|
|
189
|
+
} else if (queryWatcher[2] && queryWatcher[2] !== qryReset.triggerTime) {
|
|
190
|
+
setQryReset((prev) => ({
|
|
191
|
+
...prev,
|
|
192
|
+
triggerTime: queryWatcher[2],
|
|
193
|
+
}));
|
|
194
|
+
}
|
|
195
|
+
} else if (qryReset.refreshTimeActive) {
|
|
196
|
+
setQryReset((prev) => ({
|
|
197
|
+
...prev,
|
|
198
|
+
triggerTime: '',
|
|
199
|
+
refreshTimeActive: '',
|
|
200
|
+
}));
|
|
154
201
|
}
|
|
202
|
+
}, [queryWatcher[2], loading]);
|
|
203
|
+
|
|
204
|
+
// console.log('refreshTime:', refreshTime + queryWatcher[0]);
|
|
155
205
|
|
|
156
|
-
|
|
157
|
-
|
|
206
|
+
return {
|
|
207
|
+
updatedCount: queryWatcher[0],
|
|
208
|
+
forceRefetch: queryWatcher[1],
|
|
209
|
+
qryReset
|
|
210
|
+
};
|
|
158
211
|
}
|
|
159
212
|
|
|
160
213
|
/**
|
|
@@ -222,17 +275,18 @@ export function useReactiveFragment(
|
|
|
222
275
|
}
|
|
223
276
|
|
|
224
277
|
useEffect(() => {
|
|
278
|
+
|
|
225
279
|
// NOTE: I tried this with Preact's effect()
|
|
226
280
|
// But that caused the render to happen 3 times more, I don't know why.
|
|
227
281
|
// I only solved it by using useEffect()
|
|
228
282
|
const idsChanged = (
|
|
229
283
|
!dataId ||
|
|
230
|
-
changedData.data
|
|
284
|
+
changedData.data?.id !== dataId ||
|
|
231
285
|
(
|
|
232
286
|
// This is absolutely needed for the chat clientId system to work properly;
|
|
233
287
|
// Otherwise messages will be stuck in "not sent" state.
|
|
234
|
-
changedData.data
|
|
235
|
-
changedData.data
|
|
288
|
+
changedData.data?.clientId &&
|
|
289
|
+
changedData.data?.clientId !== data.clientId
|
|
236
290
|
)
|
|
237
291
|
);
|
|
238
292
|
|
|
@@ -324,7 +378,7 @@ export function useReactiveFragment(
|
|
|
324
378
|
// }
|
|
325
379
|
// }, [updateQueryCount]);
|
|
326
380
|
|
|
327
|
-
return changedData.data;
|
|
381
|
+
return changedData.data ?? null;
|
|
328
382
|
}
|
|
329
383
|
|
|
330
384
|
/**
|
|
@@ -580,12 +634,11 @@ export function useQuery(
|
|
|
580
634
|
query: any,
|
|
581
635
|
options?: GraphQLQueryOptions,
|
|
582
636
|
): GraphQLQueryResult {
|
|
583
|
-
const { onCompleted, onError, openModalPopUp, variables, cacheMap, skip, eagerFragmentKeyMap } = options || {};
|
|
637
|
+
const { onCompleted, onError, openModalPopUp, variables, cacheMap, skip, eagerFragmentKeyMap, doTest } = options || {};
|
|
584
638
|
const variablesKey = makeVariablesKey(variables);
|
|
585
639
|
const connectedToServer = useConnectedToServerValue();
|
|
586
640
|
const screenIsFocused = useScreenIsFocusedValue();
|
|
587
641
|
const updateObservers = useUpdateObservers();
|
|
588
|
-
const [updatedCount, forceRefetch] = useWatchQuery(query, variablesKey);
|
|
589
642
|
|
|
590
643
|
// useQuery() assumes query is a single graphql query (and not a multi/chain)
|
|
591
644
|
// Multiple queries can be chained using this hook,
|
|
@@ -597,6 +650,7 @@ export function useQuery(
|
|
|
597
650
|
unique: null as string | null,
|
|
598
651
|
abortCtrl: null as AbortController | null,
|
|
599
652
|
reconnectedAt: null as number | null,
|
|
653
|
+
json: '',
|
|
600
654
|
init: false
|
|
601
655
|
});
|
|
602
656
|
|
|
@@ -607,16 +661,25 @@ export function useQuery(
|
|
|
607
661
|
variables,
|
|
608
662
|
loading: false,
|
|
609
663
|
error: null as ServerErrorObj | null,
|
|
610
|
-
lastUpdatedCount: 0
|
|
664
|
+
lastUpdatedCount: 0,
|
|
665
|
+
lastRefreshTriggerTime: '',
|
|
611
666
|
});
|
|
612
667
|
|
|
668
|
+
const { updatedCount, forceRefetch, qryReset: { triggerTime, refreshTime } } = useWatchQuery(
|
|
669
|
+
query,
|
|
670
|
+
variablesKey,
|
|
671
|
+
qryValues.lastUpdatedCount,
|
|
672
|
+
qryValues.loading
|
|
673
|
+
);
|
|
674
|
+
|
|
675
|
+
const [queryOutput, setQueryOutput] = useState({ queryKey, refreshTime, data: null });
|
|
676
|
+
|
|
613
677
|
// Query function for the client
|
|
614
678
|
// NOTE: useQuery() assumes {query} and {options} will *never* change, except for {options.variables}
|
|
615
679
|
|
|
616
680
|
const doQuery = async (queryVariables?: any, triedCount: number = 0) => {
|
|
617
681
|
|
|
618
682
|
const currentHookId = QRY_TRACKER.get(queryKey);
|
|
619
|
-
|
|
620
683
|
if (!currentHookId) {
|
|
621
684
|
QRY_TRACKER.set(queryKey, tracker.current.unique);
|
|
622
685
|
} else if (currentHookId) {
|
|
@@ -744,16 +807,20 @@ export function useQuery(
|
|
|
744
807
|
|
|
745
808
|
// Refetch when variables, skip status or query observer changes
|
|
746
809
|
|
|
810
|
+
// const isTest = queryName === 'logEntries';
|
|
811
|
+
|
|
747
812
|
useEffect(() => {
|
|
813
|
+
// isTest && console.log('useQuery check:', refreshTime, updatedCount, qryValues.lastUpdatedCount);
|
|
814
|
+
|
|
748
815
|
if (
|
|
749
816
|
!skip && (
|
|
817
|
+
(triggerTime && triggerTime !== qryValues.lastRefreshTriggerTime) ||
|
|
750
818
|
updatedCount !== qryValues.lastUpdatedCount ||
|
|
751
819
|
variablesKey !== makeVariablesKey(qryValues.variables) ||
|
|
752
820
|
!checkDataCompleteness(qryValues.data, query)
|
|
753
821
|
)
|
|
754
822
|
) {
|
|
755
823
|
const cachedResult = fetchCachedData(query, variablesKey, updateObservers);
|
|
756
|
-
|
|
757
824
|
if (cachedResult) {
|
|
758
825
|
setQryValues({
|
|
759
826
|
...qryValues,
|
|
@@ -761,21 +828,22 @@ export function useQuery(
|
|
|
761
828
|
data: cachedResult,
|
|
762
829
|
error: null,
|
|
763
830
|
loading: false, // IMPORTANT: must be false here
|
|
831
|
+
lastRefreshTriggerTime: triggerTime,
|
|
764
832
|
lastUpdatedCount: updatedCount || 0,
|
|
765
833
|
});
|
|
766
834
|
|
|
767
835
|
if (forceRefetch && !qryValues.loading) {
|
|
768
836
|
doQuery();
|
|
769
|
-
// console.log('FORCE REFETCH AT:', query.definitions[0].name.value);
|
|
837
|
+
// isTest && console.log('FORCE REFETCH AT:', query.definitions[0].name.value);
|
|
770
838
|
// console.log('::', variablesKey, skip, updatedCount);
|
|
771
839
|
}
|
|
772
840
|
|
|
773
841
|
} else if (!qryValues.loading) {
|
|
774
|
-
//
|
|
842
|
+
// isTest && console.log(query, '>>>>> $0|' + variablesKey + skip + updatedCount + '|');
|
|
775
843
|
doQuery();
|
|
776
844
|
}
|
|
777
845
|
}
|
|
778
|
-
}, [variablesKey, skip, updatedCount]);
|
|
846
|
+
}, [variablesKey, skip, updatedCount, triggerTime]);
|
|
779
847
|
|
|
780
848
|
// Refetch when the app reconnects and has no data
|
|
781
849
|
|
|
@@ -826,19 +894,38 @@ export function useQuery(
|
|
|
826
894
|
}
|
|
827
895
|
|
|
828
896
|
return result;
|
|
829
|
-
}, [doQuery]);
|
|
897
|
+
}, [doQuery, skip]);
|
|
898
|
+
|
|
899
|
+
const queryData = loadDataFromCache(
|
|
900
|
+
qryValues.data,
|
|
901
|
+
query,
|
|
902
|
+
variablesKey,
|
|
903
|
+
cacheMap,
|
|
904
|
+
eagerFragmentKeyMap,
|
|
905
|
+
);
|
|
906
|
+
|
|
907
|
+
useEffect(() => {
|
|
908
|
+
const jsonStr = queryKey + '|' + JSON.stringify(queryData[queryName]);
|
|
909
|
+
if (tracker.current.json === jsonStr) {
|
|
910
|
+
return;
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
tracker.current.json = jsonStr;
|
|
914
|
+
|
|
915
|
+
// This makes queryKey and data always in sync
|
|
916
|
+
setQueryOutput({
|
|
917
|
+
queryKey,
|
|
918
|
+
refreshTime,
|
|
919
|
+
data: queryData,
|
|
920
|
+
});
|
|
921
|
+
}, [queryData[queryName]]);
|
|
830
922
|
|
|
831
923
|
return {
|
|
832
924
|
...qryValues,
|
|
833
|
-
data: loadDataFromCache(
|
|
834
|
-
qryValues.data,
|
|
835
|
-
query,
|
|
836
|
-
variablesKey,
|
|
837
|
-
cacheMap,
|
|
838
|
-
eagerFragmentKeyMap,
|
|
839
|
-
),
|
|
840
925
|
queryName,
|
|
841
|
-
queryKey,
|
|
926
|
+
queryKey: queryOutput.queryKey,
|
|
927
|
+
data: queryOutput.data || queryData,
|
|
928
|
+
refreshTime: queryOutput.refreshTime,
|
|
842
929
|
variablesKey,
|
|
843
930
|
refetch,
|
|
844
931
|
fetchMore,
|
package/hooks/use-account-mtn.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { UseMutationParams } from '@jsb188/graphql/types.d';
|
|
2
2
|
import { OpenModalPopUpFn, useCurrentAccount } from '@jsb188/react/states';
|
|
3
|
-
import { editAccountMtn } from '../gql/mutations/accountMutations';
|
|
3
|
+
import { requestChangeEmailMtn, editAccountMtn } from '../gql/mutations/accountMutations';
|
|
4
4
|
import { useMutation } from './index';
|
|
5
5
|
|
|
6
6
|
/**
|
|
@@ -40,4 +40,29 @@ export function useEditAccount(
|
|
|
40
40
|
...mtnValues,
|
|
41
41
|
...mtnHandlers,
|
|
42
42
|
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Change logged in user's e-mail address
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
export function useRequestChangeEmail(
|
|
50
|
+
params?: UseMutationParams | null,
|
|
51
|
+
openModalPopUp?: OpenModalPopUpFn
|
|
52
|
+
) {
|
|
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
|
+
};
|
|
43
68
|
}
|
package/hooks/use-aiChat-mtn.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { updateQuery } from '../cache/';
|
|
1
|
+
import { resetQuery, updateQuery } from '../cache/';
|
|
2
2
|
import { useMutation } from '../client';
|
|
3
3
|
import { updateAIChats } from './use-aiChat-qry';
|
|
4
4
|
import { sendAIChatMessageMtn, startAIChatMtn, stopAIChatMessageMtn } from '../gql/mutations/aiChatMutations';
|
|
5
5
|
import type { UseMutationParams } from '../types.d';
|
|
6
6
|
import { useOpenModalPopUp } from '@jsb188/react/states';
|
|
7
|
+
import { useEffect, useRef } from 'react';
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* Start AI chat
|
|
@@ -41,6 +42,7 @@ export function useStartAIChat(params: UseMutationParams = {}) {
|
|
|
41
42
|
export function useAIChatTextarea(variablesKey: string, params: UseMutationParams = {}) {
|
|
42
43
|
|
|
43
44
|
const { onCompleted, onError } = params;
|
|
45
|
+
const didSendRef = useRef(false);
|
|
44
46
|
const openModalPopUp = useOpenModalPopUp();
|
|
45
47
|
|
|
46
48
|
// Send AI chat message
|
|
@@ -50,6 +52,8 @@ export function useAIChatTextarea(variablesKey: string, params: UseMutationParam
|
|
|
50
52
|
{
|
|
51
53
|
openModalPopUp,
|
|
52
54
|
onCompleted: (data: any, err: any, variables: any) => {
|
|
55
|
+
didSendRef.current = true;
|
|
56
|
+
|
|
53
57
|
onCompleted?.(data, err, variables);
|
|
54
58
|
|
|
55
59
|
const aiChatId = variables.aiChatId;
|
|
@@ -83,6 +87,18 @@ export function useAIChatTextarea(variablesKey: string, params: UseMutationParam
|
|
|
83
87
|
},
|
|
84
88
|
);
|
|
85
89
|
|
|
90
|
+
// Reset query on unmount if a message was sent;
|
|
91
|
+
// This fixes an issue where in-progress messages that are stopped midway remain broken
|
|
92
|
+
// as a result of the SSE pipe breaking at unmount.
|
|
93
|
+
|
|
94
|
+
useEffect(() => {
|
|
95
|
+
return () => {
|
|
96
|
+
if (didSendRef.current) {
|
|
97
|
+
resetQuery(`#aiChat:${variablesKey}`, true, updateObservers);
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
}, []);
|
|
101
|
+
|
|
86
102
|
return {
|
|
87
103
|
send: {
|
|
88
104
|
sendAIChatMessage,
|
package/hooks/use-aiChat-qry.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { getENVVariable } from '@jsb188/app';
|
|
2
2
|
import { getAuthToken } from '@jsb188/app/utils/api';
|
|
3
|
+
import { makeVariablesKey } from '@jsb188/app/utils/logic';
|
|
3
4
|
import SSE from '@jsb188/sse';
|
|
4
5
|
import { useEffect, useState } from 'react';
|
|
5
|
-
import { loadFragment, loadQuery,
|
|
6
|
+
import { loadFragment, loadQuery, resetQuery, updateQuery } from '../cache/';
|
|
6
7
|
import { useQuery } from '../client';
|
|
7
8
|
import { aiChatMessagesQry, aiChatQry, aiChatsQry } from '../gql/queries/aiChatQueries';
|
|
8
9
|
import handleSSEData, { type PublishPayload } from '../sse';
|
|
@@ -12,8 +13,8 @@ import type { UpdateObserversFn, UseQueryParams } from '../types.d';
|
|
|
12
13
|
* Constants
|
|
13
14
|
*/
|
|
14
15
|
|
|
15
|
-
const AI_CHATS_LIMIT =
|
|
16
|
-
const AI_CHAT_MESSAGES_LIMIT =
|
|
16
|
+
const AI_CHATS_LIMIT = 70; // Must match server code exactly
|
|
17
|
+
const AI_CHAT_MESSAGES_LIMIT = 55; // Must match server code exactly
|
|
17
18
|
|
|
18
19
|
/**
|
|
19
20
|
* Helper; get generating (status) key from SSE data
|
|
@@ -49,7 +50,7 @@ function getGeneratingFragmentId(sseData: PublishPayload) {
|
|
|
49
50
|
|
|
50
51
|
export function useAIChatWithSSE(aiChatId?: string, initialSessionKey?: string | null, params: UseQueryParams = {}) {
|
|
51
52
|
|
|
52
|
-
const { data, ...
|
|
53
|
+
const { data, ...rest } = useQuery(aiChatQry, {
|
|
53
54
|
variables: {
|
|
54
55
|
aiChatId
|
|
55
56
|
},
|
|
@@ -57,7 +58,7 @@ export function useAIChatWithSSE(aiChatId?: string, initialSessionKey?: string |
|
|
|
57
58
|
...params,
|
|
58
59
|
});
|
|
59
60
|
|
|
60
|
-
const { updateObservers } =
|
|
61
|
+
const { updateObservers } = rest;
|
|
61
62
|
const [sessionKey, setSessionKey] = useState<string | null>(initialSessionKey || null);
|
|
62
63
|
const [generatingFragmentId, setGeneratingFragmentId] = useState<string | null>(null);
|
|
63
64
|
const aiChat = data?.aiChat || loadFragment(`$aiChatFragment:${aiChatId}`);
|
|
@@ -95,7 +96,6 @@ export function useAIChatWithSSE(aiChatId?: string, initialSessionKey?: string |
|
|
|
95
96
|
};
|
|
96
97
|
|
|
97
98
|
connection.connect(connectParams);
|
|
98
|
-
|
|
99
99
|
// const removeNetworkListeners = reconnectOnBrowserNetwork(connection, connectParams);
|
|
100
100
|
|
|
101
101
|
return () => {
|
|
@@ -134,16 +134,24 @@ export function useAIChatWithSSE(aiChatId?: string, initialSessionKey?: string |
|
|
|
134
134
|
}, false, updateObservers);
|
|
135
135
|
}
|
|
136
136
|
}
|
|
137
|
-
|
|
138
137
|
}
|
|
139
138
|
}, [dataAIChatId]);
|
|
140
139
|
|
|
140
|
+
useEffect(() => {
|
|
141
|
+
// Force reset if chat message is in progress
|
|
142
|
+
if (aiChat?.inProgress) {
|
|
143
|
+
return () => {
|
|
144
|
+
resetQuery(rest.queryKey, true, updateObservers);
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
}, [aiChat?.inProgress]);
|
|
148
|
+
|
|
141
149
|
return {
|
|
142
150
|
aiChat,
|
|
143
151
|
generatingFragmentId,
|
|
144
152
|
sessionKey,
|
|
145
153
|
setSessionKey,
|
|
146
|
-
...
|
|
154
|
+
...rest
|
|
147
155
|
};
|
|
148
156
|
}
|
|
149
157
|
|
|
@@ -204,15 +212,16 @@ export function updateAIChats(aiChat: any, updateObservers: UpdateObserversFn) {
|
|
|
204
212
|
* Fetch AI chats list
|
|
205
213
|
*/
|
|
206
214
|
|
|
207
|
-
export function useAIChats(filter?: 'CAL_DATE' | 'CHATS') {
|
|
215
|
+
export function useAIChats(organizationId: string, filter?: 'CAL_DATE' | 'CHATS') {
|
|
208
216
|
const { data, ...other } = useQuery(aiChatsQry, {
|
|
209
217
|
variables: {
|
|
218
|
+
organizationId,
|
|
210
219
|
filter,
|
|
211
220
|
cursor: null,
|
|
212
221
|
after: false,
|
|
213
222
|
limit: AI_CHATS_LIMIT
|
|
214
223
|
},
|
|
215
|
-
skip:
|
|
224
|
+
skip: !organizationId,
|
|
216
225
|
});
|
|
217
226
|
|
|
218
227
|
return {
|
package/hooks/use-auth-mtn.ts
CHANGED
|
@@ -4,7 +4,7 @@ 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, confirmPhoneVerificationCodeMtn, requestTokenizedEmailMtn, sendPhoneVerificationCodeMtn, signUpWithEmailMtn } from '../gql/mutations/authMutations';
|
|
7
|
+
import { changeAccountPasswordMtn, checkUsernameOrEmailMtn, confirmPasswordMtn, confirmPhoneVerificationCodeMtn, requestTokenizedEmailMtn, sendPhoneVerificationCodeMtn, signUpWithEmailMtn } from '../gql/mutations/authMutations';
|
|
8
8
|
import { signOutMtn } from '../gql/mutations/userAuthMutations';
|
|
9
9
|
import { useMutation } from './index';
|
|
10
10
|
|
|
@@ -135,7 +135,7 @@ export function useChangeAccountPasswordForm(params: UseMutationParams = {}) {
|
|
|
135
135
|
|
|
136
136
|
export function useRequestTokenizedEmail(params: UseMutationParams = {}, openModalPopUp?: OpenModalPopUpFn) {
|
|
137
137
|
const { onCompleted, onError } = params;
|
|
138
|
-
const [requestTokenizedEmail, mtnValues] = useMutation(
|
|
138
|
+
const [requestTokenizedEmail, mtnValues, mtnHandlers] = useMutation(
|
|
139
139
|
requestTokenizedEmailMtn,
|
|
140
140
|
{
|
|
141
141
|
checkMountedBeforeCallback: true,
|
|
@@ -148,6 +148,7 @@ export function useRequestTokenizedEmail(params: UseMutationParams = {}, openMod
|
|
|
148
148
|
return {
|
|
149
149
|
requestTokenizedEmail,
|
|
150
150
|
...mtnValues,
|
|
151
|
+
...mtnHandlers,
|
|
151
152
|
};
|
|
152
153
|
}
|
|
153
154
|
|
|
@@ -254,6 +255,28 @@ export function useSignUpWithEmail(params: AuthHookParams = {}, openModalPopUp?:
|
|
|
254
255
|
};
|
|
255
256
|
}
|
|
256
257
|
|
|
258
|
+
/**
|
|
259
|
+
* Confirm password
|
|
260
|
+
* NOTE: *must* be signed in already
|
|
261
|
+
*/
|
|
262
|
+
|
|
263
|
+
export function useConfirmPassword(params: UseMutationParams = {}, openModalPopUp?: OpenModalPopUpFn) {
|
|
264
|
+
const [confirmPassword, mtnValues, mtnHandlers] = useMutation(
|
|
265
|
+
confirmPasswordMtn,
|
|
266
|
+
{
|
|
267
|
+
checkMountedBeforeCallback: true,
|
|
268
|
+
openModalPopUp,
|
|
269
|
+
...params,
|
|
270
|
+
},
|
|
271
|
+
);
|
|
272
|
+
|
|
273
|
+
return {
|
|
274
|
+
confirmPassword,
|
|
275
|
+
...mtnValues,
|
|
276
|
+
...mtnHandlers,
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
|
|
257
280
|
// Before are deprecated HOC that needs to be migrated to hooks
|
|
258
281
|
// Before are deprecated HOC that needs to be migrated to hooks
|
|
259
282
|
// Before are deprecated HOC that needs to be migrated to hooks
|