-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: get balances on interval (#66)
* underline footer links on hover * useBalancePolling hook
- Loading branch information
1 parent
f35ca28
commit 48d439a
Showing
5 changed files
with
148 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
web/src/features/GetBalancePolling/hooks/useGetBalancePolling.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { useCallback, useEffect, useState } from "react"; | ||
|
||
interface PollingConfig { | ||
enabled: boolean; | ||
intervalMS?: number; | ||
onError?: (error: Error) => void; | ||
} | ||
|
||
/** | ||
* Hook to poll for balance at a given interval | ||
* @param fetchBalance | ||
* @param config | ||
*/ | ||
export default function useBalancePolling<T>( | ||
fetchBalance: () => Promise<T>, | ||
config: PollingConfig, | ||
) { | ||
const [balance, setBalance] = useState<T | null>(null); | ||
const [isLoading, setIsLoading] = useState(false); | ||
const [error, setError] = useState<Error | null>(null); | ||
|
||
const getBalance = useCallback(async () => { | ||
if (!config.enabled) return; | ||
|
||
setIsLoading(true); | ||
try { | ||
const result = await fetchBalance(); | ||
setBalance(result); | ||
setError(null); | ||
} catch (e) { | ||
const error = | ||
e instanceof Error ? e : new Error("Failed to fetch balance"); | ||
setError(error); | ||
config.onError?.(error); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}, [fetchBalance, config.enabled, config.onError]); | ||
|
||
useEffect(() => { | ||
getBalance().then((_) => {}); | ||
|
||
// setup polling if enabled | ||
if (config.enabled && config.intervalMS) { | ||
const intervalId = setInterval(getBalance, config.intervalMS); | ||
return () => clearInterval(intervalId); | ||
} | ||
return undefined; | ||
}, [getBalance, config.enabled, config.intervalMS]); | ||
|
||
return { | ||
balance, | ||
isLoading, | ||
error, | ||
refetch: getBalance, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import useBalancePolling from "./hooks/useGetBalancePolling"; | ||
|
||
export { useBalancePolling }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,9 @@ | |
.content { | ||
a { | ||
color: $astria-orange-soft; | ||
&:hover { | ||
text-decoration: underline; | ||
} | ||
} | ||
} | ||
} |