Skip to content

Commit ae1689e

Browse files
committed
[ADD] useSessionStorageState app
1 parent a31c869 commit ae1689e

17 files changed

Lines changed: 556 additions & 225 deletions

File tree

apps/react-tools-demo/scripts/generateMarkdown.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,14 @@ function splitType(string){
5858
function parseType(string, tab=0) {
5959
const typeArray= [];
6060
let currTab = tab;
61-
if(string.startsWith("[")) {
61+
if(string.includes(" | ")) {
62+
typeArray.push(Array(currTab*4).fill(true).map(() => " ").join("")+ "- __Union of__: ");
63+
currTab++;
64+
string.split(" | ").forEach(split => {
65+
typeArray.push(...parseType(split, currTab, true))
66+
});
67+
}
68+
else if(string.startsWith("[")) {
6269
typeArray.push(Array(currTab*4).fill(true).map(() => " ").join("")+ "- __Array__: ");
6370
currTab++;
6471
string = string.substring(1, string.length-1);
@@ -244,14 +251,27 @@ function buildHooksUtilsMarkdownObject(file) {
244251
}
245252
});
246253
let codeLines = fileSplitted.slice(fileSplitted.findIndex(line => line === lines.at(-1))+2);
247-
let typeLine = codeLines[codeLines.findIndex(line => !line.startsWith("//") && !line.startsWith(" */") && !line.startsWith("*/") && !line.startsWith(("/*")))];
254+
let typeLineIndex = codeLines.findIndex(line => !line.startsWith("//") && !line.startsWith(" */") && !line.startsWith("*/") && !line.startsWith(("/*")));
255+
let typeLine = codeLines[typeLineIndex];
248256
if(typeLine.startsWith("export")) {
249257
typeLine = typeLine.substring("export ".length);
250258
}
251259
if(typeLine.startsWith("default")) {
252260
typeLine = typeLine.substring("default ".length);
253261
}
254262
if(typeLine.startsWith("function")) {
263+
if(codeLines[typeLineIndex+1].startsWith("function")) {
264+
//is a overloaded function
265+
let index = typeLineIndex+1,
266+
line = codeLines[index].substring("function ".length).split("").reverse().join("");
267+
while(!line.startsWith("{")) {
268+
line = codeLines[index+1].split("").reverse().join("");
269+
index++;
270+
}
271+
line = codeLines[index].substring("function ".length, codeLines[index].length-1);
272+
obj.type = line;
273+
return obj;
274+
}
255275
typeLine = typeLine.substring("function ".length);
256276
}
257277
if(typeLine.startsWith("const")) {

apps/react-tools-demo/src/components/hooks/useLocalStorage/UseLocalStorage.tsx

Lines changed: 0 additions & 51 deletions
This file was deleted.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { BaseSyntheticEvent, useCallback, useEffect, useRef } from "react";
2+
import { useLocalStorageState } from "../../../../../../packages/react-tools/src/hooks";
3+
4+
/**
5+
The component has:
6+
- An internal _useLocalStorage_ state with __key__ _demo_ and __initialState__ _prova_ from which gets __remove__ function.
7+
- A Child component 1 that uses _useLocalStorage_ with _read_ __mode__ and __key__ _demo_ to read from localStorage.
8+
- A Child component 2 that uses _useLocalStorage_ with _write_ __mode__ and __key__ _demo_ to write to LocalStorage with an onSubmit form event with a input text value.
9+
- An useEffect that invokes __remove__ function and delete item with key _demo_ from localStorage when the component unmounts.
10+
11+
When component is mounted, the Child1 state is _prova_. It can be changed by Child2. If you change state and open the page into another tab, after mounting, the Child1 display the new value of state and every changes made is reflected in both tabs.
12+
*/
13+
const Child = () => {
14+
const [state] = useLocalStorageState<string>({key:"demo", opts: {mode: "read"}});
15+
return (<>
16+
<h3>Child component 1</h3>
17+
<p>State is {state}</p>
18+
</>)
19+
}
20+
21+
const Child2 = () => {
22+
const [setState] = useLocalStorageState<string>({key:"demo", opts: {mode: "write"}});
23+
return (<>
24+
<h3>Child component 2</h3>
25+
<form onSubmit={(e: BaseSyntheticEvent) => {
26+
setState(e.target[0].value);
27+
e.preventDefault();
28+
}}>
29+
<input type="text" />
30+
</form>
31+
</>)
32+
}
33+
34+
const UseLocalStorageState = () => {
35+
const [, , , remove] = useLocalStorageState({ key: "demo", initialState: "prova" });
36+
const render = useRef(0);
37+
const clear = useCallback(() => remove(), [remove]);
38+
39+
useEffect(() => {
40+
render.current++;
41+
return () => {
42+
render.current === 2 && clear();
43+
}
44+
}, [clear]);
45+
46+
return (<>
47+
<Child />
48+
<br />
49+
<Child2/>
50+
</>);
51+
}
52+
53+
UseLocalStorageState.displayName = "UseLocalStorageState";
54+
55+
export { UseLocalStorageState };
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { BaseSyntheticEvent, useCallback, useEffect } from "react";
2+
import { useSessionStorageState } from "../../../../../../packages/react-tools/src/hooks";
3+
4+
/**
5+
The component has:
6+
- An internal _useSessionStorage_ state with __key__ _demo_ and __initialState__ _prova_ from which gets __remove__ function.
7+
- A Child component 1 that uses _useSessionStorage_ with _read_ __mode__ and __key__ _demo_ to read from sessionStorage.
8+
- A Child component 2 that uses _useSessionStorage_ with _write_ __mode__ and __key__ _demo_ to write to sessionStorage with an onSubmit form event with a input text value.
9+
- An useEffect that invokes __remove__ function and delete item with key _demo_ from sessionStorage when the component unmounts.
10+
11+
When component is mounted, the Child1 state is _prova_. It can be changed by Child2. If you open the page into another tab, changes aren't reflected in both tabs.
12+
*/
13+
const Child = () => {
14+
const [state] = useSessionStorageState<string>({key:"demo", opts: {mode: "read"}});
15+
return (<>
16+
<h3>Child component 1</h3>
17+
<p>State is {state}</p>
18+
</>)
19+
}
20+
21+
const Child2 = () => {
22+
const [setState] = useSessionStorageState<string>({key:"demo", opts: {mode: "write"}});
23+
return (<>
24+
<h3>Child component 2</h3>
25+
<form onSubmit={(e: BaseSyntheticEvent) => {
26+
setState(e.target[0].value);
27+
e.preventDefault();
28+
}}>
29+
<input type="text" />
30+
</form>
31+
</>)
32+
}
33+
34+
const UseSessionStorageState = () => {
35+
const [, , , remove] = useSessionStorageState({ key: "demo", initialState: "prova" });
36+
37+
const clear = useCallback(() => remove(), [remove]);
38+
39+
useEffect(() => {
40+
return () => {
41+
clear();
42+
}
43+
}, [clear]);
44+
45+
return (<>
46+
<Child />
47+
<br />
48+
<Child2/>
49+
</>);
50+
}
51+
52+
UseSessionStorageState.displayName = "UseSessionStorageState";
53+
54+
export { UseSessionStorageState };

apps/react-tools-demo/src/constants/components.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ export const COMPONENTS = [
77
"useReducerGetReset",
88
"useReducerHistory",
99
"useReducerHistoryGetter",
10-
"useLocalStorage",
10+
"useLocalStorageState",
11+
"useSessionStorageState",
1112
"useMemoizedFunction",
1213
"useMemoCompare",
1314
"useMemoDeepCompare",

apps/react-tools-demo/src/markdown/useEventDispatcher.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,7 @@ target on which dispatch event
5656
> ### Returns
5757
>
5858
> __dispatch__: function that dispatch the event on target
59-
> - _(evt: Event | CustomEvent) => void_
59+
> - __Union of__:
60+
> - _(evt: Event_
61+
> - _CustomEvent) => void_
6062
>

apps/react-tools-demo/src/markdown/useLocalStorage.md

Lines changed: 0 additions & 82 deletions
This file was deleted.

0 commit comments

Comments
 (0)