about summary refs log tree commit diff
path: root/src/components/hooks/useDelayedLoading.ts
diff options
context:
space:
mode:
authorMinseo Lee <itoupluk427@gmail.com>2024-03-19 10:52:29 +0900
committerMinseo Lee <itoupluk427@gmail.com>2024-03-19 10:52:29 +0900
commitad43d594c9f63fc85e6927d23cd3f3f21406b002 (patch)
tree8a20f9f9051ff066bd54c5bc126ccc548e2cb16c /src/components/hooks/useDelayedLoading.ts
parent73dae9f7b5c169aa303e9ef9487040e850998edf (diff)
parent3abf302b0b189c50acf11489bf60bdaeb187b722 (diff)
downloadvoidsky-ad43d594c9f63fc85e6927d23cd3f3f21406b002.tar.zst
Merge remote-tracking branch 'upstream/main' into patch-3
Diffstat (limited to 'src/components/hooks/useDelayedLoading.ts')
-rw-r--r--src/components/hooks/useDelayedLoading.ts15
1 files changed, 15 insertions, 0 deletions
diff --git a/src/components/hooks/useDelayedLoading.ts b/src/components/hooks/useDelayedLoading.ts
new file mode 100644
index 000000000..6c7e2ede0
--- /dev/null
+++ b/src/components/hooks/useDelayedLoading.ts
@@ -0,0 +1,15 @@
+import React from 'react'
+
+export function useDelayedLoading(delay: number, initialState: boolean = true) {
+  const [isLoading, setIsLoading] = React.useState(initialState)
+
+  React.useEffect(() => {
+    let timeout: NodeJS.Timeout
+    // on initial load, show a loading spinner for a hot sec to prevent flash
+    if (isLoading) timeout = setTimeout(() => setIsLoading(false), delay)
+
+    return () => timeout && clearTimeout(timeout)
+  }, [isLoading, delay])
+
+  return isLoading
+}