@capsitech/react-utilities 0.1.2
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.
- package/README.md +3 -0
- package/lib/Hooks/index.d.ts +45 -0
- package/lib/Hooks/index.js +98 -0
- package/lib/Hooks/useInfiniteScroll.d.ts +7 -0
- package/lib/Hooks/useInfiniteScroll.js +22 -0
- package/lib/Hooks/useNetworkState.d.ts +67 -0
- package/lib/Hooks/useNetworkState.js +41 -0
- package/lib/Hooks/useShortcuts.d.ts +4 -0
- package/lib/Hooks/useShortcuts.js +91 -0
- package/lib/Utilities/ApiUtility.axios.d.ts +60 -0
- package/lib/Utilities/ApiUtility.axios.js +305 -0
- package/lib/Utilities/BrowserInfo.d.ts +74 -0
- package/lib/Utilities/BrowserInfo.js +153 -0
- package/lib/Utilities/Countries.d.ts +14 -0
- package/lib/Utilities/Countries.js +290 -0
- package/lib/Utilities/CustomEventEmitter.d.ts +12 -0
- package/lib/Utilities/CustomEventEmitter.js +30 -0
- package/lib/Utilities/FastCompare.d.ts +1 -0
- package/lib/Utilities/FastCompare.js +128 -0
- package/lib/Utilities/HideablePromise.d.ts +5 -0
- package/lib/Utilities/HideablePromise.js +10 -0
- package/lib/Utilities/LoadScripts.d.ts +9 -0
- package/lib/Utilities/LoadScripts.js +51 -0
- package/lib/Utilities/MTDFraudPrevention.d.ts +28 -0
- package/lib/Utilities/MTDFraudPrevention.js +157 -0
- package/lib/Utilities/Nationalities.d.ts +5 -0
- package/lib/Utilities/Nationalities.js +245 -0
- package/lib/Utilities/RouteUtils.d.ts +120 -0
- package/lib/Utilities/RouteUtils.js +206 -0
- package/lib/Utilities/SuspenseRoute.d.ts +7 -0
- package/lib/Utilities/SuspenseRoute.js +10 -0
- package/lib/Utilities/TimeZones.d.ts +10 -0
- package/lib/Utilities/TimeZones.js +1069 -0
- package/lib/Utilities/Types.d.ts +19 -0
- package/lib/Utilities/Types.js +1 -0
- package/lib/Utilities/Utils.d.ts +174 -0
- package/lib/Utilities/Utils.js +331 -0
- package/lib/Utilities/dayjs.d.ts +18 -0
- package/lib/Utilities/dayjs.js +56 -0
- package/lib/Utilities/index.d.ts +15 -0
- package/lib/Utilities/index.js +15 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +2 -0
- package/package.json +92 -0
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { matchPath, Route } from "react-router-dom";
|
|
3
|
+
import { SuspenseRoute } from "./SuspenseRoute";
|
|
4
|
+
export var FeatureStatus;
|
|
5
|
+
(function (FeatureStatus) {
|
|
6
|
+
FeatureStatus[FeatureStatus["placeholder"] = 0] = "placeholder";
|
|
7
|
+
FeatureStatus[FeatureStatus["alpha"] = 1] = "alpha";
|
|
8
|
+
FeatureStatus[FeatureStatus["beta"] = 2] = "beta";
|
|
9
|
+
FeatureStatus[FeatureStatus["release"] = 3] = "release";
|
|
10
|
+
})(FeatureStatus || (FeatureStatus = {}));
|
|
11
|
+
class RouteUtilsBase {
|
|
12
|
+
hasValidRole = (rolesRequired, rolesExists) => {
|
|
13
|
+
if (!rolesRequired || !rolesExists)
|
|
14
|
+
return true;
|
|
15
|
+
return rolesRequired.some((r) => rolesExists.includes(r));
|
|
16
|
+
};
|
|
17
|
+
hasValidFeatures = (featuresRequired, featuresExists) => {
|
|
18
|
+
if (featuresRequired === undefined || featuresRequired === null)
|
|
19
|
+
return true;
|
|
20
|
+
return featuresExists && featuresRequired.some((r) => featuresExists.includes(r));
|
|
21
|
+
};
|
|
22
|
+
hasValidBusinessType = (typesRequired, businessType) => {
|
|
23
|
+
if (typesRequired === undefined ||
|
|
24
|
+
typesRequired === null ||
|
|
25
|
+
businessType === undefined ||
|
|
26
|
+
businessType === null)
|
|
27
|
+
return true;
|
|
28
|
+
return typesRequired?.some((v) => v === businessType);
|
|
29
|
+
};
|
|
30
|
+
hasValidUserConfig = (configRequired, configExists) => {
|
|
31
|
+
if (configRequired == undefined || configRequired === null || !configExists)
|
|
32
|
+
return true;
|
|
33
|
+
return Object.keys(configExists).some((key) => {
|
|
34
|
+
return configExists[key] && configRequired?.some((v) => v === key);
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
hasValidUserParam = (paramsRequired, paramsExists) => {
|
|
38
|
+
if (paramsRequired == undefined || paramsRequired === null || !paramsExists)
|
|
39
|
+
return true;
|
|
40
|
+
return Object.keys(paramsExists).some((key) => {
|
|
41
|
+
return paramsExists[key] && paramsExists[key]?.enabled && paramsRequired?.some((v) => v === key);
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
hasValidTags = (tagsRequired, tagsExists) => {
|
|
45
|
+
if (!tagsRequired || !tagsExists)
|
|
46
|
+
return true;
|
|
47
|
+
return Object.keys(tagsExists).some((key) => {
|
|
48
|
+
return tagsExists[key] && tagsRequired?.some((v) => v === key);
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
isValidLink = (link, rolesExists, featuresExists, businessType, configExists, paramsExists, tagsExists) => {
|
|
52
|
+
//check for user role
|
|
53
|
+
if (!this.hasValidRole(link.roles, rolesExists))
|
|
54
|
+
return false;
|
|
55
|
+
//check for app feature
|
|
56
|
+
if (!this.hasValidFeatures(link.features, featuresExists))
|
|
57
|
+
return false;
|
|
58
|
+
//check for user config
|
|
59
|
+
if (!this.hasValidUserConfig(link.userConfig, configExists))
|
|
60
|
+
return false;
|
|
61
|
+
//check for user params
|
|
62
|
+
if (!this.hasValidUserParam(link.userParams, paramsExists))
|
|
63
|
+
return false;
|
|
64
|
+
//check for business type
|
|
65
|
+
if (!this.hasValidBusinessType(link.businessTypes, businessType))
|
|
66
|
+
return false;
|
|
67
|
+
//check for tag
|
|
68
|
+
if (!this.hasValidTags(link.tags, tagsExists))
|
|
69
|
+
return false;
|
|
70
|
+
return true;
|
|
71
|
+
};
|
|
72
|
+
getPath = (link, trimUrl) => {
|
|
73
|
+
if (trimUrl && link?.url) {
|
|
74
|
+
return link.url.replace(trimUrl, '');
|
|
75
|
+
}
|
|
76
|
+
return link.url;
|
|
77
|
+
};
|
|
78
|
+
getRoutesFromGroups = (groups, SecureRoute, roles, features, businessType, userConfig, userParams, tags, trimUrl) => {
|
|
79
|
+
const routes = [];
|
|
80
|
+
for (const group of groups) {
|
|
81
|
+
routes.push(this.getRoutesFromGroup(group.links, SecureRoute, roles, features, businessType, userConfig, userParams, tags, trimUrl));
|
|
82
|
+
}
|
|
83
|
+
return routes;
|
|
84
|
+
};
|
|
85
|
+
getRoutesFromGroup = (links, SecureRoute, roles, features, businessType, userConfig, userParams, tags, trimUrl) => {
|
|
86
|
+
const routes = [];
|
|
87
|
+
for (const link of links) {
|
|
88
|
+
if (link.component) {
|
|
89
|
+
//check for valid link
|
|
90
|
+
if (!this.isValidLink(link, roles, features, businessType, userConfig, userParams, tags))
|
|
91
|
+
continue;
|
|
92
|
+
//const exact = typeof link.exact === 'undefined' ? true : link.exact;
|
|
93
|
+
routes.push(link.roles || link.features ? (_jsx(Route, { path: this.getPath(link, trimUrl), element: _jsx(SecureRoute, { component: link.component, roles: link.roles }) }, link.key)) : (_jsx(Route, { path: this.getPath(link, trimUrl), element: _jsx(SuspenseRoute, { children: link.component }) }, link.key)));
|
|
94
|
+
}
|
|
95
|
+
//add nested links
|
|
96
|
+
if (link.links) {
|
|
97
|
+
routes.push(...this.getRoutesFromGroup(link.links, SecureRoute, roles, features, businessType, userConfig, userParams, tags, trimUrl));
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return routes;
|
|
101
|
+
};
|
|
102
|
+
getLinksFromGroups = (links, roles, features, businessType, userConfig, userParams, currentPath, params, tags) => {
|
|
103
|
+
const groups = [];
|
|
104
|
+
for (const link of links) {
|
|
105
|
+
//check for valid link
|
|
106
|
+
if (!this.isValidLink(link, roles, features, businessType, userConfig, userParams, tags))
|
|
107
|
+
continue;
|
|
108
|
+
groups.push(link);
|
|
109
|
+
//add nested links
|
|
110
|
+
if (link.links) {
|
|
111
|
+
link.links = this.getLinksFromGroup(link.links, roles, features, businessType, userConfig, userParams, currentPath, params, tags);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return groups;
|
|
115
|
+
};
|
|
116
|
+
getLinksFromGroup = (links, roles, features, businessType, userConfig, userParams, currentPath, params, tags) => {
|
|
117
|
+
const routes = [];
|
|
118
|
+
for (const link of links) {
|
|
119
|
+
//check for valid link
|
|
120
|
+
if (!this.isValidLink(link, roles, features, businessType, userConfig, userParams, tags))
|
|
121
|
+
continue;
|
|
122
|
+
let url = link.url, activeUrl = link.activeUrl;
|
|
123
|
+
if (url && params) {
|
|
124
|
+
for (const key in params) {
|
|
125
|
+
url = url?.replace(`:${key}`, params[key]);
|
|
126
|
+
if (activeUrl)
|
|
127
|
+
activeUrl = activeUrl.replace(`:${key}`, params[key]);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
link.url = url;
|
|
131
|
+
link.activeUrl = activeUrl;
|
|
132
|
+
routes.push(link);
|
|
133
|
+
//add nested links
|
|
134
|
+
if (link.links) {
|
|
135
|
+
if (currentPath && link.activeUrl) {
|
|
136
|
+
link.isExpanded = link.url === currentPath || currentPath.indexOf(link.activeUrl) === 0;
|
|
137
|
+
}
|
|
138
|
+
link.links = this.getLinksFromGroup(link.links, roles, features, businessType, userConfig, userParams, currentPath, params, tags);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return routes;
|
|
142
|
+
};
|
|
143
|
+
getRoutesFromLinks = (links, SecureRoute, roles, features, businessType, userConfig, userParams, tags, trimUrl) => {
|
|
144
|
+
const routes = [];
|
|
145
|
+
for (const link of links) {
|
|
146
|
+
if (link.component) {
|
|
147
|
+
//check for valid link
|
|
148
|
+
if (!this.isValidLink(link, roles, features, businessType, userConfig, userParams, tags))
|
|
149
|
+
continue;
|
|
150
|
+
//const exact = typeof link.exact === 'undefined' ? true : link.exact;
|
|
151
|
+
routes.push(link.roles ? (_jsx(Route, { path: this.getPath(link, trimUrl), element: _jsx(SecureRoute, { component: link.component, roles: link.roles }) }, link.key)) : (_jsx(Route, { path: this.getPath(link, trimUrl) || '#', element: _jsx(SuspenseRoute, { children: link.component }) }, link.key)));
|
|
152
|
+
}
|
|
153
|
+
//add nested links
|
|
154
|
+
if (link.links) {
|
|
155
|
+
routes.push(...this.getRoutesFromLinks(link.links, SecureRoute, roles, features, businessType, userConfig, userParams, tags, trimUrl));
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return routes;
|
|
159
|
+
};
|
|
160
|
+
getLinksFromLinks = (links, roles, features, businessType, userConfig, userParams, currentPath, params, tags) => {
|
|
161
|
+
const routes = [];
|
|
162
|
+
for (const link of links) {
|
|
163
|
+
//check for valid link
|
|
164
|
+
if (!this.isValidLink(link, roles, features, businessType, userConfig, userParams, tags))
|
|
165
|
+
continue;
|
|
166
|
+
let url = link.url, activeUrl = link.activeUrl;
|
|
167
|
+
if (url && params) {
|
|
168
|
+
for (const key in params) {
|
|
169
|
+
url = url?.replace(`:${key}`, params[key]);
|
|
170
|
+
if (activeUrl)
|
|
171
|
+
activeUrl = activeUrl.replace(`:${key}`, params[key]);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
link.url = url;
|
|
175
|
+
link.activeUrl = activeUrl;
|
|
176
|
+
routes.push(link);
|
|
177
|
+
//add nested links
|
|
178
|
+
if (link.links) {
|
|
179
|
+
if (currentPath && link.activeUrl) {
|
|
180
|
+
link.isExpanded = link.url === currentPath || currentPath.indexOf(link.activeUrl) === 0;
|
|
181
|
+
}
|
|
182
|
+
link.links = this.getLinksFromLinks(link.links, roles, features, businessType, userConfig, userParams, currentPath, params, tags);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return routes;
|
|
186
|
+
};
|
|
187
|
+
getSelectedNavKey = (pagesGroup, currentPath) => {
|
|
188
|
+
let selectedLink = undefined;
|
|
189
|
+
for (const group of pagesGroup) {
|
|
190
|
+
selectedLink = group.links
|
|
191
|
+
.flatMap((l) => {
|
|
192
|
+
if (l.links)
|
|
193
|
+
return l.links;
|
|
194
|
+
else
|
|
195
|
+
return [l];
|
|
196
|
+
})
|
|
197
|
+
.find((l) => l.url === currentPath ||
|
|
198
|
+
(l.activeUrls && l.activeUrls.some((la) => matchPath(la, currentPath))) ||
|
|
199
|
+
matchPath(l.url, currentPath));
|
|
200
|
+
if (selectedLink)
|
|
201
|
+
break;
|
|
202
|
+
}
|
|
203
|
+
return selectedLink?.key;
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
export const RouteUtils = new RouteUtilsBase();
|
|
@@ -0,0 +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 {};
|
|
@@ -0,0 +1,10 @@
|
|
|
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
|
+
return isSuspenseComponent(Component) ? (_jsx(React.Suspense, { fallback: _jsx(FallbackComponent, { label: label }), children: _jsx(Component, {}) })) : (_jsx(Component, {}));
|
|
10
|
+
};
|