11"use client" ;
22
3+ import { secondsToMilliseconds } from "date-fns" ;
34import { useCallback , useMemo } from "react" ;
45
56import {
67 createIndexingStatusQueryOptions ,
78 QueryParameter ,
89 useENSNodeSDKConfig ,
910 type useIndexingStatus ,
11+ useNow ,
1012 useSwrQuery ,
1113 WithSDKConfigParameter ,
1214} from "@ensnode/ensnode-react" ;
1315import {
16+ CrossChainIndexingStatusSnapshotOmnichain ,
17+ createRealtimeIndexingStatusProjection ,
1418 type IndexingStatusRequest ,
1519 IndexingStatusResponseCodes ,
16- IndexingStatusResponseOk ,
20+ RealtimeIndexingStatusProjection ,
1721} from "@ensnode/ensnode-sdk" ;
1822
19- const DEFAULT_REFETCH_INTERVAL = 10 * 1000 ;
23+ const DEFAULT_REFETCH_INTERVAL = secondsToMilliseconds ( 10 ) ;
24+
25+ const REALTIME_PROJECTION_REFRESH_RATE = secondsToMilliseconds ( 1 ) ;
2026
2127interface UseIndexingStatusParameters
2228 extends IndexingStatusRequest ,
23- QueryParameter < IndexingStatusResponseOk > { }
29+ QueryParameter < CrossChainIndexingStatusSnapshotOmnichain > { }
2430
2531/**
2632 * A proxy hook for {@link useIndexingStatus} which applies
@@ -31,6 +37,7 @@ export function useIndexingStatusWithSwr(
3137) {
3238 const { config, query = { } } = parameters ;
3339 const _config = useENSNodeSDKConfig ( config ) ;
40+ const now = useNow ( REALTIME_PROJECTION_REFRESH_RATE ) ;
3441
3542 const queryOptions = useMemo ( ( ) => createIndexingStatusQueryOptions ( _config ) , [ _config ] ) ;
3643 const queryKey = useMemo ( ( ) => [ "swr" , ...queryOptions . queryKey ] , [ queryOptions . queryKey ] ) ;
@@ -46,18 +53,35 @@ export function useIndexingStatusWithSwr(
4653 ) ;
4754 }
4855
49- // successful response to be cached
50- return response ;
56+ // The indexing status snapshot has been fetched and successfully validated for caching.
57+ // Therefore, return it so that query cache for `queryOptions.queryKey` will:
58+ // - Replace the currently cached value (if any) with this new value.
59+ // - Return this non-null value.
60+ return response . realtimeProjection . snapshot ;
5161 } ) ,
5262 [ queryOptions . queryFn ] ,
5363 ) ;
5464
65+ // Call select function to `createRealtimeIndexingStatusProjection` each time
66+ // `now` is updated.
67+ const select = useCallback (
68+ (
69+ cachedSnapshot : CrossChainIndexingStatusSnapshotOmnichain ,
70+ ) : RealtimeIndexingStatusProjection => {
71+ const realtimeProjection = createRealtimeIndexingStatusProjection ( cachedSnapshot , now ) ;
72+
73+ return realtimeProjection ;
74+ } ,
75+ [ now ] ,
76+ ) ;
77+
5578 return useSwrQuery ( {
5679 ...queryOptions ,
5780 refetchInterval : query . refetchInterval ?? DEFAULT_REFETCH_INTERVAL , // Indexing status changes frequently
5881 ...query ,
5982 enabled : query . enabled ?? queryOptions . enabled ,
6083 queryKey,
6184 queryFn,
85+ select,
6286 } ) ;
6387}
0 commit comments