about summary refs log tree commit diff
path: root/src/state/preferences/autoplay.tsx
diff options
context:
space:
mode:
authorSamuel Newman <mozzius@protonmail.com>2024-04-19 22:10:37 +0100
committerGitHub <noreply@github.com>2024-04-19 22:10:37 +0100
commit8b33ffdfb5ca606708c8104ecad4fa5430268483 (patch)
tree9dd6ad037a19fc8257023c9760633c9749fd9d4f /src/state/preferences/autoplay.tsx
parentade2ea6172a71d60bd7ce6aed97d09dbddb352d0 (diff)
downloadvoidsky-8b33ffdfb5ca606708c8104ecad4fa5430268483.tar.zst
Add disable autoplay preference and group related settings into a dedicated page (#3626)
* add autoplay preference

* group accessibility settings into a dedicated page

* fix gray background on web

* Put a11y first

---------

Co-authored-by: Dan Abramov <dan.abramov@gmail.com>
Diffstat (limited to 'src/state/preferences/autoplay.tsx')
-rw-r--r--src/state/preferences/autoplay.tsx42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/state/preferences/autoplay.tsx b/src/state/preferences/autoplay.tsx
new file mode 100644
index 000000000..d5aa049f3
--- /dev/null
+++ b/src/state/preferences/autoplay.tsx
@@ -0,0 +1,42 @@
+import React from 'react'
+
+import * as persisted from '#/state/persisted'
+
+type StateContext = boolean
+type SetContext = (v: boolean) => void
+
+const stateContext = React.createContext<StateContext>(
+  Boolean(persisted.defaults.disableAutoplay),
+)
+const setContext = React.createContext<SetContext>((_: boolean) => {})
+
+export function Provider({children}: {children: React.ReactNode}) {
+  const [state, setState] = React.useState(
+    Boolean(persisted.get('disableAutoplay')),
+  )
+
+  const setStateWrapped = React.useCallback(
+    (autoplayDisabled: persisted.Schema['disableAutoplay']) => {
+      setState(Boolean(autoplayDisabled))
+      persisted.write('disableAutoplay', autoplayDisabled)
+    },
+    [setState],
+  )
+
+  React.useEffect(() => {
+    return persisted.onUpdate(() => {
+      setState(Boolean(persisted.get('disableAutoplay')))
+    })
+  }, [setStateWrapped])
+
+  return (
+    <stateContext.Provider value={state}>
+      <setContext.Provider value={setStateWrapped}>
+        {children}
+      </setContext.Provider>
+    </stateContext.Provider>
+  )
+}
+
+export const useAutoplayDisabled = () => React.useContext(stateContext)
+export const useSetAutoplayDisabled = () => React.useContext(setContext)