helia icon indicating copy to clipboard operation
helia copied to clipboard

Attempted import error: 'RTCSessionDescription' is not exported

Open thachp opened this issue 8 months ago • 4 comments

I have no luck writing a ReactJS Hook for Helia. I keep getting the following errors... Any thoughts?

Repro(s)

  1. Start a new NextJS project
  2. Add Helia
  3. copy & paste hooks below.
  4. Use it in any react component

// version "helia": "4.2.3", "next": "14.2.3",

Error

 ⨯ ../../node_modules/.pnpm/@[email protected][email protected]/node_modules/@libp2p/webrtc/dist/src/private-to-private/initiate-connection.js
Attempted import error: 'RTCSessionDescription' is not exported from '../webrtc/index.js' (imported as 'RTCSessionDescription').

Sample Codes

import { json } from "@helia/json";
import { Helia, createHelia } from "helia";
import { useCallback, useEffect, useState } from "react";

export type HeliaJsonResult = {
  helia: Helia | null;
  nodeId: string | null;
  nodeIsOnline: boolean;
  addJson: (jsonData: any) => Promise<string>;
  getJson: (cid: string) => Promise<any>;
  error: Error | null;
  loading: boolean;
};

export const useHeliaJson = (): HeliaJsonResult => {
  const [helia, setHelia] = useState<Helia | null>(null);
  const [nodeId, setNodeId] = useState<string | null>(null);
  const [nodeIsOnline, setNodeIsOnline] = useState<boolean>(false);
  const [error, setError] = useState<Error | null>(null);
  const [loading, setLoading] = useState<boolean>(true);

  useEffect(() => {
    const init = async () => {
      try {
        const heliaInstance = await createHelia();
        setHelia(heliaInstance);

        console.log("Helia instance: ", heliaInstance);

        const nodeId = heliaInstance.libp2p.peerId.toString();
        const nodeIsOnline = heliaInstance.libp2p.status === "started";
        setNodeId(nodeId);
        setNodeIsOnline(nodeIsOnline);

        setLoading(false);
      } catch (err) {
        setError(err as Error);
        setLoading(false);
      }
    };

    init();
  }, []);

  const addJson = useCallback(
    async (jsonData: any): Promise<string> => {
      if (!helia) {
        throw new Error("Helia instance is not initialized");
      }
      try {
        const cid = await json(helia).add(jsonData);
        return cid.toString();
      } catch (err) {
        setError(err as Error);
        throw err;
      }
    },
    [helia]
  );

  const getJson = useCallback(
    async (cid: string): Promise<any> => {
      if (!helia) {
        throw new Error("Helia instance is not initialized");
      }
      try {
        const data = await json(helia).get<any>(cid as any);
        return data;
      } catch (err) {
        setError(err as Error);
        throw err;
      }
    },
    [helia]
  );

  return {
    helia,
    nodeId,
    nodeIsOnline,
    addJson,
    getJson,
    error,
    loading,
  };
};

export default useHeliaJson;

``

thachp avatar Jun 02 '24 05:06 thachp