@deix/rossini-core 0.6.0 → 0.6.1

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/lib/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deix/rossini-core",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
4
4
  "main": "lib/src/index.js",
5
5
  "type": "commonjs",
6
6
  "repository": {
@@ -3,7 +3,8 @@
3
3
  *
4
4
  * @param {string} name the name of the variable in local storage.
5
5
  * @param {T} defaultValue the default value
6
+ * @param {"local" | "session"} storage the type of storage where to persist the state
6
7
  */
7
- declare function usePersistedState<T>(name: string, defaultValue: T): [T, (value: T) => void];
8
+ declare function usePersistedState<T>(name: string, defaultValue: T, storage?: "local" | "session"): [T, (value: T) => void];
8
9
  export default usePersistedState;
9
10
  //# sourceMappingURL=usePersistedState.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"usePersistedState.d.ts","sourceRoot":"","sources":["../../../../src/utils/hooks/usePersistedState.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,iBAAS,iBAAiB,CAAC,CAAC,EAC1B,IAAI,EAAE,MAAM,EACZ,YAAY,EAAE,CAAC,GACd,CAAC,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC,CA8BzB;AAED,eAAe,iBAAiB,CAAC"}
1
+ {"version":3,"file":"usePersistedState.d.ts","sourceRoot":"","sources":["../../../../src/utils/hooks/usePersistedState.ts"],"names":[],"mappings":"AAOA;;;;;;GAMG;AACH,iBAAS,iBAAiB,CAAC,CAAC,EAC1B,IAAI,EAAE,MAAM,EACZ,YAAY,EAAE,CAAC,EACf,OAAO,GAAE,OAAO,GAAG,SAAmB,GACrC,CAAC,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC,CA8CzB;AAED,eAAe,iBAAiB,CAAC"}
@@ -1,26 +1,45 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const react_1 = require("react");
4
+ const storageMap = {
5
+ local: "localStorage",
6
+ session: "sessionStorage",
7
+ };
4
8
  /**
5
9
  * Use a variable stored persistently on local storage.
6
10
  *
7
11
  * @param {string} name the name of the variable in local storage.
8
12
  * @param {T} defaultValue the default value
13
+ * @param {"local" | "session"} storage the type of storage where to persist the state
9
14
  */
10
- function usePersistedState(name, defaultValue) {
15
+ function usePersistedState(name, defaultValue, storage = "local") {
11
16
  const [value, setValue] = (0, react_1.useState)(defaultValue);
12
17
  const firstRender = (0, react_1.useRef)(true);
13
18
  (0, react_1.useEffect)(() => {
14
19
  try {
15
- const localValueString = window.localStorage.getItem(name);
16
- if (localValueString) {
17
- const localValue = JSON.parse(localValueString);
18
- setValue(localValue);
20
+ if (storage === "local") {
21
+ const localValueString = window.localStorage.getItem(name);
22
+ if (localValueString) {
23
+ const localValue = JSON.parse(localValueString);
24
+ setValue(localValue);
25
+ }
26
+ }
27
+ else {
28
+ const localValueString = window.sessionStorage.getItem(name);
29
+ if (localValueString) {
30
+ const localValue = JSON.parse(localValueString);
31
+ setValue(localValue);
32
+ }
19
33
  }
20
34
  }
21
35
  catch (err) {
22
36
  console.log(err);
23
- window.localStorage.removeItem(name);
37
+ if (storage === "local") {
38
+ window.localStorage.removeItem(name);
39
+ }
40
+ else {
41
+ window.sessionStorage.removeItem(name);
42
+ }
24
43
  }
25
44
  }, [name, setValue]);
26
45
  (0, react_1.useEffect)(() => {
@@ -29,7 +48,12 @@ function usePersistedState(name, defaultValue) {
29
48
  return;
30
49
  }
31
50
  try {
32
- window.localStorage.setItem(name, JSON.stringify(value));
51
+ if (storage === "local") {
52
+ window.localStorage.setItem(name, JSON.stringify(value));
53
+ }
54
+ else {
55
+ window.sessionStorage.setItem(name, JSON.stringify(value));
56
+ }
33
57
  }
34
58
  catch (err) {
35
59
  console.error(err);