mirror of https://github.com/usememos/memos
fix(web): delay loading skeleton to avoid flash on fast loads
Render the memo list skeleton only after loading exceeds a short delay (SKELETON_LOADING_DELAY_MS). Fast/self-hosted loads now finish before the threshold and never flash the skeleton, while slow loads still show it. Close #6047pull/6051/head
parent
e3e4ae1051
commit
9e84f61029
@ -0,0 +1,23 @@
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
/**
|
||||
* Returns a flag that turns true only after `active` has stayed true for `delay` ms,
|
||||
* and turns false immediately once `active` becomes false.
|
||||
*
|
||||
* Useful for delaying loading indicators (e.g. skeletons) so they don't flash on fast operations.
|
||||
*/
|
||||
export const useDelayedFlag = (active: boolean, delay: number): boolean => {
|
||||
const [delayed, setDelayed] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (!active) {
|
||||
setDelayed(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const timeout = window.setTimeout(() => setDelayed(true), delay);
|
||||
return () => window.clearTimeout(timeout);
|
||||
}, [active, delay]);
|
||||
|
||||
return delayed;
|
||||
};
|
||||
Loading…
Reference in New Issue