import React from "react"; import type { Waku } from "@waku/interfaces"; import type { BootstrapNodeOptions, CreateNodeResult, CreateWakuNodeOptions, ReactChildrenProps, } from "./types"; import { useCreateLightNode } from "./useCreateWaku"; type WakuContextType = CreateNodeResult; const WakuContext = React.createContext>({ node: undefined, isLoading: false, error: undefined, }); /** * Hook to retrieve Waku node from Context. By default generic Waku type will be used. * @example * const { node, isLoading, error } = useWaku(); * @example * const { node, isLoading, error } = useWaku(); * @example * const { node, isLoading, error } = useWaku(); * @example * const { node, isLoading, error } = useWaku(); * @returns WakuContext */ export const useWaku = (): WakuContextType => React.useContext(WakuContext) as WakuContextType; type ProviderProps = ReactChildrenProps & BootstrapNodeOptions; /** * Provider for creating Light Node based on options passed. * @example * const App = (props) => ( * * * * ); * const Component = (props) => { * const { node, isLoading, error } = useWaku(); * ... * }; * @param {Object} props - options to create a node and other React props * @param {CreateWakuNodeOptions} props.options - optional options for creating Light Node * @param {Protocols} props.protocols - optional protocols list to initiate node with * @returns React Light Node provider component */ export const LightNodeProvider: React.FunctionComponent< ProviderProps > = (props) => { const result = useCreateLightNode({ options: props.options, protocols: props.protocols, }); return ( {props.children} ); };