Skip to main content

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.

intermediateuseFetchpollingofflineerrorspagination
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>
);
};
Run locallynpx tiged BetterTyped/hyper-fetch/examples/apps/dashboard hyper-fetch-apps-dashboard && cd hyper-fetch-apps-dashboard && npm install && npm run dev
What it shows
  1. Parallel requests: each KPI card has its own useFetch, so they load at the same time (Parallel requests).
  2. Polling: refresh and refreshTime fetch the live number every 3 seconds (Polling).
  3. Server-side sort and pagination: the query params are the whole table state (Data table, Pagination).
  4. Offline banner: useAppManager follows the online state of the browser (Offline first).
  5. Global fetching indicator: useIsFetching(client) drives a bar at the top of the page.
  6. Errors per widget: a failure stays in its card, next to the last good data, with a retry button (Error handling).
  7. Derived values: the select option sums the revenue without changing the cached response (Select).

How it works​

  1. api/stats.ts has one request for the three counters. :resource in the endpoint is a param, so countOf("users"), countOf("products") and countOf("carts") each get their own cache key. limit: 1 and select: "id" keep the answers small, because the cards only need total.

  2. KpiCards renders one component per card, and each calls useFetch. The four requests run in parallel and every card shows its own loading state. refetchOnFocus and refetchOnReconnect refresh the numbers when you come back.

  3. RevenueCard loads every cart and adds up the totals in the select option. The cache keeps the full response, and the card re-renders only when the sum changes.

  4. Widget is the shared frame. It shows a spinner before the first response, the data after it, and an error with a Retry button that calls refetch(). When an error follows a good response, the old value stays visible.

  5. OnlineNow polls:

    useFetch(getOnlineNow, {
    refresh: true,
    refreshTime: 3000,
    refetchBlurred: false,
    });

    refetchBlurred: false pauses 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 a setMock that returns a new number every time. With a real API, delete the mock.

  6. UsersTable keeps the page and the sort in state and builds the request with usersPage(page, sortBy, order). dummyjson sorts and pages with sortBy, order, limit and skip. Only query params change, so the old rows stay on screen until the new ones arrive, and pages you visited render from the cache.

  7. StatusBar.tsx has the two global parts. FetchingBar reads useIsFetching(client), the number of running fetches of the whole client. OfflineBanner reads isOnline from useAppManager(client).

  8. The demo switches call setMockingEnabled on the revenue request and setOnline on the app manager. The broken revenue request answers 503, and client.cache.invalidate(getRevenue) makes the card fetch it again at once.

When to use it
  • 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.

Summary

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.