11"use client" ;
22
33import { useQuery } from "@tanstack/react-query" ;
4+ import { getUnixTime } from "date-fns" ;
45import { useEffect , useState } from "react" ;
56
67import {
7- type IndexingStatusResponse ,
8+ CrossChainIndexingStatusSnapshot ,
9+ createRealtimeIndexingStatusProjection ,
810 IndexingStatusResponseOk ,
911 OmnichainIndexingStatusIds ,
1012} from "@ensnode/ensnode-sdk" ;
@@ -13,10 +15,7 @@ import { IndexingStats } from "@/components/indexing-status/indexing-stats";
1315import { Button } from "@/components/ui/button" ;
1416import { Card , CardContent , CardDescription , CardHeader , CardTitle } from "@/components/ui/card" ;
1517
16- import {
17- indexingStatusResponseError ,
18- indexingStatusResponseOkOmnichain ,
19- } from "../indexing-status-api.mock" ;
18+ import { indexingStatusResponseOkOmnichain } from "../indexing-status-api.mock" ;
2019
2120type LoadingVariant = "Loading" | "Loading Error" ;
2221type ResponseOkVariant = keyof typeof indexingStatusResponseOkOmnichain ;
@@ -37,7 +36,7 @@ let loadingTimeoutId: number;
3736
3837async function fetchMockedIndexingStatus (
3938 selectedVariant : Variant ,
40- ) : Promise < IndexingStatusResponseOk > {
39+ ) : Promise < CrossChainIndexingStatusSnapshot > {
4140 // always try clearing loading timeout when performing a mocked fetch
4241 // this way we get a fresh and very long request to observe the loading state
4342 if ( loadingTimeoutId ) {
@@ -48,14 +47,19 @@ async function fetchMockedIndexingStatus(
4847 case OmnichainIndexingStatusIds . Unstarted :
4948 case OmnichainIndexingStatusIds . Backfill :
5049 case OmnichainIndexingStatusIds . Following :
51- case OmnichainIndexingStatusIds . Completed :
52- return indexingStatusResponseOkOmnichain [ selectedVariant ] as IndexingStatusResponseOk ;
50+ case OmnichainIndexingStatusIds . Completed : {
51+ const response = indexingStatusResponseOkOmnichain [
52+ selectedVariant
53+ ] as IndexingStatusResponseOk ;
54+
55+ return response . realtimeProjection . snapshot ;
56+ }
5357 case "Error ResponseCode" :
5458 throw new Error (
5559 "Received Indexing Status response with responseCode other than 'ok' which will not be cached." ,
5660 ) ;
5761 case "Loading" :
58- return new Promise < IndexingStatusResponseOk > ( ( _resolve , reject ) => {
62+ return new Promise < CrossChainIndexingStatusSnapshot > ( ( _resolve , reject ) => {
5963 loadingTimeoutId = + setTimeout ( reject , 5 * 60 * 1_000 ) ;
6064 } ) ;
6165 case "Loading Error" :
@@ -67,10 +71,12 @@ export default function MockIndexingStatusPage() {
6771 const [ selectedVariant , setSelectedVariant ] = useState < Variant > (
6872 OmnichainIndexingStatusIds . Unstarted ,
6973 ) ;
74+ const now = getUnixTime ( new Date ( ) ) ;
7075
7176 const mockedIndexingStatus = useQuery ( {
7277 queryKey : [ "mock" , "useIndexingStatus" , selectedVariant ] ,
7378 queryFn : ( ) => fetchMockedIndexingStatus ( selectedVariant ) ,
79+ select : ( cachedSnapshot ) => createRealtimeIndexingStatusProjection ( cachedSnapshot , now ) ,
7480 retry : false , // allows loading error to be observed immediately
7581 } ) ;
7682
0 commit comments