@gateweb/react-utils 0.0.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.
@@ -0,0 +1,68 @@
1
+ 'use client';
2
+ var react = require('react');
3
+
4
+ /**
5
+ * 倒數計時器
6
+ *
7
+ * 可以透過 start() 來啟動倒數計時器
8
+ * 可以透過 stop() 來停止倒數計時器
9
+ * 可以透過 reset() 來重置倒數計時器
10
+ *
11
+ * @param initialCountdown 倒數計時器初始值
12
+ * @param enableReinitialize 允許重設初始值
13
+ */ const useCountdown = (initialCountdown, enableReinitialize = false)=>{
14
+ const [countdown, setCountdown] = react.useState(initialCountdown);
15
+ const [isCounting, setIsCounting] = react.useState(false);
16
+ const initialValues = react.useRef(initialCountdown);
17
+ const isMounted = react.useRef(false);
18
+ react.useEffect(()=>{
19
+ isMounted.current = true;
20
+ return ()=>{
21
+ isMounted.current = false;
22
+ };
23
+ }, []);
24
+ react.useEffect(()=>{
25
+ if (isMounted.current && initialValues.current !== initialCountdown && enableReinitialize) {
26
+ initialValues.current = initialCountdown;
27
+ setCountdown(initialCountdown);
28
+ }
29
+ }, [
30
+ initialCountdown,
31
+ enableReinitialize
32
+ ]);
33
+ react.useEffect(()=>{
34
+ let timer;
35
+ if (isCounting && countdown > 0) {
36
+ timer = setTimeout(()=>{
37
+ setCountdown(countdown - 1);
38
+ }, 1000);
39
+ } else {
40
+ setIsCounting(false);
41
+ }
42
+ return ()=>{
43
+ if (timer) clearTimeout(timer);
44
+ };
45
+ }, [
46
+ countdown,
47
+ isCounting
48
+ ]);
49
+ const start = ()=>{
50
+ setIsCounting(true);
51
+ };
52
+ const stop = ()=>{
53
+ setIsCounting(false);
54
+ };
55
+ const reset = ()=>{
56
+ setCountdown(initialValues.current);
57
+ setIsCounting(false);
58
+ };
59
+ return {
60
+ countdown,
61
+ isCounting,
62
+ start,
63
+ stop,
64
+ reset
65
+ };
66
+ };
67
+
68
+ exports.useCountdown = useCountdown;
@@ -0,0 +1,53 @@
1
+ 'use client';
2
+ /**
3
+ * 從 localStorage 取得資料,支援槽狀取值
4
+ *
5
+ * @param key 鍵值
6
+ * @param deCode 是否解碼
7
+ * @returns 取得的資料
8
+ * @example
9
+ * const data = getLocalStorage('key');
10
+ *
11
+ * const data = getLocalStorage('key.subKey');
12
+ */ const getLocalStorage = (key, deCode = true)=>{
13
+ const keys = key.split('.');
14
+ const storage = localStorage.getItem(keys[0]);
15
+ if (!storage) return undefined;
16
+ let currentObject;
17
+ try {
18
+ if (deCode) {
19
+ currentObject = JSON.parse(window.atob(storage));
20
+ } else {
21
+ currentObject = JSON.parse(storage);
22
+ }
23
+ } catch (error) {
24
+ return undefined;
25
+ }
26
+ // let currentObject = JSON.parse(storage);
27
+ if (keys.length === 1) {
28
+ return currentObject;
29
+ }
30
+ keys.shift();
31
+ while(keys.length > 0){
32
+ const currentKey = keys.shift();
33
+ if (!currentKey) break;
34
+ currentObject = currentObject[currentKey];
35
+ if (!currentObject) break;
36
+ }
37
+ return currentObject;
38
+ };
39
+ /**
40
+ * 將資料存入 localStorage
41
+ * @param key 鍵值
42
+ * @param value 可序列化的資料
43
+ * @param enCode 是否編碼
44
+ */ const setLocalStorage = (key, value, enCode = true)=>{
45
+ if (enCode) {
46
+ localStorage.setItem(key, window.btoa(JSON.stringify(value)));
47
+ } else {
48
+ localStorage.setItem(key, JSON.stringify(value));
49
+ }
50
+ };
51
+
52
+ exports.getLocalStorage = getLocalStorage;
53
+ exports.setLocalStorage = setLocalStorage;
@@ -0,0 +1,27 @@
1
+ 'use client';
2
+ /**
3
+ * Downloads a file from a given source.
4
+ *
5
+ * @param source - The source of the file to be downloaded. It can be a URL string or a Blob object.
6
+ * @param filename - The name of the file to be downloaded. Defaults to the current timestamp if not provided.
7
+ * @param fileExtension - The file extension to be appended to the filename. Optional.
8
+ *
9
+ * @example
10
+ * downloadFile('http://example.com/file.txt', 'testfile', 'txt');
11
+ * downloadFile(new Blob(['test content'], { type: 'text/plain' }), 'testfile', 'txt');
12
+ */ const downloadFile = (source, filename = new Date().getTime().toString(), fileExtension)=>{
13
+ const url = typeof source === 'string' ? source : URL.createObjectURL(source);
14
+ const link = document.createElement('a');
15
+ link.id = `download-${new Date().getTime()}`;
16
+ document.body.appendChild(link);
17
+ if (!fileExtension) {
18
+ link.download = filename;
19
+ } else {
20
+ link.download = `${filename}.${fileExtension}`;
21
+ }
22
+ link.href = url;
23
+ link.target = '_blank';
24
+ link.click();
25
+ };
26
+
27
+ export { downloadFile as d };