contentoh-components-library 21.6.0 → 21.6.1
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.
|
@@ -91,40 +91,47 @@ var AiProductEditionProvider = function AiProductEditionProvider(_ref) {
|
|
|
91
91
|
inputsUsingAi = _useState16[0],
|
|
92
92
|
setInputsUsingAi = _useState16[1];
|
|
93
93
|
|
|
94
|
-
var
|
|
95
|
-
var
|
|
94
|
+
var AI_MAX_CREDITS = 2;
|
|
95
|
+
var AI_RESET_MS = 24 * 60 * 60 * 1000;
|
|
96
96
|
var RATE_LIMIT_KEY = "ai_generation_limit_data";
|
|
97
97
|
|
|
98
|
-
var checkAndManageRateLimit = function checkAndManageRateLimit(
|
|
98
|
+
var checkAndManageRateLimit = function checkAndManageRateLimit(_ref2) {
|
|
99
|
+
var articleId = _ref2.articleId,
|
|
100
|
+
descriptionId = _ref2.descriptionId,
|
|
101
|
+
attributeId = _ref2.attributeId,
|
|
102
|
+
type = _ref2.type;
|
|
99
103
|
var now = Date.now();
|
|
100
104
|
var storedData = localStorage.getItem(RATE_LIMIT_KEY);
|
|
101
105
|
var allRateData = storedData ? JSON.parse(storedData) : {};
|
|
102
|
-
var
|
|
103
|
-
|
|
104
|
-
|
|
106
|
+
var idPart = descriptionId !== null && descriptionId !== void 0 ? descriptionId : attributeId;
|
|
107
|
+
var key = idPart ? "".concat(articleId, "_").concat(idPart, "_").concat(type) : "".concat(articleId);
|
|
108
|
+
var keyTimestamps = allRateData[key] || [];
|
|
109
|
+
keyTimestamps = keyTimestamps.filter(function (timestamp) {
|
|
110
|
+
return now - timestamp < AI_RESET_MS;
|
|
105
111
|
});
|
|
106
112
|
|
|
107
|
-
if (
|
|
108
|
-
allRateData[
|
|
113
|
+
if (keyTimestamps.length >= AI_MAX_CREDITS) {
|
|
114
|
+
allRateData[key] = keyTimestamps;
|
|
109
115
|
localStorage.setItem(RATE_LIMIT_KEY, JSON.stringify(allRateData));
|
|
110
116
|
return {
|
|
111
117
|
allowed: false,
|
|
112
|
-
message: "Has alcanzado el
|
|
118
|
+
message: idPart ? "Has alcanzado el límite de intentos para este atributo. Vuelve a intentarlo en 24 horas." : "Has alcanzado el límite de intentos para este producto. Vuelve a intentarlo en 24 horas."
|
|
113
119
|
};
|
|
114
|
-
}
|
|
120
|
+
} // Registramos el intento
|
|
115
121
|
|
|
116
|
-
|
|
117
|
-
|
|
122
|
+
|
|
123
|
+
keyTimestamps.push(now);
|
|
124
|
+
allRateData[key] = keyTimestamps;
|
|
118
125
|
localStorage.setItem(RATE_LIMIT_KEY, JSON.stringify(allRateData));
|
|
119
126
|
return {
|
|
120
127
|
allowed: true
|
|
121
128
|
};
|
|
122
129
|
};
|
|
123
130
|
|
|
124
|
-
function setCurrentSuggestionValue(
|
|
125
|
-
var inputId =
|
|
126
|
-
index =
|
|
127
|
-
value =
|
|
131
|
+
function setCurrentSuggestionValue(_ref3) {
|
|
132
|
+
var inputId = _ref3.inputId,
|
|
133
|
+
index = _ref3.index,
|
|
134
|
+
value = _ref3.value;
|
|
128
135
|
setCurrentSuggestion(function (prev) {
|
|
129
136
|
return (0, _objectSpread3.default)((0, _objectSpread3.default)({}, prev), {}, (0, _defineProperty2.default)({}, inputId, {
|
|
130
137
|
index: index,
|
|
@@ -142,13 +149,13 @@ var AiProductEditionProvider = function AiProductEditionProvider(_ref) {
|
|
|
142
149
|
}
|
|
143
150
|
|
|
144
151
|
function _regenerateProductSuggestions() {
|
|
145
|
-
_regenerateProductSuggestions = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(
|
|
152
|
+
_regenerateProductSuggestions = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(_ref4) {
|
|
146
153
|
var inputName, currentValue, description, maxChar, type, articleId, versionId, descriptionId, attributeId, newSuggestions;
|
|
147
154
|
return _regenerator.default.wrap(function _callee$(_context) {
|
|
148
155
|
while (1) {
|
|
149
156
|
switch (_context.prev = _context.next) {
|
|
150
157
|
case 0:
|
|
151
|
-
inputName =
|
|
158
|
+
inputName = _ref4.inputName, currentValue = _ref4.currentValue, description = _ref4.description, maxChar = _ref4.maxChar, type = _ref4.type, articleId = _ref4.articleId, versionId = _ref4.versionId, descriptionId = _ref4.descriptionId, attributeId = _ref4.attributeId;
|
|
152
159
|
|
|
153
160
|
if (!(!description || !maxChar || !type)) {
|
|
154
161
|
_context.next = 3;
|
|
@@ -218,14 +225,14 @@ var AiProductEditionProvider = function AiProductEditionProvider(_ref) {
|
|
|
218
225
|
|
|
219
226
|
|
|
220
227
|
function _generateProductSuggestions() {
|
|
221
|
-
_generateProductSuggestions = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(
|
|
222
|
-
var
|
|
228
|
+
_generateProductSuggestions = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(_ref5) {
|
|
229
|
+
var _ref5$inputName, inputName, _ref5$currentValue, currentValue, _ref5$description, description, _ref5$maxChar, maxChar, _ref5$type, type, articleId, versionId, descriptionId, attributeId, _state$product, _JSON$parse, _JSON$parse2, _JSON$parse2$error, _JSON$parse$data, _JSON$parse3, rateLimitStatus, upc, productName, retailer, category, version, payload, _yield$axios$post, data, errorsRaw, errorMessage, results, _error$message;
|
|
223
230
|
|
|
224
231
|
return _regenerator.default.wrap(function _callee2$(_context2) {
|
|
225
232
|
while (1) {
|
|
226
233
|
switch (_context2.prev = _context2.next) {
|
|
227
234
|
case 0:
|
|
228
|
-
|
|
235
|
+
_ref5$inputName = _ref5.inputName, inputName = _ref5$inputName === void 0 ? "" : _ref5$inputName, _ref5$currentValue = _ref5.currentValue, currentValue = _ref5$currentValue === void 0 ? "" : _ref5$currentValue, _ref5$description = _ref5.description, description = _ref5$description === void 0 ? "" : _ref5$description, _ref5$maxChar = _ref5.maxChar, maxChar = _ref5$maxChar === void 0 ? 100 : _ref5$maxChar, _ref5$type = _ref5.type, type = _ref5$type === void 0 ? 'description' : _ref5$type, articleId = _ref5.articleId, versionId = _ref5.versionId, descriptionId = _ref5.descriptionId, attributeId = _ref5.attributeId;
|
|
229
236
|
_context2.prev = 1;
|
|
230
237
|
|
|
231
238
|
if (isAiAvailable) {
|
|
@@ -236,7 +243,12 @@ var AiProductEditionProvider = function AiProductEditionProvider(_ref) {
|
|
|
236
243
|
return _context2.abrupt("return");
|
|
237
244
|
|
|
238
245
|
case 4:
|
|
239
|
-
rateLimitStatus = checkAndManageRateLimit(
|
|
246
|
+
rateLimitStatus = checkAndManageRateLimit({
|
|
247
|
+
articleId: articleId,
|
|
248
|
+
descriptionId: descriptionId,
|
|
249
|
+
attributeId: attributeId,
|
|
250
|
+
type: type
|
|
251
|
+
});
|
|
240
252
|
|
|
241
253
|
if (rateLimitStatus.allowed) {
|
|
242
254
|
_context2.next = 7;
|
package/package.json
CHANGED
|
@@ -40,35 +40,39 @@ export const AiProductEditionProvider = ({
|
|
|
40
40
|
const [inputsGeneratedWithAi, setInputsGeneratedWithAi] = useState({});
|
|
41
41
|
const [inputsUsingAi, setInputsUsingAi] = useState({});
|
|
42
42
|
|
|
43
|
-
const
|
|
44
|
-
const
|
|
43
|
+
const AI_MAX_CREDITS = 2;
|
|
44
|
+
const AI_RESET_MS = 24 * 60 * 60 * 1000;
|
|
45
45
|
const RATE_LIMIT_KEY = "ai_generation_limit_data";
|
|
46
46
|
|
|
47
|
-
const checkAndManageRateLimit = (
|
|
47
|
+
const checkAndManageRateLimit = ({ articleId, descriptionId, attributeId, type }) => {
|
|
48
48
|
const now = Date.now();
|
|
49
49
|
const storedData = localStorage.getItem(RATE_LIMIT_KEY);
|
|
50
50
|
|
|
51
51
|
let allRateData = storedData ? JSON.parse(storedData) : {};
|
|
52
52
|
|
|
53
|
-
|
|
53
|
+
const idPart = descriptionId ?? attributeId;
|
|
54
54
|
|
|
55
|
-
|
|
55
|
+
const key = idPart ? `${articleId}_${idPart}_${type}` : `${articleId}`;
|
|
56
56
|
|
|
57
|
-
|
|
57
|
+
let keyTimestamps = allRateData[key] || [];
|
|
58
58
|
|
|
59
|
-
|
|
59
|
+
keyTimestamps = keyTimestamps.filter(timestamp => (now - timestamp) < AI_RESET_MS);
|
|
60
|
+
|
|
61
|
+
if (keyTimestamps.length >= AI_MAX_CREDITS) {
|
|
62
|
+
allRateData[key] = keyTimestamps;
|
|
60
63
|
localStorage.setItem(RATE_LIMIT_KEY, JSON.stringify(allRateData));
|
|
61
64
|
|
|
62
65
|
return {
|
|
63
66
|
allowed: false,
|
|
64
|
-
message:
|
|
67
|
+
message: idPart
|
|
68
|
+
? "Has alcanzado el límite de intentos para este atributo. Vuelve a intentarlo en 24 horas."
|
|
69
|
+
: "Has alcanzado el límite de intentos para este producto. Vuelve a intentarlo en 24 horas."
|
|
65
70
|
};
|
|
66
71
|
}
|
|
67
72
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
allRateData[
|
|
71
|
-
|
|
73
|
+
// Registramos el intento
|
|
74
|
+
keyTimestamps.push(now);
|
|
75
|
+
allRateData[key] = keyTimestamps;
|
|
72
76
|
localStorage.setItem(RATE_LIMIT_KEY, JSON.stringify(allRateData));
|
|
73
77
|
|
|
74
78
|
return { allowed: true };
|
|
@@ -150,7 +154,12 @@ export const AiProductEditionProvider = ({
|
|
|
150
154
|
|
|
151
155
|
if(!isAiAvailable) return;
|
|
152
156
|
|
|
153
|
-
const rateLimitStatus = checkAndManageRateLimit(
|
|
157
|
+
const rateLimitStatus = checkAndManageRateLimit({
|
|
158
|
+
articleId,
|
|
159
|
+
descriptionId,
|
|
160
|
+
attributeId,
|
|
161
|
+
type
|
|
162
|
+
});
|
|
154
163
|
|
|
155
164
|
if (!rateLimitStatus.allowed) {
|
|
156
165
|
return {
|