Skip to content

Commit f25af3d

Browse files
authored
feat(example-portfolio): add use(Primary)Account(s) hooks (#1109)
Signed-off-by: Jasper Van der Jeugt <jasper.vanderjeugt@digitalasset.com>
1 parent e4f034b commit f25af3d

19 files changed

+76
-66
lines changed

examples/portfolio/src/components/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright (c) 2025-2026 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
14
export interface ActionItem {
25
id: string
36
tag: string

examples/portfolio/src/components/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright (c) 2025-2026 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
14
import type { ActionItem } from './types'
25

36
export const isReceiver = (item: ActionItem) => {

examples/portfolio/src/contexts/ConnectionContext.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
import { createContext, useContext } from 'react'
5+
import * as sdk from '@canton-network/dapp-sdk'
56

67
export type ConnectionStatus = {
78
connected: boolean
89
sessionToken?: string
910
primaryParty?: string
11+
accounts: sdk.dappAPI.Wallet[]
1012
error?: string
1113
}
1214

examples/portfolio/src/contexts/ConnectionProvider.tsx

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export const ConnectionProvider: React.FC<{ children: React.ReactNode }> = ({
1010
}) => {
1111
const [connectionStatus, setConnectionStatus] = useState<ConnectionStatus>({
1212
connected: false,
13+
accounts: [],
1314
})
1415

1516
const connect = () => {
@@ -19,10 +20,15 @@ export const ConnectionProvider: React.FC<{ children: React.ReactNode }> = ({
1920
connected: status.isConnected,
2021
sessionToken: status.session?.accessToken,
2122
error: undefined,
23+
accounts: [],
2224
})
2325
})
2426
.catch((err) => {
25-
setConnectionStatus({ connected: false, error: err.details })
27+
setConnectionStatus({
28+
connected: false,
29+
error: err.details,
30+
accounts: [],
31+
})
2632
})
2733
}
2834

@@ -32,6 +38,7 @@ export const ConnectionProvider: React.FC<{ children: React.ReactNode }> = ({
3238
sdk.disconnect().then(() =>
3339
setConnectionStatus({
3440
connected: false,
41+
accounts: [],
3542
})
3643
)
3744
}
@@ -84,21 +91,10 @@ export const ConnectionProvider: React.FC<{ children: React.ReactNode }> = ({
8491
.then((wallets) => {
8592
const requestedAccounts =
8693
wallets as sdk.dappAPI.RequestAccountsResult
87-
if (requestedAccounts?.length > 0) {
88-
const primaryWallet = requestedAccounts.find(
89-
(w) => w.primary
90-
)
91-
if (primaryWallet) {
92-
setConnectionStatus((c) => ({
93-
...c,
94-
primaryParty: primaryWallet.partyId,
95-
}))
96-
} else {
97-
// TODO: Throw error
98-
}
99-
} else {
100-
// TODO: Throw error
101-
}
94+
setConnectionStatus((c) => ({
95+
...c,
96+
accounts: requestedAccounts,
97+
}))
10298
})
10399
.catch((err) => {
104100
console.error('Error requesting wallets:', err)
@@ -112,23 +108,10 @@ export const ConnectionProvider: React.FC<{ children: React.ReactNode }> = ({
112108
const onAccountsChanged = (
113109
wallets: sdk.dappAPI.AccountsChangedEvent
114110
) => {
115-
let primaryWallet = undefined
116-
if (wallets.length > 0) {
117-
primaryWallet = wallets.find((w) => w.primary)
118-
}
119-
120-
if (primaryWallet) {
121-
setConnectionStatus((c) => ({
122-
...c,
123-
primaryParty: primaryWallet!.partyId,
124-
}))
125-
} else {
126-
setConnectionStatus((c) => {
127-
const noParty = { ...c }
128-
delete noParty.primaryParty
129-
return noParty
130-
})
131-
}
111+
setConnectionStatus((c) => ({
112+
...c,
113+
accounts: wallets,
114+
}))
132115
}
133116
provider.on<sdk.dappAPI.TxChangedEvent>('txChanged', messageListener)
134117
provider.on<sdk.dappAPI.AccountsChangedEvent>(

examples/portfolio/src/hooks/query-keys.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright (c) 2025-2026 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
14
export const queryKeys = {
25
listPendingTransfers: (party: string | undefined) => [
36
'listPendingTransfers',

examples/portfolio/src/hooks/query-options.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright (c) 2025-2026 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
14
import { queryOptions } from '@tanstack/react-query'
25
import { listPendingTransfers } from '../services/portfolio-service-implementation'
36
import { queryKeys } from './query-keys'
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) 2025-2026 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import { useMemo } from 'react'
5+
import * as sdk from '@canton-network/dapp-sdk'
6+
import { useConnection } from '../contexts/ConnectionContext'
7+
8+
export const useAccounts = (): sdk.dappAPI.Wallet[] => {
9+
const {
10+
status: { accounts },
11+
} = useConnection()
12+
return accounts
13+
}
14+
15+
export const usePrimaryAccount = (): sdk.dappAPI.Wallet | null => {
16+
const accounts = useAccounts()
17+
return useMemo(() => accounts.find((a) => a.primary) ?? null, [accounts])
18+
}

examples/portfolio/src/hooks/useAllocationRequests.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44
import { type PrettyContract } from '@canton-network/core-ledger-client'
55
import { type AllocationRequestView } from '@canton-network/core-token-standard'
66
import { useQuery, type UseQueryResult } from '@tanstack/react-query'
7-
import { useConnection } from '../contexts/ConnectionContext'
87
import { usePortfolio } from '../contexts/PortfolioContext'
8+
import { usePrimaryAccount } from './useAccounts'
99

1010
export const useAllocationRequests = (): UseQueryResult<
1111
PrettyContract<AllocationRequestView>[] | undefined
1212
> => {
13-
const {
14-
status: { primaryParty },
15-
} = useConnection()
13+
const primaryParty = usePrimaryAccount()?.partyId
1614
const { listAllocationRequests } = usePortfolio()
1715
return useQuery({
1816
queryKey: ['listAllocationRequests', primaryParty],

examples/portfolio/src/hooks/useAllocations.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44
import { type PrettyContract } from '@canton-network/core-ledger-client'
55
import { type AllocationView } from '@canton-network/core-token-standard'
66
import { useQuery, type UseQueryResult } from '@tanstack/react-query'
7-
import { useConnection } from '../contexts/ConnectionContext'
7+
import { usePrimaryAccount } from './useAccounts'
88
import { usePortfolio } from '../contexts/PortfolioContext'
99

1010
export const useAllocations = (): UseQueryResult<
1111
PrettyContract<AllocationView>[] | undefined
1212
> => {
13-
const {
14-
status: { primaryParty },
15-
} = useConnection()
13+
const primaryParty = usePrimaryAccount()?.partyId
1614
const { listAllocations } = usePortfolio()
1715
return useQuery({
1816
queryKey: ['listAllocations', primaryParty],

examples/portfolio/src/hooks/useExerciseTransfer.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright (c) 2025-2026 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
14
import { PartyId } from '@canton-network/core-types'
25
import { useQueryClient, useMutation } from '@tanstack/react-query'
36
import { usePortfolio } from '../contexts/PortfolioContext'

0 commit comments

Comments
 (0)