Dashboard
An admin dashboard on dummyjson: four KPI cards, a live "online now" number and a customers table sorted and paged by the server. Turn on Break the revenue service and only the revenue card fails. Turn on Go offline and a banner appears while polling pauses.
Opens the example in StackBlitz in a new tab. The first start installs packages and takes a few seconds.
import { useAppManager, useIsFetching } from "@hyper-fetch/react";
import { client } from "../api/client";
/** A thin bar at the top while any request of the client is running. */
export const FetchingBar = () => {
const fetching = useIsFetching(client);
return (
<div
className={fetching > 0 ? "fetching-bar active" : "fetching-bar"}
role="progressbar"
aria-busy={fetching > 0}
/>
);
};
/** The app manager follows the browser's online and offline events. */
export const OfflineBanner = () => {
const { isOnline } = useAppManager(client);
if (isOnline) return null;
return (
<p className="offline" role="status">
You are offline. The widgets keep their last data and refresh when the connection is back.
</p>
);
};
npx tiged BetterTyped/hyper-fetch/examples/apps/dashboard hyper-fetch-apps-dashboard && cd hyper-fetch-apps-dashboard && npm install && npm run dev- Parallel requests: each KPI card has its own
useFetch, so they load at the same time (Parallel requests). - Polling:
refreshandrefreshTimefetch the live number every 3 seconds (Polling). - Server-side sort and pagination: the query params are the whole table state (Data table, Pagination).
- Offline banner:
useAppManagerfollows the online state of the browser (Offline first). - Global fetching indicator:
useIsFetching(client)drives a bar at the top of the page. - Errors per widget: a failure stays in its card, next to the last good data, with a retry button (Error handling).
- Derived values: the
selectoption sums the revenue without changing the cached response (Select).
How it works
-
api/stats.tshas one request for the three counters.:resourcein the endpoint is a param, socountOf("users"),countOf("products")andcountOf("carts")each get their own cache key.limit: 1andselect: "id"keep the answers small, because the cards only needtotal. -
KpiCardsrenders one component per card, and each callsuseFetch. The four requests run in parallel and every card shows its own loading state.refetchOnFocusandrefetchOnReconnectrefresh the numbers when you come back. -
RevenueCardloads every cart and adds up the totals in theselectoption. The cache keeps the full response, and the card re-renders only when the sum changes. -
Widgetis the shared frame. It shows a spinner before the first response, the data after it, and an error with a Retry button that callsrefetch(). When an error follows a good response, the old value stays visible. -
OnlineNowpolls:useFetch(getOnlineNow, {refresh: true,refreshTime: 3000,refetchBlurred: false,});refetchBlurred: falsepauses polling while the tab is in the background, and requests wait while the app is offline. dummyjson numbers never change, so this request is answered by asetMockthat returns a new number every time. With a real API, delete the mock. -
UsersTablekeeps the page and the sort in state and builds the request withusersPage(page, sortBy, order). dummyjson sorts and pages withsortBy,order,limitandskip. Only query params change, so the old rows stay on screen until the new ones arrive, and pages you visited render from the cache. -
StatusBar.tsxhas the two global parts.FetchingBarreadsuseIsFetching(client), the number of running fetches of the whole client.OfflineBannerreadsisOnlinefromuseAppManager(client). -
The demo switches call
setMockingEnabledon the revenue request andsetOnlineon the app manager. The broken revenue request answers503, andclient.cache.invalidate(getRevenue)makes the card fetch it again at once.
- Admin panels and dashboards that combine many independent widgets.
- Screens with numbers that change on the server, such as sales, queues or monitoring.
- Tables with many rows, where sorting and paging must happen on the server.
Related
Each widget owns its request, so cards load in parallel and fail alone. refresh polls the live number, query params
drive the server-sorted table, and two hooks read client-wide state: useIsFetching for the loading bar and
useAppManager for the offline banner.
