From 6eb2d394570b9c9d96c9f99ab4d3af3a220437f2 Mon Sep 17 00:00:00 2001 From: Henrichy Date: Sat, 21 Feb 2026 19:03:52 +0100 Subject: [PATCH] useStellarAccount hook done --- dongle/.vscode/settings.json | 3 + dongle/hooks/useStellarAccount.md | 135 ++++++++++++++++++++++++++++++ dongle/hooks/useStellarAccount.ts | 89 ++++++++++++++++++++ dongle/package-lock.json | 1 + 4 files changed, 228 insertions(+) create mode 100644 dongle/.vscode/settings.json create mode 100644 dongle/hooks/useStellarAccount.md create mode 100644 dongle/hooks/useStellarAccount.ts diff --git a/dongle/.vscode/settings.json b/dongle/.vscode/settings.json new file mode 100644 index 0000000..5480842 --- /dev/null +++ b/dongle/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "kiroAgent.configureMCP": "Disabled" +} \ No newline at end of file diff --git a/dongle/hooks/useStellarAccount.md b/dongle/hooks/useStellarAccount.md new file mode 100644 index 0000000..f0d1b61 --- /dev/null +++ b/dongle/hooks/useStellarAccount.md @@ -0,0 +1,135 @@ +# useStellarAccount Hook + +A React hook for fetching and managing Stellar account data. + +## Overview + +`useStellarAccount` provides a simple interface to fetch Stellar account information including balances, with built-in loading and error state management. + +## Usage + +```tsx +import { useStellarAccount } from '@/hooks/useStellarAccount'; + +function AccountDashboard() { + const { account, balances, loading, error } = useStellarAccount(); + + if (loading) return
Loading account data...
; + if (error) return
Error: {error.message}
; + if (!account) return
No account connected
; + + return ( +
+

Account: {account.account_id}

+

Balances:

+ +
+ ); +} +``` + +## API + +### Parameters + +- `publicKey` (optional): Stellar public key to fetch account data for. If not provided, uses the public key from the Wallet Context. + +### Return Value + +```typescript +{ + account: StellarAccount | null; // Full account data + balances: StellarBalance[]; // Array of account balances + loading: boolean; // Loading state + error: Error | null; // Error state +} +``` + +## Types + +### StellarAccount + +```typescript +interface StellarAccount { + id: string; + account_id: string; + sequence: string; + subentry_count: number; + balances: StellarBalance[]; +} +``` + +### StellarBalance + +```typescript +interface StellarBalance { + asset_type: string; + asset_code?: string; + asset_issuer?: string; + balance: string; +} +``` + +## Examples + +### Using with Wallet Context + +```tsx +// Uses public key from wallet context automatically +const { account, balances, loading, error } = useStellarAccount(); +``` + +### Using with Specific Public Key + +```tsx +// Fetch data for a specific account +const { account, balances, loading, error } = useStellarAccount( + 'GXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' +); +``` + +### Handling States + +```tsx +function AccountView() { + const { account, balances, loading, error } = useStellarAccount(); + + if (loading) { + return ; + } + + if (error) { + return ; + } + + if (!account) { + return ; + } + + return ; +} +``` + +## Dependencies + +This hook requires: + +1. **Stellar Service Layer** (#6) - Provides `getAccountInfo` method +2. **Wallet Context** (#4) - Provides global wallet state with `publicKey` + +## Current Status + +⚠️ **Placeholder Implementation**: This hook currently uses mock implementations for its dependencies. Once the Stellar Service and Wallet Context are implemented, the placeholders will be replaced with actual integrations. + +## Future Enhancements + +- Add refresh/refetch functionality +- Add caching to reduce API calls +- Support for real-time account updates +- Add retry logic for failed requests diff --git a/dongle/hooks/useStellarAccount.ts b/dongle/hooks/useStellarAccount.ts new file mode 100644 index 0000000..064befe --- /dev/null +++ b/dongle/hooks/useStellarAccount.ts @@ -0,0 +1,89 @@ +import { useState, useEffect } from 'react'; + +// Placeholder types - will be replaced when Stellar service is implemented +interface StellarBalance { + asset_type: string; + asset_code?: string; + asset_issuer?: string; + balance: string; +} + +interface StellarAccount { + id: string; + account_id: string; + sequence: string; + subentry_count: number; + balances: StellarBalance[]; +} + +interface UseStellarAccountReturn { + account: StellarAccount | null; + balances: StellarBalance[]; + loading: boolean; + error: Error | null; +} + +// Placeholder for Stellar service - will be replaced with actual service from #6 +const stellarService = { + getAccountInfo: async (publicKey: string): Promise => { + // Mock implementation - replace with actual Stellar SDK call + throw new Error('Stellar service not yet implemented'); + } +}; + +// Placeholder for Wallet context - will be replaced with actual context from #4 +const useWallet = () => { + // Mock implementation - replace with actual wallet context + return { + publicKey: null as string | null + }; +}; + +/** + * Custom hook to fetch and manage Stellar account data + * + * @param publicKey - Optional Stellar public key. If not provided, uses key from wallet context + * @returns Account data, balances, loading state, and error state + */ +export function useStellarAccount(publicKey?: string): UseStellarAccountReturn { + const { publicKey: walletPublicKey } = useWallet(); + const [account, setAccount] = useState(null); + const [loading, setLoading] = useState(false); + const [error, setError] = useState(null); + + const effectivePublicKey = publicKey || walletPublicKey; + + useEffect(() => { + if (!effectivePublicKey) { + setAccount(null); + setError(null); + setLoading(false); + return; + } + + const fetchAccountData = async () => { + setLoading(true); + setError(null); + + try { + const accountData = await stellarService.getAccountInfo(effectivePublicKey); + setAccount(accountData); + } catch (err) { + const error = err instanceof Error ? err : new Error('Failed to fetch account data'); + setError(error); + setAccount(null); + } finally { + setLoading(false); + } + }; + + fetchAccountData(); + }, [effectivePublicKey]); + + return { + account, + balances: account?.balances || [], + loading, + error + }; +} diff --git a/dongle/package-lock.json b/dongle/package-lock.json index fd607db..c590dc4 100644 --- a/dongle/package-lock.json +++ b/dongle/package-lock.json @@ -3217,6 +3217,7 @@ "integrity": "sha512-whOE1HFo/qJDyX4SnXzP4N6zOWn79WhnCUY/iDR0mPfQZO8wcYE4JClzI2oZrhBnnMUCBCHZhO6VQyoBU95mZA==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@rtsao/scc": "^1.1.0", "array-includes": "^3.1.9",