@elementor/session 0.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 ADDED
@@ -0,0 +1,7 @@
1
+ # @elementor/session
2
+
3
+ ## 0.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 43f1684: Save previous link value in session
@@ -0,0 +1,17 @@
1
+ import * as React from 'react';
2
+ import { PropsWithChildren } from 'react';
3
+
4
+ declare const getSessionStorageItem: <T>(key: string) => T | undefined;
5
+ declare const setSessionStorageItem: (key: string, item: unknown) => void;
6
+ declare const removeSessionStorageItem: (key: string) => void;
7
+
8
+ declare const useSessionStorage: <T>(key: string) => readonly [T | null | undefined, (newValue: T) => void, () => void];
9
+
10
+ type ContextValue = {
11
+ prefix: string;
12
+ };
13
+ declare const Context: React.Context<ContextValue | null>;
14
+ type Props = PropsWithChildren<ContextValue>;
15
+ declare function SessionStorageProvider({ children, prefix }: Props): React.JSX.Element;
16
+
17
+ export { Context, SessionStorageProvider, getSessionStorageItem, removeSessionStorageItem, setSessionStorageItem, useSessionStorage };
@@ -0,0 +1,17 @@
1
+ import * as React from 'react';
2
+ import { PropsWithChildren } from 'react';
3
+
4
+ declare const getSessionStorageItem: <T>(key: string) => T | undefined;
5
+ declare const setSessionStorageItem: (key: string, item: unknown) => void;
6
+ declare const removeSessionStorageItem: (key: string) => void;
7
+
8
+ declare const useSessionStorage: <T>(key: string) => readonly [T | null | undefined, (newValue: T) => void, () => void];
9
+
10
+ type ContextValue = {
11
+ prefix: string;
12
+ };
13
+ declare const Context: React.Context<ContextValue | null>;
14
+ type Props = PropsWithChildren<ContextValue>;
15
+ declare function SessionStorageProvider({ children, prefix }: Props): React.JSX.Element;
16
+
17
+ export { Context, SessionStorageProvider, getSessionStorageItem, removeSessionStorageItem, setSessionStorageItem, useSessionStorage };
package/dist/index.js ADDED
@@ -0,0 +1,122 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ Context: () => Context,
34
+ SessionStorageProvider: () => SessionStorageProvider,
35
+ getSessionStorageItem: () => getSessionStorageItem,
36
+ removeSessionStorageItem: () => removeSessionStorageItem,
37
+ setSessionStorageItem: () => setSessionStorageItem,
38
+ useSessionStorage: () => useSessionStorage
39
+ });
40
+ module.exports = __toCommonJS(index_exports);
41
+
42
+ // src/session-storage.ts
43
+ var getSessionStorageItem = (key) => {
44
+ return JSON.parse(sessionStorage.getItem(key) || "{}")?.item;
45
+ };
46
+ var setSessionStorageItem = (key, item) => {
47
+ sessionStorage.setItem(key, JSON.stringify({ item }));
48
+ window.dispatchEvent(
49
+ new StorageEvent("storage", {
50
+ key,
51
+ storageArea: sessionStorage
52
+ })
53
+ );
54
+ };
55
+ var removeSessionStorageItem = (key) => {
56
+ sessionStorage.removeItem(key);
57
+ window.dispatchEvent(
58
+ new StorageEvent("storage", {
59
+ key,
60
+ storageArea: sessionStorage
61
+ })
62
+ );
63
+ };
64
+
65
+ // src/use-session-storage.ts
66
+ var import_react2 = require("react");
67
+
68
+ // src/session-storage-context.tsx
69
+ var React = __toESM(require("react"));
70
+ var import_react = require("react");
71
+ var Context = (0, import_react.createContext)(null);
72
+ function SessionStorageProvider({ children, prefix }) {
73
+ const contextPrefix = (0, import_react.useContext)(Context)?.prefix ?? "";
74
+ const chainedPrefix = contextPrefix ? `${contextPrefix}/${prefix}` : prefix;
75
+ return /* @__PURE__ */ React.createElement(Context.Provider, { value: { prefix: chainedPrefix } }, children);
76
+ }
77
+
78
+ // src/use-session-storage.ts
79
+ var useSessionStorage = (key) => {
80
+ const prefix = (0, import_react2.useContext)(Context)?.prefix ?? "";
81
+ const prefixedKey = `${prefix}/${key}`;
82
+ const [value, setValue] = (0, import_react2.useState)();
83
+ (0, import_react2.useEffect)(() => {
84
+ return subscribeToSessionStorage(prefixedKey, (newValue) => {
85
+ setValue(newValue ?? null);
86
+ });
87
+ }, [prefixedKey]);
88
+ const saveValue = (newValue) => {
89
+ setSessionStorageItem(prefixedKey, newValue);
90
+ };
91
+ const removeValue = () => {
92
+ removeSessionStorageItem(prefixedKey);
93
+ };
94
+ return [value, saveValue, removeValue];
95
+ };
96
+ var subscribeToSessionStorage = (key, subscriber) => {
97
+ subscriber(getSessionStorageItem(key));
98
+ const abortController = new AbortController();
99
+ window.addEventListener(
100
+ "storage",
101
+ (e) => {
102
+ if (e.key !== key || e.storageArea !== sessionStorage) {
103
+ return;
104
+ }
105
+ subscriber(getSessionStorageItem(key));
106
+ },
107
+ { signal: abortController.signal }
108
+ );
109
+ return () => {
110
+ abortController.abort();
111
+ };
112
+ };
113
+ // Annotate the CommonJS export names for ESM import in node:
114
+ 0 && (module.exports = {
115
+ Context,
116
+ SessionStorageProvider,
117
+ getSessionStorageItem,
118
+ removeSessionStorageItem,
119
+ setSessionStorageItem,
120
+ useSessionStorage
121
+ });
122
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/session-storage.ts","../src/use-session-storage.ts","../src/session-storage-context.tsx"],"sourcesContent":["export * from './session-storage';\nexport * from './use-session-storage';\nexport * from './session-storage-context';\n","export const getSessionStorageItem = < T >( key: string ): T | undefined => {\n\treturn JSON.parse( sessionStorage.getItem( key ) || '{}' )?.item;\n};\n\nexport const setSessionStorageItem = ( key: string, item: unknown ) => {\n\tsessionStorage.setItem( key, JSON.stringify( { item } ) );\n\n\t// The browser doesn't dispatch the `storage` event for the current tab,\n\t// so we need to dispatch it manually.\n\t//\n\t// https://developer.mozilla.org/en-US/docs/Web/API/Window/storage_event\n\twindow.dispatchEvent(\n\t\tnew StorageEvent( 'storage', {\n\t\t\tkey,\n\t\t\tstorageArea: sessionStorage,\n\t\t} )\n\t);\n};\n\nexport const removeSessionStorageItem = ( key: string ) => {\n\tsessionStorage.removeItem( key );\n\n\t// The browser doesn't dispatch the `storage` event for the current tab,\n\t// so we need to dispatch it manually.\n\t//\n\t// https://developer.mozilla.org/en-US/docs/Web/API/Window/storage_event\n\twindow.dispatchEvent(\n\t\tnew StorageEvent( 'storage', {\n\t\t\tkey,\n\t\t\tstorageArea: sessionStorage,\n\t\t} )\n\t);\n};\n","import { useContext, useEffect, useState } from 'react';\n\nimport { getSessionStorageItem, removeSessionStorageItem, setSessionStorageItem } from './session-storage';\nimport { Context } from './session-storage-context';\n\nexport const useSessionStorage = < T >( key: string ) => {\n\tconst prefix = useContext( Context )?.prefix ?? '';\n\tconst prefixedKey = `${ prefix }/${ key }`;\n\n\tconst [ value, setValue ] = useState< T | null >();\n\n\tuseEffect( () => {\n\t\treturn subscribeToSessionStorage< T | null >( prefixedKey, ( newValue ) => {\n\t\t\tsetValue( newValue ?? null );\n\t\t} );\n\t}, [ prefixedKey ] );\n\n\tconst saveValue = ( newValue: T ) => {\n\t\tsetSessionStorageItem( prefixedKey, newValue );\n\t};\n\n\tconst removeValue = () => {\n\t\tremoveSessionStorageItem( prefixedKey );\n\t};\n\n\treturn [ value, saveValue, removeValue ] as const;\n};\n\nconst subscribeToSessionStorage = < T >( key: string, subscriber: ( value: T ) => void ) => {\n\tsubscriber( getSessionStorageItem( key ) as T );\n\n\tconst abortController = new AbortController();\n\n\twindow.addEventListener(\n\t\t'storage',\n\t\t( e ) => {\n\t\t\tif ( e.key !== key || e.storageArea !== sessionStorage ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tsubscriber( getSessionStorageItem( key ) as T );\n\t\t},\n\t\t{ signal: abortController.signal }\n\t);\n\n\treturn () => {\n\t\tabortController.abort();\n\t};\n};\n","import * as React from 'react';\nimport { createContext, type PropsWithChildren, useContext } from 'react';\n\ntype ContextValue = {\n\tprefix: string;\n};\n\nexport const Context = createContext< ContextValue | null >( null );\n\ntype Props = PropsWithChildren< ContextValue >;\n\nexport function SessionStorageProvider( { children, prefix }: Props ) {\n\tconst contextPrefix = useContext( Context )?.prefix ?? '';\n\tconst chainedPrefix = contextPrefix ? `${ contextPrefix }/${ prefix }` : prefix;\n\n\treturn <Context.Provider value={ { prefix: chainedPrefix } }>{ children }</Context.Provider>;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAO,IAAM,wBAAwB,CAAO,QAAgC;AAC3E,SAAO,KAAK,MAAO,eAAe,QAAS,GAAI,KAAK,IAAK,GAAG;AAC7D;AAEO,IAAM,wBAAwB,CAAE,KAAa,SAAmB;AACtE,iBAAe,QAAS,KAAK,KAAK,UAAW,EAAE,KAAK,CAAE,CAAE;AAMxD,SAAO;AAAA,IACN,IAAI,aAAc,WAAW;AAAA,M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;AACxD;AAAA,MACD;AAEA,iBAAY,sBAAuB,GAAI,CAAO;AAAA,IAC/C;AAAA,IACA,EAAE,QAAQ,gBAAgB,OAAO;AAAA,EAClC;AAEA,SAAO,MAAM;AACZ,oBAAgB,MAAM;AAAA,EACvB;AACD;","names":["import_react"]}
package/dist/index.mjs ADDED
@@ -0,0 +1,80 @@
1
+ // src/session-storage.ts
2
+ var getSessionStorageItem = (key) => {
3
+ return JSON.parse(sessionStorage.getItem(key) || "{}")?.item;
4
+ };
5
+ var setSessionStorageItem = (key, item) => {
6
+ sessionStorage.setItem(key, JSON.stringify({ item }));
7
+ window.dispatchEvent(
8
+ new StorageEvent("storage", {
9
+ key,
10
+ storageArea: sessionStorage
11
+ })
12
+ );
13
+ };
14
+ var removeSessionStorageItem = (key) => {
15
+ sessionStorage.removeItem(key);
16
+ window.dispatchEvent(
17
+ new StorageEvent("storage", {
18
+ key,
19
+ storageArea: sessionStorage
20
+ })
21
+ );
22
+ };
23
+
24
+ // src/use-session-storage.ts
25
+ import { useContext as useContext2, useEffect, useState } from "react";
26
+
27
+ // src/session-storage-context.tsx
28
+ import * as React from "react";
29
+ import { createContext, useContext } from "react";
30
+ var Context = createContext(null);
31
+ function SessionStorageProvider({ children, prefix }) {
32
+ const contextPrefix = useContext(Context)?.prefix ?? "";
33
+ const chainedPrefix = contextPrefix ? `${contextPrefix}/${prefix}` : prefix;
34
+ return /* @__PURE__ */ React.createElement(Context.Provider, { value: { prefix: chainedPrefix } }, children);
35
+ }
36
+
37
+ // src/use-session-storage.ts
38
+ var useSessionStorage = (key) => {
39
+ const prefix = useContext2(Context)?.prefix ?? "";
40
+ const prefixedKey = `${prefix}/${key}`;
41
+ const [value, setValue] = useState();
42
+ useEffect(() => {
43
+ return subscribeToSessionStorage(prefixedKey, (newValue) => {
44
+ setValue(newValue ?? null);
45
+ });
46
+ }, [prefixedKey]);
47
+ const saveValue = (newValue) => {
48
+ setSessionStorageItem(prefixedKey, newValue);
49
+ };
50
+ const removeValue = () => {
51
+ removeSessionStorageItem(prefixedKey);
52
+ };
53
+ return [value, saveValue, removeValue];
54
+ };
55
+ var subscribeToSessionStorage = (key, subscriber) => {
56
+ subscriber(getSessionStorageItem(key));
57
+ const abortController = new AbortController();
58
+ window.addEventListener(
59
+ "storage",
60
+ (e) => {
61
+ if (e.key !== key || e.storageArea !== sessionStorage) {
62
+ return;
63
+ }
64
+ subscriber(getSessionStorageItem(key));
65
+ },
66
+ { signal: abortController.signal }
67
+ );
68
+ return () => {
69
+ abortController.abort();
70
+ };
71
+ };
72
+ export {
73
+ Context,
74
+ SessionStorageProvider,
75
+ getSessionStorageItem,
76
+ removeSessionStorageItem,
77
+ setSessionStorageItem,
78
+ useSessionStorage
79
+ };
80
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/session-storage.ts","../src/use-session-storage.ts","../src/session-storage-context.tsx"],"sourcesContent":["export const getSessionStorageItem = < T >( key: string ): T | undefined => {\n\treturn JSON.parse( sessionStorage.getItem( key ) || '{}' )?.item;\n};\n\nexport const setSessionStorageItem = ( key: string, item: unknown ) => {\n\tsessionStorage.setItem( key, JSON.stringify( { item } ) );\n\n\t// The browser doesn't dispatch the `storage` event for the current tab,\n\t// so we need to dispatch it manually.\n\t//\n\t// https://developer.mozilla.org/en-US/docs/Web/API/Window/storage_event\n\twindow.dispatchEvent(\n\t\tnew StorageEvent( 'storage', {\n\t\t\tkey,\n\t\t\tstorageArea: sessionStorage,\n\t\t} )\n\t);\n};\n\nexport const removeSessionStorageItem = ( key: string ) => {\n\tsessionStorage.removeItem( key );\n\n\t// The browser doesn't dispatch the `storage` event for the current tab,\n\t// so we need to dispatch it manually.\n\t//\n\t// https://developer.mozilla.org/en-US/docs/Web/API/Window/storage_event\n\twindow.dispatchEvent(\n\t\tnew StorageEvent( 'storage', {\n\t\t\tkey,\n\t\t\tstorageArea: sessionStorage,\n\t\t} )\n\t);\n};\n","import { useContext, useEffect, useState } from 'react';\n\nimport { getSessionStorageItem, removeSessionStorageItem, setSessionStorageItem } from './session-storage';\nimport { Context } from './session-storage-context';\n\nexport const useSessionStorage = < T >( key: string ) => {\n\tconst prefix = useContext( Context )?.prefix ?? '';\n\tconst prefixedKey = `${ prefix }/${ key }`;\n\n\tconst [ value, setValue ] = useState< T | null >();\n\n\tuseEffect( () => {\n\t\treturn subscribeToSessionStorage< T | null >( prefixedKey, ( newValue ) => {\n\t\t\tsetValue( newValue ?? null );\n\t\t} );\n\t}, [ prefixedKey ] );\n\n\tconst saveValue = ( newValue: T ) => {\n\t\tsetSessionStorageItem( prefixedKey, newValue );\n\t};\n\n\tconst removeValue = () => {\n\t\tremoveSessionStorageItem( prefixedKey );\n\t};\n\n\treturn [ value, saveValue, removeValue ] as const;\n};\n\nconst subscribeToSessionStorage = < T >( key: string, subscriber: ( value: T ) => void ) => {\n\tsubscriber( getSessionStorageItem( key ) as T );\n\n\tconst abortController = new AbortController();\n\n\twindow.addEventListener(\n\t\t'storage',\n\t\t( e ) => {\n\t\t\tif ( e.key !== key || e.storageArea !== sessionStorage ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tsubscriber( getSessionStorageItem( key ) as T );\n\t\t},\n\t\t{ signal: abortController.signal }\n\t);\n\n\treturn () => {\n\t\tabortController.abort();\n\t};\n};\n","import * as React from 'react';\nimport { createContext, type PropsWithChildren, useContext } from 'react';\n\ntype ContextValue = {\n\tprefix: string;\n};\n\nexport const Context = createContext< ContextValue | null >( null );\n\ntype Props = PropsWithChildren< ContextValue >;\n\nexport function SessionStorageProvider( { children, prefix }: Props ) {\n\tconst contextPrefix = useContext( Context )?.prefix ?? '';\n\tconst chainedPrefix = contextPrefix ? `${ contextPrefix }/${ prefix }` : prefix;\n\n\treturn <Context.Provider value={ { prefix: chainedPrefix } }>{ children }</Context.Provider>;\n}\n"],"mappings":";AAAO,IAAM,wBAAwB,CAAO,QAAgC;AAC3E,SAAO,KAAK,MAAO,eAAe,QAAS,GAAI,KAAK,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AM,4BAA4B,CAAO,KAAa,eAAsC;AAC3F,aAAY,sBAAuB,GAAI,CAAO;AAE9C,QAAM,kBAAkB,IAAI,gBAAgB;AAE5C,SAAO;AAAA,IACN;AAAA,IACA,CAAE,MAAO;AACR,UAAK,EAAE,QAAQ,OAAO,EAAE,gBAAgB,gBAAiB;AACxD;AAAA,MACD;AAEA,iBAAY,sBAAuB,GAAI,CAAO;AAAA,IAC/C;AAAA,IACA,EAAE,QAAQ,gBAAgB,OAAO;AAAA,EAClC;AAEA,SAAO,MAAM;AACZ,oBAAgB,MAAM;AAAA,EACvB;AACD;","names":["useContext","useContext"]}
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@elementor/session",
3
+ "description": "This package provides a session management utility.",
4
+ "version": "0.1.0",
5
+ "private": false,
6
+ "author": "Elementor Team",
7
+ "homepage": "https://elementor.com/",
8
+ "license": "GPL-3.0-or-later",
9
+ "main": "dist/index.js",
10
+ "module": "dist/index.mjs",
11
+ "types": "dist/index.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "import": "./dist/index.mjs",
16
+ "require": "./dist/index.js"
17
+ },
18
+ "./package.json": "./package.json"
19
+ },
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/elementor/elementor-packages.git",
23
+ "directory": "packages/libs/session"
24
+ },
25
+ "bugs": {
26
+ "url": "https://github.com/elementor/elementor-packages/issues"
27
+ },
28
+ "publishConfig": {
29
+ "access": "public"
30
+ },
31
+ "files": [
32
+ "README.md",
33
+ "CHANGELOG.md",
34
+ "/dist",
35
+ "/src",
36
+ "!**/__tests__"
37
+ ],
38
+ "peerDependencies": {
39
+ "react": "^18.3.1"
40
+ },
41
+ "scripts": {
42
+ "build": "tsup --config=../../tsup.build.ts",
43
+ "dev": "tsup --config=../../tsup.dev.ts"
44
+ }
45
+ }
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './session-storage';
2
+ export * from './use-session-storage';
3
+ export * from './session-storage-context';
@@ -0,0 +1,17 @@
1
+ import * as React from 'react';
2
+ import { createContext, type PropsWithChildren, useContext } from 'react';
3
+
4
+ type ContextValue = {
5
+ prefix: string;
6
+ };
7
+
8
+ export const Context = createContext< ContextValue | null >( null );
9
+
10
+ type Props = PropsWithChildren< ContextValue >;
11
+
12
+ export function SessionStorageProvider( { children, prefix }: Props ) {
13
+ const contextPrefix = useContext( Context )?.prefix ?? '';
14
+ const chainedPrefix = contextPrefix ? `${ contextPrefix }/${ prefix }` : prefix;
15
+
16
+ return <Context.Provider value={ { prefix: chainedPrefix } }>{ children }</Context.Provider>;
17
+ }
@@ -0,0 +1,33 @@
1
+ export const getSessionStorageItem = < T >( key: string ): T | undefined => {
2
+ return JSON.parse( sessionStorage.getItem( key ) || '{}' )?.item;
3
+ };
4
+
5
+ export const setSessionStorageItem = ( key: string, item: unknown ) => {
6
+ sessionStorage.setItem( key, JSON.stringify( { item } ) );
7
+
8
+ // The browser doesn't dispatch the `storage` event for the current tab,
9
+ // so we need to dispatch it manually.
10
+ //
11
+ // https://developer.mozilla.org/en-US/docs/Web/API/Window/storage_event
12
+ window.dispatchEvent(
13
+ new StorageEvent( 'storage', {
14
+ key,
15
+ storageArea: sessionStorage,
16
+ } )
17
+ );
18
+ };
19
+
20
+ export const removeSessionStorageItem = ( key: string ) => {
21
+ sessionStorage.removeItem( key );
22
+
23
+ // The browser doesn't dispatch the `storage` event for the current tab,
24
+ // so we need to dispatch it manually.
25
+ //
26
+ // https://developer.mozilla.org/en-US/docs/Web/API/Window/storage_event
27
+ window.dispatchEvent(
28
+ new StorageEvent( 'storage', {
29
+ key,
30
+ storageArea: sessionStorage,
31
+ } )
32
+ );
33
+ };
@@ -0,0 +1,49 @@
1
+ import { useContext, useEffect, useState } from 'react';
2
+
3
+ import { getSessionStorageItem, removeSessionStorageItem, setSessionStorageItem } from './session-storage';
4
+ import { Context } from './session-storage-context';
5
+
6
+ export const useSessionStorage = < T >( key: string ) => {
7
+ const prefix = useContext( Context )?.prefix ?? '';
8
+ const prefixedKey = `${ prefix }/${ key }`;
9
+
10
+ const [ value, setValue ] = useState< T | null >();
11
+
12
+ useEffect( () => {
13
+ return subscribeToSessionStorage< T | null >( prefixedKey, ( newValue ) => {
14
+ setValue( newValue ?? null );
15
+ } );
16
+ }, [ prefixedKey ] );
17
+
18
+ const saveValue = ( newValue: T ) => {
19
+ setSessionStorageItem( prefixedKey, newValue );
20
+ };
21
+
22
+ const removeValue = () => {
23
+ removeSessionStorageItem( prefixedKey );
24
+ };
25
+
26
+ return [ value, saveValue, removeValue ] as const;
27
+ };
28
+
29
+ const subscribeToSessionStorage = < T >( key: string, subscriber: ( value: T ) => void ) => {
30
+ subscriber( getSessionStorageItem( key ) as T );
31
+
32
+ const abortController = new AbortController();
33
+
34
+ window.addEventListener(
35
+ 'storage',
36
+ ( e ) => {
37
+ if ( e.key !== key || e.storageArea !== sessionStorage ) {
38
+ return;
39
+ }
40
+
41
+ subscriber( getSessionStorageItem( key ) as T );
42
+ },
43
+ { signal: abortController.signal }
44
+ );
45
+
46
+ return () => {
47
+ abortController.abort();
48
+ };
49
+ };