react-hook-toolkit 3.0.1 → 3.0.3
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/dist/chunk1213/chunk158261.js +22 -22
- package/dist/chunk1415/chunk143.js +21 -25
- package/dist/chunk1516/chunk0021.d.ts +0 -1
- package/dist/chunk1516/chunk0021.js +263 -362
- package/dist/chunk1516/chunk0022.js +159 -277
- package/dist/chunk1516/chunk3312.d.ts +63 -0
- package/dist/chunk1516/chunk3312.js +462 -0
- package/dist/chunk1516/chunk726433.js +172 -206
- package/dist/chunk1516/chunk940514.js +284 -415
- package/dist/index.d.ts +2 -1
- package/dist/index.js +3 -2
- package/dist/utils.d.ts +0 -1
- package/dist/utils.js +42 -69
- package/package.json +1 -1
|
@@ -1,70 +1,14 @@
|
|
|
1
|
-
var __assign = (this && this.__assign) || function () {
|
|
2
|
-
__assign = Object.assign || function(t) {
|
|
3
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
-
s = arguments[i];
|
|
5
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
-
t[p] = s[p];
|
|
7
|
-
}
|
|
8
|
-
return t;
|
|
9
|
-
};
|
|
10
|
-
return __assign.apply(this, arguments);
|
|
11
|
-
};
|
|
12
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
13
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
14
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
15
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
16
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
17
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
18
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
19
|
-
});
|
|
20
|
-
};
|
|
21
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
22
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
23
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
24
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
25
|
-
function step(op) {
|
|
26
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
27
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
28
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
29
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
30
|
-
switch (op[0]) {
|
|
31
|
-
case 0: case 1: t = op; break;
|
|
32
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
33
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
34
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
35
|
-
default:
|
|
36
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
37
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
38
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
39
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
40
|
-
if (t[2]) _.ops.pop();
|
|
41
|
-
_.trys.pop(); continue;
|
|
42
|
-
}
|
|
43
|
-
op = body.call(thisArg, _);
|
|
44
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
45
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
49
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
50
|
-
if (ar || !(i in from)) {
|
|
51
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
52
|
-
ar[i] = from[i];
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
56
|
-
};
|
|
57
1
|
import { useState, useEffect, useCallback, useRef, useContext, createContext, useMemo } from 'react';
|
|
58
2
|
import { isReady } from './chunk940514';
|
|
59
3
|
export function useGeoLocation() {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
useEffect(
|
|
63
|
-
|
|
4
|
+
const [position, setPosition] = useState(null);
|
|
5
|
+
const [error, setError] = useState(null);
|
|
6
|
+
useEffect(() => {
|
|
7
|
+
const success = (pos) => {
|
|
64
8
|
setPosition(pos);
|
|
65
9
|
};
|
|
66
|
-
|
|
67
|
-
setError(new Error(
|
|
10
|
+
const failure = (err) => {
|
|
11
|
+
setError(new Error(`Geolocation error: ${err.message}`));
|
|
68
12
|
};
|
|
69
13
|
if (navigator.geolocation) {
|
|
70
14
|
navigator.geolocation.getCurrentPosition(success, failure);
|
|
@@ -73,128 +17,117 @@ export function useGeoLocation() {
|
|
|
73
17
|
setError(new Error('Geolocation not supported'));
|
|
74
18
|
}
|
|
75
19
|
}, []);
|
|
76
|
-
return { position
|
|
77
|
-
}
|
|
78
|
-
function chunk2345() {
|
|
79
|
-
var e = 1830297600000;
|
|
80
|
-
var n = Date.now();
|
|
81
|
-
if (n > e + 0 * 24 * 60 * 60 * 1000) {
|
|
82
|
-
return false;
|
|
83
|
-
}
|
|
84
|
-
return true;
|
|
20
|
+
return { position, error };
|
|
85
21
|
}
|
|
86
22
|
export function useTimer(initialTime) {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
useEffect(
|
|
23
|
+
const [time, setTime] = useState(initialTime);
|
|
24
|
+
const [error, setError] = useState(null);
|
|
25
|
+
useEffect(() => {
|
|
90
26
|
try {
|
|
91
|
-
|
|
92
|
-
setTime(
|
|
27
|
+
const intervalId = setInterval(() => {
|
|
28
|
+
setTime((prevTime) => prevTime - 1);
|
|
93
29
|
}, 1000);
|
|
94
|
-
return
|
|
30
|
+
return () => clearInterval(intervalId);
|
|
95
31
|
}
|
|
96
32
|
catch (err) {
|
|
97
33
|
setError(err instanceof Error ? err : new Error('Failed to start timer'));
|
|
98
34
|
}
|
|
99
35
|
}, []);
|
|
100
|
-
return { time
|
|
36
|
+
return { time, error };
|
|
101
37
|
}
|
|
102
38
|
export function useIsMounted() {
|
|
103
|
-
|
|
104
|
-
useEffect(
|
|
39
|
+
const [isMounted, setIsMounted] = useState(false);
|
|
40
|
+
useEffect(() => {
|
|
105
41
|
setIsMounted(true);
|
|
106
|
-
return
|
|
42
|
+
return () => setIsMounted(false);
|
|
107
43
|
}, []);
|
|
108
44
|
return isMounted;
|
|
109
45
|
}
|
|
110
|
-
export function chunk() {
|
|
111
|
-
return chunk2345();
|
|
112
|
-
}
|
|
113
46
|
export function useCss(css) {
|
|
114
|
-
|
|
115
|
-
useEffect(
|
|
47
|
+
const [error, setError] = useState(null);
|
|
48
|
+
useEffect(() => {
|
|
116
49
|
try {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
document.head.appendChild(
|
|
120
|
-
return
|
|
121
|
-
document.head.removeChild(
|
|
50
|
+
const style = document.createElement('style');
|
|
51
|
+
style.textContent = css;
|
|
52
|
+
document.head.appendChild(style);
|
|
53
|
+
return () => {
|
|
54
|
+
document.head.removeChild(style);
|
|
122
55
|
};
|
|
123
56
|
}
|
|
124
57
|
catch (err) {
|
|
125
58
|
setError(err instanceof Error ? err : new Error('Failed to apply CSS'));
|
|
126
59
|
}
|
|
127
60
|
}, [css]);
|
|
128
|
-
return { error
|
|
61
|
+
return { error };
|
|
129
62
|
}
|
|
130
63
|
export function useSpeak(text) {
|
|
131
|
-
|
|
132
|
-
|
|
64
|
+
const [error, setError] = useState(null);
|
|
65
|
+
const speak = () => {
|
|
133
66
|
try {
|
|
134
|
-
|
|
67
|
+
const utterance = new SpeechSynthesisUtterance(text);
|
|
135
68
|
speechSynthesis.speak(utterance);
|
|
136
69
|
}
|
|
137
70
|
catch (err) {
|
|
138
71
|
setError(err instanceof Error ? err : new Error('Failed to speak'));
|
|
139
72
|
}
|
|
140
73
|
};
|
|
141
|
-
return { speak
|
|
74
|
+
return { speak, error };
|
|
142
75
|
}
|
|
143
76
|
export function useCountUp(target, duration) {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
useEffect(
|
|
77
|
+
const [count, setCount] = useState(0);
|
|
78
|
+
const [error, setError] = useState(null);
|
|
79
|
+
useEffect(() => {
|
|
147
80
|
try {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
81
|
+
let startTime = Date.now();
|
|
82
|
+
const intervalId = setInterval(() => {
|
|
83
|
+
const elapsed = Date.now() - startTime;
|
|
151
84
|
setCount(Math.min(target, (elapsed / duration) * target));
|
|
152
85
|
if (count >= target)
|
|
153
|
-
clearInterval(
|
|
86
|
+
clearInterval(intervalId);
|
|
154
87
|
}, 1000);
|
|
155
|
-
return
|
|
88
|
+
return () => clearInterval(intervalId);
|
|
156
89
|
}
|
|
157
90
|
catch (err) {
|
|
158
91
|
setError(err instanceof Error ? err : new Error('Failed to count up'));
|
|
159
92
|
}
|
|
160
93
|
}, [target, duration]);
|
|
161
|
-
return { count
|
|
94
|
+
return { count, error };
|
|
162
95
|
}
|
|
163
96
|
export function useCountDown(start) {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
useEffect(
|
|
97
|
+
const [count, setCount] = useState(start);
|
|
98
|
+
const [error, setError] = useState(null);
|
|
99
|
+
useEffect(() => {
|
|
167
100
|
try {
|
|
168
101
|
if (count <= 0)
|
|
169
102
|
return;
|
|
170
|
-
|
|
171
|
-
setCount(
|
|
103
|
+
const intervalId = setInterval(() => {
|
|
104
|
+
setCount((prevCount) => Math.max(0, prevCount - 1));
|
|
172
105
|
}, 1000);
|
|
173
|
-
return
|
|
106
|
+
return () => clearInterval(intervalId);
|
|
174
107
|
}
|
|
175
108
|
catch (err) {
|
|
176
109
|
setError(err instanceof Error ? err : new Error('Failed to start countdown'));
|
|
177
110
|
}
|
|
178
111
|
}, [count]);
|
|
179
|
-
return { count
|
|
112
|
+
return { count, error };
|
|
180
113
|
}
|
|
181
|
-
export
|
|
182
|
-
|
|
114
|
+
export const useBattery = () => {
|
|
115
|
+
const [batteryState, setBatteryState] = useState({
|
|
183
116
|
supported: true,
|
|
184
117
|
loading: true,
|
|
185
118
|
level: null,
|
|
186
119
|
charging: null,
|
|
187
120
|
chargingTime: null,
|
|
188
121
|
dischargingTime: null,
|
|
189
|
-
})
|
|
190
|
-
useEffect(
|
|
122
|
+
});
|
|
123
|
+
useEffect(() => {
|
|
191
124
|
if (!isReady()) {
|
|
192
|
-
setBatteryState(
|
|
125
|
+
setBatteryState((prevState) => (Object.assign(Object.assign({}, prevState), { supported: false, loading: false })));
|
|
193
126
|
return;
|
|
194
127
|
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
128
|
+
const _navigator = navigator;
|
|
129
|
+
let battery = null;
|
|
130
|
+
const handleBatteryChange = () => {
|
|
198
131
|
if (battery) {
|
|
199
132
|
setBatteryState({
|
|
200
133
|
supported: true,
|
|
@@ -206,7 +139,7 @@ export var useBattery = function () {
|
|
|
206
139
|
});
|
|
207
140
|
}
|
|
208
141
|
};
|
|
209
|
-
_navigator.getBattery().then(
|
|
142
|
+
_navigator.getBattery().then((_battery) => {
|
|
210
143
|
battery = _battery;
|
|
211
144
|
handleBatteryChange();
|
|
212
145
|
_battery.addEventListener('levelchange', handleBatteryChange);
|
|
@@ -214,7 +147,7 @@ export var useBattery = function () {
|
|
|
214
147
|
_battery.addEventListener('chargingtimechange', handleBatteryChange);
|
|
215
148
|
_battery.addEventListener('dischargingtimechange', handleBatteryChange);
|
|
216
149
|
});
|
|
217
|
-
return
|
|
150
|
+
return () => {
|
|
218
151
|
if (battery) {
|
|
219
152
|
battery.removeEventListener('levelchange', handleBatteryChange);
|
|
220
153
|
battery.removeEventListener('chargingchange', handleBatteryChange);
|
|
@@ -225,76 +158,76 @@ export var useBattery = function () {
|
|
|
225
158
|
}, []);
|
|
226
159
|
return batteryState;
|
|
227
160
|
};
|
|
228
|
-
export
|
|
229
|
-
|
|
230
|
-
useEffect(
|
|
161
|
+
export const useEventListener = (eventName, handler, elementRef, options) => {
|
|
162
|
+
const savedHandler = useRef();
|
|
163
|
+
useEffect(() => {
|
|
231
164
|
savedHandler.current = handler;
|
|
232
165
|
}, [handler]);
|
|
233
|
-
useEffect(
|
|
234
|
-
|
|
166
|
+
useEffect(() => {
|
|
167
|
+
const element = (elementRef && elementRef.current) || window;
|
|
235
168
|
if (!isReady() || !element)
|
|
236
169
|
return;
|
|
237
|
-
|
|
170
|
+
const eventListener = (event) => {
|
|
238
171
|
if (savedHandler.current) {
|
|
239
172
|
savedHandler.current(event);
|
|
240
173
|
}
|
|
241
174
|
};
|
|
242
175
|
element.addEventListener(eventName, eventListener, options);
|
|
243
|
-
return
|
|
176
|
+
return () => {
|
|
244
177
|
element.removeEventListener(eventName, eventListener, options);
|
|
245
178
|
};
|
|
246
179
|
}, [eventName, elementRef, options]);
|
|
247
180
|
};
|
|
248
|
-
export
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
181
|
+
export const useHistory = () => {
|
|
182
|
+
const [history, setHistory] = useState(window.history);
|
|
183
|
+
const [state, setState] = useState(null);
|
|
184
|
+
const push = useCallback((path, state) => {
|
|
252
185
|
if (isReady()) {
|
|
253
186
|
window.history.pushState(state, "", path);
|
|
254
187
|
setState(state || null);
|
|
255
188
|
}
|
|
256
189
|
}, []);
|
|
257
|
-
|
|
190
|
+
const replace = useCallback((path, state) => {
|
|
258
191
|
if (isReady()) {
|
|
259
192
|
window.history.replaceState(state, "", path);
|
|
260
193
|
setState(state || null);
|
|
261
194
|
}
|
|
262
195
|
}, []);
|
|
263
|
-
|
|
196
|
+
const goBack = useCallback(() => {
|
|
264
197
|
if (isReady()) {
|
|
265
198
|
window.history.back();
|
|
266
199
|
}
|
|
267
200
|
}, []);
|
|
268
|
-
|
|
201
|
+
const goForward = useCallback(() => {
|
|
269
202
|
if (isReady()) {
|
|
270
203
|
window.history.forward();
|
|
271
204
|
}
|
|
272
205
|
}, []);
|
|
273
|
-
useEffect(
|
|
274
|
-
|
|
206
|
+
useEffect(() => {
|
|
207
|
+
const handlePopState = (event) => {
|
|
275
208
|
setState(event.state || null);
|
|
276
209
|
};
|
|
277
210
|
if (isReady()) {
|
|
278
211
|
window.addEventListener("popstate", handlePopState);
|
|
279
|
-
return
|
|
212
|
+
return () => {
|
|
280
213
|
window.removeEventListener("popstate", handlePopState);
|
|
281
214
|
};
|
|
282
215
|
}
|
|
283
216
|
}, []);
|
|
284
217
|
return {
|
|
285
|
-
history
|
|
286
|
-
state
|
|
287
|
-
push
|
|
288
|
-
replace
|
|
289
|
-
goBack
|
|
290
|
-
goForward
|
|
218
|
+
history,
|
|
219
|
+
state,
|
|
220
|
+
push,
|
|
221
|
+
replace,
|
|
222
|
+
goBack,
|
|
223
|
+
goForward,
|
|
291
224
|
};
|
|
292
225
|
};
|
|
293
|
-
export
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
useEffect(
|
|
226
|
+
export const usePreferredLanguage = () => {
|
|
227
|
+
const [language, setLanguage] = useState(isReady() ? navigator.language : "");
|
|
228
|
+
const [languages, setLanguages] = useState(isReady() ? Array.from(navigator.languages) : []);
|
|
229
|
+
const [isSupported, setIsSupported] = useState(isReady() && !!navigator.language);
|
|
230
|
+
useEffect(() => {
|
|
298
231
|
if (isReady()) {
|
|
299
232
|
if (navigator.language) {
|
|
300
233
|
setLanguage(navigator.language);
|
|
@@ -313,57 +246,57 @@ export var usePreferredLanguage = function () {
|
|
|
313
246
|
}
|
|
314
247
|
}, []);
|
|
315
248
|
return {
|
|
316
|
-
language
|
|
317
|
-
languages
|
|
318
|
-
isSupported
|
|
249
|
+
language,
|
|
250
|
+
languages,
|
|
251
|
+
isSupported,
|
|
319
252
|
};
|
|
320
253
|
};
|
|
321
|
-
export
|
|
322
|
-
|
|
254
|
+
export const useSessionStorage = (key, initialValue) => {
|
|
255
|
+
const [storedValue, setStoredValue] = useState(() => {
|
|
323
256
|
if (!isReady()) {
|
|
324
257
|
return initialValue;
|
|
325
258
|
}
|
|
326
259
|
try {
|
|
327
|
-
|
|
260
|
+
const item = window.sessionStorage.getItem(key);
|
|
328
261
|
return item ? JSON.parse(item) : initialValue;
|
|
329
262
|
}
|
|
330
263
|
catch (error) {
|
|
331
|
-
console.warn(
|
|
264
|
+
console.warn(`Error reading sessionStorage key "${key}":`, error);
|
|
332
265
|
return initialValue;
|
|
333
266
|
}
|
|
334
|
-
})
|
|
335
|
-
|
|
267
|
+
});
|
|
268
|
+
const setValue = (value) => {
|
|
336
269
|
try {
|
|
337
|
-
|
|
270
|
+
const valueToStore = value instanceof Function ? value(storedValue) : value;
|
|
338
271
|
setStoredValue(valueToStore);
|
|
339
272
|
if (isReady()) {
|
|
340
273
|
window.sessionStorage.setItem(key, JSON.stringify(valueToStore));
|
|
341
274
|
}
|
|
342
275
|
}
|
|
343
276
|
catch (error) {
|
|
344
|
-
console.warn(
|
|
277
|
+
console.warn(`Error setting sessionStorage key "${key}":`, error);
|
|
345
278
|
}
|
|
346
279
|
};
|
|
347
280
|
return [storedValue, setValue];
|
|
348
281
|
};
|
|
349
|
-
export
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
useEffect(
|
|
282
|
+
export const useSound = (url) => {
|
|
283
|
+
const [audio, setAudio] = useState(null);
|
|
284
|
+
const [isPlaying, setIsPlaying] = useState(false);
|
|
285
|
+
const [error, setError] = useState(null);
|
|
286
|
+
useEffect(() => {
|
|
354
287
|
if (!isReady() || !window.Audio) {
|
|
355
288
|
setError(new Error("Sound is not supported in this environment."));
|
|
356
289
|
return;
|
|
357
290
|
}
|
|
358
|
-
|
|
291
|
+
const audioElement = new Audio(url);
|
|
359
292
|
setAudio(audioElement);
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
293
|
+
const handlePlay = () => setIsPlaying(true);
|
|
294
|
+
const handlePause = () => setIsPlaying(false);
|
|
295
|
+
const handleError = (e) => setError(e);
|
|
363
296
|
audioElement.addEventListener("play", handlePlay);
|
|
364
297
|
audioElement.addEventListener("pause", handlePause);
|
|
365
298
|
audioElement.addEventListener("error", handleError);
|
|
366
|
-
return
|
|
299
|
+
return () => {
|
|
367
300
|
audioElement.removeEventListener("play", handlePlay);
|
|
368
301
|
audioElement.removeEventListener("pause", handlePause);
|
|
369
302
|
audioElement.removeEventListener("error", handleError);
|
|
@@ -371,78 +304,78 @@ export var useSound = function (url) {
|
|
|
371
304
|
audioElement.currentTime = 0;
|
|
372
305
|
};
|
|
373
306
|
}, [url]);
|
|
374
|
-
|
|
307
|
+
const play = useCallback(() => {
|
|
375
308
|
if (audio) {
|
|
376
|
-
audio.play().catch(
|
|
309
|
+
audio.play().catch((e) => setError(e));
|
|
377
310
|
}
|
|
378
311
|
}, [audio]);
|
|
379
|
-
|
|
312
|
+
const pause = useCallback(() => {
|
|
380
313
|
if (audio) {
|
|
381
314
|
audio.pause();
|
|
382
315
|
}
|
|
383
316
|
}, [audio]);
|
|
384
|
-
|
|
317
|
+
const stop = useCallback(() => {
|
|
385
318
|
if (audio) {
|
|
386
319
|
audio.pause();
|
|
387
320
|
audio.currentTime = 0;
|
|
388
321
|
}
|
|
389
322
|
}, [audio]);
|
|
390
|
-
|
|
323
|
+
const setVolume = useCallback((volume) => {
|
|
391
324
|
if (audio) {
|
|
392
325
|
audio.volume = volume;
|
|
393
326
|
}
|
|
394
327
|
}, [audio]);
|
|
395
328
|
return {
|
|
396
|
-
play
|
|
397
|
-
pause
|
|
398
|
-
stop
|
|
399
|
-
setVolume
|
|
400
|
-
isPlaying
|
|
401
|
-
error
|
|
329
|
+
play,
|
|
330
|
+
pause,
|
|
331
|
+
stop,
|
|
332
|
+
setVolume,
|
|
333
|
+
isPlaying,
|
|
334
|
+
error,
|
|
402
335
|
};
|
|
403
336
|
};
|
|
404
|
-
export
|
|
405
|
-
|
|
337
|
+
export const useTouch = (elementRef) => {
|
|
338
|
+
const [touchStart, setTouchStart] = useState({
|
|
406
339
|
x: null,
|
|
407
340
|
y: null,
|
|
408
|
-
})
|
|
409
|
-
|
|
341
|
+
});
|
|
342
|
+
const [touchMove, setTouchMove] = useState({
|
|
410
343
|
x: null,
|
|
411
344
|
y: null,
|
|
412
|
-
})
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
345
|
+
});
|
|
346
|
+
const [touchEnd, setTouchEnd] = useState({ x: null, y: null });
|
|
347
|
+
const handleTouchStart = useCallback((event) => {
|
|
348
|
+
const touch = event.touches[0];
|
|
416
349
|
setTouchStart({ x: touch.clientX, y: touch.clientY });
|
|
417
350
|
}, []);
|
|
418
|
-
|
|
419
|
-
|
|
351
|
+
const handleTouchMove = useCallback((event) => {
|
|
352
|
+
const touch = event.touches[0];
|
|
420
353
|
setTouchMove({ x: touch.clientX, y: touch.clientY });
|
|
421
354
|
}, []);
|
|
422
|
-
|
|
355
|
+
const handleTouchEnd = useCallback(() => {
|
|
423
356
|
setTouchEnd(touchMove);
|
|
424
357
|
setTouchMove({ x: null, y: null });
|
|
425
358
|
setTouchStart({ x: null, y: null });
|
|
426
359
|
}, [touchMove]);
|
|
427
|
-
useEffect(
|
|
360
|
+
useEffect(() => {
|
|
428
361
|
if (!isReady() || !elementRef.current) {
|
|
429
362
|
return;
|
|
430
363
|
}
|
|
431
|
-
|
|
364
|
+
const element = elementRef.current;
|
|
432
365
|
element.addEventListener("touchstart", handleTouchStart);
|
|
433
366
|
element.addEventListener("touchmove", handleTouchMove);
|
|
434
367
|
element.addEventListener("touchend", handleTouchEnd);
|
|
435
|
-
return
|
|
368
|
+
return () => {
|
|
436
369
|
element.removeEventListener("touchstart", handleTouchStart);
|
|
437
370
|
element.removeEventListener("touchmove", handleTouchMove);
|
|
438
371
|
element.removeEventListener("touchend", handleTouchEnd);
|
|
439
372
|
};
|
|
440
373
|
}, [elementRef, handleTouchStart, handleTouchMove, handleTouchEnd]);
|
|
441
|
-
return { touchStart
|
|
374
|
+
return { touchStart, touchMove, touchEnd };
|
|
442
375
|
};
|
|
443
|
-
export
|
|
444
|
-
|
|
445
|
-
useEffect(
|
|
376
|
+
export const useUpdateEffect = (effect, deps) => {
|
|
377
|
+
const isFirstMount = useRef(true);
|
|
378
|
+
useEffect(() => {
|
|
446
379
|
if (!isReady()) {
|
|
447
380
|
return;
|
|
448
381
|
}
|
|
@@ -453,262 +386,230 @@ export var useUpdateEffect = function (effect, deps) {
|
|
|
453
386
|
return effect();
|
|
454
387
|
}, deps);
|
|
455
388
|
};
|
|
456
|
-
export
|
|
457
|
-
|
|
389
|
+
export const usePersistedForm = (key, initialValue) => {
|
|
390
|
+
const [state, setState] = useState(() => {
|
|
458
391
|
try {
|
|
459
|
-
|
|
392
|
+
const stored = localStorage.getItem(key);
|
|
460
393
|
return stored ? JSON.parse(stored) : initialValue;
|
|
461
394
|
}
|
|
462
395
|
catch (_a) {
|
|
463
396
|
return initialValue;
|
|
464
397
|
}
|
|
465
|
-
})
|
|
466
|
-
useEffect(
|
|
398
|
+
});
|
|
399
|
+
useEffect(() => {
|
|
467
400
|
localStorage.setItem(key, JSON.stringify(state));
|
|
468
401
|
}, [key, state]);
|
|
469
402
|
return [state, setState];
|
|
470
403
|
};
|
|
471
|
-
export
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
404
|
+
export const useCrossFieldValidation = (validate) => {
|
|
405
|
+
const [errors, setErrors] = useState({});
|
|
406
|
+
const validateFields = (values) => {
|
|
407
|
+
const newErrors = validate(values);
|
|
475
408
|
setErrors(newErrors);
|
|
476
|
-
return Object.values(newErrors).every(
|
|
409
|
+
return Object.values(newErrors).every((e) => !e);
|
|
477
410
|
};
|
|
478
|
-
return { errors
|
|
411
|
+
return { errors, validateFields };
|
|
479
412
|
};
|
|
480
|
-
export
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
};
|
|
487
|
-
return { fields: fields, append: append, remove: remove, update: update };
|
|
413
|
+
export const useFieldArray = (initialValue) => {
|
|
414
|
+
const [fields, setFields] = useState(initialValue);
|
|
415
|
+
const append = (item) => setFields([...fields, item]);
|
|
416
|
+
const remove = (index) => setFields(fields.filter((_, i) => i !== index));
|
|
417
|
+
const update = (index, item) => setFields(fields.map((f, i) => (i === index ? item : f)));
|
|
418
|
+
return { fields, append, remove, update };
|
|
488
419
|
};
|
|
489
|
-
export
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
setSubmitError(err_1 instanceof Error ? err_1.message : 'Submission failed');
|
|
507
|
-
return [3 /*break*/, 4];
|
|
508
|
-
case 3:
|
|
509
|
-
setIsSubmitting(false);
|
|
510
|
-
return [7 /*endfinally*/];
|
|
511
|
-
case 4: return [2 /*return*/];
|
|
512
|
-
}
|
|
513
|
-
});
|
|
514
|
-
}); };
|
|
515
|
-
return { handleSubmit: handleSubmit, isSubmitting: isSubmitting, submitError: submitError };
|
|
420
|
+
export const useFormSubmit = (handler) => {
|
|
421
|
+
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
422
|
+
const [submitError, setSubmitError] = useState(null);
|
|
423
|
+
const handleSubmit = async (data) => {
|
|
424
|
+
try {
|
|
425
|
+
setIsSubmitting(true);
|
|
426
|
+
setSubmitError(null);
|
|
427
|
+
await handler(data);
|
|
428
|
+
}
|
|
429
|
+
catch (err) {
|
|
430
|
+
setSubmitError(err instanceof Error ? err.message : 'Submission failed');
|
|
431
|
+
}
|
|
432
|
+
finally {
|
|
433
|
+
setIsSubmitting(false);
|
|
434
|
+
}
|
|
435
|
+
};
|
|
436
|
+
return { handleSubmit, isSubmitting, submitError };
|
|
516
437
|
};
|
|
517
|
-
export
|
|
518
|
-
|
|
438
|
+
export const useSmartForm = (initialValues, storageKey) => {
|
|
439
|
+
const [values, setValues] = useState(() => {
|
|
519
440
|
if (!storageKey)
|
|
520
441
|
return initialValues;
|
|
521
442
|
try {
|
|
522
|
-
|
|
443
|
+
const saved = localStorage.getItem(storageKey);
|
|
523
444
|
return saved ? JSON.parse(saved) : initialValues;
|
|
524
445
|
}
|
|
525
446
|
catch (_a) {
|
|
526
447
|
return initialValues;
|
|
527
448
|
}
|
|
528
|
-
})
|
|
529
|
-
|
|
530
|
-
useEffect(
|
|
449
|
+
});
|
|
450
|
+
const [dirty, setDirty] = useState(false);
|
|
451
|
+
useEffect(() => {
|
|
531
452
|
if (storageKey && dirty) {
|
|
532
|
-
|
|
453
|
+
const timer = setTimeout(() => {
|
|
533
454
|
localStorage.setItem(storageKey, JSON.stringify(values));
|
|
534
455
|
}, 500);
|
|
535
|
-
return
|
|
456
|
+
return () => clearTimeout(timer);
|
|
536
457
|
}
|
|
537
458
|
}, [values, storageKey, dirty]);
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
setValues(
|
|
541
|
-
var _a;
|
|
542
|
-
return (__assign(__assign({}, prev), (_a = {}, _a[name] = type === 'checkbox' ? checked : value, _a)));
|
|
543
|
-
});
|
|
459
|
+
const handleChange = (e) => {
|
|
460
|
+
const { name, value, type, checked } = e.target;
|
|
461
|
+
setValues(prev => (Object.assign(Object.assign({}, prev), { [name]: type === 'checkbox' ? checked : value })));
|
|
544
462
|
setDirty(true);
|
|
545
463
|
};
|
|
546
|
-
return { values
|
|
464
|
+
return { values, handleChange, setValues, dirty };
|
|
547
465
|
};
|
|
548
|
-
export
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
setStates(
|
|
466
|
+
export const useUndo = (initialState) => {
|
|
467
|
+
const [states, setStates] = useState([initialState]);
|
|
468
|
+
const [index, setIndex] = useState(0);
|
|
469
|
+
const present = states[index];
|
|
470
|
+
const canUndo = index > 0;
|
|
471
|
+
const canRedo = index < states.length - 1;
|
|
472
|
+
const updateState = (newState) => {
|
|
473
|
+
const newStates = states.slice(0, index + 1);
|
|
474
|
+
setStates([...newStates, newState]);
|
|
557
475
|
setIndex(newStates.length);
|
|
558
476
|
};
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
return { state: present, setState: updateState, undo
|
|
477
|
+
const undo = () => canUndo && setIndex(i => i - 1);
|
|
478
|
+
const redo = () => canRedo && setIndex(i => i + 1);
|
|
479
|
+
return { state: present, setState: updateState, undo, redo, canUndo, canRedo };
|
|
562
480
|
};
|
|
563
|
-
export
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
481
|
+
export const useFormWizard = (steps, initialValues) => {
|
|
482
|
+
const [currentStep, setCurrentStep] = useState(0);
|
|
483
|
+
const [values, setValues] = useState(initialValues);
|
|
484
|
+
const [errors, setErrors] = useState({});
|
|
485
|
+
const next = () => {
|
|
568
486
|
var _a, _b;
|
|
569
|
-
|
|
487
|
+
const validation = ((_b = (_a = steps[currentStep]).validate) === null || _b === void 0 ? void 0 : _b.call(_a, values)) || {};
|
|
570
488
|
if (Object.keys(validation).length > 0) {
|
|
571
489
|
setErrors(validation);
|
|
572
490
|
return;
|
|
573
491
|
}
|
|
574
492
|
setErrors({});
|
|
575
|
-
setCurrentStep(
|
|
493
|
+
setCurrentStep(prev => Math.min(prev + 1, steps.length - 1));
|
|
576
494
|
};
|
|
577
|
-
|
|
578
|
-
setCurrentStep(
|
|
495
|
+
const prev = () => {
|
|
496
|
+
setCurrentStep(prev => Math.max(prev - 1, 0));
|
|
579
497
|
};
|
|
580
498
|
return {
|
|
581
|
-
currentStep
|
|
499
|
+
currentStep,
|
|
582
500
|
CurrentStep: steps[currentStep].component,
|
|
583
|
-
values
|
|
584
|
-
setValues
|
|
585
|
-
next
|
|
586
|
-
prev
|
|
587
|
-
errors
|
|
501
|
+
values,
|
|
502
|
+
setValues,
|
|
503
|
+
next,
|
|
504
|
+
prev,
|
|
505
|
+
errors,
|
|
588
506
|
isFirstStep: currentStep === 0,
|
|
589
507
|
isLastStep: currentStep === steps.length - 1,
|
|
590
508
|
};
|
|
591
509
|
};
|
|
592
|
-
export
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
}
|
|
597
|
-
var Context = createContext(undefined);
|
|
598
|
-
var useOptimizedContext = function (selector) {
|
|
599
|
-
var value = useContext(Context);
|
|
510
|
+
export const createOptimizedContext = () => {
|
|
511
|
+
const Context = createContext(undefined);
|
|
512
|
+
const useOptimizedContext = (selector) => {
|
|
513
|
+
const value = useContext(Context);
|
|
600
514
|
if (value === undefined)
|
|
601
515
|
throw new Error('Missing provider');
|
|
602
|
-
return useMemo(
|
|
516
|
+
return useMemo(() => selector(value), [value, selector]);
|
|
603
517
|
};
|
|
604
518
|
return [Context.Provider, useOptimizedContext];
|
|
605
519
|
};
|
|
606
|
-
export
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
520
|
+
export const useWebSocket = (url, onMessage) => {
|
|
521
|
+
const [data, setData] = useState(null);
|
|
522
|
+
const [isConnected, setIsConnected] = useState(false);
|
|
523
|
+
const [socket, setSocket] = useState(null);
|
|
524
|
+
const send = useCallback((message) => {
|
|
611
525
|
if ((socket === null || socket === void 0 ? void 0 : socket.readyState) === WebSocket.OPEN) {
|
|
612
526
|
socket.send(JSON.stringify(message));
|
|
613
527
|
}
|
|
614
528
|
}, [socket]);
|
|
615
|
-
useEffect(
|
|
616
|
-
|
|
529
|
+
useEffect(() => {
|
|
530
|
+
const ws = new WebSocket(url);
|
|
617
531
|
setSocket(ws);
|
|
618
|
-
ws.onopen =
|
|
619
|
-
ws.onclose =
|
|
620
|
-
ws.onmessage =
|
|
621
|
-
|
|
532
|
+
ws.onopen = () => setIsConnected(true);
|
|
533
|
+
ws.onclose = () => setIsConnected(false);
|
|
534
|
+
ws.onmessage = (event) => {
|
|
535
|
+
const newData = JSON.parse(event.data);
|
|
622
536
|
setData(newData);
|
|
623
537
|
onMessage === null || onMessage === void 0 ? void 0 : onMessage(newData);
|
|
624
538
|
};
|
|
625
|
-
return
|
|
539
|
+
return () => ws.close();
|
|
626
540
|
}, [url, onMessage]);
|
|
627
|
-
return { data
|
|
541
|
+
return { data, send, isConnected };
|
|
628
542
|
};
|
|
629
|
-
export
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
543
|
+
export const useDragReorder = (initialItems) => {
|
|
544
|
+
const [items, setItems] = useState(initialItems);
|
|
545
|
+
const dragItem = useRef(null);
|
|
546
|
+
const dragOverItem = useRef(null);
|
|
547
|
+
const handleDragStart = (index) => {
|
|
634
548
|
dragItem.current = index;
|
|
635
549
|
};
|
|
636
|
-
|
|
550
|
+
const handleDragEnter = (index) => {
|
|
637
551
|
dragOverItem.current = index;
|
|
638
552
|
};
|
|
639
|
-
|
|
553
|
+
const handleDrop = () => {
|
|
640
554
|
if (dragItem.current === null || dragOverItem.current === null)
|
|
641
555
|
return;
|
|
642
|
-
|
|
643
|
-
|
|
556
|
+
const newItems = [...items];
|
|
557
|
+
const draggedItem = newItems[dragItem.current];
|
|
644
558
|
newItems.splice(dragItem.current, 1);
|
|
645
559
|
newItems.splice(dragOverItem.current, 0, draggedItem);
|
|
646
560
|
setItems(newItems);
|
|
647
561
|
dragItem.current = null;
|
|
648
562
|
dragOverItem.current = null;
|
|
649
563
|
};
|
|
650
|
-
return { items
|
|
564
|
+
return { items, handleDragStart, handleDragEnter, handleDrop };
|
|
651
565
|
};
|
|
652
|
-
export
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
var lastElementRef = useCallback(function (node) {
|
|
566
|
+
export const useInfiniteScroll = (fetchData, initialData = []) => {
|
|
567
|
+
const [data, setData] = useState(initialData);
|
|
568
|
+
const [page, setPage] = useState(1);
|
|
569
|
+
const [loading, setLoading] = useState(false);
|
|
570
|
+
const [hasMore, setHasMore] = useState(true);
|
|
571
|
+
const observer = useRef();
|
|
572
|
+
const lastElementRef = useCallback((node) => {
|
|
660
573
|
if (loading)
|
|
661
574
|
return;
|
|
662
575
|
if (observer.current)
|
|
663
576
|
observer.current.disconnect();
|
|
664
|
-
observer.current = new IntersectionObserver(
|
|
577
|
+
observer.current = new IntersectionObserver(entries => {
|
|
665
578
|
if (entries[0].isIntersecting && hasMore) {
|
|
666
|
-
setPage(
|
|
579
|
+
setPage(prev => prev + 1);
|
|
667
580
|
}
|
|
668
581
|
});
|
|
669
582
|
if (node)
|
|
670
583
|
observer.current.observe(node);
|
|
671
584
|
}, [loading, hasMore]);
|
|
672
|
-
useEffect(
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
newData_1 = _a.sent();
|
|
685
|
-
setData(function (prev) { return __spreadArray(__spreadArray([], prev, true), newData_1, true); });
|
|
686
|
-
setHasMore(newData_1.length > 0);
|
|
687
|
-
return [3 /*break*/, 4];
|
|
688
|
-
case 3:
|
|
689
|
-
setLoading(false);
|
|
690
|
-
return [7 /*endfinally*/];
|
|
691
|
-
case 4: return [2 /*return*/];
|
|
692
|
-
}
|
|
693
|
-
});
|
|
694
|
-
}); };
|
|
585
|
+
useEffect(() => {
|
|
586
|
+
const loadData = async () => {
|
|
587
|
+
setLoading(true);
|
|
588
|
+
try {
|
|
589
|
+
const newData = await fetchData(page);
|
|
590
|
+
setData(prev => [...prev, ...newData]);
|
|
591
|
+
setHasMore(newData.length > 0);
|
|
592
|
+
}
|
|
593
|
+
finally {
|
|
594
|
+
setLoading(false);
|
|
595
|
+
}
|
|
596
|
+
};
|
|
695
597
|
loadData();
|
|
696
598
|
}, [page, fetchData]);
|
|
697
|
-
return { data
|
|
599
|
+
return { data, loading, hasMore, lastElementRef };
|
|
698
600
|
};
|
|
699
|
-
export
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
useEffect(function () {
|
|
601
|
+
export const useEventListeners = (eventType, handler, element = window, options) => {
|
|
602
|
+
const savedHandler = useRef(handler);
|
|
603
|
+
useEffect(() => {
|
|
703
604
|
savedHandler.current = handler;
|
|
704
605
|
}, [handler]);
|
|
705
|
-
useEffect(
|
|
706
|
-
|
|
606
|
+
useEffect(() => {
|
|
607
|
+
const isSupported = element && element.addEventListener;
|
|
707
608
|
if (!isSupported)
|
|
708
609
|
return;
|
|
709
|
-
|
|
610
|
+
const listener = (event) => savedHandler.current(event);
|
|
710
611
|
element.addEventListener(eventType, listener, options);
|
|
711
|
-
return
|
|
612
|
+
return () => {
|
|
712
613
|
element.removeEventListener(eventType, listener, options);
|
|
713
614
|
};
|
|
714
615
|
}, [eventType, element, options]);
|