Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 75 additions & 18 deletions src/frontend/src/components/portal/metric-groups-view.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { useState } from "react";
import { useMemo, useState } from "react";

import { useViewer } from "@/auth";
import { CenteredSpinner } from "@/components/widgets/centered-spinner";
import { ComingSoon } from "@/components/widgets/coming-soon";
import { GroupDrilldownSheet } from "@/components/widgets/dashboard/group-drilldown-sheet";
import { IcNeedsAttention } from "@/components/widgets/dashboard/ic-needs-attention";
import { PersonCoverage } from "@/components/widgets/dashboard/person-coverage";
import { KpiTile } from "@/components/widgets/dashboard/kpi-tile";
import { previousPeriodRange } from "@/api/period-to-date-range";
import { usePortalPeriod } from "@/hooks/use-portal-period";
Expand All @@ -14,7 +16,12 @@ import {
orderAttentionItems,
} from "@/lib/insight/attention";
import { typicalPeerPool } from "@/lib/insight/peer-pool";
import { KPI_ROW_COLLECTION, GROUPS, type GroupId } from "@/lib/insight/groups";
import {
KPI_ROW,
KPI_ROW_COLLECTION,
GROUPS,
type GroupId,
} from "@/lib/insight/groups";
import { metricKpiTiles } from "@/lib/insight/kpi-row";
import { injectCohortPeer } from "@/lib/insight/within-team-peer";
import {
Expand All @@ -25,6 +32,14 @@ import { normalizePersonId } from "@/lib/metrics/entity";
import { cn } from "@/lib/utils";
import { useCohortLabel } from "@/lib/portal/use-cohort-label";
import { usePersonCohort } from "@/lib/portal/use-person-cohort";
import {
personTrendPoints,
runningBucketStart,
TREND_BUCKETS,
trendBucket,
trendRange,
} from "@/lib/portal/person-trend";
import { usePersonSectionStandings } from "@/lib/portal/use-person-sections";
import {
collectionSetPending,
useMetricCollection,
Expand Down Expand Up @@ -77,11 +92,6 @@ export interface MetricGroupsViewProps {
* modal. Passing it also suppresses the modal sheets entirely.
*/
onSelectGroup?: (id: GroupId) => void;
/**
* Render the per-group section cards. Off ⇒ a general glance (KPI row +
* needs-attention only) with sections reached from the sidebar instead.
*/
showSections?: boolean;
}

/**
Expand All @@ -95,12 +105,15 @@ export function MetricGroupsView({
groupIds,
showKpis = false,
onSelectGroup,
showSections = true,
}: MetricGroupsViewProps) {
const cohortLabel = useCohortLabel();
const { period, dateRange } = usePortalPeriod();
const { focusMode } = useSettings();
const entityId = normalizePersonId(personId);
// Whose page this is. A person reading their own page and a manager
// reading it want different things first — see `orderAttentionItems`.
const { personId: viewerPersonId } = useViewer();
const isSelf = normalizePersonId(viewerPersonId ?? "") === entityId;
const entity = { type: "person" as const, ids: [entityId] };

const defs = GROUPS.filter((d) => groupIds.includes(d.id));
Expand Down Expand Up @@ -159,6 +172,27 @@ export function MetricGroupsView({
dateRange
);

const sectionStandings = usePersonSectionStandings(personId);

// Deliberately outside every loading and error gate below: the tiles render
// from the current period, and the line appears when it arrives. A first
// screen must not wait on decoration.
const bucket = trendBucket(period);
const trendCollection = useMemo(
() => ({
metrics: KPI_ROW.map((key) => ({
key,
views: [{ view: "timeseries" as const, bucket }],
})),
}),
[bucket]
);
const trendData = useMetricCollection(
showKpis ? trendCollection : EMPTY_COLLECTION,
showKpis ? entity : CLOSED_ENTITY,
trendRange(dateRange.to, bucket)
);

const [openGroup, setOpenGroup] = useState<GroupId | null>(null);
// With `onSelectGroup` a card/alert navigates to the section inline; without
// it, it opens the drilldown modal.
Expand Down Expand Up @@ -255,6 +289,14 @@ export function MetricGroupsView({
// What the row actually rendered — the block skips exactly those, no more.
const headlineKeys = new Set(tiles.map((t) => t.key));

// Sections with no reading at all. Named plainly: a page that shows only
// what it can see otherwise reads as a whole picture of a person.
const uncovered = showKpis
? sectionStandings
.filter((st) => !st.isPending && !st.hasData)
.map((st) => st.title)
: [];

// Deduped across groups, not within one: a metric and the wider metric that
// contains it need not live in the same section.
// Thinned across groups AND against the row above: a metric and the one it
Expand All @@ -271,7 +313,8 @@ export function MetricGroupsView({
headlineKeys
)
),
headlineKeys
headlineKeys,
isSelf
)
: [];

Expand All @@ -280,6 +323,11 @@ export function MetricGroupsView({
<main className="flex flex-1 flex-col gap-8 p-4 md:p-6">
{showKpis ? (
<>
{/* Before the numbers, not after them. On a person's own page the
first question is what this thing knows about them, and a page
that answers it only at the bottom has already been read as a
complete picture by then. */}
<PersonCoverage uncovered={uncovered} />
<section className="flex flex-col gap-3">
<p className="flex flex-wrap items-baseline gap-x-2 text-xs font-medium tracking-wider text-muted-foreground uppercase">
At a glance
Expand All @@ -292,15 +340,12 @@ export function MetricGroupsView({
· compared with {peerPool} people in the same {cohortLabel}
</span>
) : null}
{/* Each tile carries TWO comparisons — a badge against the
person's own last period, a line against the cohort — and
neither said which was which, so "-13%" could be read as
either. Named once here rather than on every tile: the
grammar is the same for all of them, and a tile is 13rem
wide. */}
{/* The lines carry no axis and no labels — a tile has room for
neither — so the one thing they cannot say for themselves
is said once, here: what a line spans. */}
<span className="font-normal tracking-normal normal-case">
· badges show the change since the previous{" "}
{PERIOD_NOUN[period]}
· lines cover the last {TREND_BUCKETS}{" "}
{bucket === "week" ? "weeks" : "months"}
</span>
</p>
{/* A counted column grid, not auto-fit: auto-fit packs in as many
Expand All @@ -323,6 +368,12 @@ export function MetricGroupsView({
<KpiTile
key={tile.key}
tile={tile}
periodNoun={PERIOD_NOUN[period]}
trend={personTrendPoints(
trendData.byKey.get(tile.key),
entityId,
runningBucketStart(dateRange.to, bucket)
)}
onOpenGroup={openOrSelect}
/>
))}
Expand All @@ -336,7 +387,13 @@ export function MetricGroupsView({
) : null}
</main>

{onSelectGroup || !showSections
{/* A click on a number is answered here, with the evidence behind it.
This used to be gated on a `showSections` flag that also hid the
per-group cards — and those cards moved to the navigation long ago,
so the flag survived only to make clicks land nowhere. Only a caller
that routes clicks elsewhere (`onSelectGroup`) has no use for the
drilldowns. */}
{onSelectGroup
? null
: defs.map((def) => (
<GroupDrilldownSheet
Expand Down
35 changes: 20 additions & 15 deletions src/frontend/src/components/portal/person-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,40 @@ import { MetricGroupsView } from "@/components/portal/metric-groups-view";
import { PersonHeader } from "@/components/portal/person-header";
import { SingleGroupView } from "@/components/portal/single-group-view";
import { GROUPS, type GroupId } from "@/lib/insight/groups";
import {
usePortalItem,
usePortalNavActions,
} from "@/lib/portal/portal-nav";
import { usePortalItem, usePortalNavActions } from "@/lib/portal/portal-nav";

const PERSON_GROUP_IDS: readonly GroupId[] = GROUPS.map((g) => g.id);

/**
* Person zone: one specific person (like the reporting tool's Person page).
* The second sidebar level lists the person's sections; selecting a section
* expands it into the full content area inline (rich drilldown) — no modal.
* "At a glance" is a health dashboard: KPI row + needs-attention + per-section
* status cards, all routing into the relevant section inline so a problem
* points straight at its section.
* Person zone: one specific person.
*
* The second sidebar level lists the person's sections and carries a mark
* beside each saying which is worth opening; selecting one expands it into the
* content area inline, with no modal. "At a glance" is the overview: the
* headline row and what needs attention, both routing into the section that
* owns the number.
*
* The per-section status cards this page used to carry are gone — they said
* again, in the middle of the page, what the navigation says on its left edge.
*/
export function PersonView({ person }: { person: string }) {
const { setItem } = usePortalNavActions();
const item = usePortalItem();
const isSection = item != null && (PERSON_GROUP_IDS as string[]).includes(item);
const { setItem } = usePortalNavActions();
const isSection =
item != null && (PERSON_GROUP_IDS as string[]).includes(item);

return (
<>
<PersonHeader person={person} />
{isSection ? (
<SingleGroupView personId={person} groupId={item as GroupId} />
) : (
// "At a glance" — a health dashboard: general KPIs + needs-attention +
// per-section status cards. Everything routes into the relevant section
// inline (no modal), so a problem points you straight at its section.
// "At a glance" — the headline row and what needs attention.
//
// A tile or an alert opens the SECTION that owns it, the same one the
// navigation lists: the number is the way in, and the section is where
// it is explained. No modal — a problem points you straight at the
// screen that covers it.
<MetricGroupsView
personId={person}
groupIds={PERSON_GROUP_IDS}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,23 @@ type Story = StoryObj<typeof KpiTile>;

/** Demo story for the Storybook UI (not a test — no `test` tag). */
export const Default: Story = {
args: { tile: makeTile() },
args: { tile: makeTile(), periodNoun: "month" },
};

/** Component test: the display-ready tile input drives every element. */
export const TestOkTile: Story = {
tags: ["test"],
args: { tile: makeTile() },
args: { tile: makeTile(), periodNoun: "month" },
play: async ({ canvas }) => {
// Singular `getByText` (throws on >1 match) doubles as a guard that the
// preview decorators wrap the story exactly once — a double-applied
// decorator would render two <KpiTile>s and fail here.
await expect(canvas.getByText("Bugs Fixed")).toBeInTheDocument();
await expect(canvas.getByText("12")).toBeInTheDocument();
await expect(canvas.getByText("+9%")).toBeInTheDocument();
await expect(canvas.getByText(/median 6/i)).toBeInTheDocument();
// The change now names what it is measured against — a bare "+9%" was
// readable as either of the tile's two comparisons.
await expect(canvas.getByText(/\+9% since last month/)).toBeInTheDocument();
await expect(canvas.getByText(/team median 6/i)).toBeInTheDocument();
},
};

Expand All @@ -92,6 +94,7 @@ export const TestNoPeers: Story = {
tags: ["test"],
args: {
tile: makeTile({ gapStatus: "neutral", medianLabel: null, delta: null }),
periodNoun: "month",
},
play: async ({ canvas }) => {
await expect(canvas.getByText("Bugs Fixed")).toBeInTheDocument();
Expand Down
Loading
Loading