refactor(dashboard): use cached useDashboard hook for snapshot data - #1382
Open
Rafiat30 wants to merge 1 commit into
Open
refactor(dashboard): use cached useDashboard hook for snapshot data#1382Rafiat30 wants to merge 1 commit into
Rafiat30 wants to merge 1 commit into
Conversation
…absCrypt#1244) DashboardView previously hand-rolled its own fetch/loading/error state for the dashboard snapshot in a useEffect, even though useDashboard (a React Query hook with staleTime/retry inherited from QueryProvider) was already exported from lib/dashboard.ts and unused. This meant every mount refetched the snapshot from scratch, with no caching, retry, or request dedup. DashboardView now calls useDashboard(session.publicKey) directly: - Loading/error state derive from the hook's isLoading/isError/error. - Local optimistic updates (topUpStreamLocally, addStreamLocally) write through queryClient.setQueryData(dashboardQueryKey(...)) instead of a local useState setter. - The SSE-triggered refresh invalidates the query (queryClient.invalidateQueries) instead of manually refetching. - The withdraw flow calls the hook's refetchSnapshot() instead of manually re-fetching and setting state. - The error state's retry button calls refetchSnapshot(). Added tests proving a remount within the configured staleTime does not trigger a duplicate network fetch (the issue's acceptance criterion), both at the hook level (lib/dashboard.test.ts) and through the full DashboardView component (dashboard-view.test.tsx).
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1244
What changed
useDashboard(frontend/src/lib/dashboard.ts) is a React Query hook — it inheritsstaleTime: 10_000,refetchOnWindowFocus: false, andretry: 1from theQueryClientcreated infrontend/src/components/providers/query-provider.tsx— but it was exported and never called anywhere.DashboardViewinstead hand-rolled its ownuseState/useEffectfetch of the snapshot, so every mount refetched from scratch with no caching, retry, or request dedup, and switching tabs/routes away and back re-fetched the same data unnecessarily.DashboardViewnow callsuseDashboard(session.publicKey)and drives all snapshot state through the React Query cache:isLoading/isError/errorinstead of separateuseStateflags.topUpStreamLocally,addStreamLocally) now write throughqueryClient.setQueryData(dashboardQueryKey(session.publicKey), updaterFn)instead of a localsetSnapshot, so the cache and the UI stay in sync.queryClient.invalidateQueries({ queryKey: dashboardQueryKey(session.publicKey) })instead of manually callingfetchDashboardDataandsetSnapshot. React Query handles the refetch and re-render.refetchSnapshot()(React Query'srefetch) instead of manually re-fetching and callingsetSnapshot.refetchSnapshot()instead of a bespokeloadSnapshotcallback.useEffectfetch-on-mount effect andloadSnapshotcallback were removed entirely —useDashboardnow owns the fetch/cache lifecycle.Because the hook's
queryFn(fetchDashboardData) andqueryKey(dashboardQueryKey(publicKey)) are unchanged, and the app's root layout already wraps everything in the single long-livedQueryProvider/QueryClient, remountingDashboardView(e.g. navigating away and back, or switching tabs) within the 10sstaleTimewindow is now served from cache instead of issuing a new network request.Files changed
Modified
frontend/src/components/dashboard/dashboard-view.tsx— replaced hand-rolled fetch/loading/error state withuseDashboard; optimistic updates and SSE refresh now go through the query cache; withdraw/retry userefetchSnapshot.frontend/src/lib/dashboard.test.ts— added adescribe("useDashboard", ...)block.New
frontend/src/components/dashboard/dashboard-view.test.tsx— component-level tests forDashboardViewwired to a realQueryClient.Tests added
In
frontend/src/lib/dashboard.test.ts(useDashboardhook, viarenderHook+QueryClientProvider):publicKeyis empty.DashboardSnapshot(2 backend requests: sender + recipient).QueryClientconfigured with the app's real defaults (staleTime: 10_000,refetchOnWindowFocus: false,retry: 1), unmounting and remountinguseDashboardwith the sameQueryClientwithin the staleTime window results in no additional fetch calls — the second mount is served from cache (fetchStatus/isFetchingconfirm no in-flight refetch).In
frontend/src/components/dashboard/dashboard-view.test.tsx(fullDashboardView, withuseStreamEvents,react-hot-toast,@/lib/soroban,@/lib/stellar, and heavy child components — wizard/modals/SSE indicator/IncomingStreams— mocked out so the test focuses on the query-cache wiring):DashboardViewwithin the staleTime window does not issue a duplicate network fetch (mirrors the issue's acceptance criterion end-to-end through the real component).useDashboardresolves.Manual test plan
npm run dev --workspace=frontend) against a backend with some streams for a connected wallet./streamsrequests (sender + recipient)./streamsrequests fire — the dashboard renders instantly from cache.staleTimewindow, switch away and back again, and confirm a fresh fetch does happen this time.queryClient.setQueryDatawrite, without a network round trip.invalidateQueries).