-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathuseStateWithHistory.ts
134 lines (113 loc) Β· 4.3 KB
/
useStateWithHistory.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { Dispatch, useCallback, useMemo, useRef, useState } from 'react';
import { useFirstMountState } from './useFirstMountState';
import { IHookStateInitAction, IHookStateSetAction, resolveHookState } from './misc/hookState';
interface HistoryState<S> {
history: S[];
position: number;
capacity: number;
back: (amount?: number) => void;
forward: (amount?: number) => void;
go: (position: number) => void;
}
export type UseStateHistoryReturn<S> = [S, Dispatch<IHookStateSetAction<S>>, HistoryState<S>];
export function useStateWithHistory<S, I extends S>(
initialState: IHookStateInitAction<S>,
capacity?: number,
initialHistory?: I[]
): UseStateHistoryReturn<S>;
export function useStateWithHistory<S = undefined>(): UseStateHistoryReturn<S | undefined>;
export function useStateWithHistory<S, I extends S>(
initialState?: IHookStateInitAction<S>,
capacity: number = 10,
initialHistory?: I[]
): UseStateHistoryReturn<S> {
if (capacity < 1) {
throw new Error(`Capacity has to be greater than 1, got '${capacity}'`);
}
const isFirstMount = useFirstMountState();
const [state, innerSetState] = useState<S>(initialState as S);
const history = useRef<S[]>((initialHistory ?? []) as S[]);
const historyPosition = useRef(0);
// do the states manipulation only on first mount, no sense to load re-renders with useless calculations
if (isFirstMount) {
if (history.current.length) {
// if last element of history !== initial - push initial to history
if (history.current[history.current.length - 1] !== initialState) {
history.current.push(initialState as I);
}
// if initial history bigger that capacity - crop the first elements out
if (history.current.length > capacity) {
history.current = history.current.slice(history.current.length - capacity);
}
} else {
// initiate the history with initial state
history.current.push(initialState as I);
}
historyPosition.current = history.current.length && history.current.length - 1;
}
const setState = useCallback(
(newState: IHookStateSetAction<S>): void => {
innerSetState((currentState) => {
newState = resolveHookState(newState, currentState);
// is state has changed
if (newState !== currentState) {
// if current position is not the last - pop element to the right
if (historyPosition.current < history.current.length - 1) {
history.current = history.current.slice(0, historyPosition.current + 1);
}
historyPosition.current = history.current.push(newState as I) - 1;
// if capacity is reached - shift first elements
if (history.current.length > capacity) {
history.current = history.current.slice(history.current.length - capacity);
}
}
return newState;
});
},
[state, capacity]
) as Dispatch<IHookStateSetAction<S>>;
const historyState = useMemo(
() => ({
history: history.current,
position: historyPosition.current,
capacity,
back: (amount: number = 1) => {
// don't do anything if we already at the left border
if (!historyPosition.current) {
return;
}
innerSetState(() => {
historyPosition.current -= Math.min(amount, historyPosition.current);
return history.current[historyPosition.current];
});
},
forward: (amount: number = 1) => {
// don't do anything if we already at the right border
if (historyPosition.current === history.current.length - 1) {
return;
}
innerSetState(() => {
historyPosition.current = Math.min(
historyPosition.current + amount,
history.current.length - 1
);
return history.current[historyPosition.current];
});
},
go: (position: number) => {
if (position === historyPosition.current) {
return;
}
innerSetState(() => {
historyPosition.current =
position < 0
? Math.max(history.current.length + position, 0)
: Math.min(history.current.length - 1, position);
return history.current[historyPosition.current];
});
},
}),
[state]
);
return [state, setState, historyState];
}