From 495c40607a374db77d5f648e0d7ace6de9054557 Mon Sep 17 00:00:00 2001 From: b3hr4d Date: Tue, 20 Feb 2024 01:21:29 +0300 Subject: [PATCH] Refactor React hooks and context*** --- packages/core/src/index.ts | 4 ++-- packages/react/src/context/actor/index.tsx | 2 +- packages/react/src/context/agent/context.tsx | 8 +++++--- packages/react/src/context/agent/hooks.ts | 4 ++-- packages/react/src/context/agent/types.ts | 4 ++-- packages/react/src/{hooks => helpers}/actor.ts | 0 packages/react/src/{hooks => helpers}/agent.ts | 0 packages/react/src/{hooks => helpers}/auth.ts | 11 +++++------ packages/react/src/helpers/index.ts | 3 +++ packages/react/src/{hooks => helpers}/types.ts | 0 packages/react/src/hooks/index.ts | 3 --- packages/react/src/index.ts | 9 +++++---- packages/react/src/types.ts | 4 ++-- 13 files changed, 27 insertions(+), 25 deletions(-) rename packages/react/src/{hooks => helpers}/actor.ts (100%) rename packages/react/src/{hooks => helpers}/agent.ts (100%) rename packages/react/src/{hooks => helpers}/auth.ts (95%) create mode 100644 packages/react/src/helpers/index.ts rename packages/react/src/{hooks => helpers}/types.ts (100%) diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 4ad6cba170..242955543a 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -276,7 +276,7 @@ export const createCandidAdapter = (options: CandidAdapterOptions) => { return new CandidAdapter(options) } +export * from "./actor" +export * from "./agent" export * as types from "./types" -export * as actor from "./actor" -export * as agent from "./agent" export * as tools from "./tools" diff --git a/packages/react/src/context/actor/index.tsx b/packages/react/src/context/actor/index.tsx index bada92a5d5..edac071327 100644 --- a/packages/react/src/context/actor/index.tsx +++ b/packages/react/src/context/actor/index.tsx @@ -5,7 +5,7 @@ import { ActorUseQueryArgs, ActorUseUpdateArgs, } from "../../types" -import { getActorHooks } from "../../hooks/actor" +import { getActorHooks } from "../../helpers/actor" import { CreateActorOptions, ActorContextType, diff --git a/packages/react/src/context/agent/context.tsx b/packages/react/src/context/agent/context.tsx index dfcfd2a30b..87a760552d 100644 --- a/packages/react/src/context/agent/context.tsx +++ b/packages/react/src/context/agent/context.tsx @@ -1,7 +1,7 @@ import React, { createContext, useMemo } from "react" import { createAgentManager } from "@ic-reactor/core" -import { getAgentHooks } from "../../hooks/agent" -import { getAuthHooks } from "../../hooks/auth" +import { getAgentHooks } from "../../helpers/agent" +import { getAuthHooks } from "../../helpers/auth" import type { AgentManagerOptions } from "@ic-reactor/core/dist/types" import type { AgentContextValue, AgentProviderProps } from "./types" @@ -20,7 +20,9 @@ AgentProvider.displayName = "AgentProvider" export { AgentProvider } -const createAgentContext = (config: AgentManagerOptions): AgentContextValue => { +export const createAgentContext = ( + config: AgentManagerOptions +): AgentContextValue => { const agentManager = createAgentManager(config) const agenthooks = getAgentHooks(agentManager) const authHooks = getAuthHooks(agentManager) diff --git a/packages/react/src/context/agent/hooks.ts b/packages/react/src/context/agent/hooks.ts index bf973d2db8..3e207e371e 100644 --- a/packages/react/src/context/agent/hooks.ts +++ b/packages/react/src/context/agent/hooks.ts @@ -25,8 +25,8 @@ export const useAgentManager = ( export const useAgent = (agentContext?: AgentContextType) => useAgentManagerContext(agentContext).useAgent() -export const useAuthStore = (agentContext?: AgentContextType) => - useAgentManagerContext(agentContext).useAuthStore() +export const useAuthState = (agentContext?: AgentContextType) => + useAgentManagerContext(agentContext).useAuthState() export const useAgentState = (agentContext?: AgentContextType) => useAgentManagerContext(agentContext).useAgentState() diff --git a/packages/react/src/context/agent/types.ts b/packages/react/src/context/agent/types.ts index 7b9397678b..213a5ba4c1 100644 --- a/packages/react/src/context/agent/types.ts +++ b/packages/react/src/context/agent/types.ts @@ -1,8 +1,8 @@ import React, { PropsWithChildren } from "react" import type { AgentManagerOptions } from "@ic-reactor/core/dist/types" import type { AgentManager } from "@ic-reactor/core/dist/agent" -import type { getAuthHooks } from "../../hooks/auth" -import type { getAgentHooks } from "../../hooks/agent" +import type { getAuthHooks } from "../../helpers/auth" +import type { getAgentHooks } from "../../helpers/agent" export type AgentContextValue = ReturnType & ReturnType & { diff --git a/packages/react/src/hooks/actor.ts b/packages/react/src/helpers/actor.ts similarity index 100% rename from packages/react/src/hooks/actor.ts rename to packages/react/src/helpers/actor.ts diff --git a/packages/react/src/hooks/agent.ts b/packages/react/src/helpers/agent.ts similarity index 100% rename from packages/react/src/hooks/agent.ts rename to packages/react/src/helpers/agent.ts diff --git a/packages/react/src/hooks/auth.ts b/packages/react/src/helpers/auth.ts similarity index 95% rename from packages/react/src/hooks/auth.ts rename to packages/react/src/helpers/auth.ts index c8b3f206cc..8a50c202ed 100644 --- a/packages/react/src/hooks/auth.ts +++ b/packages/react/src/helpers/auth.ts @@ -8,13 +8,12 @@ import { AuthArgs } from "../types" export const getAuthHooks = (agentManager: AgentManager) => { const { authenticate: authenticator, authStore, isLocalEnv } = agentManager - const useAuthStore = () => { - const authState = useStore(authStore, (state) => state) - return authState + const useAuthState = () => { + return useStore(authStore, (state) => state) } const useUserPrincipal = () => { - const { identity } = useAuthStore() + const { identity } = useAuthState() return identity?.getPrincipal() } @@ -31,7 +30,7 @@ export const getAuthHooks = (agentManager: AgentManager) => { const [loginLoading, setLoginLoading] = useState(false) const [loginError, setLoginError] = useState(null) const { authClient, authenticated, authenticating, identity } = - useAuthStore() + useAuthState() const authenticate = useCallback(async () => { const authenticatePromise: Promise = new Promise( @@ -152,7 +151,7 @@ export const getAuthHooks = (agentManager: AgentManager) => { return { useUserPrincipal, - useAuthStore, + useAuthState, useAuthClient, } } diff --git a/packages/react/src/helpers/index.ts b/packages/react/src/helpers/index.ts new file mode 100644 index 0000000000..fa7b3f065c --- /dev/null +++ b/packages/react/src/helpers/index.ts @@ -0,0 +1,3 @@ +export * from "./agent" +export * from "./actor" +export * from "./auth" diff --git a/packages/react/src/hooks/types.ts b/packages/react/src/helpers/types.ts similarity index 100% rename from packages/react/src/hooks/types.ts rename to packages/react/src/helpers/types.ts diff --git a/packages/react/src/hooks/index.ts b/packages/react/src/hooks/index.ts index b15a84c865..3b1f03fd24 100644 --- a/packages/react/src/hooks/index.ts +++ b/packages/react/src/hooks/index.ts @@ -1,5 +1,2 @@ export * from "./useActor" export * from "./useCandid" -export * from "./agent" -export * from "./actor" -export * from "./auth" diff --git a/packages/react/src/index.ts b/packages/react/src/index.ts index faaa537e8f..7eff834e88 100644 --- a/packages/react/src/index.ts +++ b/packages/react/src/index.ts @@ -1,6 +1,6 @@ import { createReActorStore } from "@ic-reactor/core" -import { getActorHooks } from "./hooks/actor" -import { getAuthHooks } from "./hooks/auth" +import { getActorHooks } from "./helpers/actor" +import { getAuthHooks } from "./helpers/auth" import { BaseActor, CreateReActorOptions } from "@ic-reactor/core/dist/types" export const createReActor = ({ @@ -48,7 +48,8 @@ export const createReActor = ({ } } -export * as agent from "./context/agent" -export * as actor from "./context/actor" +export * from "./context/agent" +export * from "./context/actor" +export * as helpers from "./helpers" export * as hooks from "./hooks" export * as types from "./types" diff --git a/packages/react/src/types.ts b/packages/react/src/types.ts index 3e4e1fbeb7..dc6622f249 100644 --- a/packages/react/src/types.ts +++ b/packages/react/src/types.ts @@ -10,12 +10,12 @@ import type { FunctionName, VisitService, } from "@ic-reactor/core/dist/types" -import type { AgentHooks, AuthHooks } from "./hooks/types" +import type { AgentHooks, AuthHooks } from "./helpers/types" export * from "@ic-reactor/core/dist/types" export * from "./context/agent/types" export * from "./context/actor/types" -export * from "./hooks/types" +export * from "./helpers/types" export type AuthArgs = { onAuthentication?: (promise: () => Promise) => void