From 3a211017d3d972fb442069e38d1b8ff1a2edbd57 Mon Sep 17 00:00:00 2001
From: Paul Frazee <pfrazee@gmail.com>
Date: Wed, 8 Nov 2023 08:52:01 -0800
Subject: Move require alt-text to new persistence + context (#1839)

---
 src/state/shell/alt-text-required.tsx | 48 +++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)
 create mode 100644 src/state/shell/alt-text-required.tsx

(limited to 'src/state/shell/alt-text-required.tsx')

diff --git a/src/state/shell/alt-text-required.tsx b/src/state/shell/alt-text-required.tsx
new file mode 100644
index 000000000..81de9e006
--- /dev/null
+++ b/src/state/shell/alt-text-required.tsx
@@ -0,0 +1,48 @@
+import React from 'react'
+import * as persisted from '#/state/persisted'
+
+type StateContext = persisted.Schema['requireAltTextEnabled']
+type SetContext = (v: persisted.Schema['requireAltTextEnabled']) => void
+
+const stateContext = React.createContext<StateContext>(
+  persisted.defaults.requireAltTextEnabled,
+)
+const setContext = React.createContext<SetContext>(
+  (_: persisted.Schema['requireAltTextEnabled']) => {},
+)
+
+export function Provider({children}: React.PropsWithChildren<{}>) {
+  const [state, setState] = React.useState(
+    persisted.get('requireAltTextEnabled'),
+  )
+
+  const setStateWrapped = React.useCallback(
+    (requireAltTextEnabled: persisted.Schema['requireAltTextEnabled']) => {
+      setState(requireAltTextEnabled)
+      persisted.write('requireAltTextEnabled', requireAltTextEnabled)
+    },
+    [setState],
+  )
+
+  React.useEffect(() => {
+    return persisted.onUpdate(() => {
+      setState(persisted.get('requireAltTextEnabled'))
+    })
+  }, [setStateWrapped])
+
+  return (
+    <stateContext.Provider value={state}>
+      <setContext.Provider value={setStateWrapped}>
+        {children}
+      </setContext.Provider>
+    </stateContext.Provider>
+  )
+}
+
+export function useRequireAltTextEnabled() {
+  return React.useContext(stateContext)
+}
+
+export function useSetRequireAltTextEnabled() {
+  return React.useContext(setContext)
+}
-- 
cgit 1.4.1

