Skip to content
Merged
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
11 changes: 5 additions & 6 deletions src/hooks/useUserPositions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ const useUserPositions = (user: string | undefined, showEmpty = false) => {
isLoading: isLoadingInitialData, // Primary loading state
isRefetching: isRefetchingInitialData,
error: initialError,
refetch: refetchInitialData,
} = useQuery<InitialDataResponse>({
// Note: Removed MarketsContextType type assertion
queryKey: positionKeys.initialData(user ?? ''),
Expand All @@ -148,8 +147,7 @@ const useUserPositions = (user: string | undefined, showEmpty = false) => {
return { finalMarketKeys };
},
enabled: !!user && markets.length > 0,
staleTime: 30000,
gcTime: 5 * 60 * 1000,
staleTime: 0,
});

// 2. Query for enhanced position data (snapshots), dependent on initialData
Expand Down Expand Up @@ -251,17 +249,18 @@ const useUserPositions = (user: string | undefined, showEmpty = false) => {
gcTime: 5 * 60 * 1000,
}); // <-- End options object here

// Refetch function targets the initial data query
// Refetch function targets both the initial data and enhanced queries
const refetch = useCallback(
async (onSuccess?: () => void) => {
try {
await refetchInitialData();
await queryClient.invalidateQueries({ queryKey: positionKeys.initialData(user ?? '') });
await queryClient.invalidateQueries({ queryKey: ['enhanced-positions', user] });
onSuccess?.();
} catch (error) {
console.error('[Positions] Error during manual refetch:', error);
}
},
[refetchInitialData],
[queryClient, user],
);

// Combine refetching states
Expand Down
17 changes: 13 additions & 4 deletions src/hooks/useUserPositionsSummaryData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useQuery } from '@tanstack/react-query';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { Address } from 'viem';
import { SupportedNetworks } from '@/utils/networks';
import {
Expand All @@ -7,7 +7,7 @@ import {
} from '@/utils/positions';
import { estimatedBlockNumber } from '@/utils/rpc';
import { MarketPositionWithEarnings } from '@/utils/types';
import useUserPositions from './useUserPositions';
import useUserPositions, { positionKeys } from './useUserPositions';
import useUserTransactions from './useUserTransactions';

type BlockNumbers = {
Expand Down Expand Up @@ -79,11 +79,12 @@ const useUserPositionsSummaryData = (user: string | undefined) => {
loading: positionsLoading,
isRefetching,
positionsError,
refetch: refetchPositions,
} = useUserPositions(user, true);

const { fetchTransactions } = useUserTransactions();

const queryClient = useQueryClient();

// Query for block numbers - this runs once and is cached
const { data: blockNums, isLoading: isLoadingBlockNums } = useQuery({
queryKey: blockKeys.all,
Expand Down Expand Up @@ -174,7 +175,15 @@ const useUserPositionsSummaryData = (user: string | undefined) => {

const refetch = async (onSuccess?: () => void) => {
try {
await refetchPositions();
// Do not invalidate block numbers: keep the old block numbers
// await queryClient.invalidateQueries({ queryKey: blockKeys.all });

// Invalidate positions initial data
await queryClient.invalidateQueries({ queryKey: positionKeys.initialData(user ?? '') });
// Invalidate positions enhanced data (invalidate all for this user)
await queryClient.invalidateQueries({ queryKey: ['enhanced-positions', user] });
// Invalidate earnings query
await queryClient.invalidateQueries({ queryKey: ['positions-earnings', user] });
if (onSuccess) {
onSuccess();
}
Expand Down
2 changes: 0 additions & 2 deletions src/hooks/useUserTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ const useUserTransactions = () => {
// 3. Create fetch promises
const fetchPromises: Promise<TransactionResponse>[] = [];

console.log('morphoNetworks', morphoNetworks);

// Morpho API Fetch
if (morphoNetworks.length > 0) {
// morphoNetworks directly contains the numeric chain IDs (e.g., [1, ...])
Expand Down