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 MAX_CREDITS = 10;
95
- var COOLDOWN_MS = 5 * 60 * 1000;
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(currentArticleId) {
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 productTimestamps = allRateData[currentArticleId] || [];
103
- productTimestamps = productTimestamps.filter(function (timestamp) {
104
- return now - timestamp < COOLDOWN_MS;
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 (productTimestamps.length >= MAX_CREDITS) {
108
- allRateData[currentArticleId] = productTimestamps;
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 limite de intentos frecuentes para este producto. Por favor, espera unos minutos."
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
- productTimestamps.push(now);
117
- allRateData[currentArticleId] = productTimestamps;
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(_ref2) {
125
- var inputId = _ref2.inputId,
126
- index = _ref2.index,
127
- value = _ref2.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(_ref3) {
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 = _ref3.inputName, currentValue = _ref3.currentValue, description = _ref3.description, maxChar = _ref3.maxChar, type = _ref3.type, articleId = _ref3.articleId, versionId = _ref3.versionId, descriptionId = _ref3.descriptionId, attributeId = _ref3.attributeId;
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(_ref4) {
222
- var _ref4$inputName, inputName, _ref4$currentValue, currentValue, _ref4$description, description, _ref4$maxChar, maxChar, _ref4$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;
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
- _ref4$inputName = _ref4.inputName, inputName = _ref4$inputName === void 0 ? "" : _ref4$inputName, _ref4$currentValue = _ref4.currentValue, currentValue = _ref4$currentValue === void 0 ? "" : _ref4$currentValue, _ref4$description = _ref4.description, description = _ref4$description === void 0 ? "" : _ref4$description, _ref4$maxChar = _ref4.maxChar, maxChar = _ref4$maxChar === void 0 ? 100 : _ref4$maxChar, _ref4$type = _ref4.type, type = _ref4$type === void 0 ? 'description' : _ref4$type, articleId = _ref4.articleId, versionId = _ref4.versionId, descriptionId = _ref4.descriptionId, attributeId = _ref4.attributeId;
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(articleId);
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "contentoh-components-library",
3
- "version": "21.6.0",
3
+ "version": "21.6.1",
4
4
  "dependencies": {
5
5
  "@aws-amplify/auth": "^4.5.3",
6
6
  "@aws-amplify/datastore": "^3.11.0",
@@ -40,35 +40,39 @@ export const AiProductEditionProvider = ({
40
40
  const [inputsGeneratedWithAi, setInputsGeneratedWithAi] = useState({});
41
41
  const [inputsUsingAi, setInputsUsingAi] = useState({});
42
42
 
43
- const MAX_CREDITS = 10;
44
- const COOLDOWN_MS = 5 * 60 * 1000;
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 = (currentArticleId) => {
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
- let productTimestamps = allRateData[currentArticleId] || [];
53
+ const idPart = descriptionId ?? attributeId;
54
54
 
55
- productTimestamps = productTimestamps.filter(timestamp => (now - timestamp) < COOLDOWN_MS);
55
+ const key = idPart ? `${articleId}_${idPart}_${type}` : `${articleId}`;
56
56
 
57
- if (productTimestamps.length >= MAX_CREDITS) {
57
+ let keyTimestamps = allRateData[key] || [];
58
58
 
59
- allRateData[currentArticleId] = productTimestamps;
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: "Has alcanzado el limite de intentos frecuentes para este producto. Por favor, espera unos minutos."
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
- productTimestamps.push(now);
70
- allRateData[currentArticleId] = productTimestamps;
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(articleId);
157
+ const rateLimitStatus = checkAndManageRateLimit({
158
+ articleId,
159
+ descriptionId,
160
+ attributeId,
161
+ type
162
+ });
154
163
 
155
164
  if (!rateLimitStatus.allowed) {
156
165
  return {