about summary refs log tree commit diff
path: root/src/state/cache
diff options
context:
space:
mode:
authorSamuel Newman <mozzius@protonmail.com>2024-06-18 19:48:34 +0100
committerGitHub <noreply@github.com>2024-06-18 21:48:34 +0300
commit5f5d845053e13169f89fc70a3f858b0a9e5ed4fd (patch)
tree2902f50fbfdce9b59b38d74c0dac96b3a2abf8b9 /src/state/cache
parent35e54e24a0b08ce0f2e3389aeb4fb0f29778170e (diff)
downloadvoidsky-5f5d845053e13169f89fc70a3f858b0a9e5ed4fd.tar.zst
Server-side thread mutes (#4518)
* update atproto/api

* move thread mutes to server side

* rm log

* move muted threads provider to inside did key

* use map instead of object
Diffstat (limited to 'src/state/cache')
-rw-r--r--src/state/cache/thread-mutes.tsx44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/state/cache/thread-mutes.tsx b/src/state/cache/thread-mutes.tsx
new file mode 100644
index 000000000..b58bd430f
--- /dev/null
+++ b/src/state/cache/thread-mutes.tsx
@@ -0,0 +1,44 @@
+import React from 'react'
+
+type StateContext = Map<string, boolean>
+type SetStateContext = (uri: string, value: boolean) => void
+
+const stateContext = React.createContext<StateContext>(new Map())
+const setStateContext = React.createContext<SetStateContext>(
+  (_: string) => false,
+)
+
+export function Provider({children}: React.PropsWithChildren<{}>) {
+  const [state, setState] = React.useState<StateContext>(() => new Map())
+
+  const setThreadMute = React.useCallback(
+    (uri: string, value: boolean) => {
+      setState(prev => {
+        const next = new Map(prev)
+        next.set(uri, value)
+        return next
+      })
+    },
+    [setState],
+  )
+  return (
+    <stateContext.Provider value={state}>
+      <setStateContext.Provider value={setThreadMute}>
+        {children}
+      </setStateContext.Provider>
+    </stateContext.Provider>
+  )
+}
+
+export function useMutedThreads() {
+  return React.useContext(stateContext)
+}
+
+export function useIsThreadMuted(uri: string, defaultValue = false) {
+  const state = React.useContext(stateContext)
+  return state.get(uri) ?? defaultValue
+}
+
+export function useSetThreadMute() {
+  return React.useContext(setStateContext)
+}