kupos-ui-components-lib 9.3.2 → 9.3.4

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.
@@ -308,6 +308,92 @@ const commonService = {
308
308
 
309
309
  return { originalPrice: price, discountedPrice };
310
310
  },
311
+
312
+ startViewerCount: (
313
+ node: HTMLSpanElement | null,
314
+ viewersConfig?: { min: number; max: number; interval?: number },
315
+ ) => {
316
+ if (!node || !viewersConfig) return;
317
+
318
+ const prevId = node.dataset.viewerId;
319
+ if (prevId) clearInterval(Number(prevId));
320
+
321
+ const { min, max, interval = 5000 } = viewersConfig;
322
+ const clamp = (v: number) => Math.min(max, Math.max(min, v));
323
+ const initialValue = Math.floor(Math.random() * (max - min + 1)) + min;
324
+
325
+ node.textContent = String(initialValue);
326
+
327
+ const id = setInterval(() => {
328
+ const current = Number(node.textContent) || initialValue;
329
+ const delta = Math.ceil(current * 0.2);
330
+ const next =
331
+ current + Math.floor(Math.random() * (2 * delta + 1)) - delta;
332
+ node.textContent = String(clamp(Math.round(next)));
333
+ }, interval);
334
+
335
+ node.dataset.viewerId = String(id);
336
+ },
337
+
338
+ startCountdown: (
339
+ node: HTMLSpanElement | null,
340
+ countdownSeconds: number = 599,
341
+ ) => {
342
+ if (!node) return;
343
+
344
+ const prevId = node.dataset.countdownId;
345
+ if (prevId) clearInterval(Number(prevId));
346
+
347
+ let remaining = countdownSeconds * 1000; // Convert to milliseconds
348
+
349
+ const formatTime = (totalMs: number) => {
350
+ const m = Math.floor(totalMs / 60000);
351
+ const s = Math.floor((totalMs % 60000) / 1000);
352
+ const ms = Math.floor((totalMs % 1000) / 10); // Show 2 digits for milliseconds
353
+ return `${String(m).padStart(2, "0")}:${String(s).padStart(2, "0")}:${String(ms).padStart(2, "0")}`;
354
+ };
355
+
356
+ node.textContent = formatTime(remaining);
357
+
358
+ const id = setInterval(() => {
359
+ remaining -= 100; // Decrease by 100ms
360
+ if (remaining <= 0) {
361
+ remaining = countdownSeconds * 1000;
362
+ }
363
+ node.textContent = formatTime(remaining);
364
+ }, 100); // Update every 100ms
365
+
366
+ node.dataset.countdownId = String(id);
367
+ },
368
+
369
+ startComprandoCount: (
370
+ node: HTMLSpanElement | null,
371
+ min: number = 4,
372
+ max: number = 16,
373
+ ) => {
374
+ if (!node) return;
375
+
376
+ const prevId = node.dataset.comprandoId;
377
+ if (prevId) clearInterval(Number(prevId));
378
+
379
+ const initialValue = Math.floor(Math.random() * (max - min + 1)) + min;
380
+ node.textContent = String(initialValue);
381
+
382
+ const id = setInterval(() => {
383
+ const current = Number(node.textContent) || initialValue;
384
+ const changePercent = 0.05; // 5% change
385
+ const change = Math.ceil(current * changePercent);
386
+ const direction = Math.random() > 0.5 ? 1 : -1;
387
+ let next = current + (change * direction);
388
+
389
+ // Clamp within min and max
390
+ next = Math.min(max, Math.max(min, next));
391
+
392
+ node.textContent = String(next);
393
+ }, 5000); // Update every 5 seconds
394
+
395
+ node.dataset.comprandoId = String(id);
396
+ },
311
397
  };
312
398
 
313
399
  export default commonService;