ag-common 0.0.125 → 0.0.129

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 (30) hide show
  1. package/dist/api/helpers/api.d.ts +7 -1
  2. package/dist/api/helpers/api.js +9 -8
  3. package/dist/common/helpers/object.d.ts +20 -0
  4. package/dist/common/helpers/object.js +41 -1
  5. package/dist/ui/components/Chevron/index.d.ts +1 -0
  6. package/dist/ui/components/Chevron/index.js +6 -6
  7. package/dist/ui/components/DropdownList/index.d.ts +1 -0
  8. package/dist/ui/components/HeadersRaw/index.d.ts +1 -0
  9. package/dist/ui/components/Icon/index.js +1 -1
  10. package/dist/ui/components/Loader/index.d.ts +1 -0
  11. package/dist/ui/components/LoginButton/index.d.ts +1 -0
  12. package/dist/ui/components/LogoutButton/index.d.ts +1 -0
  13. package/dist/ui/components/Sidebar/index.d.ts +13 -0
  14. package/dist/ui/components/Sidebar/index.js +110 -0
  15. package/dist/ui/components/Table/index.d.ts +1 -0
  16. package/dist/ui/components/TextEdit/CheckboxEdit.d.ts +1 -0
  17. package/dist/ui/components/TextEdit/ColourEdit.d.ts +1 -0
  18. package/dist/ui/components/TextEdit/ListboxEdit.d.ts +1 -0
  19. package/dist/ui/components/TextEdit/TextEdit.d.ts +1 -0
  20. package/dist/ui/components/TextEdit/images.d.ts +1 -0
  21. package/dist/ui/components/Toast/index.d.ts +1 -0
  22. package/dist/ui/components/UserImage/index.d.ts +1 -0
  23. package/dist/ui/components/index.d.ts +1 -0
  24. package/dist/ui/components/index.js +1 -0
  25. package/dist/ui/helpers/callOpenApi/cached.js +10 -8
  26. package/dist/ui/helpers/callOpenApi/hook.js +2 -1
  27. package/dist/ui/helpers/lang.d.ts +1 -0
  28. package/dist/ui/helpers/useQueryString.d.ts +21 -6
  29. package/dist/ui/helpers/useQueryString.js +37 -31
  30. package/package.json +1 -1
@@ -2,7 +2,13 @@ import { APIGatewayProxyResult, DYNAMOKEYS } from '../types';
2
2
  export declare const returnCode: <T>(statusCode: number, body?: T | undefined, extraHeaders?: {
3
3
  [a: string]: string;
4
4
  } | undefined, fullSiteUrl?: string | undefined) => APIGatewayProxyResult;
5
- export declare const stripPKs: <T>(r: T, excludePK?: boolean) => T;
5
+ /**
6
+ * strip all dynamo generated keys. can optionally keep PK
7
+ * @param record
8
+ * @param keepPk if true, will keep PK. default true
9
+ * @returns stripped record
10
+ */
11
+ export declare const stripPKs: <T>(record: T, keepPk?: boolean) => T;
6
12
  export declare const generateDynamoPKS: ({ type, L1, L2, L3, L4, L5, L6, additionalPKValues, }: {
7
13
  type: string;
8
14
  L1: string;
@@ -33,21 +33,22 @@ const returnCode = (statusCode, body, extraHeaders, fullSiteUrl) => {
33
33
  };
34
34
  };
35
35
  exports.returnCode = returnCode;
36
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
37
- const stripPKs = (r,
38
36
  /**
39
- * default true
40
- * if true, will keep PK
37
+ * strip all dynamo generated keys. can optionally keep PK
38
+ * @param record
39
+ * @param keepPk if true, will keep PK. default true
40
+ * @returns stripped record
41
41
  */
42
- excludePK = true) => {
43
- if (!r) {
42
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
43
+ const stripPKs = (record, keepPk = true) => {
44
+ if (!record) {
44
45
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
45
46
  return null;
46
47
  }
47
48
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
48
49
  // @ts-ignore
49
- const { PK, PK1, PK2, PK3, PK4, PK5, L1, L2, L3, L4, L5, L6 } = r, rest = __rest(r, ["PK", "PK1", "PK2", "PK3", "PK4", "PK5", "L1", "L2", "L3", "L4", "L5", "L6"]);
50
- if (!excludePK) {
50
+ const { PK, PK1, PK2, PK3, PK4, PK5, L1, L2, L3, L4, L5, L6 } = record, rest = __rest(record, ["PK", "PK1", "PK2", "PK3", "PK4", "PK5", "L1", "L2", "L3", "L4", "L5", "L6"]);
51
+ if (keepPk) {
51
52
  //@ts-ignore
52
53
  rest.PK = PK;
53
54
  }
@@ -15,4 +15,24 @@ export interface IArrayType<T> {
15
15
  export declare function objectToArray<T>(obj: {
16
16
  [a: string]: T;
17
17
  }): IArrayType<T>[];
18
+ /**
19
+ * Recursively alphabetically sort an object
20
+ * @param object
21
+ * @param depthLeft
22
+ * @returns
23
+ */
18
24
  export declare const objectAlphaSort: (object: any, depthLeft?: number) => object;
25
+ /**
26
+ * Convert a random object type to a record<string,string>
27
+ * @param entries Could be URLSearchParams
28
+ * @returns
29
+ */
30
+ export declare function paramsToObject(entries: any): Record<string, string>;
31
+ /**
32
+ * stringify an object of key values. Could be used to stringify a querystring url
33
+ * @param obj
34
+ * @param joinKeyValue
35
+ * @param joinKeys
36
+ * @returns
37
+ */
38
+ export declare function objectToString(obj: Record<string, string>, joinKeyValue: string, joinKeys: string): string;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.objectAlphaSort = exports.objectToArray = exports.getObjectKeysAsNumber = exports.objectKeysToLowerCase = exports.isJson = exports.tryJsonParse = void 0;
3
+ exports.objectToString = exports.paramsToObject = exports.objectAlphaSort = exports.objectToArray = exports.getObjectKeysAsNumber = exports.objectKeysToLowerCase = exports.isJson = exports.tryJsonParse = void 0;
4
+ const _1 = require(".");
4
5
  const tryJsonParse = (str, defaultValue) => {
5
6
  if (!str) {
6
7
  return null;
@@ -52,6 +53,12 @@ function objectToArray(obj) {
52
53
  return ret;
53
54
  }
54
55
  exports.objectToArray = objectToArray;
56
+ /**
57
+ * Recursively alphabetically sort an object
58
+ * @param object
59
+ * @param depthLeft
60
+ * @returns
61
+ */
55
62
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
56
63
  const objectAlphaSort = (object, depthLeft = -1) => {
57
64
  if (depthLeft === 0) {
@@ -74,3 +81,36 @@ const objectAlphaSort = (object, depthLeft = -1) => {
74
81
  }
75
82
  };
76
83
  exports.objectAlphaSort = objectAlphaSort;
84
+ /**
85
+ * Convert a random object type to a record<string,string>
86
+ * @param entries Could be URLSearchParams
87
+ * @returns
88
+ */
89
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
90
+ function paramsToObject(entries) {
91
+ const result = {};
92
+ for (const [key, value] of entries) {
93
+ result[key] = value;
94
+ }
95
+ return result;
96
+ }
97
+ exports.paramsToObject = paramsToObject;
98
+ /**
99
+ * stringify an object of key values. Could be used to stringify a querystring url
100
+ * @param obj
101
+ * @param joinKeyValue
102
+ * @param joinKeys
103
+ * @returns
104
+ */
105
+ function objectToString(obj, joinKeyValue, joinKeys) {
106
+ let ret = '';
107
+ if (!obj || Object.keys(obj).length === 0) {
108
+ return ret;
109
+ }
110
+ Object.entries(obj).forEach(([key, value]) => {
111
+ ret += `${joinKeys}${key}${joinKeyValue}${value}`;
112
+ });
113
+ ret = (0, _1.trim)(ret, joinKeyValue);
114
+ return ret;
115
+ }
116
+ exports.objectToString = objectToString;
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  export declare const Chevron: ({ width, className, colour, onToggle, point, }: {
2
3
  /**
3
4
  * default right
@@ -18,25 +18,25 @@ const IconStyled = (0, styled_components_1.default)(Icon_1.Icon) `
18
18
  margin: 0;
19
19
  padding: 0;
20
20
  `;
21
- const ChevronIcon = (react_1.default.createElement("svg", { xmlns: "http://www.w3.org/2000/svg", viewBox: "0 -256 1792 1792" },
22
- react_1.default.createElement("path", { d: "M1679.339 301.56q0 53-37 90l-651 651q-38 38-91 38-54 0-90-38l-651-651q-38-36-38-90 0-53 38-91l74-75q39-37 91-37 53 0 90 37l486 486 486-486q37-37 90-37 52 0 91 37l75 75q37 39 37 91z" })));
21
+ const ChevronIcon = (react_1.default.createElement("svg", { width: "24", height: "24", fill: "none", xmlns: "http://www.w3.org/2000/svg" },
22
+ react_1.default.createElement("path", { d: "M10.294 9.698a.988.988 0 0 1 0-1.407 1.01 1.01 0 0 1 1.419 0l2.965 2.94a1.09 1.09 0 0 1 0 1.548l-2.955 2.93a1.01 1.01 0 0 1-1.638-.322.988.988 0 0 1 .218-1.085l2.318-2.297-2.327-2.307Z", fill: "inherit", className: "_14TyY7wB85QoVU" })));
23
23
  const Chevron = ({ width = '1.2rem', className, colour = 'black', onToggle, point = 'right', }) => {
24
24
  let rotate = 0;
25
25
  switch (point) {
26
26
  case 'down': {
27
- rotate = 180;
27
+ rotate = 270;
28
28
  break;
29
29
  }
30
30
  case 'left': {
31
- rotate = 270;
31
+ rotate = 0;
32
32
  break;
33
33
  }
34
34
  case 'up': {
35
- rotate = 0;
35
+ rotate = 90;
36
36
  break;
37
37
  }
38
38
  case 'right': {
39
- rotate = 90;
39
+ rotate = 180;
40
40
  }
41
41
  }
42
42
  return (react_1.default.createElement(SChevron, { className: className, onClick: () => onToggle === null || onToggle === void 0 ? void 0 : onToggle(), onTouchStart: () => onToggle === null || onToggle === void 0 ? void 0 : onToggle(), onKeyPress: (e) => e.key === 'Enter' && (onToggle === null || onToggle === void 0 ? void 0 : onToggle()) },
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  export declare function DropdownList<T>({ options, value, onChange, placeholder, className, renderF, children, shadow, }: {
2
3
  options: T[];
3
4
  value?: T;
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  export declare const HeadersRaw: ({ title, image, SiteShort, FullSiteUrl, siteDesc, }: {
2
3
  title?: string | undefined;
3
4
  image?: string | undefined;
@@ -27,12 +27,12 @@ const react_1 = __importDefault(require("react"));
27
27
  const styled_components_1 = __importStar(require("styled-components"));
28
28
  const common_1 = require("../../styles/common");
29
29
  exports.IconF = styled_components_1.default.span `
30
+ transition: all 200ms;
30
31
  display: flex;
31
32
  justify-content: center;
32
33
  align-items: center;
33
34
  font-size: 2rem;
34
35
  padding: ${({ padding }) => padding || '0'};
35
- transition: background-color 200ms;
36
36
  margin: ${({ margin }) => (!margin ? 'unset' : margin)};
37
37
  cursor: ${({ disabled, canHover }) => disabled || !canHover ? 'inherit' : 'pointer'};
38
38
  > svg {
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  export declare const Loader: ({ name }: {
2
3
  name: string;
3
4
  }) => JSX.Element;
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  export declare const LoginButton: ({ className, text, invert, savePath, loginPath, }: {
2
3
  invert?: boolean | undefined;
3
4
  text: string;
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  export declare const LogoutButton: ({ className, invert, logout, }: {
2
3
  invert?: boolean | undefined;
3
4
  className?: string | undefined;
@@ -0,0 +1,13 @@
1
+ /// <reference types="react" />
2
+ export declare const Sidebar: ({ children, className, key, cookieDocument, }: {
3
+ children: any;
4
+ className?: string | undefined;
5
+ /**
6
+ * used for localstorage. default 'sidebar'
7
+ */
8
+ key?: string | undefined;
9
+ /**
10
+ * optionally pass in SSR cookiedocument
11
+ */
12
+ cookieDocument?: string | undefined;
13
+ }) => JSX.Element;
@@ -0,0 +1,110 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Sidebar = void 0;
7
+ const react_1 = __importDefault(require("react"));
8
+ const styled_components_1 = __importDefault(require("styled-components"));
9
+ const cookie_1 = require("../../helpers/cookie");
10
+ const common_1 = require("../../styles/common");
11
+ const Chevron_1 = require("../Chevron");
12
+ const Base = styled_components_1.default.div `
13
+ position: relative;
14
+ transition: all 200ms;
15
+ width: 15rem;
16
+ border-right: solid 1px #ccc;
17
+ margin-right: 1rem;
18
+ padding-left: 0.5rem;
19
+
20
+ &[data-open='false'] {
21
+ width: 0.5rem;
22
+ background-color: rgba(0, 0, 0, 0.1);
23
+ cursor: pointer;
24
+ &:hover,
25
+ &:hover [data-hover='true'] {
26
+ background-color: #ccc;
27
+ }
28
+ &:hover {
29
+ border-right: solid 1px #999;
30
+ }
31
+ }
32
+ ${common_1.NoTextSelect};
33
+
34
+ &:hover {
35
+ [data-content] {
36
+ left: 1rem;
37
+ }
38
+ }
39
+ `;
40
+ const ContentBlock = styled_components_1.default.div `
41
+ left: -18rem;
42
+ transition left 200ms;
43
+ &[data-open='false'] {
44
+ position: absolute;
45
+ top: 0;
46
+ z-index: 1;
47
+ width: 15rem;
48
+ }
49
+ `;
50
+ const Content = styled_components_1.default.div `
51
+ display: flex;
52
+ flex-flow: column;
53
+ width: 100%;
54
+ height: 100%;
55
+ margin-top: 2rem;
56
+
57
+ &[data-open='false'] {
58
+ padding: 1rem;
59
+ background-color: white;
60
+ ${(0, common_1.Shadow)()};
61
+ border-radius: 1rem;
62
+ }
63
+ `;
64
+ const Hamburger = styled_components_1.default.div `
65
+ position: absolute;
66
+ transition: all 200ms;
67
+ &[data-open='false'] {
68
+ top: 0.5rem;
69
+ left: 0.25rem;
70
+ }
71
+ &[data-open='true'] {
72
+ top: 0.5rem;
73
+ right: -0.75rem;
74
+ }
75
+ display: flex;
76
+ justify-content: center;
77
+ align-items: center;
78
+ width: 1.5rem;
79
+ height: 1.5rem;
80
+ background-color: rgba(255, 255, 255, 0.9);
81
+ &:hover {
82
+ background-color: rgba(0, 0, 0, 0.1);
83
+ }
84
+ border-radius: 50%;
85
+ border: solid 1px rgba(0, 0, 0, 0.5);
86
+
87
+ cursor: pointer;
88
+ `;
89
+ const ChevronStyled = (0, styled_components_1.default)(Chevron_1.Chevron) `
90
+ svg {
91
+ fill: #555;
92
+ }
93
+ `;
94
+ const Sidebar = ({ children, className, key = 'sidebar', cookieDocument, }) => {
95
+ const [openRaw, setOpenRaw] = (0, cookie_1.useCookie)({
96
+ key,
97
+ defaultValue: 'false',
98
+ cookieDocument: cookieDocument,
99
+ });
100
+ const open = openRaw === 'true';
101
+ const setOpen = (o) => setOpenRaw(o.toString());
102
+ return (react_1.default.createElement(Base, { className: className, "data-open": open, onClick: () => !open && setOpen(true), "data-hover": true },
103
+ react_1.default.createElement(Hamburger, { "data-open": open, onClick: () => setOpen(!open), "data-hover": true },
104
+ react_1.default.createElement(ChevronStyled, { point: open ? 'right' : 'left', width: "100%" })),
105
+ react_1.default.createElement(ContentBlock, { "data-content": true, "data-open": open },
106
+ react_1.default.createElement(Content, { "data-open": open, onClick: (e) => {
107
+ e.stopPropagation();
108
+ } }, children))));
109
+ };
110
+ exports.Sidebar = Sidebar;
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  export interface TableItem {
2
3
  content: JSX.Element;
3
4
  groupTitle: string;
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  export declare const CheckboxEdit: ({ defaultValue, onSubmit, noGrow, }: {
2
3
  defaultValue: boolean;
3
4
  onSubmit: (val: boolean) => void;
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  export declare const ColourEdit: ({ defaultValue, onSubmit, }: {
2
3
  defaultValue: string;
3
4
  onSubmit: (val: string) => void;
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  export declare const ListboxEdit: ({ defaultValue, onSubmit, values, }: {
2
3
  defaultValue: string;
3
4
  onSubmit: (val: string[]) => void;
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  import { StyledComponent } from 'styled-components';
2
3
  export declare const ValueReadonly: StyledComponent<"div", any, {}, never>;
3
4
  export declare const TextEdit: ({ defaultValue, defaultEditing, onSubmit, disableEdit, placeholder, onEditingChange, onClickOutsideWithNoValue, onClickNotEditing, className, singleLine, }: {
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  export declare const UndoIcon: () => JSX.Element;
2
3
  export declare const PencilIcon: () => JSX.Element;
3
4
  export declare const SaveIcon: () => JSX.Element;
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  import { ToastPosition } from 'react-hot-toast';
2
3
  interface Options {
3
4
  appearance: 'error' | 'success';
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  import { AxiosWrapper, User } from '../../helpers/jwt';
2
3
  export declare const UserImageIcon: JSX.Element;
3
4
  export declare const UserImage: ({ image, className, }: {
@@ -15,6 +15,7 @@ export * from './LogoutButton';
15
15
  export * from './Modal';
16
16
  export * from './Prompt';
17
17
  export * from './RowOrColumn';
18
+ export * from './Sidebar';
18
19
  export * from './Table';
19
20
  export * from './TextEdit';
20
21
  export * from './TextInput';
@@ -27,6 +27,7 @@ __exportStar(require("./LogoutButton"), exports);
27
27
  __exportStar(require("./Modal"), exports);
28
28
  __exportStar(require("./Prompt"), exports);
29
29
  __exportStar(require("./RowOrColumn"), exports);
30
+ __exportStar(require("./Sidebar"), exports);
30
31
  __exportStar(require("./Table"), exports);
31
32
  __exportStar(require("./TextEdit"), exports);
32
33
  __exportStar(require("./TextInput"), exports);
@@ -41,16 +41,18 @@ const callOpenApiCachedRaw = (p) => {
41
41
  if (!callOpenApiCache) {
42
42
  callOpenApiCache = new node_cache_1.default({ stdTTL: p.cacheTtl || 120 });
43
43
  }
44
- const ssrCached = (_a = p.ssrCacheItems) === null || _a === void 0 ? void 0 : _a.find((s) => s.cacheKey === p.cacheKey);
45
- if (!callOpenApiCache.get(userPrefixedCacheKey) &&
46
- ((_b = ssrCached === null || ssrCached === void 0 ? void 0 : ssrCached.prefillData) === null || _b === void 0 ? void 0 : _b.data)) {
47
- callOpenApiCache.set(userPrefixedCacheKey, (_c = ssrCached.prefillData) === null || _c === void 0 ? void 0 : _c.data);
44
+ //get ssr cache value
45
+ const ssrCached = (_c = (_b = (_a = p.ssrCacheItems) === null || _a === void 0 ? void 0 : _a.find((s) => s.cacheKey === p.cacheKey)) === null || _b === void 0 ? void 0 : _b.prefillData) === null || _c === void 0 ? void 0 : _c.data;
46
+ //if we have ssr cache and there is no existing cache then set
47
+ if (!callOpenApiCache.get(userPrefixedCacheKey) && ssrCached) {
48
+ callOpenApiCache.set(userPrefixedCacheKey, ssrCached);
48
49
  }
49
- const cached = callOpenApiCache.get(userPrefixedCacheKey);
50
- if (cached) {
51
- return { data: cached };
50
+ //return cached data, or ssr data if that has already expired (ttl <=0)
51
+ const data = callOpenApiCache.get(userPrefixedCacheKey) || ssrCached;
52
+ if (!data) {
53
+ return undefined;
52
54
  }
53
- return undefined;
55
+ return { data };
54
56
  };
55
57
  exports.callOpenApiCachedRaw = callOpenApiCachedRaw;
56
58
  const callOpenApiCached = (p) => __awaiter(void 0, void 0, void 0, function* () {
@@ -12,6 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.useCallOpenApi = void 0;
13
13
  const react_1 = require("react");
14
14
  const cached_1 = require("./cached");
15
+ const direct_1 = require("./direct");
15
16
  /**
16
17
  * hooks+cached call to callOpenApi
17
18
  * @param p
@@ -33,7 +34,7 @@ const useCallOpenApi = (p) => {
33
34
  (0, react_1.useEffect)(() => {
34
35
  function run() {
35
36
  return __awaiter(this, void 0, void 0, function* () {
36
- const resp = yield (0, cached_1.callOpenApiCached)(p);
37
+ const resp = yield (0, direct_1.callOpenApi)(p);
37
38
  setData((d) => (Object.assign(Object.assign({}, resp), { loaded: true, loading: false, loadcount: d.loadcount + 1, reFetch: () => __awaiter(this, void 0, void 0, function* () { }), url: '', datetime: new Date().getTime() })));
38
39
  });
39
40
  }
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  import { TLang, TResource } from '../../common/helpers/i18n';
2
3
  export declare const useTranslation: <T extends {
3
4
  [a: string]: TResource;
@@ -1,18 +1,33 @@
1
1
  export declare const isServer: boolean;
2
- export declare const useQueryStringRaw: <T>({ name, searchOverride, defaultValue, stringify, parse, }: {
2
+ /**
3
+ * hook for query string value
4
+ * @param param0 can provide for SSR - queryValues will default to window if available
5
+ * @returns
6
+ */
7
+ export declare const useQueryStringRaw: <T>({ name, queryValues, defaultValue, stringify, parse, }: {
3
8
  name: string;
4
- searchOverride?: string | undefined;
9
+ queryValues?: Record<string, string> | undefined;
5
10
  defaultValue: T;
6
11
  stringify: (v: T) => string | undefined;
7
12
  parse: (v: string | undefined) => T;
8
13
  }) => [T, (v: T) => void];
9
- export declare const useQueryStringArray: ({ name, searchOverride, defaultValue, }: {
14
+ /**
15
+ * hook for query string value - string array type
16
+ * @param param0 can provide for SSR - queryValues will default to window if available
17
+ * @returns
18
+ */
19
+ export declare const useQueryStringArray: ({ name, queryValues, defaultValue, }: {
10
20
  name: string;
11
- searchOverride?: string | undefined;
21
+ queryValues?: Record<string, string> | undefined;
12
22
  defaultValue: string[];
13
23
  }) => [string[], (v: string[]) => void];
14
- export declare const useQueryStringSingle: ({ name, searchOverride, defaultValue, }: {
24
+ /**
25
+ * hook for query string value - single value
26
+ * @param param0 can provide for SSR - queryValues will default to window if available
27
+ * @returns
28
+ */
29
+ export declare const useQueryStringSingle: ({ name, queryValues, defaultValue, }: {
15
30
  name: string;
16
- searchOverride?: string | undefined;
31
+ queryValues?: Record<string, string> | undefined;
17
32
  defaultValue: string | undefined;
18
33
  }) => [string | undefined, (v: string | undefined) => void];
@@ -2,60 +2,66 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useQueryStringSingle = exports.useQueryStringArray = exports.useQueryStringRaw = exports.isServer = void 0;
4
4
  const react_1 = require("react");
5
+ const object_1 = require("../../common/helpers/object");
5
6
  exports.isServer = typeof window === 'undefined';
6
- const useQueryStringRaw = ({ name, searchOverride, defaultValue, stringify, parse, }) => {
7
- const raw = exports.isServer ? searchOverride : window.location.search;
8
- const [state, setStateRaw] = (0, react_1.useState)(() => {
9
- if (!raw) {
10
- return defaultValue;
11
- }
12
- const parsed = new URLSearchParams(raw);
13
- const g = parsed.get(name);
14
- if (!g) {
15
- return defaultValue;
16
- }
17
- return parse(g) || defaultValue;
18
- });
7
+ /**
8
+ * hook for query string value
9
+ * @param param0 can provide for SSR - queryValues will default to window if available
10
+ * @returns
11
+ */
12
+ const useQueryStringRaw = ({ name, queryValues, defaultValue, stringify, parse, }) => {
13
+ // eslint-disable-next-line react-hooks/exhaustive-deps
14
+ const qv = exports.isServer
15
+ ? queryValues || {}
16
+ : // eslint-disable-next-line @typescript-eslint/no-explicit-any
17
+ (0, object_1.paramsToObject)(new URLSearchParams(window.location.search));
18
+ const qsv = parse(qv[name]) || defaultValue;
19
+ const [state, setStateRaw] = (0, react_1.useState)(qsv);
20
+ //
19
21
  const setState = (v) => {
20
- const searchParams = new URLSearchParams(window.location.search);
21
22
  const sv = !v ? undefined : stringify(v);
22
23
  if (sv) {
23
- searchParams.set(name, sv);
24
+ qv[name] = sv;
24
25
  }
25
26
  else {
26
- searchParams.delete(name);
27
- }
28
- let qs = '';
29
- if (Array.from(searchParams).length !== 0) {
30
- qs = '?' + searchParams.toString();
27
+ delete qv[name];
31
28
  }
32
- const loc = `${location.pathname}${qs}${window.location.hash}`;
29
+ const qs = '?' + (0, object_1.objectToString)(qv, '=', '&');
30
+ const loc = location.pathname + qs + window.location.hash;
33
31
  window.history.replaceState({}, '', loc);
34
32
  setStateRaw(v);
35
33
  };
34
+ //
36
35
  (0, react_1.useEffect)(() => {
37
- const parsed = new URLSearchParams(raw);
38
- const g = parsed.get(name);
39
- const newv = g ? parse(g) : defaultValue;
40
- if (JSON.stringify(state) !== JSON.stringify(newv)) {
41
- setStateRaw(newv);
36
+ if (JSON.stringify(state) !== JSON.stringify(qsv)) {
37
+ setStateRaw(qsv);
42
38
  }
43
- }, [defaultValue, name, parse, raw, state]);
39
+ }, [name, parse, qsv, state]);
44
40
  return [state, setState];
45
41
  };
46
42
  exports.useQueryStringRaw = useQueryStringRaw;
47
- const useQueryStringArray = ({ name, searchOverride, defaultValue, }) => (0, exports.useQueryStringRaw)({
43
+ /**
44
+ * hook for query string value - string array type
45
+ * @param param0 can provide for SSR - queryValues will default to window if available
46
+ * @returns
47
+ */
48
+ const useQueryStringArray = ({ name, queryValues, defaultValue, }) => (0, exports.useQueryStringRaw)({
48
49
  name,
49
50
  defaultValue,
50
- searchOverride,
51
+ queryValues,
51
52
  stringify: (v) => (v.length === 0 ? undefined : v.join(',')),
52
53
  parse: (v) => (!v ? defaultValue : v.split(',')),
53
54
  });
54
55
  exports.useQueryStringArray = useQueryStringArray;
55
- const useQueryStringSingle = ({ name, searchOverride, defaultValue, }) => (0, exports.useQueryStringRaw)({
56
+ /**
57
+ * hook for query string value - single value
58
+ * @param param0 can provide for SSR - queryValues will default to window if available
59
+ * @returns
60
+ */
61
+ const useQueryStringSingle = ({ name, queryValues, defaultValue, }) => (0, exports.useQueryStringRaw)({
56
62
  name,
57
63
  defaultValue,
58
- searchOverride,
64
+ queryValues,
59
65
  stringify: (v) => v,
60
66
  parse: (v) => (!v ? defaultValue : v),
61
67
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ag-common",
3
- "version": "0.0.125",
3
+ "version": "0.0.129",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "author": "Andrei Gec <@andreigec> (https://gec.dev/)",