Recipes
Recipe: Custom Dashboard
Overview
Section titled “Overview”A dashboard is three pieces: a backend function that computes the numbers in
SQL, a component that loads them with useInsightData and draws them with the
blocks from @rebasepro/ui, and a place to show it — here, a page
of its own in the drawer.
Count on the server. Fetching rows into the browser and adding them up is right only while the table fits in one page of results; past that the total is quietly wrong.
Compute the numbers on the server
Section titled “Compute the numbers on the server”One route per thing the page draws, run as the signed-in user, so permissions and row-level security apply to the figures too:
// functions/dashboard.tsimport { defineFunction, requireAuth } from "@rebasepro/server/functions";
export default defineFunction((app, { rebase }) => { app.use("/*", requireAuth);
// GET /api/functions/dashboard/summary app.get("/summary", async (c) => { if (!rebase.sql) return c.json({ error: "SQL not available" }, 501); const [row] = await rebase.sql(` SELECT COUNT(*) FILTER (WHERE order_date >= now() - interval '30 days') AS orders, COUNT(*) FILTER (WHERE order_date >= now() - interval '60 days' AND order_date < now() - interval '30 days') AS previous_orders, COALESCE(SUM(total) FILTER (WHERE order_date >= now() - interval '30 days'), 0) AS revenue FROM orders `); return c.json({ orders: Number(row.orders), previousOrders: Number(row.previous_orders), revenue: Number(row.revenue) }); });
// GET /api/functions/dashboard/daily — every day present, an empty one is zero app.get("/daily", async (c) => { if (!rebase.sql) return c.json({ error: "SQL not available" }, 501); const rows = await rebase.sql(` SELECT to_char(days.day, 'YYYY-MM-DD') AS day, COALESCE(SUM(o.total), 0) AS revenue FROM generate_series(current_date - 29, current_date, interval '1 day') AS days(day) LEFT JOIN orders o ON o.order_date >= days.day AND o.order_date < days.day + interval '1 day' GROUP BY days.day ORDER BY days.day `); return c.json(rows.map((row) => ({ day: String(row.day), revenue: Number(row.revenue) }))); });});Draw them
Section titled “Draw them”useInsightData(key, load) loads once per key and signed-in user, and every
component reading the same key shares the result. The blocks draw their own
loading, error and empty states:
import { useRebaseClient } from "@rebasepro/app";import type { createRebaseClient } from "@rebasepro/client";import { ChartCard, KpiGrid, KpiTile, TimeSeriesChart } from "@rebasepro/ui";import { useInsightData } from "@rebasepro/plugin-insights";
type RebaseClient = ReturnType<typeof createRebaseClient>;type Summary = { orders: number; previousOrders: number; revenue: number };type Day = { day: string; revenue: number };
function DashboardView() { const client = useRebaseClient<RebaseClient>(); const read = <T,>(path: string) => () => { if (!client) throw new Error("No Rebase client"); return client.functions.invoke<T>("dashboard", undefined, { method: "GET", path }); }; const summary = useInsightData("dashboard/summary", read<Summary>("summary")); const daily = useInsightData("dashboard/daily", read<Day[]>("daily"));
return ( <div className="max-w-6xl mx-auto px-6 py-8"> <KpiGrid periodDays={30}> <KpiTile label="Orders" icon="ShoppingCart" value={summary.data?.orders} previous={summary.data?.previousOrders} loading={summary.loading} error={summary.error} /> <KpiTile label="Revenue" icon="DollarSign" value={summary.data?.revenue} format={{ style: "currency", currency: "USD", notation: "compact" }} loading={summary.loading} error={summary.error} /> </KpiGrid> <ChartCard title="Revenue per day" className="mt-4"> <TimeSeriesChart series={[{ id: "revenue", label: "Revenue", points: (daily.data ?? []).map((d) => ({ at: d.day, value: d.revenue })) }]} format={{ style: "currency", currency: "USD" }} loading={daily.loading} error={daily.error} /> </ChartCard> </div> );}The same components go above a collection’s list or at the top of the home
page through <Rebase slots> — collection.widgets, home.children.start —
instead of a page of their own.
Register as a Custom View
Section titled “Register as a Custom View”const views: AppView[] = [ { slug: "dashboard", name: "Dashboard", view: <DashboardView />, icon: "LayoutDashboard", group: "Analytics" }];Pass it to the navigation controller:
const navigationStateController = useBuildNavigationStateController({ views, collections: () => collections, // These four are required — the controller resolves navigation against them. authController, data, collectionRegistryController, urlController});The dashboard now appears in the sidebar under “Analytics” and is accessible at /dashboard.
Pinning the group to the bottom
Section titled “Pinning the group to the bottom”Groups named "Admin" or "Settings" sink below the others in the drawer, by
string comparison on the name. That is easy to lose — translate the label and
the ordering silently stops happening — so say it explicitly instead:
{ slug: "dashboard", name: "Dashboard", view: <DashboardView />, group: "Ajustes", pinToBottom: true }Setting pinToBottom on any one view in a group pins the whole group, since
the drawer orders groups rather than individual views.
Navigating from a custom view
Section titled “Navigating from a custom view”A view component receives no props. To route somewhere — another custom view, a
collection, an entity — reach for useUrlController, which is exported from
@rebasepro/cms:
import { useUrlController } from "@rebasepro/cms";
function DashboardView() { const urlController = useUrlController();
return ( <> <button onClick={() => urlController.navigate(urlController.buildAppUrlPath("reports"))}> Reports </button> <button onClick={() => urlController.navigate(urlController.buildUrlCollectionPath("orders"))}> All orders </button> <button onClick={() => urlController.navigate(urlController.buildUrlCollectionPath("orders/B34SAP8Z"))}> Order B34SAP8Z </button> </> );}Build the path rather than hard-coding it: collection URLs are prefixed
(orders → /c/orders) and the prefix is not part of the public contract.
useSidePanel opens an entity in the side panel instead of navigating, which is
usually what you want for a row in a list.
Charts and figures that open their records
Section titled “Charts and figures that open their records”BarChart draws categories as bars with their values beside them. Given
onBarClick, each row is a button: open the list filtered to what the bar
counts, or one record in the side panel.
import { useUrlController } from "@rebasepro/cms";import { BarChart, ChartCard } from "@rebasepro/ui";
function OrdersByStatus({ counts, loading }: { counts?: Record<string, number>; loading: boolean }) { const urls = useUrlController(); const statuses = [{ id: "pending", label: "Pending" }, { id: "shipped", label: "Shipped" }, { id: "delivered", label: "Delivered" }]; return ( <ChartCard title="Orders by status"> <BarChart bars={statuses.map((s) => ({ key: s.id, label: s.label, value: counts?.[s.id] ?? 0 }))} loading={loading} onBarClick={(bar) => urls.navigate( `${urls.buildUrlCollectionPath("orders")}?status_op=${encodeURIComponent("==")}&status_value=${bar.key}` )} /> </ChartCard> );}A KpiTile opens its records the same way. Given onClick, the whole tile
is a button; given href, it is a link, which a modified click opens in a new
tab and a plain click hands to onClick, so the admin’s router navigates
without reloading the page:
const pending = `${urls.buildUrlCollectionPath("orders")}?status_op=${encodeURIComponent("==")}&status_value=pending`;
<KpiTile label="Pending orders" icon="Inbox" value={counts?.pending} loading={loading} href={pending} onClick={() => urls.navigate(pending)}/>Without either, the tile is a plain card. Do not wrap a tile in a <button>
or <a> of your own: the tile’s control is named by its label, figure and
change, and takes the focus ring.
The charts are SVG in the admin’s own tokens, in light and dark, with a hover
and arrow-key readout and a table for screen readers; no chart library is
needed. For a form they do not draw, any React chart library works inside a
ChartCard.
Next Steps
Section titled “Next Steps”- Custom Views — Frontend overview
- Hooks Reference — Available hooks
- Slots — Placing the same components on the home page or above a collection