Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(today): calmer pulse hovercard and single tab stop per metric (#128216)

Shy Alterfc0b4e282215
diff --git a/frontend/bin/css-coupling-baseline.txt b/frontend/bin/css-coupling-baseline.txt
index bf28d4ff4e8a..7e991d661884 100644
--- a/frontend/bin/css-coupling-baseline.txt
+++ b/frontend/bin/css-coupling-baseline.txt
@@ -37,12 +37,9 @@ frontend/src/scenes/project-homepage/today/TodayExplore.tsx -> .TodayJumpRow
frontend/src/scenes/project-homepage/today/TodayFigureMark.tsx -> .TodayFigureMark
frontend/src/scenes/project-homepage/today/TodayForYou.tsx -> .TodayRow
frontend/src/scenes/project-homepage/today/TodayForYou.tsx -> .TodayRows
-frontend/src/scenes/project-homepage/today/TodayHomePage.tsx -> .TodayHome
-frontend/src/scenes/project-homepage/today/TodayHomePage.tsx -> .TodayPulse
frontend/src/scenes/project-homepage/today/TodayHoverMark.tsx -> .TodayHoverMark
frontend/src/scenes/project-homepage/today/TodayImplementMenu.tsx -> .TodayImplementMenu
frontend/src/scenes/project-homepage/today/TodayKeyClauseMark.tsx -> .TodayKeyClauseMark
-frontend/src/scenes/project-homepage/today/TodayPulse.tsx -> .TodayPulse
frontend/src/scenes/project-homepage/today/TodayRecents.tsx -> .TodayJumpList
frontend/src/scenes/project-homepage/today/TodayRecents.tsx -> .TodayJumpRow
frontend/src/scenes/project-homepage/today/TodayReportBody.tsx -> .TodayReportArticle
diff --git a/frontend/src/scenes/project-homepage/today/Today.scss b/frontend/src/scenes/project-homepage/today/Today.scss
index 695c115796ee..bd977e3d010c 100644
--- a/frontend/src/scenes/project-homepage/today/Today.scss
+++ b/frontend/src/scenes/project-homepage/today/Today.scss
@@ -248,11 +248,30 @@ html[data-today-transition]::view-transition-new(root) {
flex-direction: column;
gap: 2px;
min-width: 0;
+ padding: 6px 10px;
+ margin: -6px -10px;
color: inherit;
+ border-radius: 8px;
- &:hover .TodayPulse__name {
- color: var(--today-ink);
- text-decoration: underline;
+ &:hover,
+ &:focus-visible {
+ background: var(--fill-hover);
+ }
+
+ @container main-content (max-width: 600px) {
+ padding: 0;
+ margin: 0;
+
+ &:hover,
+ &:focus-visible {
+ background: none;
+ }
+ }
+}
+
+.TodayPulse__metric:has(.TodayPulse__link:hover, .TodayPulse__link:focus-visible) {
+ @container main-content (max-width: 600px) {
+ background: var(--fill-hover);
}
}
@@ -291,6 +310,11 @@ html[data-today-transition]::view-transition-new(root) {
color: var(--today-muted);
}
+.TodayPulseCard .TodayChange {
+ font-size: inherit;
+ color: inherit;
+}
+
.TodayPulse__spark {
display: block;
width: 100%;
diff --git a/frontend/src/scenes/project-homepage/today/TodayChange.tsx b/frontend/src/scenes/project-homepage/today/TodayChange.tsx
index c055bf1ad4d5..72b68ebcb576 100644
--- a/frontend/src/scenes/project-homepage/today/TodayChange.tsx
+++ b/frontend/src/scenes/project-homepage/today/TodayChange.tsx
@@ -1,11 +1,25 @@
import { type TodayTone, useTodayTonePalette } from './todayTone'
-export function TodayChange({ change, tone }: { change: number | null; tone: TodayTone }): JSX.Element | null {
+export function TodayChange({
+ change,
+ tone,
+ unit = '%',
+}: {
+ change: number | null
+ tone: TodayTone
+ unit?: '%' | 'pp'
+}): JSX.Element | null {
const palette = useTodayTonePalette()
if (change === null) {
return null
}
- const rounded = Math.round(change)
+ const rounded = unit === 'pp' ? Math.round(change * 10) / 10 : Math.round(change)
+ const text =
+ unit === 'pp'
+ ? `${rounded > 0 ? '+' : rounded < 0 ? '−' : ''}${Math.abs(rounded)} pp`
+ : rounded === 0
+ ? '0%'
+ : `${rounded > 0 ? '+' : '−'}${Math.abs(rounded).toLocaleString('en-US')}%`
return (
<span
className="TodayChange"
@@ -13,7 +27,7 @@ export function TodayChange({ change, tone }: { change: number | null; tone: Tod
// eslint-disable-next-line react/forbid-dom-props
style={tone === 'neutral' ? undefined : { color: palette[tone] }}
>
- {rounded === 0 ? '0%' : `${rounded > 0 ? '+' : '−'}${Math.abs(rounded)}%`}
+ {text}
</span>
)
}
diff --git a/frontend/src/scenes/project-homepage/today/TodayPulse.tsx b/frontend/src/scenes/project-homepage/today/TodayPulse.tsx
index c706a7a4789c..411b8ce91d86 100644
--- a/frontend/src/scenes/project-homepage/today/TodayPulse.tsx
+++ b/frontend/src/scenes/project-homepage/today/TodayPulse.tsx
@@ -18,6 +18,7 @@ import { TodayChange } from './TodayChange'
import { todayHighlightsLogic } from './todayHighlightsLogic'
import { todayNumber } from './todayNumbers'
import { TodayPulseCard } from './TodayPulseCard'
+import { pulseChange } from './todayPulseFacts'
import { TodayPulseHoverCard } from './TodayPulseHoverCard'
import { pulseOpenHandlers } from './todayPulseOpenHandlers'
import type { TodayTone } from './todayTone'
@@ -65,6 +66,10 @@ function metricFormatter(metric: HighlightMetricApi, currency: CurrencyCode): (v
return (value) => formatAggregationAxisValue(trendsFilter, value, currency)
}
+function cardNumber(value: number): string {
+ return humanFriendlyNumber(value, Math.abs(value) >= 100 ? 0 : 2)
+}
+
const REORDER_MS = 220
function useSettleOnReorder(listRef: RefObject<HTMLUListElement>, order: string): void {
@@ -166,6 +171,7 @@ export function TodayPulse({ metrics, slots }: { metrics: HighlightMetricApi[];
const isInsight = metric.key.startsWith('insight:')
const tone = isInsight ? 'neutral' : metricTone(metric.change_pct, metric.value)
const formatValue = metric.value_format ? metricFormatter(metric, baseCurrency) : todayNumber
+ const change = pulseChange(metric)
const content = (
<>
<span className="TodayPulse__name">{metric.name}</span>
@@ -173,7 +179,7 @@ export function TodayPulse({ metrics, slots }: { metrics: HighlightMetricApi[];
<span className="TodayPulse__value" translate="no">
{formatValue(metric.value)}
</span>
- <TodayChange change={metric.change_pct} tone={tone} />
+ <TodayChange change={change.value} unit={change.unit} tone={tone} />
</span>
<span className="TodayPulse__spark">
<Suspense fallback={null}>
@@ -190,6 +196,7 @@ export function TodayPulse({ metrics, slots }: { metrics: HighlightMetricApi[];
return (
<li key={metric.key} data-pulse-key={metric.key} className="TodayPulse__metric">
<TodayPulseHoverCard
+ focusable={!metric.url}
onOpenChange={(open) => {
if (open) {
pulseMetricPreviewed(metric, position)
@@ -205,7 +212,7 @@ export function TodayPulse({ metrics, slots }: { metrics: HighlightMetricApi[];
<TodayPulseCard
metric={metric}
tone={tone}
- formatValue={metric.value_format ? formatValue : humanFriendlyNumber}
+ formatValue={metric.value_format ? formatValue : cardNumber}
onOpen={() => pulseMetricOpened(metric, position)}
onHide={() => hidePulseMetric(metric, position)}
/>
diff --git a/frontend/src/scenes/project-homepage/today/TodayPulseCard.tsx b/frontend/src/scenes/project-homepage/today/TodayPulseCard.tsx
index 930bacad8942..9da949196390 100644
--- a/frontend/src/scenes/project-homepage/today/TodayPulseCard.tsx
+++ b/frontend/src/scenes/project-homepage/today/TodayPulseCard.tsx
@@ -1,4 +1,6 @@
-import { Suspense, lazy, useState } from 'react'
+import './Today.scss'
+
+import { useState } from 'react'
import { IconArrowRight, IconHide, IconInfo } from '@posthog/icons'
import {
@@ -13,35 +15,20 @@ import {
TooltipTrigger,
} from '@posthog/quill'
-import { dayjs } from 'lib/dayjs'
import { LinkPrimitive } from 'lib/lemon-ui/Link'
-import { retryImport } from 'lib/utils/retryImport'
-
-import { TodayHoverCardFact } from '~/layout/today/TodayHoverCardFact'
+import { cn } from 'lib/utils/css-classes'
import type { HighlightMetricApi } from 'products/today/frontend/generated/api.schemas'
import { TodayChange } from './TodayChange'
-import { pulsePeak, pulsePeriodCaption, pulseWhy } from './todayPulseFacts'
+import { todayNumber } from './todayNumbers'
+import { pulseChange, pulsePeriodCaption, pulsePriorLabel, pulseWhy } from './todayPulseFacts'
+import { TodayPulseMovers } from './TodayPulseMovers'
import { pulseOpenHandlers } from './todayPulseOpenHandlers'
import type { TodayTone } from './todayTone'
-// The charts are most of the code the home page adds, and this card opens only on hover.
-const TodayPulseMovers = lazy(() =>
- retryImport(() => import('./TodayPulseMovers').then((m) => ({ default: m.TodayPulseMovers })))
-)
-
-const PERIOD_FORMATS: Record<HighlightMetricApi['interval'], string> = {
- day: 'ddd, D MMM',
- week: '[week of] D MMM',
- month: 'MMMM YYYY',
-}
-
function openLabel(metric: HighlightMetricApi): string {
- if (metric.dashboard_id !== null || metric.reason === 'dashboard') {
- return metric.dashboard_name ? `Open ${metric.dashboard_name}` : 'Open dashboard'
- }
- return metric.key.startsWith('insight:') ? 'Open insight' : 'Open as a trend'
+ return metric.dashboard_id !== null || metric.reason === 'dashboard' ? 'Open dashboard' : 'Open insight'
}
export function TodayPulseCard({
@@ -58,76 +45,61 @@ export function TodayPulseCard({
onHide: () => void
}): JSX.Element {
const [whyOpen, setWhyOpen] = useState(false)
- const previous = metric.previous_value
- const peak = pulsePeak(metric)
const why = pulseWhy(metric)
+ const change = pulseChange(metric)
return (
- <div className="flex flex-col gap-3 p-3" data-attr="today-pulse-card">
- <div className="flex flex-col gap-0.5">
- <Text size="sm" weight="semibold" render={<span />} className="truncate">
- {metric.name}
- </Text>
- <div className="flex items-baseline gap-2">
- <Heading size="xl" render={<span />} className="tabular-nums" translate="no">
- {formatValue(metric.value)}
- </Heading>
- <span className="text-sm text-muted-foreground">
- <TodayChange change={metric.change_pct} tone={tone} />
- </span>
- </div>
- <Text size="xs" variant="muted" render={<span />}>
- {pulsePeriodCaption(metric)}
- </Text>
- </div>
- {(previous !== null || peak !== null) && (
+ <div className="TodayPulseCard flex flex-col" data-attr="today-pulse-card">
+ <div className="flex flex-col gap-4 p-3 pb-3">
<div className="flex flex-col gap-1">
- {previous !== null && (
- <TodayHoverCardFact label="Before">
- <span className="tabular-nums">{formatValue(previous)}</span>
- </TodayHoverCardFact>
- )}
- {peak !== null && (
- <TodayHoverCardFact label={peak.label}>
- <span className="tabular-nums">
- {formatValue(peak.value)}
- <span className="text-muted-foreground">
- {' '}
- · {peak.latest ?? dayjs(peak.start).format(PERIOD_FORMATS[metric.interval])}
- </span>
+ <div className="flex items-baseline justify-between gap-3">
+ <Text size="sm" weight="semibold" render={<span />} className="line-clamp-2">
+ {metric.name}
+ </Text>
+ <Text size="xs" variant="muted" render={<span />} className="shrink-0">
+ {pulsePeriodCaption(metric)}
+ </Text>
+ </div>
+ <div className="flex flex-wrap items-baseline gap-x-2">
+ <Heading size="xl" render={<span />} className="tabular-nums" translate="no">
+ {formatValue(metric.value)}
+ </Heading>
+ {change.value !== null && (
+ <span className="text-sm text-muted-foreground">
+ <TodayChange change={change.value} unit={change.unit} tone={tone} />
+ <span> {pulsePriorLabel(metric)}</span>
</span>
- </TodayHoverCardFact>
- )}
- </div>
- )}
- {metric.movers.length > 1 && (
- <div className="flex flex-col gap-1.5">
- <Text size="xs" variant="muted" render={<span />}>
- What changed most
- </Text>
- <Suspense fallback={null}>
- <TodayPulseMovers movers={metric.movers} formatValue={formatValue} />
- </Suspense>
+ )}
+ </div>
</div>
- )}
+ {metric.movers.length > 1 && (
+ <div className="flex flex-col gap-1">
+ <Text size="xs" variant="muted" render={<span />}>
+ What changed most
+ </Text>
+ <TodayPulseMovers
+ movers={metric.movers}
+ formatValue={metric.value_format ? formatValue : todayNumber}
+ />
+ </div>
+ )}
+ </div>
<Collapsible
variant="folder"
open={whyOpen}
onOpenChange={setWhyOpen}
- className="flex flex-col border-t border-border pt-2"
+ className={cn('flex flex-col px-3 py-1', metric.url ? 'border-t border-border' : '-mt-2')}
>
<div className="flex min-h-6 items-center justify-between gap-2">
{metric.url ? (
- <Button
- size="xs"
- variant="link-muted"
- className="-ms-2"
- render={<LinkPrimitive to={metric.url} />}
+ <LinkPrimitive
+ to={metric.url}
+ className="inline-flex items-center gap-1 text-xs text-muted-foreground hover:text-foreground"
{...pulseOpenHandlers(onOpen)}
data-attr="today-pulse-card-open"
>
{openLabel(metric)}
<IconArrowRight />
- </Button>
+ </LinkPrimitive>
) : (
<span />
)}
@@ -162,7 +134,7 @@ export function TodayPulseCard({
</div>
</div>
{why && (
- <CollapsibleContent className="px-0 pb-0">
+ <CollapsibleContent className="px-0 pb-2">
<Text size="xs" variant="muted" className="m-0 pt-1">
{why}
</Text>
diff --git a/frontend/src/scenes/project-homepage/today/TodayPulseHoverCard.tsx b/frontend/src/scenes/project-homepage/today/TodayPulseHoverCard.tsx
index 5b6412421d6f..88f5ec3a7609 100644
--- a/frontend/src/scenes/project-homepage/today/TodayPulseHoverCard.tsx
+++ b/frontend/src/scenes/project-homepage/today/TodayPulseHoverCard.tsx
@@ -11,10 +11,12 @@ export function TodayPulseHoverCard({
children,
card,
onOpenChange,
+ focusable = true,
}: {
children: ReactNode
card: ReactNode
onOpenChange: (open: boolean) => void
+ focusable?: boolean
}): JSX.Element {
const [open, setOpenState] = useState(false)
const openRef = useRef(false)
@@ -55,6 +57,7 @@ export function TodayPulseHoverCard({
ref={triggerRef}
render={<div />}
className="TodayPulse__trigger"
+ tabIndex={focusable ? 0 : -1}
onFocus={() => !open && setOpen(true)}
onBlur={(event: FocusEvent) => {
if (!popupRef.current?.contains(event.relatedTarget as Node | null)) {
diff --git a/frontend/src/scenes/project-homepage/today/TodayPulseMovers.test.tsx b/frontend/src/scenes/project-homepage/today/TodayPulseMovers.test.tsx
index 91e784712d6e..e8a5bee1974d 100644
--- a/frontend/src/scenes/project-homepage/today/TodayPulseMovers.test.tsx
+++ b/frontend/src/scenes/project-homepage/today/TodayPulseMovers.test.tsx
@@ -20,7 +20,7 @@ describe('TodayPulseMovers', () => {
expect(screen.getByText('v-50')).toBeInTheDocument()
expect(screen.getByText('v120')).toBeInTheDocument()
- expect(screen.getAllByRole('listitem').map((row) => row.textContent?.split(/[+−-]/)[0])).toEqual([
+ expect(screen.getAllByRole('listitem').map((row) => row.firstElementChild?.textContent)).toEqual([
'Refunds',
'Orders',
])
diff --git a/frontend/src/scenes/project-homepage/today/TodayPulseMovers.tsx b/frontend/src/scenes/project-homepage/today/TodayPulseMovers.tsx
index 36798f5f2905..a802ab690ff7 100644
--- a/frontend/src/scenes/project-homepage/today/TodayPulseMovers.tsx
+++ b/frontend/src/scenes/project-homepage/today/TodayPulseMovers.tsx
@@ -1,13 +1,6 @@
-import { useMemo } from 'react'
-
-import { BarList, type Series } from '@posthog/quill-charts'
-
-import { useChartTheme } from 'lib/charts/hooks'
-
import type { HighlightMoverApi } from 'products/today/frontend/generated/api.schemas'
import { TodayChange } from './TodayChange'
-import { useTodayTonePalette } from './todayTone'
export function TodayPulseMovers({
movers,
@@ -16,53 +9,22 @@ export function TodayPulseMovers({
movers: HighlightMoverApi[]
formatValue: (value: number) => string
}): JSX.Element {
- const theme = useChartTheme()
- const palette = useTodayTonePalette()
- const series = useMemo<Series<HighlightMoverApi>[]>(
- () =>
- movers.map((mover) => ({
- key: mover.label,
- label: mover.label,
- data: [mover.value],
- color: palette.line,
- meta: mover,
- })),
- [movers, palette]
- )
- if (movers.some((mover) => mover.value < 0)) {
- return (
- <ul className="m-0 flex list-none flex-col gap-1 p-0" data-attr="today-pulse-card-movers">
- {series.map((row) => (
- <li key={row.key} className="flex min-w-0 items-baseline justify-between gap-3 text-xs">
- <span className="flex min-w-0 items-baseline gap-1.5">
- <span className="truncate">{row.label}</span>
- <span className="shrink-0 text-muted-foreground">
- <TodayChange change={row.meta?.change_pct ?? null} tone="neutral" />
- </span>
- </span>
- <span className="shrink-0 text-muted-foreground tabular-nums">{formatValue(row.data[0])}</span>
- </li>
- ))}
- </ul>
- )
- }
return (
- <BarList
- series={series}
- theme={theme}
- config={{ valueDisplay: 'value', tooltip: { enabled: false } }}
- valueFormatter={formatValue}
- renderLabel={(row) => (
- <span className="flex min-w-0 items-baseline gap-1.5">
- <span className="truncate">{row.label}</span>
- {row.meta && (
- <span className="shrink-0 text-xs text-muted-foreground">
- <TodayChange change={row.meta.change_pct} tone="neutral" />
- </span>
- )}
- </span>
- )}
- dataAttr="today-pulse-card-movers"
- />
+ <ul
+ className="m-0 grid list-none grid-cols-[minmax(0,1fr)_auto_minmax(3rem,auto)] gap-x-3 p-0"
+ data-attr="today-pulse-card-movers"
+ >
+ {movers.map((mover) => (
+ <li key={mover.label} className="col-span-3 grid grid-cols-subgrid items-baseline py-1 text-xs">
+ <span className="min-w-0 truncate">{mover.label}</span>
+ <span className="text-end tabular-nums" translate="no">
+ {formatValue(mover.value).replace(/^-/, '−')}
+ </span>
+ <span className="text-end text-muted-foreground tabular-nums">
+ <TodayChange change={mover.change_pct} tone="neutral" />
+ </span>
+ </li>
+ ))}
+ </ul>
)
}
diff --git a/frontend/src/scenes/project-homepage/today/todayPulseFacts.test.ts b/frontend/src/scenes/project-homepage/today/todayPulseFacts.test.ts
index e578e15af070..950211adf118 100644
--- a/frontend/src/scenes/project-homepage/today/todayPulseFacts.test.ts
+++ b/frontend/src/scenes/project-homepage/today/todayPulseFacts.test.ts
@@ -1,6 +1,6 @@
import { dayjs } from 'lib/dayjs'
-import { pulsePeak, pulseWhy } from './todayPulseFacts'
+import { pulseChange, pulseWhy } from './todayPulseFacts'
import { makePulseMetric } from './todayTestFixtures'
describe('todayPulseFacts', () => {
@@ -32,12 +32,49 @@ describe('todayPulseFacts', () => {
expect(pulseWhy(makePulseMetric(overrides), today)).toBe(expected)
})
- it('finds the highest value with the period it belongs to, skipping missing values', () => {
- const metric = makePulseMetric({
- interval: 'day',
- series: [3, NaN, 9, 4],
- period_starts: ['2026-09-26', '2026-09-27', '2026-09-28', '2026-09-29'],
- })
- expect(pulsePeak(metric)).toEqual({ label: 'Top day', value: 9, start: '2026-09-28', latest: null })
+ it.each([
+ ['a count', { value: 120, previous_value: 100, change_pct: 20, value_format: null }, { value: 20, unit: '%' }],
+ [
+ 'a 0-100 rate',
+ {
+ value: 3.4,
+ previous_value: 3,
+ change_pct: 13.3,
+ value_format: { format: 'percentage', prefix: null, postfix: null, decimal_places: 1 },
+ },
+ { value: 3.4 - 3, unit: 'pp' },
+ ],
+ [
+ 'a 0-1 rate',
+ {
+ value: 0.25,
+ previous_value: 0.2,
+ change_pct: 25,
+ value_format: { format: 'percentage_scaled', prefix: null, postfix: null, decimal_places: 1 },
+ },
+ { value: (0.25 - 0.2) * 100, unit: 'pp' },
+ ],
+ [
+ 'a rate that starts from zero',
+ {
+ value: 5,
+ previous_value: 0,
+ change_pct: null,
+ value_format: { format: 'percentage', prefix: null, postfix: null, decimal_places: 1 },
+ },
+ { value: 5, unit: 'pp' },
+ ],
+ [
+ 'a rate with no earlier period',
+ {
+ value: 5,
+ previous_value: null,
+ change_pct: null,
+ value_format: { format: 'percentage', prefix: null, postfix: null, decimal_places: 1 },
+ },
+ { value: null, unit: 'pp' },
+ ],
+ ] as const)('measures the change of %s', (_name, overrides, expected) => {
+ expect(pulseChange(makePulseMetric(overrides))).toEqual(expected)
})
})
diff --git a/frontend/src/scenes/project-homepage/today/todayPulseFacts.ts b/frontend/src/scenes/project-homepage/today/todayPulseFacts.ts
index 8b1c837f4dd2..85c467a84fae 100644
--- a/frontend/src/scenes/project-homepage/today/todayPulseFacts.ts
+++ b/frontend/src/scenes/project-homepage/today/todayPulseFacts.ts
@@ -3,49 +3,23 @@ import { dayjs } from 'lib/dayjs'
import type { HighlightMetricApi } from 'products/today/frontend/generated/api.schemas'
const PERIOD_CAPTIONS: Record<HighlightMetricApi['interval'], string> = {
- day: 'Last 7 days, against the 7 days before',
- week: 'Last week, against the week before',
- month: 'Last month, against the month before',
+ day: 'Last 7 days',
+ week: 'Last week',
+ month: 'Last month',
}
-export function pulsePeriodCaption(metric: HighlightMetricApi): string {
- return PERIOD_CAPTIONS[metric.interval]
-}
-
-const PEAK_LABELS: Record<HighlightMetricApi['interval'], string> = {
- day: 'Top day',
- week: 'Top week',
- month: 'Top month',
+const PRIOR_LABELS: Record<HighlightMetricApi['interval'], string> = {
+ day: 'vs prior 7 days',
+ week: 'vs prior week',
+ month: 'vs prior month',
}
-const LATEST_PERIODS: Record<HighlightMetricApi['interval'], string> = {
- day: 'yesterday',
- week: 'last week',
- month: 'last month',
+export function pulsePeriodCaption(metric: HighlightMetricApi): string {
+ return PERIOD_CAPTIONS[metric.interval]
}
-export interface PulsePeak {
- label: string
- value: number
- start: string
- latest: string | null
-}
-
-export function pulsePeak(metric: HighlightMetricApi): PulsePeak | null {
- let peak: PulsePeak | null = null
- const lastIndex = metric.series.length - 1
- for (const [index, value] of metric.series.entries()) {
- const start = metric.period_starts[index]
- if (start && !Number.isNaN(value) && (peak === null || value > peak.value)) {
- peak = {
- label: PEAK_LABELS[metric.interval],
- value,
- start,
- latest: index === lastIndex ? LATEST_PERIODS[metric.interval] : null,
- }
- }
- }
- return peak
+export function pulsePriorLabel(metric: HighlightMetricApi): string {
+ return PRIOR_LABELS[metric.interval]
}
function openedWhen(day: string, today: dayjs.Dayjs): string {
@@ -85,3 +59,19 @@ export function pulseWhy(metric: HighlightMetricApi, today: dayjs.Dayjs = dayjs(
}
return `You opened ${subject} on ${metric.opened_days} of the last 14 days, most recently ${when}.${suffix}`
}
+
+export interface PulseChange {
+ value: number | null
+ unit: '%' | 'pp'
+}
+
+const RATE_SCALES: Record<string, number> = { percentage: 1, percentage_scaled: 100 }
+
+export function pulseChange(metric: HighlightMetricApi): PulseChange {
+ const scale = RATE_SCALES[metric.value_format?.format ?? '']
+ if (scale) {
+ const previous = metric.previous_value
+ return { value: previous === null ? null : (metric.value - previous) * scale, unit: 'pp' }
+ }
+ return { value: metric.change_pct, unit: '%' }
+}