Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion components/features/lending/components/WithdrawForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { AmountInput } from "@/components/shared/ui/AmountInput";
import Button from "@/components/shared/ui/Button";
import { WalletGate } from "@/components/shared/ui/WalletGate";
import HealthFactorBadge from "@/components/shared/ui/HealthFactorBadge";
import { WalletGate } from "@/components/shared/ui/WalletGate";
import PositionSummary from "@/components/features/dashboard/components/PositionSummary";
import {
CRITICAL_HEALTH_FACTOR_THRESHOLD,
Expand Down
21 changes: 16 additions & 5 deletions hooks/usePositions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export interface UsePositionsResult {
isLoading: boolean;
error: Error | null;
refetch: () => Promise<void>;
isStale: boolean;
isOffline: boolean;
}

function parseAmountValue(value: unknown): number {
Expand Down Expand Up @@ -242,9 +244,14 @@ export function usePositions(onError?: (error: Error) => void): UsePositionsResu
const [supplyPositions, setSupplyPositions] = useState<SupplyPosition[]>([]);
const [isLoading, setIsLoading] = useState<boolean>(true);
const [error, setError] = useState<Error | null>(null);
const [isStale, setIsStale] = useState(false);
const [isOffline, setIsOffline] = useState(false);
const hasLoadedOnceRef = useRef(false);

const fetchPositions = useCallback(async () => {
const fetchPositions = useCallback(async (
abortSignal?: AbortSignal,
attempts = 0,
): Promise<void> => {
if (!hasLoadedOnceRef.current) {
setIsLoading(true);
}
Expand All @@ -259,20 +266,24 @@ export function usePositions(onError?: (error: Error) => void): UsePositionsResu
const mappedSupply = mapSupplyPositionsResponse(data);
setPositions(mapped);
setSupplyPositions(mappedSupply);
setIsStale(false);
setIsOffline(false);
} catch (err) {
if (err instanceof DOMException && err.name === "AbortError") {
return; // Aborted
}

setIsStale(true);
const errorObj = err instanceof Error ? err : new Error(String(err));

if (!navigator.onLine) {

const isCurrentlyOffline =
typeof navigator !== "undefined" && !navigator.onLine;
if (isCurrentlyOffline) {
setIsOffline(true);
}

const maxAttempts = 5;
if (attempts < maxAttempts && navigator.onLine) {
if (attempts < maxAttempts && !isCurrentlyOffline) {
const backoff = Math.min(1000 * (2 ** attempts), 10000);
const jitter = Math.random() * 500;
setTimeout(() => {
Expand Down Expand Up @@ -304,7 +315,7 @@ export function usePositions(onError?: (error: Error) => void): UsePositionsResu
error,
refetch: () => fetchPositions(),
isStale,
isOffline
isOffline,
};
}

Expand Down