Skip to content

Commit

Permalink
Refactor agentContext parameter in hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
b3hr4d committed Apr 4, 2024
1 parent 1935427 commit 8c6bb37
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 30 deletions.
5 changes: 1 addition & 4 deletions packages/react/src/context/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ export interface CreateAgentCotextParameters extends AgentManagerParameters {
export interface CreateAgentContextReturnType
extends AgentHooksReturnType,
AuthHooksReturnType {
useAgentManager: (
agentContext?: React.Context<AgentContext | null>
) => AgentManager
useAgentManager: () => AgentManager
AgentProvider: React.FC<AgentProviderProps>
}

Expand Down Expand Up @@ -54,7 +52,6 @@ export interface CreateActorContextParameters
> {
didjsId?: string
canisterId?: string
agentContext?: React.Context<AgentContext | null>
idlFactory?: IDL.InterfaceFactory
loadingComponent?: React.ReactNode
}
17 changes: 12 additions & 5 deletions packages/react/src/helpers/authHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export const authHooks = (agentManager: AgentManager): AuthHooksReturnType => {
onLoginError,
onLoggedOut,
}: UseAuthParameters = {}) => {
const network = React.useRef("ic")

const [loginState, setLoginState] = React.useState<LoginState>({
loading: false,
error: undefined,
Expand Down Expand Up @@ -136,11 +138,16 @@ export const authHooks = (agentManager: AgentManager): AuthHooksReturnType => {
[onLoggedOut]
)

React.useEffect(() => {
if (!authenticating && !authenticated) {
authenticate()
}
}, [])
React.useEffect(
() =>
agentManager.subscribeAgentState((state) => {
if (network.current !== state.network) {
network.current = state.network
authenticate()
}
}),
[]
)

return {
authenticated,
Expand Down
12 changes: 4 additions & 8 deletions packages/react/src/helpers/extractAgentContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ import type {
export const extractAgentContext = (
agentContext: React.Context<AgentContext | null>
): Omit<CreateAgentContextReturnType, "AgentProvider"> => {
const useAgentContext = (
mybeAgentContext?: React.Context<AgentContext | null>
) => {
const context = React.useContext(mybeAgentContext || agentContext)
const useAgentContext = () => {
const context = React.useContext(agentContext)

if (!context) {
throw new Error("Agent hooks must be used within a AgentProvider")
Expand All @@ -46,10 +44,8 @@ export const extractAgentContext = (
return context
}

const useAgentManager = (
agentContext?: React.Context<AgentContext | null>
): AgentManager => {
const context = useAgentContext(agentContext)
const useAgentManager = (): AgentManager => {
const context = useAgentContext()

return context.agentManager
}
Expand Down
9 changes: 3 additions & 6 deletions packages/react/src/hooks/agent/useCandidAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { CandidAdapter } from "@ic-reactor/core/dist/classes"
import { AgentContext } from "../../types"
import { useAgentManager } from "./useAgentManager"
import { useEffect, useState } from "react"
import { createCandidAdapter } from "@ic-reactor/core"
import { useAgent } from "./useAgent"

export interface UseCandidAdapterParams {
agentContext?: React.Context<AgentContext | null>
didjsCanisterId?: string
}
/**
Expand Down Expand Up @@ -33,10 +31,9 @@ export interface UseCandidAdapterParams {
export const useCandidAdapter = (config: UseCandidAdapterParams) => {
const [candidAdapter, setCandidAdapter] = useState<CandidAdapter>()

const agentManager = useAgentManager(config.agentContext)
const agent = useAgent()

useEffect(() => {
const agent = agentManager.getAgent()
try {
const candidManager = createCandidAdapter({
agent,
Expand All @@ -47,7 +44,7 @@ export const useCandidAdapter = (config: UseCandidAdapterParams) => {
// eslint-disable-next-line no-console
console.error("Error creating CandidAdapter", error)
}
}, [agentManager, config.didjsCanisterId])
}, [agent, config.didjsCanisterId])

return candidAdapter
}
2 changes: 0 additions & 2 deletions packages/react/src/hooks/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { IDL } from "@dfinity/candid"
import { AgentContext } from "../context/types"
import {
ActorHooksReturnType,
ActorManagerParameters,
Expand All @@ -13,7 +12,6 @@ export interface UseActorParameters
> {
canisterId: string
idlFactory?: IDL.InterfaceFactory
agentContext?: React.Context<AgentContext | null>
didjsCanisterId?: string
}

Expand Down
4 changes: 1 addition & 3 deletions packages/react/src/hooks/useActor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ export const useActor = <A = BaseActor>(
const {
canisterId,
idlFactory: maybeIdlFactory,
agentContext,
didjsCanisterId,
...actorConfig
} = config
Expand All @@ -99,7 +98,6 @@ export const useActor = <A = BaseActor>(
})

const candidAdapter = useCandidAdapter({
agentContext,
didjsCanisterId,
})

Expand Down Expand Up @@ -132,7 +130,7 @@ export const useActor = <A = BaseActor>(
}
}, [canisterId, candidAdapter, authenticating, didjsCanisterId])

const agentManager = useAgentManager(agentContext)
const agentManager = useAgentManager()

const initialActorManager = useCallback(
(idlFactory: IDL.InterfaceFactory) => {
Expand Down
3 changes: 1 addition & 2 deletions packages/react/test/query.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ const config: CreateReactorCoreParameters = {
idlFactory,
}

const { useAuth, useQueryCall, useUpdateCall } =
createReactor<typeof backend>(config)
const { useQueryCall } = createReactor<typeof backend>(config)

describe("createReactor", () => {
it("should query on mount", async () => {
Expand Down

0 comments on commit 8c6bb37

Please sign in to comment.