"use client"; import type { CreateNodeOptions, IWaku, LightNode } from "@waku/interfaces"; import { createLightNode } from "@waku/sdk"; import * as React from "react"; import type { CreateNodeResult } from "./types.js"; type NodeFactory> = (options?: T) => Promise; type CreateNodeParams> = T & { factory: NodeFactory; }; const useCreateNode = >( params: CreateNodeParams ): CreateNodeResult => { const { factory, ...options } = params; const [node, setNode] = React.useState(undefined); const [isLoading, setLoading] = React.useState(true); const [error, setError] = React.useState(undefined); React.useEffect(() => { let cancelled = false; setLoading(true); factory(options as T) .then(async (node) => { if (cancelled) { return; } await node.start(); await node.waitForPeers(); setNode(node); setLoading(false); }) .catch((err) => { setLoading(false); setError(`Failed at creating node: ${err?.message || "no message"}`); }); return () => { cancelled = true; }; }, []); return { node, error, isLoading }; }; export const useCreateLightNode = ( params?: CreateNodeOptions ): CreateNodeResult => { return useCreateNode({ ...params, factory: createLightNode }); };