diff --git a/packages/core/src/client/OpChanClient.ts b/packages/core/src/client/OpChanClient.ts index cecc0fe..795a298 100644 --- a/packages/core/src/client/OpChanClient.ts +++ b/packages/core/src/client/OpChanClient.ts @@ -10,15 +10,11 @@ import { MessageService } from '../lib/services/MessageService'; export interface OpChanClientConfig { ordiscanApiKey: string; - debug?: boolean; - isDevelopment?: boolean; - isProduction?: boolean; } export class OpChanClient { readonly config: OpChanClientConfig; - // Exposed subsystems readonly messageManager: DefaultMessageManager = messageManager; readonly database: LocalDatabase = localDatabase; readonly forumActions = new ForumActions(); @@ -32,8 +28,7 @@ export class OpChanClient { this.config = config; const env: EnvironmentConfig = { - isDevelopment: config.isDevelopment ?? config.debug ?? false, - isProduction: config.isProduction ?? !config.debug, + apiKeys: { ordiscan: config.ordiscanApiKey, }, diff --git a/packages/core/src/lib/utils/environment.ts b/packages/core/src/lib/utils/environment.ts index 7dfe545..9a16e16 100644 --- a/packages/core/src/lib/utils/environment.ts +++ b/packages/core/src/lib/utils/environment.ts @@ -4,31 +4,19 @@ */ export interface EnvironmentConfig { - isDevelopment?: boolean; - isProduction?: boolean; apiKeys?: { ordiscan?: string; }; } -class Environment { +class Environment { private config: EnvironmentConfig = { - isDevelopment: false, - isProduction: true, }; public configure(config: EnvironmentConfig): void { this.config = { ...this.config, ...config }; } - public get isDev(): boolean { - return this.config.isDevelopment || false; - } - - public get isProduction(): boolean { - return this.config.isProduction ?? true; - } - public get ordiscanApiKey(): string | undefined { return this.config.apiKeys?.ordiscan; } diff --git a/packages/react/src/contexts/AuthContext.tsx b/packages/react/src/contexts/AuthContext.tsx index eac52a7..5ba92bf 100644 --- a/packages/react/src/contexts/AuthContext.tsx +++ b/packages/react/src/contexts/AuthContext.tsx @@ -1,8 +1,9 @@ import React, { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react'; -import { User, EVerificationStatus, OpChanClient, EDisplayPreference } from '@opchan/core'; -import { walletManager, delegationManager, messageManager, LocalDatabase, localDatabase, WalletManager } from '@opchan/core'; +import { User, EVerificationStatus, EDisplayPreference } from '@opchan/core'; +import { delegationManager, type messageManager, localDatabase } from '@opchan/core'; import { DelegationDuration } from '@opchan/core'; import { useAppKitAccount } from '@reown/appkit/react'; +import { useClient } from './ClientContext'; export interface AuthContextValue { currentUser: User | null; @@ -24,10 +25,8 @@ export interface AuthContextValue { const AuthContext = createContext(null); -export const AuthProvider: React.FC<{ - client: OpChanClient; - children: React.ReactNode -}> = ({ client, children }) => { +export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => { + const client = useClient(); const [currentUser, setCurrentUser] = useState(null); const [isAuthenticating, setIsAuthenticating] = useState(false); @@ -78,7 +77,7 @@ export const AuthProvider: React.FC<{ await localDatabase.storeUser(updatedUser); return false; } - }, [client.userIdentityService, currentUser]); + }, [client, currentUser]); // Hydrate user from LocalDatabase on mount useEffect(() => { @@ -307,10 +306,10 @@ export const AuthProvider: React.FC<{ delegateKey, getDelegationStatus, clearDelegation, - signMessage: messageManager.sendMessage.bind(messageManager), + signMessage: client.messageManager.sendMessage.bind(client.messageManager), verifyMessage, }; - }, [currentUser, isAuthenticating, connectWallet, disconnectWallet, verifyOwnership, delegateKey, getDelegationStatus, clearDelegation, verifyMessage]); + }, [client, currentUser, isAuthenticating, connectWallet, disconnectWallet, verifyOwnership, delegateKey, getDelegationStatus, clearDelegation, verifyMessage]); return {children}; }; diff --git a/packages/react/src/contexts/ForumContext.tsx b/packages/react/src/contexts/ForumContext.tsx index b86f306..7703335 100644 --- a/packages/react/src/contexts/ForumContext.tsx +++ b/packages/react/src/contexts/ForumContext.tsx @@ -1,8 +1,9 @@ import React, { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react'; -import { localDatabase, ForumActions, OpChanClient, getDataFromCache } from '@opchan/core'; -import { transformCell, transformPost, transformComment } from '@opchan/core'; +import { localDatabase, getDataFromCache } from '@opchan/core'; import { useAuth } from './AuthContext'; import { Cell, Post, Comment, UserVerificationStatus, EVerificationStatus } from '@opchan/core'; +import { useClient } from './ClientContext'; +import type { ForumActions } from '@opchan/core'; export interface ForumContextValue { cells: Cell[]; @@ -24,10 +25,8 @@ export interface ForumContextValue { const ForumContext = createContext(null); -export const ForumProvider: React.FC<{ - client: OpChanClient; - children: React.ReactNode -}> = ({ client, children }) => { +export const ForumProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => { + const client = useClient(); const { currentUser } = useAuth(); const [cells, setCells] = useState([]); const [posts, setPosts] = useState([]); @@ -38,7 +37,7 @@ export const ForumProvider: React.FC<{ const [isNetworkConnected, setIsNetworkConnected] = useState(false); const [error, setError] = useState(null); - const actions = useMemo(() => new ForumActions(), []); + const actions = useMemo(() => client.forumActions, [client]); const updateFromCache = useCallback(async () => { try { diff --git a/packages/react/src/provider/OpChanProvider.tsx b/packages/react/src/provider/OpChanProvider.tsx index f954131..66432ee 100644 --- a/packages/react/src/provider/OpChanProvider.tsx +++ b/packages/react/src/provider/OpChanProvider.tsx @@ -1,6 +1,6 @@ import React, { useEffect, useMemo, useRef, useState } from 'react'; import { OpChanClient } from '@opchan/core'; -import { localDatabase, messageManager } from '@opchan/core'; +import { localDatabase } from '@opchan/core'; import { ClientProvider } from '../contexts/ClientContext'; import { AuthProvider } from '../contexts/AuthContext'; import { ForumProvider } from '../contexts/ForumContext'; @@ -30,8 +30,6 @@ export const OpChanProvider: React.FC = ({ // Configure environment and create client const client = new OpChanClient({ ordiscanApiKey, - debug: !!debug, - isDevelopment: !!debug, }); clientRef.current = client; @@ -59,9 +57,9 @@ export const OpChanProvider: React.FC = ({ return ( - + - {children} + {children}