@tap-payments/os-micro-frontend-shared 0.1.407 → 0.1.409

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.
@@ -19,10 +19,10 @@ function DueDateCell(_a) {
19
19
  var { dueDate, expiryDate } = _a, props = __rest(_a, ["dueDate", "expiryDate"]);
20
20
  const dueMeta = useMemo(() => ({ label: formatDate(dayjs(dueDate).valueOf()), tooltip: 'Due Date' }), [dueDate]);
21
21
  const expiryMeta = useMemo(() => ({ label: formatDate(dayjs(expiryDate).valueOf()), tooltip: 'Expiry Date' }), [expiryDate]);
22
- const renderIcon = (meta) => (_jsx(Tooltip, Object.assign({ title: meta.tooltip }, { children: _jsx(TextLabel, { children: meta.label }) })));
23
- const centerIcon = useMemo(() => (dueMeta ? renderIcon(dueMeta) : expiryMeta ? renderIcon(expiryMeta) : null), [dueMeta, expiryMeta]);
24
- const rightIcons = useMemo(() => (expiryMeta && dueMeta ? [renderIcon(expiryMeta)] : []), [dueMeta, expiryMeta]);
25
- if (!dueMeta && !expiryMeta)
22
+ const renderChipContent = (meta) => (_jsx(Tooltip, Object.assign({ title: meta.tooltip }, { children: _jsx(TextLabel, { children: meta.label }) })));
23
+ const centerIcon = useMemo(() => (dueDate ? renderChipContent(dueMeta) : expiryDate ? renderChipContent(expiryMeta) : null), [dueMeta, expiryMeta, dueDate, expiryDate]);
24
+ const rightIcons = useMemo(() => (expiryDate && dueDate ? [renderChipContent(expiryMeta)] : []), [dueMeta, expiryMeta, dueDate, expiryDate]);
25
+ if (!dueDate && !expiryDate)
26
26
  return null;
27
27
  return (_jsx(TableCell, Object.assign({}, props, { sx: { position: 'relative', overflow: 'visible' } }, { children: _jsx(RightLeftExpandingCenterChip, { sx: { justifyContent: 'flex-start' }, centerIcon: centerIcon, rightIcons: rightIcons }) })));
28
28
  }
@@ -3,7 +3,6 @@ export * from './useMouseState';
3
3
  export * from './useDelayToSetValue';
4
4
  export * from './useActionMenu';
5
5
  export * from './useBadgesCount';
6
- export * from './useCheckInternetConnection';
7
6
  export * from './useCheckUserLoggedIn';
8
7
  export * from './useIsParameterSelected';
9
8
  export * from './useStickyHeaderShadow';
@@ -15,3 +14,4 @@ export * from './useAppEventListener';
15
14
  export * from './useSelectedMerchantDetails';
16
15
  export * from './useToast';
17
16
  export * from './useScrolledTo';
17
+ export * from './useNetworkState';
@@ -3,7 +3,6 @@ export * from './useMouseState';
3
3
  export * from './useDelayToSetValue';
4
4
  export * from './useActionMenu';
5
5
  export * from './useBadgesCount';
6
- export * from './useCheckInternetConnection';
7
6
  export * from './useCheckUserLoggedIn';
8
7
  export * from './useIsParameterSelected';
9
8
  export * from './useStickyHeaderShadow';
@@ -15,3 +14,4 @@ export * from './useAppEventListener';
15
14
  export * from './useSelectedMerchantDetails';
16
15
  export * from './useToast';
17
16
  export * from './useScrolledTo';
17
+ export * from './useNetworkState';
@@ -0,0 +1,12 @@
1
+ export interface UseNetworkStateOptions {
2
+ checkUrl?: string;
3
+ checkIntervalMs?: number;
4
+ debounceMs?: number;
5
+ timeoutMs?: number;
6
+ }
7
+ export interface OnlineState {
8
+ isOnline: boolean;
9
+ isChecking: boolean;
10
+ error: string | null;
11
+ }
12
+ export declare const useNetworkState: (options?: UseNetworkStateOptions) => OnlineState;
@@ -0,0 +1,112 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { useCallback, useEffect, useRef, useState } from 'react';
11
+ export const useNetworkState = (options = {}) => {
12
+ const { checkUrl = '/status', checkIntervalMs = 30000, // poll /status only while offline
13
+ debounceMs = 800, timeoutMs = 5000, } = options;
14
+ const isBrowser = typeof window !== 'undefined' && typeof navigator !== 'undefined';
15
+ const [isOnline, setIsOnline] = useState(isBrowser ? navigator.onLine : true);
16
+ const [isChecking, setIsChecking] = useState(false);
17
+ const [error, setError] = useState(null);
18
+ const debounceTimer = useRef(null);
19
+ const intervalId = useRef(null);
20
+ const applyStatusWithDebounce = useCallback((nextOnline) => {
21
+ if (debounceTimer.current !== null) {
22
+ window.clearTimeout(debounceTimer.current);
23
+ }
24
+ debounceTimer.current = window.setTimeout(() => {
25
+ setIsOnline(nextOnline);
26
+ debounceTimer.current = null;
27
+ }, debounceMs);
28
+ }, [debounceMs]);
29
+ useEffect(() => {
30
+ if (!isBrowser) {
31
+ return;
32
+ }
33
+ let abortController = null;
34
+ const clearPolling = () => {
35
+ if (intervalId.current !== null) {
36
+ window.clearInterval(intervalId.current);
37
+ intervalId.current = null;
38
+ }
39
+ };
40
+ const checkConnectivity = () => __awaiter(void 0, void 0, void 0, function* () {
41
+ setIsChecking(true);
42
+ setError(null);
43
+ abortController === null || abortController === void 0 ? void 0 : abortController.abort();
44
+ abortController = new AbortController();
45
+ try {
46
+ const timeout = window.setTimeout(() => {
47
+ abortController === null || abortController === void 0 ? void 0 : abortController.abort();
48
+ }, timeoutMs);
49
+ const res = yield fetch(checkUrl, {
50
+ method: 'GET',
51
+ cache: 'no-store',
52
+ signal: abortController.signal,
53
+ });
54
+ window.clearTimeout(timeout);
55
+ const ok = res.ok;
56
+ applyStatusWithDebounce(ok);
57
+ if (ok)
58
+ clearPolling();
59
+ else
60
+ setError(`Health check failed with status ${res.status}`);
61
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
62
+ }
63
+ catch (_err) {
64
+ applyStatusWithDebounce(false);
65
+ setError('Unable to reach network');
66
+ }
67
+ finally {
68
+ setIsChecking(false);
69
+ }
70
+ });
71
+ const handleOnline = () => {
72
+ // Browser says we're online: trust it, no /status request
73
+ clearPolling();
74
+ applyStatusWithDebounce(true);
75
+ };
76
+ const handleOffline = () => {
77
+ // Browser says we're offline: reflect that and poll /status until back
78
+ applyStatusWithDebounce(false);
79
+ clearPolling();
80
+ checkConnectivity();
81
+ intervalId.current = window.setInterval(checkConnectivity, checkIntervalMs);
82
+ };
83
+ window.addEventListener('online', handleOnline);
84
+ window.addEventListener('offline', handleOffline);
85
+ // If initially offline, start polling /status; if online, do not hit /status
86
+ if (!navigator.onLine) {
87
+ checkConnectivity();
88
+ intervalId.current = window.setInterval(checkConnectivity, checkIntervalMs);
89
+ }
90
+ return () => {
91
+ window.removeEventListener('online', handleOnline);
92
+ window.removeEventListener('offline', handleOffline);
93
+ if (debounceTimer.current !== null) {
94
+ window.clearTimeout(debounceTimer.current);
95
+ }
96
+ clearPolling();
97
+ abortController === null || abortController === void 0 ? void 0 : abortController.abort();
98
+ };
99
+ }, [isBrowser, checkUrl, checkIntervalMs, debounceMs, timeoutMs, applyStatusWithDebounce]);
100
+ if (!isBrowser) {
101
+ return {
102
+ isOnline: false,
103
+ isChecking: false,
104
+ error: 'Online status is only available in a browser environment.',
105
+ };
106
+ }
107
+ return {
108
+ isOnline,
109
+ isChecking,
110
+ error,
111
+ };
112
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tap-payments/os-micro-frontend-shared",
3
3
  "description": "Shared components and utilities for Tap Payments micro frontends",
4
- "version": "0.1.407",
4
+ "version": "0.1.409",
5
5
  "testVersion": 0,
6
6
  "type": "module",
7
7
  "main": "build/index.js",
@@ -1 +0,0 @@
1
- export declare const useCheckInternetConnection: () => boolean;
@@ -1,17 +0,0 @@
1
- import { useState, useEffect } from 'react';
2
- export const useCheckInternetConnection = () => {
3
- const [isOnline, setIsOnline] = useState(navigator.onLine);
4
- useEffect(() => {
5
- const handleOnlineStatus = () => {
6
- setIsOnline(navigator.onLine);
7
- };
8
- window.addEventListener('online', handleOnlineStatus);
9
- window.addEventListener('offline', handleOnlineStatus);
10
- return () => {
11
- window.removeEventListener('online', handleOnlineStatus);
12
- window.removeEventListener('offline', handleOnlineStatus);
13
- };
14
- }, []);
15
- const isOffline = !isOnline;
16
- return isOffline;
17
- };