import React from "react"; import type { Waku } from "@waku/interfaces"; import type { BootstrapNodeOptions, CrateWakuHook, FullNodeOptions, LightNodeOptions, RelayNodeOptions, } from "./types"; import { useCreateFullNode, useCreateLightNode, useCreateRelayNode, } from "./useCreateWaku"; type WakuContextType = CrateWakuHook; export const WakuContext = React.createContext>({ node: null, isLoading: false, error: null, }); /** * 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 ReactChildrenProps = { children?: React.ReactNode; }; 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 {LightNodeOptions} 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} ); }; /** * Provider for creating Relay 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 {RelayNodeOptions} props.options - optional options for creating Relay Node * @param {Protocols} props.protocols - optional protocols list to initiate node with * @returns React Relay Node provider component */ export const RelayNodeProvider: React.FunctionComponent< ProviderProps > = (props) => { const result = useCreateRelayNode({ options: props.options, protocols: props.protocols, }); return ( {props.children} ); }; /** * Provider for creating Full 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 {FullNodeOptions} props.options - optional options for creating Full Node * @param {Protocols} props.protocols - optional protocols list to initiate node with * @returns React Full Node provider component */ export const FullNodeProvider: React.FunctionComponent< ProviderProps > = (props) => { const result = useCreateFullNode({ options: props.options, protocols: props.protocols, }); return ( {props.children} ); };