@scalio-cloud/ui 0.70.0 → 0.71.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.
Files changed (3) hide show
  1. package/README.md +1 -1
  2. package/package.json +1 -1
  3. package/react/nav.js +55 -11
package/README.md CHANGED
@@ -174,7 +174,7 @@ const NAV = { sections: [
174
174
  ] },
175
175
  ] };
176
176
 
177
- const apps = useScalioApps(() => api.get("/apps").then((r) => r.data)); // once per page load
177
+ const apps = useScalioApps(() => api.get("/apps").then((r) => r.data)); // once per page load, retried after 1/2/4 s (not on 401/403/404)
178
178
  <AppShell
179
179
  switcher={apps.features.switcher && <ProductSwitcher current="citadel" links={apps.links} stage={apps.stage} sso={sso} lang={lang} />}
180
180
  header={<AppHeader ... onMenu={() => setDrawer(true)}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scalio-cloud/ui",
3
- "version": "0.70.0",
3
+ "version": "0.71.0",
4
4
  "description": "Scalio Control Room design tokens, Tailwind preset, self-hosted fonts, the shared app header, sign-in layout, sidebar and page blocks",
5
5
  "license": "SEE LICENSE IN README.md",
6
6
  "private": false,
package/react/nav.js CHANGED
@@ -421,24 +421,66 @@ function ProductSwitcher({ current, links, stage, sso, lang, labels, products, t
421
421
 
422
422
  // GET /api/apps once per page load. fetchApps returns a promise with the JSON answer.
423
423
  // Until the answer is in (or on error) switcher and support stay hidden.
424
+ // A failed call is retried after 1, 2 and 4 s (options.retryDelays in ms), but not on 401/403/404 (error.response.status
425
+ // as with axios, or error.status). When the last component using the hook unmounts, a waiting retry is dropped;
426
+ // after the last retry the next mount asks again. All users share one call; the first caller's options count.
427
+ const SCALIO_APPS_RETRY_DELAYS = [1000, 2000, 4000];
424
428
  let appsCache = null;
425
429
  let appsPending = null;
430
+ let appsUsers = 0;
431
+ let appsAbort = null;
426
432
 
427
- function useScalioApps(fetchApps) {
433
+ function errorStatus(e) {
434
+ return e && (e.status || (e.response && e.response.status));
435
+ }
436
+
437
+ function loadScalioApps(fetchApps, delays) {
438
+ let attempt = 0;
439
+ const run = () =>
440
+ Promise.resolve()
441
+ .then(fetchApps)
442
+ .catch((e) => {
443
+ const status = errorStatus(e);
444
+ if (status === 401 || status === 403 || status === 404 || attempt >= delays.length || appsUsers <= 0) throw e;
445
+ const pause = delays[attempt++];
446
+ return new Promise((resolve, reject) => {
447
+ const timer = setTimeout(() => {
448
+ appsAbort = null;
449
+ resolve(run());
450
+ }, pause);
451
+ appsAbort = () => {
452
+ clearTimeout(timer);
453
+ appsAbort = null;
454
+ reject(e);
455
+ };
456
+ });
457
+ });
458
+ return run().then((d) => {
459
+ appsCache = d || {};
460
+ return appsCache;
461
+ });
462
+ }
463
+
464
+ function useScalioApps(fetchApps, options) {
428
465
  const [data, setData] = React.useState(appsCache);
466
+ const delays = (options && options.retryDelays) || SCALIO_APPS_RETRY_DELAYS;
429
467
  React.useEffect(() => {
430
468
  if (appsCache || !fetchApps) return undefined;
431
469
  let alive = true;
432
- appsPending =
433
- appsPending ||
434
- Promise.resolve()
435
- .then(fetchApps)
436
- .then((d) => {
437
- appsCache = d || {};
438
- return appsCache;
439
- });
440
- appsPending.then((d) => alive && setData(d)).catch(() => { appsPending = null; });
441
- return () => { alive = false; };
470
+ appsUsers++;
471
+ if (!appsPending) {
472
+ const pending = loadScalioApps(fetchApps, delays);
473
+ appsPending = pending;
474
+ pending.catch(() => {
475
+ if (appsPending === pending) appsPending = null;
476
+ });
477
+ }
478
+ appsPending.then((d) => alive && setData(d)).catch(() => {});
479
+ return () => {
480
+ alive = false;
481
+ appsUsers--;
482
+ if (appsUsers <= 0 && appsAbort) appsAbort();
483
+ };
442
484
  }, [fetchApps]);
443
485
  return {
444
486
  links: (data && data.links) || {},
@@ -449,6 +491,7 @@ function useScalioApps(fetchApps) {
449
491
  }
450
492
 
451
493
  function resetScalioApps() {
494
+ if (appsAbort) appsAbort();
452
495
  appsCache = null;
453
496
  appsPending = null;
454
497
  }
@@ -550,6 +593,7 @@ module.exports = {
550
593
  stageBadge,
551
594
  scalioFeatures,
552
595
  useScalioApps,
596
+ SCALIO_APPS_RETRY_DELAYS,
553
597
  resetScalioApps,
554
598
  SupportDrawer,
555
599
  visibleSections,