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.

fix(today): keep key numbers in their places during a visit (#129375)

Shy Alterdbbd4740f46e
diff --git a/frontend/src/scenes/project-homepage/today/TodayPulse.tsx b/frontend/src/scenes/project-homepage/today/TodayPulse.tsx
index 411b8ce91d86..959d7f4e5a2e 100644
--- a/frontend/src/scenes/project-homepage/today/TodayPulse.tsx
+++ b/frontend/src/scenes/project-homepage/today/TodayPulse.tsx
@@ -114,8 +114,7 @@ export function TodayPulse({ metrics, slots }: { metrics: HighlightMetricApi[];
const listRef = useRef<HTMLUListElement>(null)
useSettleOnReorder(listRef, cards.map((tile) => tile.key).join('|'))
const { baseCurrency } = useValues(teamLogic)
- const { pulseMetricOpened, pulseMetricPreviewed, hidePulseMetric, holdPulseOrder, releasePulseOrder, retryPulse } =
- useActions(todayHighlightsLogic)
+ const { pulseMetricOpened, pulseMetricPreviewed, hidePulseMetric, retryPulse } = useActions(todayHighlightsLogic)
return (
<ul ref={listRef} className="TodayPulse" aria-label="Key numbers, against the period before">
{cards.map((slot, position) => {
@@ -200,12 +199,6 @@ export function TodayPulse({ metrics, slots }: { metrics: HighlightMetricApi[];
onOpenChange={(open) => {
if (open) {
pulseMetricPreviewed(metric, position)
- holdPulseOrder(
- metric.key,
- cards.map((tile) => tile.key)
- )
- } else {
- releasePulseOrder(metric.key)
}
}}
card={
diff --git a/frontend/src/scenes/project-homepage/today/todayHighlightsLogic.test.ts b/frontend/src/scenes/project-homepage/today/todayHighlightsLogic.test.ts
index 400a5e9261e9..717cc94b3b29 100644
--- a/frontend/src/scenes/project-homepage/today/todayHighlightsLogic.test.ts
+++ b/frontend/src/scenes/project-homepage/today/todayHighlightsLogic.test.ts
@@ -14,6 +14,7 @@ describe('todayHighlightsLogic', () => {
let highlightCalls: number
beforeEach(() => {
+ window.localStorage.clear()
highlightResponses = [[200, makeHighlights()]]
highlightCalls = 0
useMocks({
@@ -64,7 +65,7 @@ describe('todayHighlightsLogic', () => {
it('hides a pulse metric in this project only, fills its place with the next suggestion, and undoes the hide', async () => {
const capture = jest.spyOn(posthog, 'capture').mockImplementation(() => undefined)
- const pulse = ['a', 'b', 'c', 'd', 'e'].map((key) => makePulseMetric({ key: `insight:${key}` }))
+ const pulse = ['a', 'b', 'c', 'd', 'e', 'f'].map((key) => makePulseMetric({ key: `insight:${key}` }))
highlightResponses = [[200, makeHighlights({ pulse })]]
const logic = todayHighlightsLogic()
logic.mount()
@@ -75,11 +76,38 @@ describe('todayHighlightsLogic', () => {
expect(shown()).toEqual(['insight:a', 'insight:b', 'insight:c', 'insight:d'])
logic.actions.hidePulseMetric(pulse[1], 1)
- expect(shown()).toEqual(['insight:a', 'insight:c', 'insight:d', 'insight:e'])
+ expect(shown()).toEqual(['insight:a', 'insight:e', 'insight:c', 'insight:d'])
+
+ logic.actions.hidePulseMetric(pulse[3], 3)
+ logic.actions.loadHighlightsSuccess(makeHighlights({ pulse: [...pulse.slice(0, 4), pulse[5], pulse[4]] }))
+ expect(shown()).toEqual(['insight:a', 'insight:e', 'insight:c', 'insight:f'])
expect(capture).toHaveBeenCalledWith('today pulse metric hidden', expect.objectContaining({ position: 1 }))
+ logic.actions.loadHighlightsSuccess(makeHighlights({ pulse: pulse.filter((metric) => metric !== pulse[1]) }))
+ expect(shown()).toEqual(['insight:a', 'insight:e', 'insight:c', 'insight:f'])
+
+ logic.actions.loadHighlightsSuccess(makeHighlights({ pulse }))
logic.actions.unhidePulseMetric(pulse[1], 1)
+ expect(shown()).toEqual(['insight:a', 'insight:b', 'insight:c', 'insight:f'])
+ })
+
+ it('keeps numbers in their places when a later result reorders or drops them, and starts fresh on a new visit', async () => {
+ const [a, b, c, d, e] = ['a', 'b', 'c', 'd', 'e'].map((key) => makePulseMetric({ key: `insight:${key}` }))
+ highlightResponses = [[200, makeHighlights({ pulse: [a, b, c, d] })]]
+ const logic = todayHighlightsLogic()
+ logic.mount()
+ await expectLogic(logic).toFinishAllListeners()
+ const shown = (): string[] => logic.values.pulseTiles.map((metric) => metric.key)
expect(shown()).toEqual(['insight:a', 'insight:b', 'insight:c', 'insight:d'])
+
+ logic.actions.loadHighlightsSuccess(makeHighlights({ pulse: [c, e, a] }))
+ expect(shown()).toEqual(['insight:a', 'insight:e', 'insight:c'])
+
+ logic.actions.pulseViewMounted(true)
+ expect(shown()).toEqual(['insight:c', 'insight:e', 'insight:a'])
+
+ logic.actions.loadHighlightsSuccess(makeHighlights({ pulse: [a, c, e] }))
+ expect(shown()).toEqual(['insight:c', 'insight:e', 'insight:a'])
})
it.each([
diff --git a/frontend/src/scenes/project-homepage/today/todayHighlightsLogic.ts b/frontend/src/scenes/project-homepage/today/todayHighlightsLogic.ts
index 6dcdf65abdad..821f842d3788 100644
--- a/frontend/src/scenes/project-homepage/today/todayHighlightsLogic.ts
+++ b/frontend/src/scenes/project-homepage/today/todayHighlightsLogic.ts
@@ -95,6 +95,83 @@ function hiddenHighlightKey(projectId: number | string, key: string): string {
return `${projectId}:${key}`
}
+export interface PulsePlace {
+ key: string
+ standIn: string | null
+}
+
+export interface PulsePlacement {
+ slots: PulseSlotApi[]
+ places: PulsePlace[]
+}
+
+export function pulseSlotsOf(highlights: HighlightsApi | null): PulseSlotApi[] {
+ return (
+ highlights?.pulse_slots ??
+ (highlights?.pulse ?? []).map((metric) => ({
+ key: metric.key,
+ name: metric.name,
+ url: metric.url,
+ interval: metric.interval,
+ status: 'ready',
+ }))
+ )
+}
+
+function needsStandIn(place: PulsePlace, present: boolean, isHidden: (key: string) => boolean): boolean {
+ return present ? isHidden(place.key) : place.standIn !== null
+}
+
+export function placePulseSlots(
+ slots: PulseSlotApi[],
+ places: PulsePlace[],
+ isHidden: (key: string) => boolean
+): PulsePlacement {
+ const byKey = new Map(slots.map((slot) => [slot.key, slot]))
+ const kept = places.slice(0, PULSE_TILES)
+ const claimed = new Set(
+ kept
+ .flatMap((place) => [place.key, ...(place.standIn !== null ? [place.standIn] : [])])
+ .filter((key) => byKey.has(key))
+ )
+ const spare = slots.filter((slot) => !claimed.has(slot.key) && !isHidden(slot.key))
+ const used = new Set<string>()
+ const take = (key: string | null): PulseSlotApi | undefined => {
+ const slot = key !== null ? byKey.get(key) : undefined
+ if (slot && !isHidden(slot.key) && !used.has(slot.key)) {
+ used.add(slot.key)
+ return slot
+ }
+ const next = spare.shift()
+ if (next) {
+ used.add(next.key)
+ }
+ return next
+ }
+ const shown: PulseSlotApi[] = []
+ const nextPlaces: PulsePlace[] = []
+ for (const place of kept) {
+ if (needsStandIn(place, byKey.has(place.key), isHidden)) {
+ const standIn = take(place.standIn)
+ if (standIn) {
+ shown.push(standIn)
+ }
+ nextPlaces.push({ key: place.key, standIn: standIn?.key ?? null })
+ continue
+ }
+ const slot = take(byKey.has(place.key) ? place.key : null)
+ if (slot) {
+ shown.push(slot)
+ nextPlaces.push({ key: slot.key, standIn: null })
+ }
+ }
+ for (const slot of spare.slice(0, PULSE_TILES - nextPlaces.length)) {
+ shown.push(slot)
+ nextPlaces.push({ key: slot.key, standIn: null })
+ }
+ return { slots: shown, places: nextPlaces }
+}
+
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface todayHighlightsLogicValues {
currentProjectId: number | string // teamLogic
@@ -108,11 +185,9 @@ export interface todayHighlightsLogicValues {
highlightsLoading: boolean
highlightsPending: boolean
highlightsWaitOver: boolean
- pulseOrderHeld: {
- keys: string[]
- owner: string
- } | null
pulsePending: boolean
+ pulsePlacement: PulsePlacement
+ pulsePlaces: PulsePlace[]
pulseSlots: PulseSlotApi[]
pulseTiles: HighlightMetricApi[]
}
@@ -140,13 +215,6 @@ export interface todayHighlightsLogicActions {
item: HighlightItemApi
position: number
}
- holdPulseOrder: (
- owner: string,
- keys: string[]
- ) => {
- keys: string[]
- owner: string
- }
loadHighlights: (refresh?: boolean) => boolean
loadHighlightsFailure: (
error: string,
@@ -185,9 +253,6 @@ export interface todayHighlightsLogicActions {
pulseViewUpdated: () => {
value: true
}
- releasePulseOrder: (owner: string) => {
- owner: string
- }
retryPulse: () => {
value: true
}
@@ -197,6 +262,9 @@ export interface todayHighlightsLogicActions {
setHiddenPulseMetrics: (hidden: Record<string, true>) => {
hidden: Record<string, true>
}
+ setPulsePlaces: (places: PulsePlace[]) => {
+ places: PulsePlace[]
+ }
stopWaitingForHighlights: () => {
value: true
}
@@ -219,16 +287,13 @@ export interface todayHighlightsLogicActions {
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface todayHighlightsLogicMeta {
__keaTypeGenInternalSelectorTypes: {
- pulseSlots: (
+ pulsePlacement: (
highlights: HighlightsApi | null,
hiddenPulseMetrics: Record<string, true>,
currentProjectId: number | string,
- pulseOrderHeld: {
- keys: string[]
- owner: string
- } | null,
- highlightsFailed: boolean
- ) => PulseSlotApi[]
+ pulsePlaces: PulsePlace[]
+ ) => PulsePlacement
+ pulseSlots: (pulsePlacement: PulsePlacement, highlightsFailed: boolean) => PulseSlotApi[]
pulseTiles: (highlights: HighlightsApi | null, pulseSlots: PulseSlotApi[]) => HighlightMetricApi[]
highlightsPending: (
highlights: HighlightsApi | null,
@@ -279,8 +344,7 @@ export const todayHighlightsLogic = kea<todayHighlightsLogicType>([
hidePulseMetric: (metric: HighlightMetricApi, position: number) => ({ metric, position }),
unhidePulseMetric: (metric: HighlightMetricApi, position: number) => ({ metric, position }),
setHiddenPulseMetrics: (hidden: Record<string, true>) => ({ hidden }),
- holdPulseOrder: (owner: string, keys: string[]) => ({ owner, keys }),
- releasePulseOrder: (owner: string) => ({ owner }),
+ setPulsePlaces: (places: PulsePlace[]) => ({ places }),
pulseViewMounted: (sample: boolean) => ({ sample }),
pulseViewUpdated: true,
retryPulse: true,
@@ -309,11 +373,10 @@ export const todayHighlightsLogic = kea<todayHighlightsLogicType>([
],
})),
reducers({
- pulseOrderHeld: [
- null as { owner: string; keys: string[] } | null,
+ pulsePlaces: [
+ [] as PulsePlace[],
{
- holdPulseOrder: (_, { owner, keys }) => ({ owner, keys }),
- releasePulseOrder: (state, { owner }) => (state?.owner === owner ? null : state),
+ setPulsePlaces: (_, { places }) => places,
},
],
hiddenHighlights: [
@@ -346,41 +409,27 @@ export const todayHighlightsLogic = kea<todayHighlightsLogicType>([
},
}),
selectors({
- pulseSlots: [
- (s) => [s.highlights, s.hiddenPulseMetrics, s.currentProjectId, s.pulseOrderHeld, s.highlightsFailed],
+ pulsePlacement: [
+ (s) => [s.highlights, s.hiddenPulseMetrics, s.currentProjectId, s.pulsePlaces],
(
highlights: HighlightsApi | null,
hiddenPulseMetrics: Record<string, true>,
currentProjectId: number | string,
- pulseOrderHeld: { owner: string; keys: string[] } | null,
- highlightsFailed: boolean
- ): PulseSlotApi[] => {
- const slots: PulseSlotApi[] =
- highlights?.pulse_slots ??
- (highlights?.pulse ?? []).map((metric) => ({
- key: metric.key,
- name: metric.name,
- url: metric.url,
- interval: metric.interval,
- status: 'ready',
- }))
- const visible = slots
- .filter((slot) => !hiddenPulseMetrics[hiddenHighlightKey(currentProjectId, slot.key)])
- .slice(0, PULSE_TILES)
- .map(
- (slot): PulseSlotApi =>
- highlightsFailed && slot.status === 'pending' ? { ...slot, status: 'error' } : slot
- )
- if (!pulseOrderHeld) {
- return visible
- }
- const held = pulseOrderHeld.keys
- const place = (key: string): number => {
- const index = held.indexOf(key)
- return index === -1 ? held.length : index
- }
- return [...visible].sort((a, b) => place(a.key) - place(b.key))
- },
+ pulsePlaces: PulsePlace[]
+ ): PulsePlacement =>
+ placePulseSlots(
+ pulseSlotsOf(highlights),
+ pulsePlaces,
+ (key) => !!hiddenPulseMetrics[hiddenHighlightKey(currentProjectId, key)]
+ ),
+ ],
+ pulseSlots: [
+ (s) => [s.pulsePlacement, s.highlightsFailed],
+ (pulsePlacement: PulsePlacement, highlightsFailed: boolean): PulseSlotApi[] =>
+ pulsePlacement.slots.map(
+ (slot): PulseSlotApi =>
+ highlightsFailed && slot.status === 'pending' ? { ...slot, status: 'error' } : slot
+ ),
],
pulseTiles: [
(s) => [s.highlights, s.pulseSlots],
@@ -436,6 +485,13 @@ export const todayHighlightsLogic = kea<todayHighlightsLogicType>([
}),
listeners(({ actions, values, cache }) => ({
pulseViewMounted: ({ sample }) => {
+ actions.setPulsePlaces(
+ placePulseSlots(
+ pulseSlotsOf(values.highlights),
+ [],
+ (key) => !!values.hiddenPulseMetrics[hiddenHighlightKey(values.currentProjectId, key)]
+ ).places
+ )
if (sample) {
return
}
@@ -555,6 +611,9 @@ export const todayHighlightsLogic = kea<todayHighlightsLogicType>([
load.requestCount += 1
}
},
+ setHiddenPulseMetrics: () => {
+ actions.setPulsePlaces(values.pulsePlacement.places)
+ },
loadHighlightsFailure: () => {
const load = cache.pulseLoad as PulseLoad | undefined
if (load && !load.finished) {
@@ -562,7 +621,6 @@ export const todayHighlightsLogic = kea<todayHighlightsLogicType>([
}
},
hidePulseMetric: ({ metric, position }) => {
- actions.releasePulseOrder(metric.key)
actions.setHiddenPulseMetrics({
...values.hiddenPulseMetrics,
[hiddenHighlightKey(values.currentProjectId, metric.key)]: true,
@@ -656,6 +714,7 @@ export const todayHighlightsLogic = kea<todayHighlightsLogicType>([
if (!highlights) {
return
}
+ actions.setPulsePlaces(values.pulsePlacement.places)
if (highlights.status === 'computing') {
cache.disposables.add(() => {
const poll = window.setTimeout(() => actions.loadHighlights(), HIGHLIGHTS_POLL_MS)