mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
migrate frontend
This commit is contained in:
27
web/src/hooks/useDebounce.ts
Normal file
27
web/src/hooks/useDebounce.ts
Normal file
@ -0,0 +1,27 @@
|
||||
import { useCallback, useRef } from "react";
|
||||
|
||||
/**
|
||||
* useDebounce: useRef + useCallback
|
||||
* @param func function
|
||||
* @param delay delay duration
|
||||
* @param deps depends
|
||||
* @returns debounced function
|
||||
*/
|
||||
export default function useDebounce<T extends (...args: any[]) => any>(func: T, delay: number, deps: any[] = []): T {
|
||||
const timer = useRef<number>();
|
||||
|
||||
const cancel = useCallback(() => {
|
||||
if (timer.current) {
|
||||
clearTimeout(timer.current);
|
||||
}
|
||||
}, []);
|
||||
|
||||
const run = useCallback((...args) => {
|
||||
cancel();
|
||||
timer.current = window.setTimeout(() => {
|
||||
func(...args);
|
||||
}, delay);
|
||||
}, deps);
|
||||
|
||||
return run as T;
|
||||
}
|
35
web/src/hooks/useLoading.ts
Normal file
35
web/src/hooks/useLoading.ts
Normal file
@ -0,0 +1,35 @@
|
||||
import { useState } from "react";
|
||||
|
||||
function useLoading(initialState: boolean = true) {
|
||||
const [state, setState] = useState({ isLoading: initialState, isFailed: false, isSucceed: false });
|
||||
|
||||
return {
|
||||
...state,
|
||||
setLoading: () => {
|
||||
setState({
|
||||
...state,
|
||||
isLoading: true,
|
||||
isFailed: false,
|
||||
isSucceed: false,
|
||||
});
|
||||
},
|
||||
setFinish: () => {
|
||||
setState({
|
||||
...state,
|
||||
isLoading: false,
|
||||
isFailed: false,
|
||||
isSucceed: true,
|
||||
});
|
||||
},
|
||||
setError: () => {
|
||||
setState({
|
||||
...state,
|
||||
isLoading: false,
|
||||
isFailed: true,
|
||||
isSucceed: false,
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export default useLoading;
|
15
web/src/hooks/useRefresh.ts
Normal file
15
web/src/hooks/useRefresh.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import { useCallback, useState } from "react";
|
||||
|
||||
function useRefresh() {
|
||||
const [_, setBoolean] = useState<Boolean>(false);
|
||||
|
||||
const refresh = useCallback(() => {
|
||||
setBoolean((ps) => {
|
||||
return !ps;
|
||||
});
|
||||
}, []);
|
||||
|
||||
return refresh;
|
||||
}
|
||||
|
||||
export default useRefresh;
|
19
web/src/hooks/useToggle.ts
Normal file
19
web/src/hooks/useToggle.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import { useCallback, useState } from "react";
|
||||
|
||||
// Parameter is the boolean, with default "false" value
|
||||
export default function useToggle(initialState = false): [boolean, (nextState?: boolean) => void] {
|
||||
// Initialize the state
|
||||
const [state, setState] = useState(initialState);
|
||||
|
||||
// Define and memorize toggler function in case we pass down the comopnent,
|
||||
// This function change the boolean value to it's opposite value
|
||||
const toggle = useCallback((nextState?: boolean) => {
|
||||
if (nextState !== undefined) {
|
||||
setState(nextState);
|
||||
} else {
|
||||
setState((state) => !state);
|
||||
}
|
||||
}, []);
|
||||
|
||||
return [state, toggle];
|
||||
}
|
Reference in New Issue
Block a user