Skip to content

Commit

Permalink
Refactor React hooks and context***
Browse files Browse the repository at this point in the history
  • Loading branch information
b3hr4d committed Feb 19, 2024
1 parent 54c9942 commit 495c406
Show file tree
Hide file tree
Showing 13 changed files with 27 additions and 25 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 1 addition & 1 deletion packages/react/src/context/actor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ActorUseQueryArgs,
ActorUseUpdateArgs,
} from "../../types"
import { getActorHooks } from "../../hooks/actor"
import { getActorHooks } from "../../helpers/actor"
import {
CreateActorOptions,
ActorContextType,
Expand Down
8 changes: 5 additions & 3 deletions packages/react/src/context/agent/context.tsx
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/context/agent/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/context/agent/types.ts
Original file line number Diff line number Diff line change
@@ -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<typeof getAuthHooks> &
ReturnType<typeof getAgentHooks> & {
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand All @@ -31,7 +30,7 @@ export const getAuthHooks = (agentManager: AgentManager) => {
const [loginLoading, setLoginLoading] = useState(false)
const [loginError, setLoginError] = useState<Error | null>(null)
const { authClient, authenticated, authenticating, identity } =
useAuthStore()
useAuthState()

const authenticate = useCallback(async () => {
const authenticatePromise: Promise<Identity> = new Promise(
Expand Down Expand Up @@ -152,7 +151,7 @@ export const getAuthHooks = (agentManager: AgentManager) => {

return {
useUserPrincipal,
useAuthStore,
useAuthState,
useAuthClient,
}
}
3 changes: 3 additions & 0 deletions packages/react/src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./agent"
export * from "./actor"
export * from "./auth"
File renamed without changes.
3 changes: 0 additions & 3 deletions packages/react/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
export * from "./useActor"
export * from "./useCandid"
export * from "./agent"
export * from "./actor"
export * from "./auth"
9 changes: 5 additions & 4 deletions packages/react/src/index.ts
Original file line number Diff line number Diff line change
@@ -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 = <A = BaseActor>({
Expand Down Expand Up @@ -48,7 +48,8 @@ export const createReActor = <A = BaseActor>({
}
}

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"
4 changes: 2 additions & 2 deletions packages/react/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Identity>) => void
Expand Down

0 comments on commit 495c406

Please sign in to comment.