about summary refs log tree commit diff
path: root/src/components/hooks
diff options
context:
space:
mode:
authorSamuel Newman <mozzius@protonmail.com>2024-03-19 15:18:29 +0000
committerSamuel Newman <mozzius@protonmail.com>2024-03-19 15:18:29 +0000
commitf491bd89cc28cba46a92b443e1f07ff73e8f7128 (patch)
tree8f7c34372afe1f600a5626ef72dd9c8b28b930a6 /src/components/hooks
parentd2a11f3344149a299372f0a7dcd01de9f58ef9a1 (diff)
parent9c49b209ca9eda8e6fab0942f7046d335c955c1a (diff)
downloadvoidsky-f491bd89cc28cba46a92b443e1f07ff73e8f7128.tar.zst
Merge remote-tracking branch 'origin/main' into samuel/alf-login
Diffstat (limited to 'src/components/hooks')
-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
+}