UNPKG

@assistant-ui/react

Version:

Typescript/React library for AI Chat

1 lines 1.09 kB
{"version":3,"sources":["../../../../src/context/react/utils/createContextHook.ts"],"sourcesContent":["\"use client\";\n\nimport { useContext, Context } from \"react\";\n\n/**\n * Creates a context hook with optional support.\n * @param context - The React context to consume.\n * @param providerName - The name of the provider for error messages.\n * @returns A hook function that provides the context value.\n */\nexport function createContextHook<T>(\n context: Context<T | null>,\n providerName: string,\n) {\n function useContextHook(options?: {\n optional?: boolean | undefined;\n }): T | null {\n const contextValue = useContext(context);\n if (!options?.optional && !contextValue) {\n throw new Error(`This component must be used within ${providerName}.`);\n }\n return contextValue;\n }\n\n return useContextHook;\n}\n"],"mappings":";;;AAEA,SAAS,kBAA2B;AAQ7B,SAAS,kBACd,SACA,cACA;AACA,WAAS,eAAe,SAEX;AACX,UAAM,eAAe,WAAW,OAAO;AACvC,QAAI,CAAC,SAAS,YAAY,CAAC,cAAc;AACvC,YAAM,IAAI,MAAM,sCAAsC,YAAY,GAAG;AAAA,IACvE;AACA,WAAO;AAAA,EACT;AAEA,SAAO;AACT;","names":[]}