about summary refs log tree commit diff
path: root/src/lib/hooks/useTimer.ts
diff options
context:
space:
mode:
authorPaul Frazee <pfrazee@gmail.com>2023-06-02 09:48:53 -0500
committerGitHub <noreply@github.com>2023-06-02 09:48:53 -0500
commit3217c7ff32b778d8a2a141fabdc6eabdf5ba018d (patch)
tree4a5f9427ab1a425d965521ca482d575cd31f4f6e /src/lib/hooks/useTimer.ts
parente9c84a192b3a64c49a227617e8b58c25d3e5d0f3 (diff)
downloadvoidsky-3217c7ff32b778d8a2a141fabdc6eabdf5ba018d.tar.zst
More custom-feed behavior fixes [APP-678] (#831)
* Remove extraneous custom-feed health check

* Fixes to custom feed preference sync

* Fix lint

* Remove dead code (client-side suggested posts constructor)

* Enforce the feed-fetch limit in the client if the generator fails to observe the parameter

* Bump the number of items fetched in the multifeed per feed from 5 to 10

* Reset the currently active feed when the pinned feeds change

* Some fixes to icons

* Add a prompt to load latest to the multifeed

* Remove debug
Diffstat (limited to 'src/lib/hooks/useTimer.ts')
-rw-r--r--src/lib/hooks/useTimer.ts32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/lib/hooks/useTimer.ts b/src/lib/hooks/useTimer.ts
new file mode 100644
index 000000000..bf3ecc07f
--- /dev/null
+++ b/src/lib/hooks/useTimer.ts
@@ -0,0 +1,32 @@
+import * as React from 'react'
+
+/**
+ * Helper hook to run persistent timers on views
+ */
+export function useTimer(time: number, handler: () => void) {
+  const timer = React.useRef(undefined)
+
+  // function to restart the timer
+  const reset = React.useCallback(() => {
+    if (timer.current) {
+      clearTimeout(timer.current)
+    }
+    timer.current = setTimeout(handler, time)
+  }, [time, timer, handler])
+
+  // function to cancel the timer
+  const cancel = React.useCallback(() => {
+    if (timer.current) {
+      clearTimeout(timer.current)
+      timer.current = undefined
+    }
+  }, [timer])
+
+  // start the timer immediately
+  React.useEffect(() => {
+    reset()
+    // eslint-disable-next-line react-hooks/exhaustive-deps
+  }, [])
+
+  return [reset, cancel]
+}