/**
 * This file was generated by PostXL generator.
 *
 * Feel free to manually edit it. The generator will not overwrite it. We suggest you
 * remove the disclaimer to avoid confusion.
 **/
import { type Context, useContext } from 'react'

/**
 * Let's you create a hook from a React context that may be safely used in the app.
 */
export function createUseContext<ContextValue>(
  context: Context<ContextValue>,
  name: string,
): () => NonNullable<ContextValue> {
  return () => {
    const value = useContext(context)

    if (!value) {
      throw new Error(`The context "${name}" is not available.`)
    }

    return value
  }
}
