@cloud-ru/uikit-product-utils 8.0.1 → 8.1.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.
- package/CHANGELOG.md +22 -0
- package/dist/cjs/hooks/index.d.ts +1 -0
- package/dist/cjs/hooks/index.js +1 -0
- package/dist/cjs/hooks/useLocalStorage.d.ts +1 -0
- package/dist/cjs/hooks/useLocalStorage.js +16 -0
- package/dist/cjs/utils/getUserAgentInfo.js +2 -1
- package/dist/esm/hooks/index.d.ts +1 -0
- package/dist/esm/hooks/index.js +1 -0
- package/dist/esm/hooks/useLocalStorage.d.ts +1 -0
- package/dist/esm/hooks/useLocalStorage.js +13 -0
- package/dist/esm/utils/getUserAgentInfo.js +2 -1
- package/package.json +2 -2
- package/src/hooks/index.ts +1 -0
- package/src/hooks/useLocalStorage.ts +19 -0
- package/src/utils/getUserAgentInfo.ts +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,28 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# 8.1.0 (2025-12-17)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* **DOCDEV-2456:** moved useLocalStorage to utils pckg ([9809058](https://gitverse.ru/cloud-ru-tech/uikit-product/commits/98090588fc37da109dd01b9f2b89f0fe776df565))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
## 8.0.2 (2025-12-08)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### Bug Fixes
|
|
21
|
+
|
|
22
|
+
* **FF-7187:** add SSR safety checks for localStorage and navigator usage ([c2c7561](https://gitverse.ru/cloud-ru-tech/uikit-product/commits/c2c7561c9a2f5e3b1bd897b696b33ea309f39dfa))
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
6
28
|
## 8.0.1 (2025-11-24)
|
|
7
29
|
|
|
8
30
|
|
package/dist/cjs/hooks/index.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function useLocalStorage<T extends string = string>(key: string, defaultValue: T): [T, (newValue: T) => void];
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useLocalStorage = useLocalStorage;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const utils_1 = require("@snack-uikit/utils");
|
|
6
|
+
function useLocalStorage(key, defaultValue) {
|
|
7
|
+
var _a;
|
|
8
|
+
const [value, setValueState] = (0, react_1.useState)(((_a = ((0, utils_1.isBrowser)() ? localStorage.getItem(key) : null)) !== null && _a !== void 0 ? _a : defaultValue));
|
|
9
|
+
const setValue = (0, react_1.useCallback)((newValue) => {
|
|
10
|
+
if ((0, utils_1.isBrowser)()) {
|
|
11
|
+
localStorage.setItem(key, newValue);
|
|
12
|
+
}
|
|
13
|
+
setValueState(newValue);
|
|
14
|
+
}, [key]);
|
|
15
|
+
return [value, setValue];
|
|
16
|
+
}
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getUserAgentInfo = getUserAgentInfo;
|
|
4
4
|
const ua_parser_js_1 = require("ua-parser-js");
|
|
5
|
+
const utils_1 = require("@snack-uikit/utils");
|
|
5
6
|
const DEVICE_TYPE = {
|
|
6
7
|
Console: 'console',
|
|
7
8
|
Mobile: 'mobile',
|
|
@@ -14,7 +15,7 @@ const DEVICE_TYPE = {
|
|
|
14
15
|
const DEVICE_TYPES = Object.values(DEVICE_TYPE);
|
|
15
16
|
const getDeviceType = (type) => DEVICE_TYPES.find(value => value === type) || DEVICE_TYPE.Desktop;
|
|
16
17
|
function getUserAgentInfo() {
|
|
17
|
-
const parser = new ua_parser_js_1.UAParser(globalThis.navigator.userAgent);
|
|
18
|
+
const parser = new ua_parser_js_1.UAParser((0, utils_1.isBrowser)() ? globalThis.navigator.userAgent : undefined);
|
|
18
19
|
const device = parser.getDevice();
|
|
19
20
|
const browser = parser.getBrowser();
|
|
20
21
|
const os = parser.getOS();
|
package/dist/esm/hooks/index.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function useLocalStorage<T extends string = string>(key: string, defaultValue: T): [T, (newValue: T) => void];
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { useCallback, useState } from 'react';
|
|
2
|
+
import { isBrowser } from '@snack-uikit/utils';
|
|
3
|
+
export function useLocalStorage(key, defaultValue) {
|
|
4
|
+
var _a;
|
|
5
|
+
const [value, setValueState] = useState(((_a = (isBrowser() ? localStorage.getItem(key) : null)) !== null && _a !== void 0 ? _a : defaultValue));
|
|
6
|
+
const setValue = useCallback((newValue) => {
|
|
7
|
+
if (isBrowser()) {
|
|
8
|
+
localStorage.setItem(key, newValue);
|
|
9
|
+
}
|
|
10
|
+
setValueState(newValue);
|
|
11
|
+
}, [key]);
|
|
12
|
+
return [value, setValue];
|
|
13
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { UAParser } from 'ua-parser-js';
|
|
2
|
+
import { isBrowser } from '@snack-uikit/utils';
|
|
2
3
|
const DEVICE_TYPE = {
|
|
3
4
|
Console: 'console',
|
|
4
5
|
Mobile: 'mobile',
|
|
@@ -11,7 +12,7 @@ const DEVICE_TYPE = {
|
|
|
11
12
|
const DEVICE_TYPES = Object.values(DEVICE_TYPE);
|
|
12
13
|
const getDeviceType = (type) => DEVICE_TYPES.find(value => value === type) || DEVICE_TYPE.Desktop;
|
|
13
14
|
export function getUserAgentInfo() {
|
|
14
|
-
const parser = new UAParser(globalThis.navigator.userAgent);
|
|
15
|
+
const parser = new UAParser(isBrowser() ? globalThis.navigator.userAgent : undefined);
|
|
15
16
|
const device = parser.getDevice();
|
|
16
17
|
const browser = parser.getBrowser();
|
|
17
18
|
const os = parser.getOS();
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloud-ru/uikit-product-utils",
|
|
3
3
|
"title": "Utils",
|
|
4
|
-
"version": "8.0
|
|
4
|
+
"version": "8.1.0",
|
|
5
5
|
"sideEffects": [
|
|
6
6
|
"*.css",
|
|
7
7
|
"*.woff",
|
|
@@ -84,5 +84,5 @@
|
|
|
84
84
|
"optional": true
|
|
85
85
|
}
|
|
86
86
|
},
|
|
87
|
-
"gitHead": "
|
|
87
|
+
"gitHead": "1a98d6d9bff32edcbb3f9b0be0a14dce3f2abe5d"
|
|
88
88
|
}
|
package/src/hooks/index.ts
CHANGED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { useCallback, useState } from 'react';
|
|
2
|
+
|
|
3
|
+
import { isBrowser } from '@snack-uikit/utils';
|
|
4
|
+
|
|
5
|
+
export function useLocalStorage<T extends string = string>(key: string, defaultValue: T): [T, (newValue: T) => void] {
|
|
6
|
+
const [value, setValueState] = useState<T>(((isBrowser() ? localStorage.getItem(key) : null) ?? defaultValue) as T);
|
|
7
|
+
|
|
8
|
+
const setValue = useCallback(
|
|
9
|
+
(newValue: T) => {
|
|
10
|
+
if (isBrowser()) {
|
|
11
|
+
localStorage.setItem(key, newValue);
|
|
12
|
+
}
|
|
13
|
+
setValueState(newValue);
|
|
14
|
+
},
|
|
15
|
+
[key],
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
return [value, setValue];
|
|
19
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { UAParser } from 'ua-parser-js';
|
|
2
2
|
|
|
3
|
-
import { ValueOf } from '@snack-uikit/utils';
|
|
3
|
+
import { isBrowser, ValueOf } from '@snack-uikit/utils';
|
|
4
4
|
|
|
5
5
|
const DEVICE_TYPE = {
|
|
6
6
|
Console: 'console',
|
|
@@ -20,7 +20,7 @@ const getDeviceType = (type: string | undefined): DeviceType =>
|
|
|
20
20
|
DEVICE_TYPES.find(value => value === type) || DEVICE_TYPE.Desktop;
|
|
21
21
|
|
|
22
22
|
export function getUserAgentInfo() {
|
|
23
|
-
const parser = new UAParser(globalThis.navigator.userAgent);
|
|
23
|
+
const parser = new UAParser(isBrowser() ? globalThis.navigator.userAgent : undefined);
|
|
24
24
|
const device = parser.getDevice();
|
|
25
25
|
const browser = parser.getBrowser();
|
|
26
26
|
const os = parser.getOS();
|