import { useState, useEffect } from 'react' import { getSafeStorage } from '../../storage/safe-store.js' import type { SafeAccount } from '../../types/safe.js' /** * Hook for accessing all Safe accounts in Ink components. * Provides the complete list of Safes across all chains. * * @example * ```tsx * function SafeListScreen() { * const { safes, loading, error } = useSafes() * * if (loading) return * if (error) return {error} * * return } /> * } * ``` */ export function useSafes() { const [safes, setSafes] = useState([]) const [loading, setLoading] = useState(true) const [error, setError] = useState(null) useEffect(() => { try { const safeStorage = getSafeStorage() const allSafes = safeStorage.getAllSafes() setSafes(allSafes) setLoading(false) } catch (err) { setError(err instanceof Error ? err.message : 'Failed to load Safes') setLoading(false) } }, []) return { safes, loading, error } } /** * Hook for accessing a specific Safe account by chainId and address. * * @param chainId - The chain ID where the Safe is deployed * @param address - The Safe address * * @example * ```tsx * function SafeDetails({ chainId, address }) { * const { safe, loading, error } = useSafe(chainId, address) * * if (loading) return * if (error) return {error} * if (!safe) return Safe not found * * return ( * * ) * } * ``` */ export function useSafe(chainId: string, address: string) { const [safe, setSafe] = useState(null) const [loading, setLoading] = useState(true) const [error, setError] = useState(null) useEffect(() => { try { const safeStorage = getSafeStorage() const foundSafe = safeStorage.getSafe(chainId, address) setSafe(foundSafe || null) setLoading(false) } catch (err) { setError(err instanceof Error ? err.message : 'Failed to load Safe') setLoading(false) } }, [chainId, address]) return { safe, loading, error } } /** * Hook for accessing Safe accounts filtered by chain. * Useful for displaying chain-specific Safe lists. * * @param chainId - The chain ID to filter by * * @example * ```tsx * function ChainSafesScreen({ chainId }) { * const { safes, loading, error } = useSafesByChain(chainId) * * return ( * *
* } /> * * ) * } * ``` */ export function useSafesByChain(chainId: string) { const [safes, setSafes] = useState([]) const [loading, setLoading] = useState(true) const [error, setError] = useState(null) useEffect(() => { try { const safeStorage = getSafeStorage() const chainSafes = safeStorage.getSafesByChain(chainId) setSafes(chainSafes) setLoading(false) } catch (err) { setError(err instanceof Error ? err.message : 'Failed to load Safes') setLoading(false) } }, [chainId]) return { safes, loading, error } } /** * Hook for checking if a Safe exists without loading full data. * Useful for validation flows. * * @param chainId - The chain ID where the Safe might exist * @param address - The Safe address to check * * @example * ```tsx * function SafeValidator({ chainId, address }) { * const { exists, loading } = useSafeExists(chainId, address) * * if (loading) return * * return ( * * Safe {exists ? 'exists' : 'does not exist'} * * ) * } * ``` */ export function useSafeExists(chainId: string, address: string) { const [exists, setExists] = useState(false) const [loading, setLoading] = useState(true) const [error, setError] = useState(null) useEffect(() => { try { const safeStorage = getSafeStorage() const doesExist = safeStorage.safeExists(chainId, address) setExists(doesExist) setLoading(false) } catch (err) { setError(err instanceof Error ? err.message : 'Failed to check Safe existence') setLoading(false) } }, [chainId, address]) return { exists, loading, error } }