yichong-canva2 2.3.0 → 2.5.0
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/README.md +186 -2
- package/dist/api/error.js +5 -1
- package/dist/api/predictions.js +106 -9
- package/dist/api/reporting.js +1 -1
- package/dist/api/request.js +19 -10
- package/dist/api/tts.js +65 -0
- package/dist/api/uploads.js +4 -4
- package/dist/api/usage_reporting.js +76 -0
- package/dist/api/usage_reporting_payload.js +44 -0
- package/dist/index.js +4 -1
- package/dist/login/cache.js +38 -0
- package/dist/login/consumption.js +94 -0
- package/dist/login/context.js +20 -2
- package/dist/login/login/apis/index.js +3 -2
- package/dist/login/login/apis/request.js +1 -13
- package/dist/login/login/login.js +2 -1
- package/dist/login/privider.js +242 -199
- package/dist/login/version.js +22 -17
- package/dist/types/api/error.d.ts +2 -0
- package/dist/types/api/index.d.ts +3 -2
- package/dist/types/api/predictions.d.ts +8 -3
- package/dist/types/api/request.d.ts +8 -3
- package/dist/types/api/tts.d.ts +8 -0
- package/dist/types/api/types.d.ts +138 -4
- package/dist/types/api/usage_reporting.d.ts +16 -0
- package/dist/types/api/usage_reporting_payload.d.ts +2 -0
- package/dist/types/login/cache.d.ts +10 -0
- package/dist/types/login/consumption.d.ts +68 -0
- package/dist/types/login/context.d.ts +10 -6
- package/dist/types/login/index.d.ts +1 -0
- package/dist/types/login/login/apis/index.d.ts +1 -1
- package/dist/types/login/privider.d.ts +10 -1
- package/dist/types/login/version.d.ts +4 -3
- package/package.json +1 -1
package/dist/login/privider.js
CHANGED
|
@@ -9,47 +9,23 @@ import { createTextModerationApi } from "../api/moderation.js";
|
|
|
9
9
|
import { createContentExtractionApi } from "../api/content.js";
|
|
10
10
|
import { createChatApi } from "../api/chat.js";
|
|
11
11
|
import { createVttApi } from "../api/vtt.js";
|
|
12
|
+
import { createTextToSpeechApi } from "../api/tts.js";
|
|
12
13
|
import { createUsageApi } from "../api/usage.js";
|
|
13
14
|
import { createUploadApi } from "../api/uploads.js";
|
|
14
15
|
import { createErrorReportingApi } from "../api/reporting.js";
|
|
16
|
+
import { createUsageReportingApi } from "../api/usage_reporting.js";
|
|
17
|
+
import { isValidUsageCount, invalidUsageResult, skippedUsage, recordUsage } from "./consumption.js";
|
|
18
|
+
import { readCache, writeCache, versionCacheKey, testCacheKey } from "./cache.js";
|
|
15
19
|
import { ApiError } from "../api/error.js";
|
|
16
20
|
import { withRequest, throwIfAborted } from "../api/transport.js";
|
|
17
21
|
import { createAccountReporter, AccountRequestError, retryableAccountError, accountDelay } from "./account.js";
|
|
18
|
-
|
|
19
|
-
function
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
const url = new URL(baseUrl + "/api/user/reduce-free-cnt?cnt=" + cnt);
|
|
26
|
-
addContextQuery(url, context);
|
|
27
|
-
return fetch(url.toString(), {
|
|
28
|
-
method: "POST",
|
|
29
|
-
headers: header,
|
|
30
|
-
redirect: "follow"
|
|
31
|
-
}).catch((error) => {
|
|
32
|
-
console.log("reduceFreeTimes: ", error);
|
|
33
|
-
reduceFreeTimes(baseUrl, passport, cnt, context);
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
function addMonthlyUsage(passport, cnt, productName, context) {
|
|
37
|
-
const headers = {
|
|
38
|
-
"X-Access-Ticket": passport,
|
|
39
|
-
"Content-Type": "application/json",
|
|
40
|
-
"X-Access-Channel": productName,
|
|
41
|
-
"Accept-Language": language(context.getLocale)
|
|
42
|
-
};
|
|
43
|
-
const data = {
|
|
44
|
-
cnt
|
|
45
|
-
};
|
|
46
|
-
const url = new URL("https://usa.imgkits.com/api/user/add-monthly-usage");
|
|
47
|
-
addContextQuery(url, context);
|
|
48
|
-
return fetch(url.toString(), {
|
|
49
|
-
method: "POST",
|
|
50
|
-
headers,
|
|
51
|
-
body: JSON.stringify(data)
|
|
52
|
-
});
|
|
22
|
+
let usageIdSequence = 0;
|
|
23
|
+
function usageId() {
|
|
24
|
+
try {
|
|
25
|
+
return globalThis.crypto.randomUUID();
|
|
26
|
+
} catch {
|
|
27
|
+
return `usage-${Date.now().toString(36)}-${(++usageIdSequence).toString(36)}-${Math.random().toString(36).slice(2)}`;
|
|
28
|
+
}
|
|
53
29
|
}
|
|
54
30
|
const YiChongCanvaProvider = ({
|
|
55
31
|
children,
|
|
@@ -61,8 +37,10 @@ const YiChongCanvaProvider = ({
|
|
|
61
37
|
apiBaseUrl = "https://api.imgkits.com",
|
|
62
38
|
contentBaseUrl = "https://ai.imgkits.com",
|
|
63
39
|
chatBaseUrl = "https://ai.imgkits.com",
|
|
40
|
+
ttsBaseUrl = "https://ai.imgkits.com",
|
|
64
41
|
uploadBaseUrl = "https://ai.imgkits.com",
|
|
65
42
|
getLocale,
|
|
43
|
+
usageReporting,
|
|
66
44
|
autoLogin = true,
|
|
67
45
|
testMode: _testMode = "noTest",
|
|
68
46
|
version = 1,
|
|
@@ -71,11 +49,26 @@ const YiChongCanvaProvider = ({
|
|
|
71
49
|
maxAvailablePoints = 1
|
|
72
50
|
}) => {
|
|
73
51
|
const [userStatus, setUserStatus] = useState({});
|
|
74
|
-
const [credits,
|
|
52
|
+
const [credits, setCreditsState] = useState(0);
|
|
53
|
+
const creditsRef = useRef(0);
|
|
54
|
+
const setCredits = (value) => {
|
|
55
|
+
creditsRef.current = value;
|
|
56
|
+
setCreditsState(value);
|
|
57
|
+
};
|
|
75
58
|
const [isSubscribed, setIsSubscribed] = useState(false);
|
|
76
59
|
const pollUserStatus = useRef(null);
|
|
77
|
-
const [test,
|
|
78
|
-
const
|
|
60
|
+
const [test, setTestState] = useState(_testMode);
|
|
61
|
+
const testRef = useRef(_testMode);
|
|
62
|
+
const setTest = (value) => {
|
|
63
|
+
testRef.current = value;
|
|
64
|
+
setTestState(value);
|
|
65
|
+
};
|
|
66
|
+
const [isInTest, setIsInTestState] = useState(false);
|
|
67
|
+
const isInTestRef = useRef(false);
|
|
68
|
+
const setIsInTest = (value) => {
|
|
69
|
+
isInTestRef.current = value;
|
|
70
|
+
setIsInTestState(value);
|
|
71
|
+
};
|
|
79
72
|
const [hasClickedUpgrade, setHasClickedUpgrade] = useState(false);
|
|
80
73
|
const stopCurrentPolling = useRef(null);
|
|
81
74
|
const sessionStopRef = useRef(null);
|
|
@@ -85,16 +78,24 @@ const YiChongCanvaProvider = ({
|
|
|
85
78
|
const loginPendingRef = useRef(false);
|
|
86
79
|
const mountedRef = useRef(true);
|
|
87
80
|
const identityScopeRef = useRef(`${appId}|${productName}`);
|
|
81
|
+
const usageSessionRef = useRef(null);
|
|
82
|
+
const usageScope = `${appId}|${productName}`;
|
|
83
|
+
if (!usageSessionRef.current || usageSessionRef.current.scope !== usageScope) {
|
|
84
|
+
usageSessionRef.current = { scope: usageScope, id: usageId(), opened: false };
|
|
85
|
+
}
|
|
86
|
+
const previousAccountFailureRef = useRef(null);
|
|
88
87
|
const userStatusRef = useRef({});
|
|
89
|
-
const apiConfigRef = useRef({ apiBaseUrl, contentBaseUrl, chatBaseUrl, baseUrl, uploadBaseUrl, productName, appId, version, getLocale });
|
|
90
|
-
apiConfigRef.current = { apiBaseUrl, contentBaseUrl, chatBaseUrl, baseUrl, uploadBaseUrl, productName, appId, version, getLocale };
|
|
88
|
+
const apiConfigRef = useRef({ apiBaseUrl, contentBaseUrl, chatBaseUrl, ttsBaseUrl, baseUrl, uploadBaseUrl, productName, appId, version, getLocale, usageReporting });
|
|
89
|
+
apiConfigRef.current = { apiBaseUrl, contentBaseUrl, chatBaseUrl, ttsBaseUrl, baseUrl, uploadBaseUrl, productName, appId, version, getLocale, usageReporting };
|
|
91
90
|
const passportRef = useRef(null);
|
|
92
91
|
const onPassport = (passport, userId, uid) => {
|
|
93
|
-
|
|
92
|
+
if (apiConfigRef.current.appId !== appId || apiConfigRef.current.productName !== productName) return;
|
|
93
|
+
passportRef.current = { passport, userId, uid, productName, appId };
|
|
94
94
|
};
|
|
95
95
|
const getApiContext = () => ({
|
|
96
96
|
...apiConfigRef.current,
|
|
97
|
-
|
|
97
|
+
// 切换应用的子组件 effect 可能早于清理 effect,不暴露上一应用的账户快照。
|
|
98
|
+
userStatus: identityScopeRef.current === `${apiConfigRef.current.appId}|${apiConfigRef.current.productName}` ? userStatusRef.current : {}
|
|
98
99
|
});
|
|
99
100
|
const [predictionApi] = useState(() => createPredictionApi(getApiContext));
|
|
100
101
|
const [textModerationApi] = useState(() => createTextModerationApi(getApiContext));
|
|
@@ -107,13 +108,28 @@ const YiChongCanvaProvider = ({
|
|
|
107
108
|
...getApiContext(),
|
|
108
109
|
apiBaseUrl: apiConfigRef.current.chatBaseUrl
|
|
109
110
|
})));
|
|
110
|
-
const [
|
|
111
|
+
const [textToSpeechApi] = useState(() => createTextToSpeechApi(() => ({
|
|
112
|
+
...getApiContext(),
|
|
113
|
+
apiBaseUrl: apiConfigRef.current.ttsBaseUrl
|
|
114
|
+
})));
|
|
115
|
+
const getUsageContext = () => {
|
|
111
116
|
var _a;
|
|
112
117
|
const context = getApiContext();
|
|
113
118
|
const credential = passportRef.current;
|
|
114
|
-
const matches = (credential == null ? void 0 : credential.userId) === context.userStatus.userId && (credential == null ? void 0 : credential.uid) === ((_a = context.userStatus.profile) == null ? void 0 : _a.id) && (credential == null ? void 0 : credential.productName) === context.productName;
|
|
119
|
+
const matches = (credential == null ? void 0 : credential.userId) === context.userStatus.userId && (credential == null ? void 0 : credential.uid) === ((_a = context.userStatus.profile) == null ? void 0 : _a.id) && (credential == null ? void 0 : credential.productName) === context.productName && (credential == null ? void 0 : credential.appId) === context.appId;
|
|
115
120
|
return { ...context, passport: matches ? credential == null ? void 0 : credential.passport : void 0 };
|
|
116
|
-
}
|
|
121
|
+
};
|
|
122
|
+
const [usageApi] = useState(() => createUsageApi(getUsageContext));
|
|
123
|
+
const usageQueueRef = useRef(null);
|
|
124
|
+
const getUsageQueue = () => {
|
|
125
|
+
var _a, _b;
|
|
126
|
+
const context = getApiContext();
|
|
127
|
+
const key = JSON.stringify([context.appId, context.productName, context.userStatus.userId, (_a = context.userStatus.profile) == null ? void 0 : _a.id]);
|
|
128
|
+
if (((_b = usageQueueRef.current) == null ? void 0 : _b.key) !== key) usageQueueRef.current = { key, tail: Promise.resolve() };
|
|
129
|
+
return usageQueueRef.current;
|
|
130
|
+
};
|
|
131
|
+
getUsageQueue();
|
|
132
|
+
const localUsageSnapshotRef = useRef(null);
|
|
117
133
|
const [uploadApi] = useState(() => createUploadApi(getApiContext));
|
|
118
134
|
const [errorReportingApi] = useState(() => createErrorReportingApi(() => {
|
|
119
135
|
var _a;
|
|
@@ -122,28 +138,54 @@ const YiChongCanvaProvider = ({
|
|
|
122
138
|
passport: (_a = passportRef.current) == null ? void 0 : _a.passport
|
|
123
139
|
};
|
|
124
140
|
}));
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
141
|
+
const [usageReportingApi] = useState(() => createUsageReportingApi(() => {
|
|
142
|
+
var _a, _b, _c;
|
|
143
|
+
const context = getApiContext();
|
|
144
|
+
const scope = `${context.appId}|${context.productName}`;
|
|
145
|
+
return {
|
|
146
|
+
...context,
|
|
147
|
+
// 子组件 effect 可能先于 Provider 的换应用 effect 执行,不能把旧用户算入新应用。
|
|
148
|
+
userStatus: identityScopeRef.current === scope ? context.userStatus : {},
|
|
149
|
+
metadata: { session_id: (_a = usageSessionRef.current) == null ? void 0 : _a.id, app_version: String(context.version), platform: "canva_addon" },
|
|
150
|
+
secrets: [context.userStatus.jwt, (_b = context.userStatus.profile) == null ? void 0 : _b.token, (_c = passportRef.current) == null ? void 0 : _c.passport].filter((value) => !!value)
|
|
151
|
+
};
|
|
152
|
+
}));
|
|
153
|
+
const reportAccountUsage = (sum_key, title, run, data = {}) => {
|
|
154
|
+
void usageReportingApi.reportUsage({ sum_key, title, data: {
|
|
155
|
+
...data,
|
|
156
|
+
operation_id: run.usageOperationId,
|
|
157
|
+
operation: run.operation,
|
|
158
|
+
duration_ms: Math.max(0, Date.now() - run.startedAt)
|
|
159
|
+
} });
|
|
160
|
+
};
|
|
161
|
+
const reportAccountRecovery = (run) => {
|
|
162
|
+
const previous = previousAccountFailureRef.current;
|
|
163
|
+
if (!previous || previous.scope !== run.appKey) return;
|
|
164
|
+
previousAccountFailureRef.current = null;
|
|
165
|
+
reportAccountUsage("account_recovered", "账户状态恢复", run, {
|
|
166
|
+
previous_operation: previous.operation,
|
|
167
|
+
retry_of: previous.operationId
|
|
168
|
+
});
|
|
169
|
+
};
|
|
128
170
|
const updateUserStatus = (user) => {
|
|
129
|
-
if (
|
|
171
|
+
if (testRef.current !== "noTest" && !user.subscription) {
|
|
130
172
|
user = {
|
|
131
173
|
...user,
|
|
132
174
|
subscription: {}
|
|
133
175
|
};
|
|
134
176
|
}
|
|
135
|
-
if (
|
|
177
|
+
if (testRef.current === "credits") {
|
|
136
178
|
user.subscription = {
|
|
137
179
|
free_cnt: 9999
|
|
138
180
|
};
|
|
139
|
-
} else if (
|
|
181
|
+
} else if (testRef.current === "subscribed") {
|
|
140
182
|
user.subscription = {
|
|
141
183
|
detail: {
|
|
142
184
|
name: "yearly",
|
|
143
185
|
ends_at: null
|
|
144
186
|
}
|
|
145
187
|
};
|
|
146
|
-
} else if (
|
|
188
|
+
} else if (testRef.current === "expired") {
|
|
147
189
|
user.subscription = {
|
|
148
190
|
detail: {
|
|
149
191
|
name: "yearly",
|
|
@@ -155,11 +197,12 @@ const YiChongCanvaProvider = ({
|
|
|
155
197
|
if (JSON.stringify(new_user) !== JSON.stringify(userStatusRef.current)) {
|
|
156
198
|
userStatusRef.current = new_user;
|
|
157
199
|
setUserStatus(new_user);
|
|
200
|
+
getUsageQueue();
|
|
158
201
|
}
|
|
159
202
|
};
|
|
160
203
|
const isFreeUnlimited = () => {
|
|
161
|
-
var _a;
|
|
162
|
-
return ((_a =
|
|
204
|
+
var _a, _b;
|
|
205
|
+
return ((_b = (_a = userStatusRef.current.profile) == null ? void 0 : _a.usage_limits) == null ? void 0 : _b.free_limit) === -1;
|
|
163
206
|
};
|
|
164
207
|
const [reportAccountFailure] = useState(() => createAccountReporter(errorReportingApi.reportError));
|
|
165
208
|
const beginAccountRun = (operation) => {
|
|
@@ -172,7 +215,8 @@ const YiChongCanvaProvider = ({
|
|
|
172
215
|
startedAt: Date.now(),
|
|
173
216
|
attempts: 0,
|
|
174
217
|
stage: operation === "login" ? "canva_token" : "status",
|
|
175
|
-
appKey: `${appId}|${productName}
|
|
218
|
+
appKey: `${appId}|${productName}`,
|
|
219
|
+
usageOperationId: usageId()
|
|
176
220
|
};
|
|
177
221
|
accountRunRef.current = run;
|
|
178
222
|
setAccountError(null);
|
|
@@ -201,6 +245,12 @@ const YiChongCanvaProvider = ({
|
|
|
201
245
|
api: error instanceof AccountRequestError ? error.api : run.api
|
|
202
246
|
};
|
|
203
247
|
setAccountError(failure);
|
|
248
|
+
previousAccountFailureRef.current = { scope: run.appKey, operation: run.operation, operationId: run.usageOperationId };
|
|
249
|
+
reportAccountUsage(run.operation === "login" ? "login_fail" : "account_fail", "账户操作失败", run, {
|
|
250
|
+
stage: failure.stage,
|
|
251
|
+
error_code: String(failure.code),
|
|
252
|
+
http_status: failure.status
|
|
253
|
+
});
|
|
204
254
|
reportAccountFailure(failure, run.appKey);
|
|
205
255
|
};
|
|
206
256
|
const login = (extraRetries = retry) => {
|
|
@@ -208,6 +258,9 @@ const YiChongCanvaProvider = ({
|
|
|
208
258
|
loginPendingRef.current = true;
|
|
209
259
|
setLoginStatus("loading");
|
|
210
260
|
pollUserStatus.current = null;
|
|
261
|
+
queueMicrotask(() => {
|
|
262
|
+
if (isCurrentRun(run)) reportAccountUsage("login_start", "开始登录", run);
|
|
263
|
+
});
|
|
211
264
|
void withRequest({ signal: run.controller.signal, timeoutMs: 3e4 }, async (signal) => {
|
|
212
265
|
for (let attempt = 0; ; attempt++) {
|
|
213
266
|
try {
|
|
@@ -231,6 +284,8 @@ const YiChongCanvaProvider = ({
|
|
|
231
284
|
setLoginStatus("success");
|
|
232
285
|
setAccountError(null);
|
|
233
286
|
updateUserStatus(userStatus2);
|
|
287
|
+
reportAccountUsage("login_success", "登录成功", run);
|
|
288
|
+
reportAccountRecovery(run);
|
|
234
289
|
window.userId = userStatus2.userId || "";
|
|
235
290
|
window.pollUserSubscribe = pollUserStatus.current = startPolling;
|
|
236
291
|
onUpdateUserStatus == null ? void 0 : onUpdateUserStatus(userStatus2);
|
|
@@ -292,6 +347,7 @@ const YiChongCanvaProvider = ({
|
|
|
292
347
|
user.userId = userId;
|
|
293
348
|
setAccountError(null);
|
|
294
349
|
updateUserStatus(user);
|
|
350
|
+
reportAccountRecovery(run);
|
|
295
351
|
finish(user);
|
|
296
352
|
onUpdateUserStatus == null ? void 0 : onUpdateUserStatus(user);
|
|
297
353
|
}).catch((error) => {
|
|
@@ -308,67 +364,45 @@ const YiChongCanvaProvider = ({
|
|
|
308
364
|
{
|
|
309
365
|
setTest("noTest");
|
|
310
366
|
}
|
|
311
|
-
const
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
if (
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
getAppVersion("canva_" + appId, getApiContext()).then((res) => {
|
|
330
|
-
if (typeof res.version !== "number") {
|
|
331
|
-
res.version = version + 1;
|
|
332
|
-
}
|
|
333
|
-
console.log("latest version: ", res.version);
|
|
334
|
-
if (res.version >= version) {
|
|
335
|
-
setIsInTest(false);
|
|
336
|
-
localStorage.setItem("version", version.toString());
|
|
337
|
-
} else {
|
|
338
|
-
setIsInTest(true);
|
|
339
|
-
}
|
|
340
|
-
});
|
|
341
|
-
} else {
|
|
342
|
-
setIsInTest(false);
|
|
343
|
-
}
|
|
344
|
-
}
|
|
367
|
+
const cacheKey = versionCacheKey(appId);
|
|
368
|
+
const cachedVersion = Number.parseInt(readCache(cacheKey) ?? "", 10);
|
|
369
|
+
setIsInTest(false);
|
|
370
|
+
if (cachedVersion === version) return;
|
|
371
|
+
const controller = new AbortController();
|
|
372
|
+
let active = true;
|
|
373
|
+
void getAppVersion("canva_" + appId, getApiContext(), { signal: controller.signal }).then((res) => {
|
|
374
|
+
if (!active || apiConfigRef.current.appId !== appId || apiConfigRef.current.version !== version) return;
|
|
375
|
+
if (typeof res.version !== "number" || !Number.isFinite(res.version)) return;
|
|
376
|
+
const inReview = res.version < version;
|
|
377
|
+
setIsInTest(inReview);
|
|
378
|
+
if (!inReview) writeCache(cacheKey, String(version));
|
|
379
|
+
}).catch(() => {
|
|
380
|
+
});
|
|
381
|
+
return () => {
|
|
382
|
+
active = false;
|
|
383
|
+
controller.abort();
|
|
384
|
+
};
|
|
345
385
|
}, [appId, version, baseUrl, productName]);
|
|
386
|
+
const getTestCacheKey = (kind) => {
|
|
387
|
+
var _a;
|
|
388
|
+
const context = getApiContext();
|
|
389
|
+
return testCacheKey(kind, context.appId, context.productName, context.userStatus.userId, (_a = context.userStatus.profile) == null ? void 0 : _a.id);
|
|
390
|
+
};
|
|
346
391
|
function updateTestInfo(info) {
|
|
347
|
-
|
|
348
|
-
"test_info",
|
|
349
|
-
JSON.stringify({
|
|
350
|
-
...info
|
|
351
|
-
})
|
|
352
|
-
);
|
|
392
|
+
writeCache(getTestCacheKey("test_info"), JSON.stringify(info));
|
|
353
393
|
}
|
|
354
394
|
function getTestInfo() {
|
|
355
|
-
const
|
|
356
|
-
if (
|
|
395
|
+
const cached = readCache(getTestCacheKey("test_info"));
|
|
396
|
+
if (cached) {
|
|
357
397
|
try {
|
|
358
|
-
|
|
398
|
+
const value = JSON.parse(cached);
|
|
399
|
+
if (value && typeof value.time === "string" && typeof value.usage === "number" && Number.isFinite(value.usage)) {
|
|
400
|
+
return { time: value.time, flag: value.flag === true, usage: value.usage };
|
|
401
|
+
}
|
|
359
402
|
} catch {
|
|
360
|
-
return {
|
|
361
|
-
time: "",
|
|
362
|
-
flag: false,
|
|
363
|
-
usage: 0
|
|
364
|
-
};
|
|
365
403
|
}
|
|
366
404
|
}
|
|
367
|
-
return {
|
|
368
|
-
time: "",
|
|
369
|
-
flag: false,
|
|
370
|
-
usage: 20
|
|
371
|
-
};
|
|
405
|
+
return { time: "", flag: false, usage: 20 };
|
|
372
406
|
}
|
|
373
407
|
function updateCredits(credits2) {
|
|
374
408
|
setCredits(credits2);
|
|
@@ -385,7 +419,17 @@ const YiChongCanvaProvider = ({
|
|
|
385
419
|
passportRef.current = null;
|
|
386
420
|
userStatusRef.current = {};
|
|
387
421
|
setUserStatus({});
|
|
422
|
+
setCredits(0);
|
|
423
|
+
setIsSubscribed(false);
|
|
424
|
+
localUsageSnapshotRef.current = null;
|
|
425
|
+
getUsageQueue();
|
|
388
426
|
pollUserStatus.current = null;
|
|
427
|
+
previousAccountFailureRef.current = null;
|
|
428
|
+
}
|
|
429
|
+
const session = usageSessionRef.current;
|
|
430
|
+
if (!session.opened) {
|
|
431
|
+
session.opened = true;
|
|
432
|
+
void usageReportingApi.reportUsage({ sum_key: "app_open", title: "打开应用" });
|
|
389
433
|
}
|
|
390
434
|
if (autoLogin) login();
|
|
391
435
|
else setLoginStatus("idle");
|
|
@@ -399,12 +443,12 @@ const YiChongCanvaProvider = ({
|
|
|
399
443
|
};
|
|
400
444
|
}, [autoLogin, appId, productName]);
|
|
401
445
|
const initCredits = () => {
|
|
402
|
-
|
|
403
|
-
if (!
|
|
446
|
+
const snapshot = userStatusRef.current;
|
|
447
|
+
if (!snapshot.subscription || !snapshot.profile) {
|
|
404
448
|
return;
|
|
405
449
|
}
|
|
406
|
-
const { free_cnt, is_subscribed, monthly_usage_cnt, free_usage_cnt } =
|
|
407
|
-
const { usage_limits } =
|
|
450
|
+
const { free_cnt, is_subscribed, monthly_usage_cnt, free_usage_cnt } = snapshot.subscription;
|
|
451
|
+
const { usage_limits } = snapshot.profile;
|
|
408
452
|
const freeTimes = (usage_limits == null ? void 0 : usage_limits.free_limit) || 0;
|
|
409
453
|
if (isInTest) {
|
|
410
454
|
let isNull = false;
|
|
@@ -413,11 +457,11 @@ const YiChongCanvaProvider = ({
|
|
|
413
457
|
}
|
|
414
458
|
const _credits = mode === "normal" ? 20 : 1e3;
|
|
415
459
|
if (mode === "server_credits" && !free_cnt) {
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
}
|
|
460
|
+
const next = _.merge({}, userStatusRef.current, { subscription: { free_time: _credits } });
|
|
461
|
+
if (JSON.stringify(next) !== JSON.stringify(userStatusRef.current)) {
|
|
462
|
+
userStatusRef.current = next;
|
|
463
|
+
setUserStatus(next);
|
|
464
|
+
}
|
|
421
465
|
}
|
|
422
466
|
const freeTimeFn = () => {
|
|
423
467
|
if (isNull) {
|
|
@@ -426,8 +470,10 @@ const YiChongCanvaProvider = ({
|
|
|
426
470
|
return free_cnt || 0;
|
|
427
471
|
}
|
|
428
472
|
};
|
|
429
|
-
const
|
|
430
|
-
|
|
473
|
+
const freeCountKey = getTestCacheKey("test_free_cnt");
|
|
474
|
+
const cachedFreeCount = readCache(freeCountKey);
|
|
475
|
+
const forceUpdateUsage = cachedFreeCount !== null && cachedFreeCount !== String(freeTimeFn());
|
|
476
|
+
writeCache(freeCountKey, String(freeTimeFn()));
|
|
431
477
|
const test_info = getTestInfo();
|
|
432
478
|
const today = (/* @__PURE__ */ new Date()).toISOString().split("T")[0];
|
|
433
479
|
if (today !== test_info.time || forceUpdateUsage) {
|
|
@@ -474,7 +520,8 @@ const YiChongCanvaProvider = ({
|
|
|
474
520
|
}
|
|
475
521
|
const initSubscribedUser = () => {
|
|
476
522
|
var _a, _b;
|
|
477
|
-
const
|
|
523
|
+
const snapshot = userStatusRef.current;
|
|
524
|
+
const { subscription, team_subscription } = snapshot;
|
|
478
525
|
let is_subscribed = false;
|
|
479
526
|
if (subscription && subscription.detail) {
|
|
480
527
|
const detail = subscription.detail;
|
|
@@ -483,11 +530,11 @@ const YiChongCanvaProvider = ({
|
|
|
483
530
|
is_subscribed = !!(new Date(detail.ends_at) > /* @__PURE__ */ new Date());
|
|
484
531
|
if (team_subscription && (team_subscription == null ? void 0 : team_subscription.detail)) is_subscribed = true;
|
|
485
532
|
}
|
|
486
|
-
const planName = checkPlanName((_b = (_a =
|
|
533
|
+
const planName = checkPlanName((_b = (_a = snapshot.subscription) == null ? void 0 : _a.detail) == null ? void 0 : _b.name);
|
|
487
534
|
setIsSubscribed(is_subscribed);
|
|
488
535
|
updateUserStatus({
|
|
489
536
|
subscription: {
|
|
490
|
-
...
|
|
537
|
+
...snapshot.subscription,
|
|
491
538
|
is_subscribed
|
|
492
539
|
},
|
|
493
540
|
planName
|
|
@@ -497,6 +544,9 @@ const YiChongCanvaProvider = ({
|
|
|
497
544
|
if (!plan_name) {
|
|
498
545
|
return "Free";
|
|
499
546
|
}
|
|
547
|
+
if (typeof plan_name !== "string") {
|
|
548
|
+
return "Premium";
|
|
549
|
+
}
|
|
500
550
|
if (plan_name.toLowerCase().includes("month")) {
|
|
501
551
|
return "Pro Monthly plan";
|
|
502
552
|
} else if (plan_name.toLowerCase().includes("year")) {
|
|
@@ -525,6 +575,7 @@ const YiChongCanvaProvider = ({
|
|
|
525
575
|
user.userId = userId;
|
|
526
576
|
setAccountError(null);
|
|
527
577
|
updateUserStatus(user);
|
|
578
|
+
reportAccountRecovery(run);
|
|
528
579
|
onUpdateUserStatus == null ? void 0 : onUpdateUserStatus(user);
|
|
529
580
|
return user;
|
|
530
581
|
}).catch((error) => {
|
|
@@ -536,81 +587,71 @@ const YiChongCanvaProvider = ({
|
|
|
536
587
|
return run.promise;
|
|
537
588
|
}
|
|
538
589
|
useEffect(() => {
|
|
539
|
-
initCredits();
|
|
590
|
+
if (localUsageSnapshotRef.current !== userStatusRef.current) initCredits();
|
|
540
591
|
initSubscribedUser();
|
|
541
|
-
}, [userStatus]);
|
|
592
|
+
}, [userStatus, isInTest, mode, maxAvailablePoints]);
|
|
593
|
+
const currentSubscribed = () => {
|
|
594
|
+
const { subscription, team_subscription } = userStatusRef.current;
|
|
595
|
+
const detail = subscription == null ? void 0 : subscription.detail;
|
|
596
|
+
return !!detail && (!detail.ends_at || new Date(detail.ends_at) > /* @__PURE__ */ new Date() || !!(team_subscription == null ? void 0 : team_subscription.detail));
|
|
597
|
+
};
|
|
542
598
|
const use = (cnt = 1, force = false, notUpload = false) => {
|
|
543
|
-
if (
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
599
|
+
if (!isValidUsageCount(cnt)) return Promise.resolve(invalidUsageResult());
|
|
600
|
+
const queue = getUsageQueue();
|
|
601
|
+
const isCurrent = () => mountedRef.current && getUsageQueue() === queue;
|
|
602
|
+
const execute = async () => {
|
|
603
|
+
var _a, _b, _c;
|
|
604
|
+
if (!isCurrent()) return skippedUsage("account_changed");
|
|
605
|
+
if (testRef.current !== "noTest") return skippedUsage("test_mode");
|
|
606
|
+
if (isInTestRef.current) {
|
|
607
|
+
const remaining = Math.max(0, creditsRef.current - cnt);
|
|
608
|
+
setCredits(remaining);
|
|
609
|
+
updateTestInfo({ ...getTestInfo(), usage: remaining });
|
|
610
|
+
return skippedUsage("in_review_test");
|
|
548
611
|
}
|
|
549
|
-
|
|
550
|
-
const
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
if (isSubscribed && !force) {
|
|
563
|
-
handleSubscribedUse(cnt, notUpload);
|
|
564
|
-
} else {
|
|
565
|
-
handleCreditsUse(cnt, notUpload);
|
|
566
|
-
}
|
|
567
|
-
};
|
|
568
|
-
const handleCreditsUse = (cnt, notUpload) => {
|
|
569
|
-
const passport = localStorage.getItem(window.userId + "_passport") || "";
|
|
570
|
-
setCredits(credits - cnt);
|
|
571
|
-
if (!notUpload) {
|
|
572
|
-
reduceFreeTimes(baseUrl, passport, cnt, getApiContext());
|
|
573
|
-
}
|
|
574
|
-
};
|
|
575
|
-
const handleSubscribedUse = (cnt, notUpload) => {
|
|
576
|
-
var _a, _b;
|
|
577
|
-
const passport = localStorage.getItem(window.userId + "_passport") || "";
|
|
578
|
-
const { profile, subscription } = userStatus;
|
|
579
|
-
const { usage_limits } = profile || {};
|
|
580
|
-
const monthly_usage_cnt = (subscription == null ? void 0 : subscription.monthly_usage_cnt) ?? 0;
|
|
581
|
-
let limitUsage = ((usage_limits == null ? void 0 : usage_limits.monthly_usage_limit) ?? 0) - monthly_usage_cnt;
|
|
582
|
-
if (((usage_limits == null ? void 0 : usage_limits.monthly_usage_limit) ?? 0) <= -1 || limitUsage >= cnt) {
|
|
583
|
-
addMonthlyUsage(passport, cnt, productName, getApiContext());
|
|
584
|
-
updateUserStatus({
|
|
585
|
-
...userStatus,
|
|
586
|
-
subscription: {
|
|
587
|
-
...userStatus.subscription,
|
|
588
|
-
monthly_usage_cnt: (((_a = userStatus.subscription) == null ? void 0 : _a.monthly_usage_cnt) || 0) + cnt
|
|
612
|
+
if (isFreeUnlimited()) return skippedUsage("free_unlimited");
|
|
613
|
+
const context = getUsageContext();
|
|
614
|
+
const snapshot = userStatusRef.current;
|
|
615
|
+
const used = ((_a = snapshot.subscription) == null ? void 0 : _a.monthly_usage_cnt) ?? 0;
|
|
616
|
+
const limit = ((_c = (_b = snapshot.profile) == null ? void 0 : _b.usage_limits) == null ? void 0 : _c.monthly_usage_limit) ?? 0;
|
|
617
|
+
const monthly = currentSubscribed() && !force ? limit <= -1 ? cnt : Math.min(cnt, Math.max(0, limit - used)) : 0;
|
|
618
|
+
const freeCnt = cnt - monthly;
|
|
619
|
+
if (context.passport || notUpload && monthly === 0) {
|
|
620
|
+
if (monthly > 0) {
|
|
621
|
+
const next = { ...snapshot, subscription: { ...snapshot.subscription, monthly_usage_cnt: used + monthly } };
|
|
622
|
+
localUsageSnapshotRef.current = next;
|
|
623
|
+
userStatusRef.current = next;
|
|
624
|
+
setUserStatus(next);
|
|
589
625
|
}
|
|
590
|
-
|
|
591
|
-
} else if (limitUsage < cnt) {
|
|
592
|
-
if (limitUsage <= 0) {
|
|
593
|
-
limitUsage = 0;
|
|
594
|
-
}
|
|
595
|
-
if (limitUsage > 0) {
|
|
596
|
-
addMonthlyUsage(passport, limitUsage, productName, getApiContext());
|
|
597
|
-
updateUserStatus({
|
|
598
|
-
...userStatus,
|
|
599
|
-
subscription: {
|
|
600
|
-
...userStatus.subscription,
|
|
601
|
-
monthly_usage_cnt: (((_b = userStatus.subscription) == null ? void 0 : _b.monthly_usage_cnt) || 0) + limitUsage
|
|
602
|
-
}
|
|
603
|
-
});
|
|
626
|
+
if (freeCnt > 0) setCredits(creditsRef.current - freeCnt);
|
|
604
627
|
}
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
628
|
+
const creditApi = createUsageApi(() => context);
|
|
629
|
+
const monthlyApi = createUsageApi(() => ({ ...context, baseUrl: "https://usa.imgkits.com" }));
|
|
630
|
+
const reporter = createErrorReportingApi(() => context);
|
|
631
|
+
return recordUsage({
|
|
632
|
+
monthly,
|
|
633
|
+
freeCnt,
|
|
634
|
+
notUpload,
|
|
635
|
+
isCurrent,
|
|
636
|
+
api: { reduceFreeCnt: creditApi.reduceFreeCnt, addMonthlyUsage: monthlyApi.addMonthlyUsage },
|
|
637
|
+
report: reporter.reportError
|
|
638
|
+
});
|
|
639
|
+
};
|
|
640
|
+
const pending = queue.tail.then(execute).catch(() => ({
|
|
641
|
+
status: "failed",
|
|
642
|
+
charged: false,
|
|
643
|
+
reason: "request_failed",
|
|
644
|
+
confirmed: { monthly: 0, freeCnt: 0 },
|
|
645
|
+
error: { code: "unexpected_error", message: "Usage recording could not be started." }
|
|
646
|
+
}));
|
|
647
|
+
queue.tail = pending;
|
|
648
|
+
return pending;
|
|
608
649
|
};
|
|
609
650
|
const canUse = (cnt = 1, force = false) => {
|
|
610
|
-
if (
|
|
651
|
+
if (currentSubscribed() && !force) {
|
|
611
652
|
return true;
|
|
612
653
|
} else {
|
|
613
|
-
return
|
|
654
|
+
return creditsRef.current - cnt >= 0;
|
|
614
655
|
}
|
|
615
656
|
};
|
|
616
657
|
const canUseLimit = (cnt = 1, force = false) => {
|
|
@@ -619,8 +660,8 @@ const YiChongCanvaProvider = ({
|
|
|
619
660
|
canUse: true
|
|
620
661
|
};
|
|
621
662
|
}
|
|
622
|
-
if (
|
|
623
|
-
const { profile, subscription } =
|
|
663
|
+
if (currentSubscribed() && !force) {
|
|
664
|
+
const { profile, subscription } = userStatusRef.current;
|
|
624
665
|
const { usage_limits } = profile || {};
|
|
625
666
|
const monthly_usage_cnt = (subscription == null ? void 0 : subscription.monthly_usage_cnt) ?? 0;
|
|
626
667
|
if (!(monthly_usage_cnt >= 0) || !usage_limits) return {
|
|
@@ -635,7 +676,7 @@ const YiChongCanvaProvider = ({
|
|
|
635
676
|
}
|
|
636
677
|
const canUse2 = totalUsage - cnt >= 0;
|
|
637
678
|
let error = "no_credits";
|
|
638
|
-
if (usage_limits.monthly_usage_limit - monthly_usage_cnt -
|
|
679
|
+
if (usage_limits.monthly_usage_limit - monthly_usage_cnt - creditsRef.current < 0 && creditsRef.current === 0) {
|
|
639
680
|
error = "monthly_usage_limit";
|
|
640
681
|
}
|
|
641
682
|
return {
|
|
@@ -643,20 +684,20 @@ const YiChongCanvaProvider = ({
|
|
|
643
684
|
canUse: canUse2
|
|
644
685
|
};
|
|
645
686
|
} else {
|
|
646
|
-
const canUse2 =
|
|
687
|
+
const canUse2 = creditsRef.current - cnt >= 0;
|
|
647
688
|
return {
|
|
648
689
|
error: canUse2 ? void 0 : "no_credits",
|
|
649
|
-
canUse:
|
|
690
|
+
canUse: creditsRef.current - cnt >= 0
|
|
650
691
|
};
|
|
651
692
|
}
|
|
652
693
|
};
|
|
653
694
|
function getPricingLink() {
|
|
654
|
-
const link = `https://www.livepolls.app/${productName}/pricing?from=canva_addon&canva_id=${
|
|
695
|
+
const link = `https://www.livepolls.app/${apiConfigRef.current.productName}/pricing?from=canva_addon&canva_id=${userStatusRef.current.userId}&close=1`;
|
|
655
696
|
return link;
|
|
656
697
|
}
|
|
657
698
|
const getLimitUsage = () => {
|
|
658
|
-
if (
|
|
659
|
-
const { profile, subscription } =
|
|
699
|
+
if (currentSubscribed()) {
|
|
700
|
+
const { profile, subscription } = userStatusRef.current;
|
|
660
701
|
const { usage_limits } = profile || {};
|
|
661
702
|
const monthly_usage_cnt = (subscription == null ? void 0 : subscription.monthly_usage_cnt) ?? 0;
|
|
662
703
|
const monthly_usage_limit = (usage_limits == null ? void 0 : usage_limits.monthly_usage_limit) ?? 0;
|
|
@@ -664,13 +705,13 @@ const YiChongCanvaProvider = ({
|
|
|
664
705
|
return null;
|
|
665
706
|
}
|
|
666
707
|
const monthly_usage = Math.max(0, monthly_usage_limit - monthly_usage_cnt);
|
|
667
|
-
return Math.max(monthly_usage +
|
|
708
|
+
return Math.max(monthly_usage + creditsRef.current, 0);
|
|
668
709
|
} else {
|
|
669
|
-
return
|
|
710
|
+
return creditsRef.current;
|
|
670
711
|
}
|
|
671
712
|
};
|
|
672
713
|
const getMonthlyUsageLimit = () => {
|
|
673
|
-
const { profile } =
|
|
714
|
+
const { profile } = userStatusRef.current;
|
|
674
715
|
const { usage_limits } = profile || {};
|
|
675
716
|
const monthly_usage_limit = (usage_limits == null ? void 0 : usage_limits.monthly_usage_limit) ?? 0;
|
|
676
717
|
if (monthly_usage_limit === -1) {
|
|
@@ -687,9 +728,11 @@ const YiChongCanvaProvider = ({
|
|
|
687
728
|
...contentExtractionApi,
|
|
688
729
|
...chatApi,
|
|
689
730
|
...vttApi,
|
|
731
|
+
...textToSpeechApi,
|
|
690
732
|
...usageApi,
|
|
691
733
|
...uploadApi,
|
|
692
734
|
...errorReportingApi,
|
|
735
|
+
...usageReportingApi,
|
|
693
736
|
userStatus,
|
|
694
737
|
updateUserStatus,
|
|
695
738
|
handleLogin: login,
|