Blocks / Metric picker

Metric picker

Pick a measure, a grouping and filters. The value is a query to run, not data.

import { MetricPicker, dashboardQueryText, dashboardEmptyQuery } from "@forgeron/ui";
/01 → Default
  1. Measure
    added up as
  2. Group by

Sum of Revenue, by Created per month

{"measure":"revenue","aggregation":"sum","groupBy":"created","interval":"month","filters":[]}
import { MetricPicker, dashboardEmptyQuery, type DashboardQuery } from "@forgeron/ui";
import { useState } from "react";

const measures = [
  { id: "revenue", label: "Revenue", hint: "deals", aggregations: ["sum", "avg", "max"] as const },
  { id: "deals", label: "Deals", hint: "deals", aggregations: ["count"] as const },
  {
    id: "tickets",
    label: "Tickets",
    hint: "support",
    aggregations: ["count", "distinct"] as const,
  },
];
const groupings = [
  { id: "created", label: "Created", kind: "time" as const },
  { id: "stage", label: "Stage" },
  { id: "owner", label: "Owner" },
];

export default function Default() {
  const [query, setQuery] = useState<DashboardQuery>({
    ...dashboardEmptyQuery,
    measure: "revenue",
    aggregation: "sum",
    groupBy: "created",
  });
  return (
    <div
      style={{ display: "flex", flexDirection: "column", gap: 12, width: "100%", maxWidth: 560 }}
    >
      <MetricPicker
        measures={measures.map((m) => ({ ...m, aggregations: [...m.aggregations] }))}
        groupings={groupings}
        value={query}
        onChange={setQuery}
      />
      <pre
        style={{
          margin: 0,
          whiteSpace: "pre-wrap",
          wordBreak: "break-all",
          font: "12px/18px var(--font-mono)",
          color: "var(--ink-muted)",
        }}
      >
        {JSON.stringify(query)}
      </pre>
    </div>
  );
}
/02 → With filters
  1. Measure
    added up as
  2. Group by
  3. Filter
    Stage

Count of Deals, by Stage, where Stage is not Lost

import { MetricPicker } from "@forgeron/ui";
import { UserIcon, Flag01Icon } from "@forgeron/icons";

export default function WithFilters() {
  return (
    <div style={{ width: "100%", maxWidth: 560 }}>
      <MetricPicker
        measures={[{ id: "deals", label: "Deals", aggregations: ["count"] }]}
        groupings={[{ id: "stage", label: "Stage" }]}
        defaultValue={{
          measure: "deals",
          aggregation: "count",
          groupBy: "stage",
          interval: "month",
          filters: [{ field: "stage", operator: "is-not", values: ["lost"] }],
        }}
        fields={[
          {
            id: "stage",
            label: "Stage",
            icon: Flag01Icon,
            options: [
              { value: "won", label: "Won" },
              { value: "open", label: "Open" },
              { value: "lost", label: "Lost" },
            ],
          },
          {
            id: "owner",
            label: "Owner",
            icon: UserIcon,
            options: [
              { value: "ana", label: "Ana" },
              { value: "bo", label: "Bo" },
            ],
          },
        ]}
      />
    </div>
  );
}
API → Props
PropTypeDefaultDescription
measures{ id, label, hint?, aggregations? }[]—What can be measured, and how each adds up.
groupings{ id, label, hint?, kind? }[]—kind time adds a slice of time (day, week, month).
fieldsFilterField[]—What can be filtered; the same as the FilterBar. Without it, no filter step.
value / defaultValue / onChange{ measure, aggregation, groupBy, interval, filters }—The question, serialisable.
intervalsDashboardInterval[]day, week, monthOffered for a time grouping.
summarybooleantrueThe query in a sentence.
labelstring"Question"Names the group.
classNamestring—Extra classes.