@capsitech/react-utilities 0.1.4 → 0.1.6

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 (45) hide show
  1. package/lib/Components/SuspenseRoute.d.ts +7 -7
  2. package/lib/Components/SuspenseRoute.js +29 -29
  3. package/lib/Components/index.d.ts +1 -1
  4. package/lib/Components/index.js +1 -1
  5. package/lib/Hooks/index.d.ts +45 -45
  6. package/lib/Hooks/index.js +98 -98
  7. package/lib/Hooks/useInfiniteScroll.d.ts +7 -7
  8. package/lib/Hooks/useInfiniteScroll.js +22 -22
  9. package/lib/Hooks/useNetworkState.d.ts +67 -67
  10. package/lib/Hooks/useNetworkState.js +41 -41
  11. package/lib/Hooks/useShortcuts.d.ts +4 -4
  12. package/lib/Hooks/useShortcuts.js +91 -91
  13. package/lib/Utilities/ApiUtility.axios.d.ts +60 -60
  14. package/lib/Utilities/ApiUtility.axios.js +305 -305
  15. package/lib/Utilities/BrowserInfo.d.ts +74 -74
  16. package/lib/Utilities/BrowserInfo.js +153 -153
  17. package/lib/Utilities/Countries.d.ts +14 -14
  18. package/lib/Utilities/Countries.js +290 -290
  19. package/lib/Utilities/CustomEventEmitter.d.ts +12 -12
  20. package/lib/Utilities/CustomEventEmitter.js +30 -30
  21. package/lib/Utilities/FastCompare.d.ts +1 -1
  22. package/lib/Utilities/FastCompare.js +128 -128
  23. package/lib/Utilities/HideablePromise.d.ts +5 -5
  24. package/lib/Utilities/HideablePromise.js +10 -10
  25. package/lib/Utilities/LoadScripts.d.ts +9 -9
  26. package/lib/Utilities/LoadScripts.js +51 -51
  27. package/lib/Utilities/MTDFraudPrevention.d.ts +28 -28
  28. package/lib/Utilities/MTDFraudPrevention.js +157 -157
  29. package/lib/Utilities/Nationalities.d.ts +5 -5
  30. package/lib/Utilities/Nationalities.js +245 -245
  31. package/lib/Utilities/RouteUtils.d.ts +129 -120
  32. package/lib/Utilities/RouteUtils.js +223 -206
  33. package/lib/Utilities/TimeZones.d.ts +10 -10
  34. package/lib/Utilities/TimeZones.js +1069 -1069
  35. package/lib/Utilities/Types.d.ts +19 -19
  36. package/lib/Utilities/Types.js +1 -1
  37. package/lib/Utilities/Utils.d.ts +174 -174
  38. package/lib/Utilities/Utils.js +331 -331
  39. package/lib/Utilities/dayjs.d.ts +18 -18
  40. package/lib/Utilities/dayjs.js +56 -56
  41. package/lib/Utilities/index.d.ts +14 -14
  42. package/lib/Utilities/index.js +14 -14
  43. package/lib/index.d.ts +3 -3
  44. package/lib/index.js +3 -3
  45. package/package.json +12 -25
@@ -1,7 +1,7 @@
1
- interface ISuspenseRouteProps {
2
- label?: string;
3
- children?: any;
4
- fallback?: any;
5
- }
6
- export declare const SuspenseRoute: ({ children: Component, label, fallback: FallbackComponent, }: ISuspenseRouteProps) => import("react/jsx-runtime").JSX.Element;
7
- export {};
1
+ interface ISuspenseRouteProps {
2
+ label?: string;
3
+ children?: any;
4
+ fallback?: any;
5
+ }
6
+ export declare const SuspenseRoute: ({ children: Component, label, fallback: FallbackComponent, }: ISuspenseRouteProps) => import("react/jsx-runtime").JSX.Element;
7
+ export {};
@@ -1,29 +1,29 @@
1
- import { jsx as _jsx } from "react/jsx-runtime";
2
- import React from "react";
3
- const isSuspenseComponent = (component) => typeof component === "object" &&
4
- component &&
5
- component.hasOwnProperty("$$typeof") &&
6
- component.hasOwnProperty("_payload") &&
7
- component.hasOwnProperty("_init");
8
- export const SuspenseRoute = ({ children: Component, label = "Loading components...", fallback: FallbackComponent, }) => {
9
- if (isSuspenseComponent(Component)) {
10
- if (!FallbackComponent)
11
- FallbackComponent = SuspenseRouteFallback;
12
- return _jsx(React.Suspense, { fallback: _jsx(FallbackComponent, { label: label }), children: _jsx(Component, {}) });
13
- }
14
- return (_jsx(Component, {}));
15
- };
16
- const SuspenseRouteFallback = ({ label = "Loading components..." }) => {
17
- return _jsx("div", { style: {
18
- position: 'fixed',
19
- top: 0,
20
- left: 0,
21
- width: '100%',
22
- height: '100%',
23
- backgroundColor: 'rgba(0, 0, 0, 0.3)',
24
- zIndex: 9999,
25
- display: 'flex',
26
- justifyContent: 'center',
27
- alignItems: 'center'
28
- }, children: label });
29
- };
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import React from "react";
3
+ const isSuspenseComponent = (component) => typeof component === "object" &&
4
+ component &&
5
+ component.hasOwnProperty("$$typeof") &&
6
+ component.hasOwnProperty("_payload") &&
7
+ component.hasOwnProperty("_init");
8
+ export const SuspenseRoute = ({ children: Component, label = "Loading components...", fallback: FallbackComponent, }) => {
9
+ if (isSuspenseComponent(Component)) {
10
+ if (!FallbackComponent)
11
+ FallbackComponent = SuspenseRouteFallback;
12
+ return _jsx(React.Suspense, { fallback: _jsx(FallbackComponent, { label: label }), children: _jsx(Component, {}) });
13
+ }
14
+ return (_jsx(Component, {}));
15
+ };
16
+ const SuspenseRouteFallback = ({ label = "Loading components..." }) => {
17
+ return _jsx("div", { style: {
18
+ position: 'fixed',
19
+ top: 0,
20
+ left: 0,
21
+ width: '100%',
22
+ height: '100%',
23
+ backgroundColor: 'rgba(0, 0, 0, 0.3)',
24
+ zIndex: 9999,
25
+ display: 'flex',
26
+ justifyContent: 'center',
27
+ alignItems: 'center'
28
+ }, children: label });
29
+ };
@@ -1 +1 @@
1
- export * from './SuspenseRoute';
1
+ export * from './SuspenseRoute';
@@ -1 +1 @@
1
- export * from './SuspenseRoute';
1
+ export * from './SuspenseRoute';
@@ -1,45 +1,45 @@
1
- import { useEffect } from 'react';
2
- export * from './useInfiniteScroll';
3
- export * from './useNetworkState';
4
- export * from './useShortcuts';
5
- /**
6
- * React effect hook that invokes only on update.
7
- * It doesn't invoke on mount
8
- */
9
- export declare const useUpdateEffect: typeof useEffect;
10
- /**
11
- * React hook to persist any value between renders,
12
- * but keeps it up-to-date if it changes.
13
- *
14
- * @param value the value or function to persist
15
- */
16
- export declare function useLatestRef<T>(value: T): import("react").MutableRefObject<T>;
17
- /**
18
- * useCallbackRef hook
19
- *
20
- * A custom hook that converts a callback to a ref to avoid triggering re-renders
21
- * ..when passed as a prop or avoid re-executing effects when passed as a dependency
22
- *
23
- * @param callback The callback to write to a ref object
24
- */
25
- export declare const useCallbackRef: <T extends (...args: any[]) => any>(callback: T | undefined) => T;
26
- /**
27
- * useFirstRenderState hook
28
- *
29
- * Returns "true" if component is just mounted (first render), else "false".
30
- */
31
- export declare const useFirstRenderState: () => boolean;
32
- /**
33
- * React Hook that provides a declarative `setInterval`
34
- *
35
- * @param callback the callback to execute at interval
36
- * @param delay the `setInterval` delay (in ms)
37
- */
38
- export declare function useInterval(callback: () => void, delay: number | null): void;
39
- /**
40
- * React hook that provides a declarative `setTimeout`
41
- *
42
- * @param callback the callback to run after specified delay
43
- * @param delay the delay (in ms)
44
- */
45
- export declare function useTimeout(callback: Function, delay: number | null): void;
1
+ import { useEffect } from 'react';
2
+ export * from './useInfiniteScroll';
3
+ export * from './useNetworkState';
4
+ export * from './useShortcuts';
5
+ /**
6
+ * React effect hook that invokes only on update.
7
+ * It doesn't invoke on mount
8
+ */
9
+ export declare const useUpdateEffect: typeof useEffect;
10
+ /**
11
+ * React hook to persist any value between renders,
12
+ * but keeps it up-to-date if it changes.
13
+ *
14
+ * @param value the value or function to persist
15
+ */
16
+ export declare function useLatestRef<T>(value: T): import("react").MutableRefObject<T>;
17
+ /**
18
+ * useCallbackRef hook
19
+ *
20
+ * A custom hook that converts a callback to a ref to avoid triggering re-renders
21
+ * ..when passed as a prop or avoid re-executing effects when passed as a dependency
22
+ *
23
+ * @param callback The callback to write to a ref object
24
+ */
25
+ export declare const useCallbackRef: <T extends (...args: any[]) => any>(callback: T | undefined) => T;
26
+ /**
27
+ * useFirstRenderState hook
28
+ *
29
+ * Returns "true" if component is just mounted (first render), else "false".
30
+ */
31
+ export declare const useFirstRenderState: () => boolean;
32
+ /**
33
+ * React Hook that provides a declarative `setInterval`
34
+ *
35
+ * @param callback the callback to execute at interval
36
+ * @param delay the `setInterval` delay (in ms)
37
+ */
38
+ export declare function useInterval(callback: () => void, delay: number | null): void;
39
+ /**
40
+ * React hook that provides a declarative `setTimeout`
41
+ *
42
+ * @param callback the callback to run after specified delay
43
+ * @param delay the delay (in ms)
44
+ */
45
+ export declare function useTimeout(callback: Function, delay: number | null): void;
@@ -1,98 +1,98 @@
1
- import { useCallback, useEffect, useRef } from 'react';
2
- export * from './useInfiniteScroll';
3
- export * from './useNetworkState';
4
- export * from './useShortcuts';
5
- /**
6
- * React effect hook that invokes only on update.
7
- * It doesn't invoke on mount
8
- */
9
- export const useUpdateEffect = (effect, deps) => {
10
- const mounted = useRef(false);
11
- useEffect(() => {
12
- if (mounted.current) {
13
- return effect();
14
- }
15
- mounted.current = true;
16
- return undefined;
17
- // eslint-disable-next-line react-hooks/exhaustive-deps
18
- }, deps);
19
- return mounted.current;
20
- };
21
- /**
22
- * React hook to persist any value between renders,
23
- * but keeps it up-to-date if it changes.
24
- *
25
- * @param value the value or function to persist
26
- */
27
- export function useLatestRef(value) {
28
- const ref = useRef(value);
29
- useEffect(() => {
30
- ref.current = value;
31
- }, [value]);
32
- return ref;
33
- }
34
- /**
35
- * useCallbackRef hook
36
- *
37
- * A custom hook that converts a callback to a ref to avoid triggering re-renders
38
- * ..when passed as a prop or avoid re-executing effects when passed as a dependency
39
- *
40
- * @param callback The callback to write to a ref object
41
- */
42
- export const useCallbackRef = (callback) => {
43
- const callbackRef = useRef(callback);
44
- useEffect(() => {
45
- callbackRef.current = callback;
46
- });
47
- return useCallback(((...args) => {
48
- return callbackRef.current?.(...args);
49
- }), []);
50
- };
51
- /**
52
- * useFirstRenderState hook
53
- *
54
- * Returns "true" if component is just mounted (first render), else "false".
55
- */
56
- export const useFirstRenderState = () => {
57
- const isFirst = useRef(true);
58
- if (isFirst.current) {
59
- isFirst.current = false;
60
- return true;
61
- }
62
- return isFirst.current;
63
- };
64
- /**
65
- * React Hook that provides a declarative `setInterval`
66
- *
67
- * @param callback the callback to execute at interval
68
- * @param delay the `setInterval` delay (in ms)
69
- */
70
- export function useInterval(callback, delay) {
71
- const savedCallback = useLatestRef(callback);
72
- useEffect(() => {
73
- const tick = () => {
74
- savedCallback.current?.();
75
- };
76
- if (delay !== null) {
77
- const id = setInterval(tick, delay);
78
- return () => clearInterval(id);
79
- }
80
- }, [delay, savedCallback]);
81
- }
82
- /**
83
- * React hook that provides a declarative `setTimeout`
84
- *
85
- * @param callback the callback to run after specified delay
86
- * @param delay the delay (in ms)
87
- */
88
- export function useTimeout(callback, delay) {
89
- const savedCallback = useLatestRef(callback);
90
- useEffect(() => {
91
- if (delay == null)
92
- return;
93
- const timeoutId = setTimeout(() => {
94
- savedCallback.current?.();
95
- }, delay);
96
- return () => clearTimeout(timeoutId);
97
- }, [delay, savedCallback]);
98
- }
1
+ import { useCallback, useEffect, useRef } from 'react';
2
+ export * from './useInfiniteScroll';
3
+ export * from './useNetworkState';
4
+ export * from './useShortcuts';
5
+ /**
6
+ * React effect hook that invokes only on update.
7
+ * It doesn't invoke on mount
8
+ */
9
+ export const useUpdateEffect = (effect, deps) => {
10
+ const mounted = useRef(false);
11
+ useEffect(() => {
12
+ if (mounted.current) {
13
+ return effect();
14
+ }
15
+ mounted.current = true;
16
+ return undefined;
17
+ // eslint-disable-next-line react-hooks/exhaustive-deps
18
+ }, deps);
19
+ return mounted.current;
20
+ };
21
+ /**
22
+ * React hook to persist any value between renders,
23
+ * but keeps it up-to-date if it changes.
24
+ *
25
+ * @param value the value or function to persist
26
+ */
27
+ export function useLatestRef(value) {
28
+ const ref = useRef(value);
29
+ useEffect(() => {
30
+ ref.current = value;
31
+ }, [value]);
32
+ return ref;
33
+ }
34
+ /**
35
+ * useCallbackRef hook
36
+ *
37
+ * A custom hook that converts a callback to a ref to avoid triggering re-renders
38
+ * ..when passed as a prop or avoid re-executing effects when passed as a dependency
39
+ *
40
+ * @param callback The callback to write to a ref object
41
+ */
42
+ export const useCallbackRef = (callback) => {
43
+ const callbackRef = useRef(callback);
44
+ useEffect(() => {
45
+ callbackRef.current = callback;
46
+ });
47
+ return useCallback(((...args) => {
48
+ return callbackRef.current?.(...args);
49
+ }), []);
50
+ };
51
+ /**
52
+ * useFirstRenderState hook
53
+ *
54
+ * Returns "true" if component is just mounted (first render), else "false".
55
+ */
56
+ export const useFirstRenderState = () => {
57
+ const isFirst = useRef(true);
58
+ if (isFirst.current) {
59
+ isFirst.current = false;
60
+ return true;
61
+ }
62
+ return isFirst.current;
63
+ };
64
+ /**
65
+ * React Hook that provides a declarative `setInterval`
66
+ *
67
+ * @param callback the callback to execute at interval
68
+ * @param delay the `setInterval` delay (in ms)
69
+ */
70
+ export function useInterval(callback, delay) {
71
+ const savedCallback = useLatestRef(callback);
72
+ useEffect(() => {
73
+ const tick = () => {
74
+ savedCallback.current?.();
75
+ };
76
+ if (delay !== null) {
77
+ const id = setInterval(tick, delay);
78
+ return () => clearInterval(id);
79
+ }
80
+ }, [delay, savedCallback]);
81
+ }
82
+ /**
83
+ * React hook that provides a declarative `setTimeout`
84
+ *
85
+ * @param callback the callback to run after specified delay
86
+ * @param delay the delay (in ms)
87
+ */
88
+ export function useTimeout(callback, delay) {
89
+ const savedCallback = useLatestRef(callback);
90
+ useEffect(() => {
91
+ if (delay == null)
92
+ return;
93
+ const timeoutId = setTimeout(() => {
94
+ savedCallback.current?.();
95
+ }, delay);
96
+ return () => clearTimeout(timeoutId);
97
+ }, [delay, savedCallback]);
98
+ }
@@ -1,7 +1,7 @@
1
- import React from 'react';
2
- /**
3
- * Infinite scrolling with intersection observer
4
- * @param scrollRef Reference object for observe bottom boundary
5
- * @param dispatch Trigger an action that updates the page number
6
- */
7
- export declare const useInfiniteScroll: (scrollRef: React.MutableRefObject<any>, dispatch: any) => void;
1
+ import React from 'react';
2
+ /**
3
+ * Infinite scrolling with intersection observer
4
+ * @param scrollRef Reference object for observe bottom boundary
5
+ * @param dispatch Trigger an action that updates the page number
6
+ */
7
+ export declare const useInfiniteScroll: (scrollRef: React.MutableRefObject<any>, dispatch: any) => void;
@@ -1,22 +1,22 @@
1
- import React from 'react';
2
- /**
3
- * Infinite scrolling with intersection observer
4
- * @param scrollRef Reference object for observe bottom boundary
5
- * @param dispatch Trigger an action that updates the page number
6
- */
7
- export const useInfiniteScroll = (scrollRef, dispatch) => {
8
- const scrollObserver = React.useCallback((node) => {
9
- new IntersectionObserver((entries) => {
10
- entries.forEach((en) => {
11
- if (en.intersectionRatio > 0) {
12
- dispatch({ type: 'next-page' });
13
- }
14
- });
15
- }).observe(node);
16
- }, [dispatch]);
17
- React.useEffect(() => {
18
- if (scrollRef.current) {
19
- scrollObserver(scrollRef.current);
20
- }
21
- }, [scrollObserver, scrollRef]);
22
- };
1
+ import React from 'react';
2
+ /**
3
+ * Infinite scrolling with intersection observer
4
+ * @param scrollRef Reference object for observe bottom boundary
5
+ * @param dispatch Trigger an action that updates the page number
6
+ */
7
+ export const useInfiniteScroll = (scrollRef, dispatch) => {
8
+ const scrollObserver = React.useCallback((node) => {
9
+ new IntersectionObserver((entries) => {
10
+ entries.forEach((en) => {
11
+ if (en.intersectionRatio > 0) {
12
+ dispatch({ type: 'next-page' });
13
+ }
14
+ });
15
+ }).observe(node);
16
+ }, [dispatch]);
17
+ React.useEffect(() => {
18
+ if (scrollRef.current) {
19
+ scrollObserver(scrollRef.current);
20
+ }
21
+ }, [scrollObserver, scrollRef]);
22
+ };
@@ -1,67 +1,67 @@
1
- export interface INetworkInformation extends EventTarget {
2
- readonly downlink: number;
3
- readonly downlinkMax: number;
4
- readonly effectiveType: 'slow-2g' | '2g' | '3g' | '4g';
5
- readonly rtt: number;
6
- readonly saveData: boolean;
7
- readonly type: 'bluetooth' | 'cellular' | 'ethernet' | 'none' | 'wifi' | 'wimax' | 'other' | 'unknown';
8
- onChange: (event: Event) => void;
9
- }
10
- export interface IUseNetworkState {
11
- /**
12
- * @desc Whether browser connected to the network or not.
13
- */
14
- online: boolean | undefined;
15
- /**
16
- * @desc Previous value of `online` property. Helps to identify if browser
17
- * just connected or lost connection.
18
- */
19
- previous: boolean | undefined;
20
- /**
21
- * @desc The {Date} object pointing to the moment when state change occurred.
22
- */
23
- since: Date | undefined;
24
- /**
25
- * @desc Effective bandwidth estimate in megabits per second, rounded to the
26
- * nearest multiple of 25 kilobits per seconds.
27
- */
28
- downlink: INetworkInformation['downlink'] | undefined;
29
- /**
30
- * @desc Maximum downlink speed, in megabits per second (Mbps), for the
31
- * underlying connection technology
32
- */
33
- downlinkMax: INetworkInformation['downlinkMax'] | undefined;
34
- /**
35
- * @desc Effective type of the connection meaning one of 'slow-2g', '2g', '3g', or '4g'.
36
- * This value is determined using a combination of recently observed round-trip time
37
- * and downlink values.
38
- */
39
- effectiveType: INetworkInformation['effectiveType'] | undefined;
40
- /**
41
- * @desc Estimated effective round-trip time of the current connection, rounded
42
- * to the nearest multiple of 25 milliseconds
43
- */
44
- rtt: INetworkInformation['rtt'] | undefined;
45
- /**
46
- * @desc {true} if the user has set a reduced data usage option on the user agent.
47
- */
48
- saveData: INetworkInformation['saveData'] | undefined;
49
- /**
50
- * @desc The type of connection a device is using to communicate with the network.
51
- * It will be one of the following values:
52
- * - bluetooth
53
- * - cellular
54
- * - ethernet
55
- * - none
56
- * - wifi
57
- * - wimax
58
- * - other
59
- * - unknown
60
- */
61
- type: INetworkInformation['type'] | undefined;
62
- }
63
- export declare const isBrowser: boolean;
64
- export declare const isNavigator: boolean;
65
- export type IHookStateInitialSetter<S> = () => S;
66
- export type IHookStateInitAction<S> = S | IHookStateInitialSetter<S>;
67
- export declare function useNetworkState(initialState?: IHookStateInitAction<IUseNetworkState>): IUseNetworkState;
1
+ export interface INetworkInformation extends EventTarget {
2
+ readonly downlink: number;
3
+ readonly downlinkMax: number;
4
+ readonly effectiveType: 'slow-2g' | '2g' | '3g' | '4g';
5
+ readonly rtt: number;
6
+ readonly saveData: boolean;
7
+ readonly type: 'bluetooth' | 'cellular' | 'ethernet' | 'none' | 'wifi' | 'wimax' | 'other' | 'unknown';
8
+ onChange: (event: Event) => void;
9
+ }
10
+ export interface IUseNetworkState {
11
+ /**
12
+ * @desc Whether browser connected to the network or not.
13
+ */
14
+ online: boolean | undefined;
15
+ /**
16
+ * @desc Previous value of `online` property. Helps to identify if browser
17
+ * just connected or lost connection.
18
+ */
19
+ previous: boolean | undefined;
20
+ /**
21
+ * @desc The {Date} object pointing to the moment when state change occurred.
22
+ */
23
+ since: Date | undefined;
24
+ /**
25
+ * @desc Effective bandwidth estimate in megabits per second, rounded to the
26
+ * nearest multiple of 25 kilobits per seconds.
27
+ */
28
+ downlink: INetworkInformation['downlink'] | undefined;
29
+ /**
30
+ * @desc Maximum downlink speed, in megabits per second (Mbps), for the
31
+ * underlying connection technology
32
+ */
33
+ downlinkMax: INetworkInformation['downlinkMax'] | undefined;
34
+ /**
35
+ * @desc Effective type of the connection meaning one of 'slow-2g', '2g', '3g', or '4g'.
36
+ * This value is determined using a combination of recently observed round-trip time
37
+ * and downlink values.
38
+ */
39
+ effectiveType: INetworkInformation['effectiveType'] | undefined;
40
+ /**
41
+ * @desc Estimated effective round-trip time of the current connection, rounded
42
+ * to the nearest multiple of 25 milliseconds
43
+ */
44
+ rtt: INetworkInformation['rtt'] | undefined;
45
+ /**
46
+ * @desc {true} if the user has set a reduced data usage option on the user agent.
47
+ */
48
+ saveData: INetworkInformation['saveData'] | undefined;
49
+ /**
50
+ * @desc The type of connection a device is using to communicate with the network.
51
+ * It will be one of the following values:
52
+ * - bluetooth
53
+ * - cellular
54
+ * - ethernet
55
+ * - none
56
+ * - wifi
57
+ * - wimax
58
+ * - other
59
+ * - unknown
60
+ */
61
+ type: INetworkInformation['type'] | undefined;
62
+ }
63
+ export declare const isBrowser: boolean;
64
+ export declare const isNavigator: boolean;
65
+ export type IHookStateInitialSetter<S> = () => S;
66
+ export type IHookStateInitAction<S> = S | IHookStateInitialSetter<S>;
67
+ export declare function useNetworkState(initialState?: IHookStateInitAction<IUseNetworkState>): IUseNetworkState;
@@ -1,41 +1,41 @@
1
- import React from 'react';
2
- export const isBrowser = typeof window !== 'undefined';
3
- export const isNavigator = typeof navigator !== 'undefined';
4
- const nav = isNavigator ? navigator : undefined;
5
- const conn = nav && (nav.connection || nav.mozConnection || nav.webkitConnection);
6
- function getConnectionState(previousState) {
7
- const online = nav?.onLine;
8
- const previousOnline = previousState?.online;
9
- return {
10
- online,
11
- previous: previousOnline,
12
- since: online !== previousOnline ? new Date() : previousState?.since,
13
- downlink: conn?.downlink,
14
- downlinkMax: conn?.downlinkMax,
15
- effectiveType: conn?.effectiveType,
16
- rtt: conn?.rtt,
17
- saveData: conn?.saveData,
18
- type: conn?.type,
19
- };
20
- }
21
- export function useNetworkState(initialState) {
22
- const [state, setState] = React.useState(initialState ?? getConnectionState);
23
- React.useEffect(() => {
24
- const handleStateChange = () => {
25
- setState(getConnectionState);
26
- };
27
- window.addEventListener('online', handleStateChange, { passive: true });
28
- window.addEventListener('offline', handleStateChange, { passive: true });
29
- if (conn) {
30
- window.addEventListener('change', handleStateChange, { passive: true });
31
- }
32
- return () => {
33
- window.removeEventListener('online', handleStateChange);
34
- window.removeEventListener('offline', handleStateChange);
35
- if (conn) {
36
- window.removeEventListener('change', handleStateChange);
37
- }
38
- };
39
- }, []);
40
- return state;
41
- }
1
+ import React from 'react';
2
+ export const isBrowser = typeof window !== 'undefined';
3
+ export const isNavigator = typeof navigator !== 'undefined';
4
+ const nav = isNavigator ? navigator : undefined;
5
+ const conn = nav && (nav.connection || nav.mozConnection || nav.webkitConnection);
6
+ function getConnectionState(previousState) {
7
+ const online = nav?.onLine;
8
+ const previousOnline = previousState?.online;
9
+ return {
10
+ online,
11
+ previous: previousOnline,
12
+ since: online !== previousOnline ? new Date() : previousState?.since,
13
+ downlink: conn?.downlink,
14
+ downlinkMax: conn?.downlinkMax,
15
+ effectiveType: conn?.effectiveType,
16
+ rtt: conn?.rtt,
17
+ saveData: conn?.saveData,
18
+ type: conn?.type,
19
+ };
20
+ }
21
+ export function useNetworkState(initialState) {
22
+ const [state, setState] = React.useState(initialState ?? getConnectionState);
23
+ React.useEffect(() => {
24
+ const handleStateChange = () => {
25
+ setState(getConnectionState);
26
+ };
27
+ window.addEventListener('online', handleStateChange, { passive: true });
28
+ window.addEventListener('offline', handleStateChange, { passive: true });
29
+ if (conn) {
30
+ window.addEventListener('change', handleStateChange, { passive: true });
31
+ }
32
+ return () => {
33
+ window.removeEventListener('online', handleStateChange);
34
+ window.removeEventListener('offline', handleStateChange);
35
+ if (conn) {
36
+ window.removeEventListener('change', handleStateChange);
37
+ }
38
+ };
39
+ }, []);
40
+ return state;
41
+ }
@@ -1,4 +1,4 @@
1
- export declare const useShortcuts: (shortcutKeys: string[] | string, callback: (keys: string) => void, options?: {
2
- overrideSystem: boolean;
3
- }) => void;
4
- export declare function disabledEventPropagation(e: KeyboardEvent): void;
1
+ export declare const useShortcuts: (shortcutKeys: string[] | string, callback: (keys: string) => void, options?: {
2
+ overrideSystem: boolean;
3
+ }) => void;
4
+ export declare function disabledEventPropagation(e: KeyboardEvent): void;