about summary refs log tree commit diff
path: root/src/view/com/util/post-embeds/VideoVolumeContext.tsx
blob: cccb93ba8b1cb1369028629a153048d177b8f6ac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import React from 'react'

const Context = React.createContext(
  {} as {
    muted: boolean
    setMuted: (muted: boolean) => void
  },
)

export function Provider({children}: {children: React.ReactNode}) {
  const [muted, setMuted] = React.useState(true)

  const value = React.useMemo(
    () => ({
      muted,
      setMuted,
    }),
    [muted, setMuted],
  )

  return <Context.Provider value={value}>{children}</Context.Provider>
}

export function useVideoVolumeState() {
  const context = React.useContext(Context)
  if (!context) {
    throw new Error(
      'useVideoVolumeState must be used within a VideoVolumeProvider',
    )
  }
  return context
}