Skip to content

Commit

Permalink
chore: useNewDAO clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
okhaimie-dev committed Aug 17, 2023
1 parent eadf5f2 commit 2507f3b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 54 deletions.
101 changes: 49 additions & 52 deletions packages/daobox-use-aragon/src/context/AragonContext.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import { createContext, useContext, useEffect, useState } from "react";
import {
Client,
Context,
ContextParams,
ContextPlugin,
TokenVotingClient,
Client,
Context,
TokenVotingClient,
type ContextParams,
} from "@aragon/sdk-client";
import { createContext, useContext, useEffect, useState } from "react";
import { CHAINS, settings } from "../constants";
import useConnectedWallet from "./useConnectedWallet";
import { SupportedChainIds } from "../types";
import { IpfsNode } from "./AragonProvider";

export interface AragonSDKContextValue {
context?: Context;
baseClient?: Client;
tokenVotingClient?: TokenVotingClient;
context?: Context;
baseClient?: Client;
tokenVotingClient?: TokenVotingClient;
}

const AragonSDKContext = createContext<AragonSDKContextValue>({});
Expand All @@ -24,60 +23,58 @@ const AragonSDKContext = createContext<AragonSDKContextValue>({});
* to the Aragon SDK.
*/
export function AragonSDKWrapper({
children,
ipfsNodes,
children,
ipfsNodes,
}: {
children: JSX.Element;
ipfsNodes?: IpfsNode[];
children: JSX.Element;
ipfsNodes?: IpfsNode[];
}): JSX.Element {
const { signer, chain } = useConnectedWallet();
const [context, setContext] = useState<Context | undefined>(undefined);
const [baseClient, setBaseClient] = useState<Client | undefined>(undefined);
const [tokenVotingClient, setTokenVotingClient] = useState<
TokenVotingClient | undefined
>(undefined);
const { signer, chain } = useConnectedWallet();
const [context, setContext] = useState<Context | undefined>(undefined);
const [baseClient, setBaseClient] = useState<Client | undefined>(undefined);
const [tokenVotingClient, setTokenVotingClient] = useState<
TokenVotingClient | undefined
>(undefined);

useEffect(() => {
if (!signer || !chain) return;
useEffect(() => {
if (!signer || !chain) return;

// check if chain is valid
if (!Object.values(CHAINS).includes(chain as SupportedChainIds)) {
console.error(`Invalid chain type: ${chain}`);
return;
}
// check if chain is valid
if (!Object.values(CHAINS).includes(chain as SupportedChainIds)) {
console.error(`Invalid chain type: ${chain}`);
return;
}

const aragonSDKContextParams: ContextParams = {
network: chain || 5,
signer,
...settings(chain as SupportedChainIds, ipfsNodes),
};
const contextInstance = new Context(aragonSDKContextParams);
const contextPlugin = ContextPlugin.fromContext(contextInstance);
setContext(contextInstance);
setBaseClient(new Client(contextInstance));
setTokenVotingClient(new TokenVotingClient(contextPlugin));
}, [signer, chain, ipfsNodes]);
const aragonSDKContextParams: ContextParams = {
network: chain || 5,
signer,
...settings(chain as SupportedChainIds, ipfsNodes),
};
const contextInstance = new Context(aragonSDKContextParams);
setContext(contextInstance);
setBaseClient(new Client(contextInstance));
}, [signer, chain, ipfsNodes]);

return (
<AragonSDKContext.Provider
value={{
context,
baseClient,
tokenVotingClient,
}}
>
{children}
</AragonSDKContext.Provider>
);
return (
<AragonSDKContext.Provider
value={{
context,
baseClient,
tokenVotingClient,
}}
>
{children}
</AragonSDKContext.Provider>
);
}

/**
* useAragon is a custom hook to access the AragonSDKContext.
* @throws {Error} if used outside of AragonSDKWrapper
*/
export function useAragon(): AragonSDKContextValue {
const context = useContext(AragonSDKContext);
if (!context)
throw new Error("useAragon hooks must be used within an AragonSDKWrapper");
return context;
const context = useContext(AragonSDKContext);
if (!context)
throw new Error("useAragon hooks must be used within an AragonSDKWrapper");
return context;
}
3 changes: 1 addition & 2 deletions packages/daobox-use-aragon/src/core/useNewDao.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
DaoCreationSteps,
DaoMetadata,
TokenVotingPluginInstall,
} from "@aragon/sdk-client";
import { useState } from "react";
import { useMutation, UseMutationResult } from "@tanstack/react-query";
Expand Down Expand Up @@ -108,7 +107,7 @@ export type NewDaoParams = MutationConfig<NewDaoReturnData, Error> & {
daoUri?: string;
ensSubdomain: string;
trustedForwarder?: string;
plugins: TokenVotingPluginInstall[];
plugins: [];
onCreateDaoTransaction?: (txHash: string) => void;
};

Expand Down

0 comments on commit 2507f3b

Please sign in to comment.